11package com .getindata .connectors .http .internal .table .lookup ;
22
3+ import java .util .List ;
34import java .util .Map ;
45
56import org .apache .flink .configuration .Configuration ;
67import 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 ;
712import org .jetbrains .annotations .NotNull ;
813import org .junit .jupiter .api .Test ;
914import static org .assertj .core .api .Assertions .assertThat ;
15+ import static org .junit .jupiter .api .Assertions .fail ;
1016
17+ import com .getindata .connectors .http .HttpStatusCodeValidationFailedException ;
1118import static com .getindata .connectors .http .internal .table .lookup .HttpLookupConnectorOptions .SOURCE_LOOKUP_FAIL_JOB_ON_ERROR ;
1219
1320public 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