From ec755d261c717fa2e3b69ae0b98fc9d4cf8ab854 Mon Sep 17 00:00:00 2001 From: Leo Thomas Date: Fri, 17 Dec 2021 10:13:08 +0530 Subject: [PATCH] fix: Google sheets Bulk insert of empty array returns an error (#9725) * fix/8924-Bulk Insert throwing error on empty data in google sheets new1 * fix/8924 Error in bulk insert empty data * fix/8924 Code review fix for Google Sheet Bulk insert error * fix/8924 Code review fix for Google Sheet Bulk insert error * Fixed code review comments * Methodconfig data changes since only empty Mono is being tested --- .../com/external/config/BulkAppendMethod.java | 8 ++- .../external/plugins/GoogleSheetsPlugin.java | 14 ++++- .../external/config/BulkAppendMethodTest.java | 58 +++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/BulkAppendMethodTest.java diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/BulkAppendMethod.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/BulkAppendMethod.java index 2aa9de6a4c..50d94ae982 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/BulkAppendMethod.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/BulkAppendMethod.java @@ -83,7 +83,13 @@ public class BulkAppendMethod implements Method { List rowObjectListFromBody = null; try { - rowObjectListFromBody = this.getRowObjectListFromBody(this.objectMapper.readTree(methodConfig.getRowObjects())); + JsonNode body = this.objectMapper.readTree(methodConfig.getRowObjects()); + + if ( body.isEmpty()) { + return Mono.empty(); + } + + rowObjectListFromBody = this.getRowObjectListFromBody(body); } catch (JsonProcessingException e) { // Should never enter here } diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/plugins/GoogleSheetsPlugin.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/plugins/GoogleSheetsPlugin.java index bc6247a10b..103a0399d4 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/plugins/GoogleSheetsPlugin.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/plugins/GoogleSheetsPlugin.java @@ -241,7 +241,19 @@ public class GoogleSheetsPlugin extends BasePlugin { System.out.println(e.getMessage()); return Mono.just(errorResult); }); - }); + }) + .switchIfEmpty(handleEmptyMono()); + } + + /** + * Method to handle empty Mono + * @return Mono + */ + private Mono handleEmptyMono() { + final ActionExecutionResult result = new ActionExecutionResult(); + result.setIsExecutionSuccess(true); + result.setBody(objectMapper.valueToTree(Map.of("message", "No operation was performed"))); + return Mono.just(result); } @Override diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/BulkAppendMethodTest.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/BulkAppendMethodTest.java new file mode 100644 index 0000000000..b939f4c6cf --- /dev/null +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/BulkAppendMethodTest.java @@ -0,0 +1,58 @@ +package com.external.config; + +import com.appsmith.external.models.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.BeforeClass; +import org.junit.Test; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.util.ArrayList; +import java.util.List; + +public class BulkAppendMethodTest { + final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * To Test if it passes the empty Mono criteria, + * else it should fail + */ + @Test + public void testBulkAppendHandleEmptyMonoExecutePrerequisites() { + String[] testDataArray = {"[]", ""}; + + for(int i=0; i monoTest = bulkAppend.executePrerequisites(getMethodConfigObject(testDataArray[i]), getOAuthObject()); + + StepVerifier.create(monoTest) + .expectComplete() + .verify(); + } + } + + /** + * Simulated oAuth2 object, just to bypass few case. + * @return + */ + private OAuth2 getOAuthObject(){ + OAuth2 oAuth2 = new OAuth2(); + oAuth2.setAuthenticationResponse(new AuthenticationResponse() ); + oAuth2.getAuthenticationResponse().setToken("welcome123"); + return oAuth2; + } + + /** + * Simulated MethodConfig object with testable data. + * @param rowObject + * @return + */ + private MethodConfig getMethodConfigObject(String rowObject){ + List properties = new ArrayList(); + MethodConfig methodConfig = new MethodConfig(properties); + + methodConfig.setRowObjects(rowObject); + + return methodConfig; + } +} \ No newline at end of file