From e68dfdd4b76f6201f2448e6b6125c1d359dfd94e Mon Sep 17 00:00:00 2001 From: Priyanka Lakhe Date: Tue, 12 Sep 2023 14:06:01 -0700 Subject: [PATCH] added tests to PassthroughResourcesImplTest.java --- .../PassthroughResourcesImplTest.java | 114 +++++++++++++++++- 1 file changed, 110 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/smartsheet/api/internal/PassthroughResourcesImplTest.java b/src/test/java/com/smartsheet/api/internal/PassthroughResourcesImplTest.java index 323c5f46..7f84eb08 100644 --- a/src/test/java/com/smartsheet/api/internal/PassthroughResourcesImplTest.java +++ b/src/test/java/com/smartsheet/api/internal/PassthroughResourcesImplTest.java @@ -16,10 +16,18 @@ package com.smartsheet.api.internal; +import com.smartsheet.api.InvalidRequestException; +import com.smartsheet.api.SmartsheetException; import com.smartsheet.api.internal.http.DefaultHttpClient; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + class PassthroughResourcesImplTest extends ResourcesImplBase { private PassthroughResourcesImpl passthroughResources; @@ -31,23 +39,121 @@ public void setUp() throws Exception { } @Test - void testGetRequest() { + void testGetRequest_happyPath() throws SmartsheetException { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String response = passthroughResources.getRequest("/sheets/rows", params); + assertThat(response).isNotBlank(); + } + + @Test + void testGetRequest_exception() { + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + server.setStatus(400); + server.setResponseBody("{\"errorCode\":1032,\"message\":\"Something went wrong\"}"); + assertThatThrownBy(() -> { + passthroughResources.getRequest("/sheets/rows", params); + }) + .isInstanceOf(InvalidRequestException.class) + .hasMessage("Something went wrong"); + } + + @Test + void testPostRequest_happyPath() throws SmartsheetException { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = "some body here"; + String response = passthroughResources.postRequest("/sheets/rows", payload, params); + assertThat(response).isNotBlank(); + } + @Test + void testPostRequest_exception() { + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = "some body here"; + server.setStatus(400); + server.setResponseBody("{\"errorCode\":1032,\"message\":\"Something went wrong\"}"); + assertThatThrownBy(() -> { + passthroughResources.postRequest("/sheets/rows", payload, params); + }) + .isInstanceOf(InvalidRequestException.class) + .hasMessage("Something went wrong"); } @Test - void testPostRequest() { + void testPutRequest_happyPath() throws SmartsheetException { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = "some body here"; + String response = passthroughResources.putRequest("/sheets/rows", payload, params); + assertThat(response).isNotBlank(); + } + @Test + void testPutRequest_exception() { + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = "some body here"; + server.setStatus(400); + server.setResponseBody("{\"errorCode\":1032,\"message\":\"Something went wrong\"}"); + assertThatThrownBy(() -> { + passthroughResources.putRequest("/sheets/rows", payload, params); + }) + .isInstanceOf(InvalidRequestException.class) + .hasMessage("Something went wrong"); } @Test - void testPutRequest() { + void testPostRequest_nullPayload_throwsException() { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = null; + assertThatThrownBy(() -> { + passthroughResources.postRequest("/sheets/rows", payload, params); + }).isInstanceOf(IllegalArgumentException.class); + } + @Test + void testPutRequest_nullPayload_throwsException() { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + Map params = new HashMap<>(); + params.put("sheetId", 1234L); + params.put("rowId", 4567L); + String payload = null; + assertThatThrownBy(() -> { + passthroughResources.putRequest("/sheets/rows", payload, params); + }).isInstanceOf(IllegalArgumentException.class); } @Test - void testDeleteRequest() { + void testDeleteRequest_happyPath() throws SmartsheetException { + server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}"); + String response = passthroughResources.deleteRequest("/sheets/1234/rows/4567"); + assertThat(response).isNotBlank(); + } + @Test + void testDeleteRequest_exception() { + server.setStatus(400); + server.setResponseBody("{\"errorCode\":1032,\"message\":\"Something went wrong\"}"); + assertThatThrownBy(() -> { + passthroughResources.deleteRequest("/sheets/1234/rows/4567"); + }) + .isInstanceOf(InvalidRequestException.class) + .hasMessage("Something went wrong"); } }