Skip to content

Commit

Permalink
added tests to PassthroughResourcesImplTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyanka Lakhe committed Sep 12, 2023
1 parent c6a0101 commit e68dfdd
Showing 1 changed file with 110 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,23 +39,121 @@ public void setUp() throws Exception {
}

@Test
void testGetRequest() {
void testGetRequest_happyPath() throws SmartsheetException {
server.setResponseBody("{\"resultCode\":0,\"message\":\"SUCCESS\"}");
Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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");
}

}

0 comments on commit e68dfdd

Please sign in to comment.