Skip to content

Commit 5cbb9ac

Browse files
Merge pull request #124 from cloudsufi/schema-issue-release
[cherrypick] [PLUGIN-1922] Changes done to handle empty tables.
2 parents 9e0e97b + cfa482b commit 5cbb9ac

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

src/main/java/io/cdap/plugin/servicenow/apiclient/ServiceNowTableAPIClientImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ public Map<String, String> getRecordFromServiceNowTable(String tableName, String
533533
* @param tableName The name of the table for which the schema is being constructed.
534534
*
535535
* @return A {@link Schema} object representing the table structure as interpreted from the Schema API.
536+
* Return null if table has no records
536537
*
537538
* @throws RuntimeException if the schema response is null or contains no result.
538539
*/
@@ -544,8 +545,9 @@ private Schema prepareStringBasedSchema(RestAPIResponse restAPIResponse, List<Se
544545
for (String key : firstRecord.keySet()) {
545546
columns.add(new ServiceNowColumn(key, "string"));
546547
}
548+
return SchemaBuilder.constructSchema(tableName, columns);
547549
}
548-
return SchemaBuilder.constructSchema(tableName, columns);
550+
return null;
549551
}
550552

551553

src/main/java/io/cdap/plugin/servicenow/source/ServiceNowInputFormat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private static ServiceNowTableInfo getTableMetaData(String tableName,
108108
int recordCount = 0;
109109
try {
110110
schema = restApi.fetchTableSchema(tableName, valueType);
111+
if (schema == null) {
112+
return null;
113+
}
111114
recordCount = restApi.getTableRecordCount(tableName);
112115
} catch (ServiceNowAPIException e) {
113116
throw new RuntimeException(String.format("Error in fetching table metadata due to reason: %s", e.getMessage()),

src/main/java/io/cdap/plugin/servicenow/source/ServiceNowMultiInputFormat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ private static ServiceNowTableInfo getTableMetaData(String tableName, ServiceNow
104104
// This is used for ServiceNowMultiSource and provides structure of the table and being dependent on
105105
// connector config, makes it a read only function
106106
schema = restApi.fetchTableSchema(tableName, SourceValueType.SHOW_ACTUAL_VALUE);
107+
if (schema == null) {
108+
return null;
109+
}
107110
recordCount = restApi.getTableRecordCount(tableName);
108111
} catch (ServiceNowAPIException e) {
109112
throw new RuntimeException(e);

src/main/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ private void fetchSchema(ServiceNowTableAPIClientImpl restApi) {
105105
// Fetch the schema
106106
try {
107107
Schema tempSchema = restApi.fetchTableSchema(tableName, multiSourcePluginConf.getValueType());
108+
if (tempSchema == null) {
109+
return;
110+
}
108111
tableFields = tempSchema.getFields();
109112
List<Schema.Field> schemaFields = new ArrayList<>(tableFields);
110113
schemaFields.add(Schema.Field.of(tableNameField, Schema.of(Schema.Type.STRING)));

src/test/java/io/cdap/plugin/servicenow/source/ServiceNowInputFormatTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
package io.cdap.plugin.servicenow.source;
1919

20+
import io.cdap.cdap.api.data.schema.Schema;
2021
import io.cdap.plugin.servicenow.apiclient.ServiceNowTableAPIClientImpl;
2122
import io.cdap.plugin.servicenow.connector.ServiceNowConnectorConfig;
2223
import io.cdap.plugin.servicenow.restapi.RestAPIResponse;
2324
import io.cdap.plugin.servicenow.util.SourceApplication;
2425
import io.cdap.plugin.servicenow.util.SourceQueryMode;
2526
import io.cdap.plugin.servicenow.util.SourceValueType;
26-
import org.apache.http.HttpStatus;
2727
import org.apache.http.client.methods.CloseableHttpResponse;
2828
import org.apache.http.impl.client.CloseableHttpClient;
2929
import org.apache.http.impl.client.HttpClientBuilder;
@@ -138,9 +138,14 @@ public void testFetchTableInfo() throws Exception {
138138
" }\n" +
139139
" ]\n" +
140140
"}";
141+
String schemaString = "{\"type\":\"record\",\"name\":\"ServiceNowColumnMetaData\",\"fields\":[{\"name\":" +
142+
"\"backgroundElementId\",\"type\":\"long\"},{\"name\":\"bgOrderPos\",\"type\":\"long\"},{\"name\":" +
143+
"\"description\",\"type\":[\"string\",\"null\"]},{\"name\":\"userId\",\"type\":\"string\"}]}";
144+
Schema schema = Schema.parseJson(schemaString);
141145
RestAPIResponse restAPIResponse = new RestAPIResponse(headers, responseBody, null);
142146
Mockito.when(restApi.executeGetWithRetries(Mockito.any())).thenReturn(restAPIResponse);
143147
Mockito.when(restApi.parseResponseToResultListOfMap(restAPIResponse.getResponseBody())).thenReturn(result);
148+
Mockito.when(restApi.fetchTableSchema("table", SourceValueType.SHOW_ACTUAL_VALUE)).thenReturn(schema);
144149
OAuthClient oAuthClient = Mockito.mock(OAuthClient.class);
145150
PowerMockito.whenNew(OAuthClient.class).
146151
withArguments(Mockito.any(URLConnectionClient.class)).thenReturn(oAuthClient);
@@ -237,9 +242,18 @@ public void testFetchTableInfoReportingMode() throws Exception {
237242
" }\n" +
238243
" ]\n" +
239244
"}";
245+
String schemaString = "{\"type\":\"record\",\"name\":\"ServiceNowColumnMetaData\",\"fields\":[{\"name\":" +
246+
"\"backgroundElementId\",\"type\":\"long\"},{\"name\":\"bgOrderPos\",\"type\":\"long\"},{\"name\":" +
247+
"\"description\",\"type\":[\"string\",\"null\"]},{\"name\":\"userId\",\"type\":\"string\"}]}";
248+
Schema schema = Schema.parseJson(schemaString);
240249
RestAPIResponse restAPIResponse = new RestAPIResponse(headers, responseBody, null);
241250
Mockito.when(restApi.executeGetWithRetries(Mockito.any())).thenReturn(restAPIResponse);
242251
Mockito.when(restApi.parseResponseToResultListOfMap(restAPIResponse.getResponseBody())).thenReturn(result);
252+
Mockito.when(restApi.fetchTableSchema("proc_po", SourceValueType.SHOW_ACTUAL_VALUE)).thenReturn(schema);
253+
Mockito.when(restApi.fetchTableSchema("proc_po_item",
254+
SourceValueType.SHOW_ACTUAL_VALUE)).thenReturn(schema);
255+
Mockito.when(restApi.fetchTableSchema("proc_rec_slip",
256+
SourceValueType.SHOW_ACTUAL_VALUE)).thenReturn(schema);
243257
OAuthClient oAuthClient = Mockito.mock(OAuthClient.class);
244258
PowerMockito.whenNew(OAuthClient.class).
245259
withArguments(Mockito.any(URLConnectionClient.class)).thenReturn(oAuthClient);
@@ -260,7 +274,7 @@ public void testFetchTableInfoReportingMode() throws Exception {
260274
thenReturn(response);
261275
SourceApplication application = SourceApplication.PROCUREMENT;
262276
SourceValueType valueType = SourceValueType.SHOW_ACTUAL_VALUE;
263-
Assert.assertEquals(4, ServiceNowInputFormat.fetchTableInfo(mode, connectorConfig, "table",
277+
Assert.assertEquals(3, ServiceNowInputFormat.fetchTableInfo(mode, connectorConfig, "table",
264278
application, valueType, true).size());
265279
}
266280

0 commit comments

Comments
 (0)