ocean-zhc commented on code in PR #9103:
URL: https://github.com/apache/seatunnel/pull/9103#discussion_r2047213925


##########
seatunnel-connectors-v2/connector-http/connector-http-base/src/test/java/org/apache/seatunnel/connectors/seatunnel/http/JsonFieldMissedReturnNullTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.http;
+
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import 
org.apache.seatunnel.connectors.seatunnel.http.client.HttpClientProvider;
+import org.apache.seatunnel.connectors.seatunnel.http.client.HttpResponse;
+import org.apache.seatunnel.connectors.seatunnel.http.config.HttpParameter;
+import org.apache.seatunnel.connectors.seatunnel.http.config.HttpRequestMethod;
+import org.apache.seatunnel.connectors.seatunnel.http.config.JsonField;
+import org.apache.seatunnel.connectors.seatunnel.http.source.HttpSourceReader;
+import 
org.apache.seatunnel.connectors.seatunnel.http.source.SimpleTextDeserializationSchema;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class JsonFieldMissedReturnNullTest {
+
+    private HttpParameter httpParameter;
+    private JsonField jsonField;
+    private SimpleTextDeserializationSchema deserializationSchema;
+
+    @Mock private SingleSplitReaderContext context;
+
+    @Mock private Collector<SeaTunnelRow> collector;
+
+    @Mock private HttpClientProvider httpClientProvider;
+
+    @Mock private HttpResponse httpResponse;
+
+    @BeforeEach
+    public void setUp() {
+        MockitoAnnotations.openMocks(this);
+
+        httpParameter = new HttpParameter();
+        httpParameter.setUrl("http://test-url.com";);
+        httpParameter.setMethod(HttpRequestMethod.GET);
+
+        Map<String, String> fields = new HashMap<>();
+        fields.put("key1", "$[*].key1");
+        fields.put("key2", "$[*].key2");
+        jsonField = JsonField.builder().fields(fields).build();
+
+        // Create the schema with two string fields
+        SeaTunnelRowType rowType =
+                new SeaTunnelRowType(
+                        new String[] {"key1", "key2"},
+                        new SeaTunnelDataType[] {BasicType.STRING_TYPE, 
BasicType.STRING_TYPE});
+        deserializationSchema = new SimpleTextDeserializationSchema(rowType);
+
+        // Setup mocks
+        when(httpResponse.getCode()).thenReturn(200);
+        when(collector.getCheckpointLock()).thenReturn(new Object());
+    }
+
+    @Test
+    public void testJsonFieldMissedReturnNull() throws Exception {
+        // Test data with missing fields
+        String testJsonData =
+                
"[{\"key1\":\"value11\",\"key2\":\"value22\"},{\"key2\":\"value33\"},{\"key1\":\"value55\"}]";
+
+        // Set json_filed_missed_return_null to true
+        httpParameter.setJsonFiledMissedReturnNull(true);
+
+        // Setup HTTP response
+        when(httpResponse.getContent()).thenReturn(testJsonData);
+        when(httpClientProvider.execute(
+                        anyString(), anyString(), any(), any(), any(), 
any(Boolean.class)))
+                .thenReturn(httpResponse);
+
+        // Create HttpSourceReader
+        HttpSourceReader sourceReader =
+                new HttpSourceReader(
+                        httpParameter, context, deserializationSchema, 
jsonField, null);
+
+        // Use reflection to inject our mocked HTTP client
+        sourceReader.open(); // This creates the real HTTP client
+        Field httpClientField = 
HttpSourceReader.class.getDeclaredField("httpClient");
+        httpClientField.setAccessible(true);
+        httpClientField.set(sourceReader, httpClientProvider);
+
+        // Capture the rows collected
+        ArgumentCaptor<SeaTunnelRow> rowCaptor = 
ArgumentCaptor.forClass(SeaTunnelRow.class);
+
+        // Call the method that processes data
+        sourceReader.pollNext(collector);
+
+        // Verify collector.collect was called 3 times (once for each JSON 
object)
+        verify(collector, times(1)).collect(rowCaptor.capture());

Review Comment:
   single call



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to