18
18
import org .apache .flink .table .data .RowData ;
19
19
import org .apache .flink .table .data .StringData ;
20
20
import org .apache .flink .table .types .DataType ;
21
+ import org .apache .flink .table .types .FieldsDataType ;
21
22
import org .apache .flink .types .Row ;
23
+ import org .apache .flink .util .Preconditions ;
24
+ import org .jetbrains .annotations .NotNull ;
22
25
import org .junit .jupiter .api .Test ;
23
26
import org .junit .jupiter .params .ParameterizedTest ;
24
27
import org .junit .jupiter .params .provider .ValueSource ;
25
28
import static org .assertj .core .api .Assertions .assertThat ;
26
29
import static org .junit .jupiter .api .Assertions .assertThrows ;
27
30
31
+ import com .getindata .connectors .http .internal .table .lookup .LookupQueryInfo ;
28
32
import com .getindata .connectors .http .internal .table .lookup .LookupRow ;
29
33
import com .getindata .connectors .http .internal .table .lookup .RowDataSingleValueLookupSchemaEntry ;
30
34
import static com .getindata .connectors .http .internal .table .lookup .HttpLookupConnectorOptions .LOOKUP_METHOD ;
31
35
import static com .getindata .connectors .http .internal .table .lookup .HttpLookupTableSourceFactory .row ;
32
36
import static com .getindata .connectors .http .internal .table .lookup .querycreators .GenericJsonAndUrlQueryCreatorFactoryTest .getTableContext ;
33
37
34
38
class GenericJsonAndUrlQueryCreatorTest {
35
-
39
+ private static final String KEY = "key1" ;
40
+ private static final String VALUE = "val1" ;
41
+ // for GET this is the minimum config
42
+ private static final List <String > QUERY_PARAMS = List .of (KEY );
43
+ // Path param ArgPath required a stringified json object. As we have PersonBean
44
+ // we can use that.
45
+ private static final Map <String , String > URL_PARAMS = Map .of (KEY , KEY );
46
+ private static final DataType dataType = row (List .of (
47
+ DataTypes .FIELD (KEY , DataTypes .STRING ())
48
+ ));
49
+ private static final ResolvedSchema resolvedSchema = ResolvedSchema .of (Column .physical (KEY ,
50
+ DataTypes .STRING ()));
51
+ private static final RowData ROWDATA = getRowData (1 , VALUE );
36
52
@ ParameterizedTest
37
53
@ ValueSource (strings = {"GET" , "PUT" , "POST" })
38
54
public void createLookupQueryTestStrAllOps (String operation ) {
39
- String key = "key1" ;
40
- String value = "val1" ;
41
- // for GET this is the minimum config
42
- List <String > query_params = List .of (key );
43
- // Path param ArgPath required a stringified json object. As we have PersonBean
44
- // we can use that.
45
- Map <String , String > url_params = Map .of (key , key );
46
- LookupRow lookupRow = new LookupRow ()
47
- .addLookupEntry (
48
- new RowDataSingleValueLookupSchemaEntry (
49
- key ,
50
- RowData .createFieldGetter (
51
- DataTypes .STRING ().getLogicalType (), 0 )
52
- ));
53
- DataType dataType = row (List .of (
54
- DataTypes .FIELD (key , DataTypes .STRING ())
55
- ));
56
- lookupRow .setLookupPhysicalRowDataType (dataType );
57
- ResolvedSchema resolvedSchema = ResolvedSchema .of (Column .physical (key ,
58
- DataTypes .STRING ()));
59
- Configuration config = new Configuration ();
60
- config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_QUERY_PARAM_FIELDS ,
61
- query_params );
62
- if (!operation .equals ("GET" )) {
63
- // add the body content for PUT and POST
64
- config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_BODY_FIELDS ,
65
- query_params );
66
- }
67
- config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_URL_MAP , url_params );
68
- config .setString (LOOKUP_METHOD , operation );
55
+ // WHEN
56
+ LookupRow lookupRow = getLookupRow ();
57
+ Configuration config = getConfiguration (operation );
69
58
GenericJsonAndUrlQueryCreator universalJsonQueryCreator =
70
59
(GenericJsonAndUrlQueryCreator ) new GenericJsonAndUrlQueryCreatorFactory ()
71
60
.createLookupQueryCreator (
@@ -74,26 +63,67 @@ public void createLookupQueryTestStrAllOps(String operation) {
74
63
getTableContext (config ,
75
64
resolvedSchema )
76
65
);
77
- var row = new GenericRowData (1 );
78
- row .setField (0 , StringData .fromString (value ));
79
- var createdQuery = universalJsonQueryCreator .createLookupQuery (row );
66
+ var createdQuery = universalJsonQueryCreator .createLookupQuery (ROWDATA );
80
67
// THEN
81
68
if (operation .equals ("GET" )) {
82
- assertThat (createdQuery .getBodyBasedUrlQueryParameters ()).isEmpty ();
83
- assertThat (createdQuery .getLookupQuery ()).isEqualTo (key + "=" + value );
69
+ validateCreatedQueryForGet (createdQuery );
84
70
} else {
85
- assertThat (createdQuery
86
- .getBodyBasedUrlQueryParameters ())
87
- .isEqualTo (key + "=" + value );
88
- assertThat (createdQuery .getLookupQuery ()).isEqualTo (
89
- "{\" "
90
- + key
91
- + "\" :\" " + value
92
- + "\" }" );
71
+ validateCreatedQueryForPutAndPost (createdQuery );
93
72
}
73
+ // validate url based parameters
94
74
assertThat (createdQuery .getPathBasedUrlParameters ().size () == 1 ).isTrue ();
95
- assertThat (createdQuery .getPathBasedUrlParameters ().get (key )).isEqualTo (value );
75
+ assertThat (createdQuery .getPathBasedUrlParameters ().get (KEY )).isEqualTo (VALUE );
76
+ }
77
+
78
+ private static void validateCreatedQueryForGet ( LookupQueryInfo createdQuery ) {
79
+ // check there is no body params and we have the expected lookup query
80
+ assertThat (createdQuery .getBodyBasedUrlQueryParameters ()).isEmpty ();
81
+ assertThat (createdQuery .getLookupQuery ()).isEqualTo (KEY + "=" + VALUE );
96
82
}
83
+ private static void validateCreatedQueryForPutAndPost (LookupQueryInfo createdQuery ) {
84
+ // check we have the expected body params and lookup query
85
+ assertThat (createdQuery
86
+ .getBodyBasedUrlQueryParameters ())
87
+ .isEqualTo (KEY + "=" + VALUE );
88
+ assertThat (createdQuery .getLookupQuery ()).isEqualTo (
89
+ "{\" "
90
+ + KEY
91
+ + "\" :\" " + VALUE
92
+ + "\" }" );
93
+ }
94
+
95
+ private static @ NotNull GenericRowData getRowData (int arity , String value ) {
96
+ var row = new GenericRowData (arity );
97
+ row .setField (0 , StringData .fromString (value ));
98
+ return row ;
99
+ }
100
+
101
+ private static @ NotNull Configuration getConfiguration (String operation ) {
102
+ Configuration config = new Configuration ();
103
+ config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_QUERY_PARAM_FIELDS ,
104
+ QUERY_PARAMS );
105
+ if (!operation .equals ("GET" )) {
106
+ // add the body content for PUT and POST
107
+ config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_BODY_FIELDS ,
108
+ QUERY_PARAMS );
109
+ }
110
+ config .set (GenericJsonAndUrlQueryCreatorFactory .REQUEST_URL_MAP , URL_PARAMS );
111
+ config .setString (LOOKUP_METHOD , operation );
112
+ return config ;
113
+ }
114
+
115
+ private static @ NotNull LookupRow getLookupRow () {
116
+ LookupRow lookupRow = new LookupRow ()
117
+ .addLookupEntry (
118
+ new RowDataSingleValueLookupSchemaEntry (
119
+ KEY ,
120
+ RowData .createFieldGetter (
121
+ DataTypes .STRING ().getLogicalType (), 0 )
122
+ ));
123
+ lookupRow .setLookupPhysicalRowDataType (dataType );
124
+ return lookupRow ;
125
+ }
126
+
97
127
@ Test
98
128
public void createLookupQueryTest () {
99
129
List <String > query_params = List .of ("key1" , "key2" );
@@ -137,8 +167,7 @@ public void createLookupQueryTest() {
137
167
getTableContext (config ,
138
168
resolvedSchema )
139
169
);
140
- var row = new GenericRowData (2 );
141
- row .setField (0 , StringData .fromString (value ));
170
+ var row = getRowData (2 , value );
142
171
row .setField (1 , StringData .fromString (value ));
143
172
var createdQuery = genericJsonAndUrlQueryCreator .createLookupQuery (row );
144
173
// THEN
@@ -148,7 +177,7 @@ public void createLookupQueryTest() {
148
177
+ "&" + key2 + "=" + value );
149
178
}
150
179
@ Test
151
- public void failserializationOpenTest () {
180
+ public void failSerializationOpenTest () {
152
181
List <String > paths_config =List .of ("key1" );
153
182
final String operation = "GET" ;
154
183
final String key = "key1" ;
@@ -226,10 +255,30 @@ public byte[] serialize(RowData element) {
226
255
DataTypes .FIELD (key3 , DataTypes .TIMESTAMP_LTZ ())
227
256
));
228
257
// WHEN
229
- Row row = GenericJsonAndUrlQueryCreator . rowDataToRow (rowData , dataType );
258
+ Row row = rowDataToRow (rowData , dataType );
230
259
// THEN
231
260
assertThat (row .getField (key1 ).equals (value ));
232
261
assertThat (row .getField (key2 ).equals ("1970-01-01T00:00:00.010" ));
233
262
assertThat (row .getField (key3 ).equals ("1970-01-01T00:00:00.010Z" ));
234
263
}
264
+ /**
265
+ * Create a Row from a RowData and DataType
266
+ * @param lookupRowData the lookup RowData
267
+ * @param rowType the datatype
268
+ * @return row return row
269
+ */
270
+ private static Row rowDataToRow (final RowData lookupRowData , final DataType rowType ) {
271
+ Preconditions .checkNotNull (lookupRowData );
272
+ Preconditions .checkNotNull (rowType );
273
+
274
+ final Row row = Row .withNames ();
275
+ final List <DataTypes .Field > rowFields = FieldsDataType .getFields (rowType );
276
+
277
+ for (int idx = 0 ; idx < rowFields .size (); idx ++) {
278
+ final String fieldName = rowFields .get (idx ).getName ();
279
+ final Object fieldValue = ((GenericRowData ) lookupRowData ).getField (idx );
280
+ row .setField (fieldName , fieldValue );
281
+ }
282
+ return row ;
283
+ }
235
284
}
0 commit comments