Skip to content

Commit 776a2fc

Browse files
committed
88 percent overage
Signed-off-by: davidradl <[email protected]>
1 parent 2e87f97 commit 776a2fc

File tree

2 files changed

+90
-24
lines changed

2 files changed

+90
-24
lines changed

src/main/java/com/getindata/connectors/http/internal/table/lookup/HttpTableLookupFunction.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,36 @@ public Collection<RowData> lookup(RowData keyRow) {
100100
}
101101
outputList.add(producedRow);
102102
}
103-
} catch (Exception e) {
104-
if (this.fail_job_on_error) throw e;
105-
Throwable cause =e.getCause();
106-
if (cause instanceof HttpStatusCodeValidationFailedException) {
107-
final GenericRowData producedRow = getProducedRowForError(
108-
cause.getMessage(),
109-
((HttpStatusCodeValidationFailedException) cause).getResponse(),
110-
metadataArity,
111-
metadataConverters);
112-
outputList.add(producedRow);
113-
} else {
114-
// cause might not have a message so just use it's toString.
115-
String msg= e.getMessage() +"," + cause;
116-
final GenericRowData producedRow = getProducedRowForError(
117-
msg,
118-
null,
119-
metadataArity,
120-
metadataConverters);
121-
outputList.add(producedRow);
122-
}
103+
} catch (RuntimeException e) {
104+
outputList = processExceptionsFromLookup(e, metadataArity);
105+
}
106+
return outputList;
107+
}
108+
List<RowData> processExceptionsFromLookup(RuntimeException e, int metadataArity) {
109+
List<RowData> outputList = new ArrayList<>();
110+
if (this.fail_job_on_error) throw e;
111+
Throwable cause = e.getCause();
112+
if (cause instanceof HttpStatusCodeValidationFailedException) {
113+
final GenericRowData producedRow = getProducedRowForError(
114+
cause.getMessage(),
115+
((HttpStatusCodeValidationFailedException) cause).getResponse(),
116+
metadataArity,
117+
metadataConverters);
118+
outputList.add(producedRow);
119+
} else {
120+
// cause might not have a message so just use it's toString.
121+
String msg= e.getMessage() +"," + cause;
122+
final GenericRowData producedRow = getProducedRowForError(
123+
msg,
124+
null,
125+
metadataArity,
126+
metadataConverters);
127+
outputList.add(producedRow);
123128
}
124129
return outputList;
125130
}
126131

132+
@VisibleForTesting
127133
private GenericRowData getProducedRowForError(String msg,
128134
HttpResponse httpResponse,
129135
int metadataArity,

src/test/java/com/getindata/connectors/http/internal/table/lookup/HttpTableLookupFunctionTest.java

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.getindata.connectors.http.internal.table.lookup;
22

3+
import java.util.List;
34
import java.util.Map;
45

56
import org.apache.flink.configuration.Configuration;
67
import org.apache.flink.configuration.ReadableConfig;
8+
import org.apache.flink.table.api.DataTypes;
9+
import org.apache.flink.table.data.RowData;
10+
import org.apache.flink.table.data.StringData;
11+
import org.apache.flink.table.types.DataType;
712
import org.jetbrains.annotations.NotNull;
813
import org.junit.jupiter.api.Test;
914
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.junit.jupiter.api.Assertions.fail;
1016

17+
import com.getindata.connectors.http.HttpStatusCodeValidationFailedException;
1118
import static com.getindata.connectors.http.internal.table.lookup.HttpLookupConnectorOptions.SOURCE_LOOKUP_FAIL_JOB_ON_ERROR;
1219

1320
public class HttpTableLookupFunctionTest {
@@ -17,7 +24,7 @@ void testconstructor() {
1724
assertThat(getHttpTableLookupFunction(true).getFail_job_on_error()).isTrue();
1825
assertThat(getHttpTableLookupFunction(false).getFail_job_on_error()).isFalse();
1926
}
20-
@Test
27+
2128
private static @NotNull HttpTableLookupFunction getHttpTableLookupFunction(Boolean flag) {
2229
ReadableConfig readableConfig;
2330
if (flag == null ) {
@@ -30,14 +37,67 @@ void testconstructor() {
3037
HttpLookupConfig httpLookupConfig = HttpLookupConfig.builder()
3138
.readableConfig(readableConfig)
3239
.build();
33-
HttpTableLookupFunction httpTableLookupFunction
40+
41+
// Create metadata converters for testing
42+
MetadataConverter[] metadataConverters = new MetadataConverter[] {
43+
// Simple converter that returns a string
44+
new MetadataConverter() {
45+
private static final long serialVersionUID = 1L;
46+
@Override
47+
public Object read(String msg, java.net.http.HttpResponse httpResponse) {
48+
return StringData.fromString(msg != null ? msg : "");
49+
}
50+
}
51+
};
52+
53+
// Create a produced data type with one physical field and one metadata field
54+
DataType producedDataType = DataTypes.ROW(List.of(
55+
DataTypes.FIELD("id", DataTypes.STRING().notNull()),
56+
DataTypes.FIELD("error", DataTypes.STRING())
57+
));
58+
59+
HttpTableLookupFunction httpTableLookupFunction
3460
= new HttpTableLookupFunction(null,
3561
null,
3662
null,
3763
httpLookupConfig,
38-
null,
39-
null);
64+
metadataConverters,
65+
producedDataType);
4066
return httpTableLookupFunction;
4167
}
68+
@Test
69+
70+
void testprocessExceptionsFromLookup() {
71+
// Create test instances with different fail_job_on_error settings
72+
HttpTableLookupFunction lookupFunctionWithFail = getHttpTableLookupFunction(true);
73+
HttpTableLookupFunction lookupFunctionWithoutFail = getHttpTableLookupFunction(false);
74+
75+
// Create test exceptions
76+
RuntimeException runtimeException1 = new RuntimeException("test1");
77+
78+
// Use null for the HttpResponse parameter
79+
HttpStatusCodeValidationFailedException cause =
80+
new HttpStatusCodeValidationFailedException("status", null);
81+
RuntimeException runtimeException2 = new RuntimeException("test2", cause);
82+
83+
// Test case 1: When fail_job_on_error is true, it should throw the exception
84+
try {
85+
lookupFunctionWithFail.processExceptionsFromLookup(runtimeException1, 3);
86+
fail("Expected RuntimeException to be thrown");
87+
} catch (RuntimeException e) {
88+
// Expected exception
89+
assertThat(e).isSameAs(runtimeException1);
90+
}
91+
92+
// Test case 2: When fail_job_on_error is false and the cause is not an HttpStatusCodeValidationFailedException
93+
List<RowData> rowData1 = lookupFunctionWithoutFail.processExceptionsFromLookup(runtimeException1, 1);
94+
assertThat(rowData1).isNotNull();
95+
assertThat(rowData1).hasSize(1);
96+
97+
// Test case 3: When fail_job_on_error is false and the cause is an HttpStatusCodeValidationFailedException
98+
List<RowData> rowData2 = lookupFunctionWithoutFail.processExceptionsFromLookup(runtimeException2, 1);
99+
assertThat(rowData2).isNotNull();
100+
assertThat(rowData2).hasSize(1);
101+
}
42102

43103
}

0 commit comments

Comments
 (0)