From ef47946709fe802e2bff406e7798ffa5ed12cfeb Mon Sep 17 00:00:00 2001 From: Priyanka Lakhe Date: Tue, 12 Sep 2023 14:25:10 -0700 Subject: [PATCH] added tests for EventResourcesImplTest.java --- .../api/internal/EventResourcesImplTest.java | 82 ++++++++++++++++++- src/test/resources/listEvents.json | 28 +++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/listEvents.json diff --git a/src/test/java/com/smartsheet/api/internal/EventResourcesImplTest.java b/src/test/java/com/smartsheet/api/internal/EventResourcesImplTest.java index 9d9106a2..8402edea 100644 --- a/src/test/java/com/smartsheet/api/internal/EventResourcesImplTest.java +++ b/src/test/java/com/smartsheet/api/internal/EventResourcesImplTest.java @@ -16,10 +16,21 @@ package com.smartsheet.api.internal; +import com.smartsheet.api.InvalidRequestException; +import com.smartsheet.api.SmartsheetException; import com.smartsheet.api.internal.http.DefaultHttpClient; +import com.smartsheet.api.models.EventResult; +import com.smartsheet.api.models.enums.EventObjectType; +import com.smartsheet.api.models.enums.EventSource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + class EventResourcesImplTest extends ResourcesImplBase { private EventResourcesImpl eventResources; @@ -31,13 +42,82 @@ public void setUp() throws Exception { } @Test - void testListEvents_happyPath() { + void testListEvents_sinceExists_happyPath() throws IOException, SmartsheetException { + server.setResponseBody(new File("src/test/resources/listEvents.json")); + String since = "2023-01-30T11:42:30Z"; + Integer maxCount = 5; + Boolean numericDates = true; + + EventResult eventResult = eventResources.listEvents(since, null, maxCount, numericDates); + + assertThat(eventResult).isNotNull(); + assertThat(eventResult.getData()).isNotNull(); + assertThat(eventResult.getMoreAvailable()).isFalse(); + assertThat(eventResult.getNextStreamPosition()).isNotBlank(); + assertThat(eventResult.getData().size()).isEqualTo(2); + + assertThat(eventResult.getData().get(0).getEventId()).isNotBlank(); + assertThat(eventResult.getData().get(0).getUserId()).isNotNull(); + assertThat(eventResult.getData().get(0).getRequestUserId()).isNotNull(); + assertThat(eventResult.getData().get(0).getAccessTokenName()).isNotBlank(); + assertThat(eventResult.getData().get(0).getEventTimestamp()).isNotNull(); + assertThat(eventResult.getData().get(0).getSource().name()).isEqualTo(EventSource.WEB_APP.name()); + assertThat(eventResult.getData().get(0).getObjectType().name()).isEqualTo(EventObjectType.ACCESS_TOKEN.name()); + + assertThat(eventResult.getData().get(1).getEventId()).isNotBlank(); + assertThat(eventResult.getData().get(1).getUserId()).isNotNull(); + assertThat(eventResult.getData().get(1).getRequestUserId()).isNotNull(); + assertThat(eventResult.getData().get(1).getAccessTokenName()).isNotBlank(); + assertThat(eventResult.getData().get(1).getEventTimestamp()).isNotNull(); + assertThat(eventResult.getData().get(1).getSource().name()).isEqualTo(EventSource.WEB_APP.name()); + assertThat(eventResult.getData().get(1).getObjectType().name()).isEqualTo(EventObjectType.ACCESS_TOKEN.name()); + } + + @Test + void testListEvents_streamPosExists_happyPath() throws IOException, SmartsheetException { + server.setResponseBody(new File("src/test/resources/listEvents.json")); + String streamPos = "2023-01-30T11:42:30Z"; + Integer maxCount = 5; + Boolean numericDates = true; + + EventResult eventResult = eventResources.listEvents(null, streamPos, maxCount, numericDates); + + assertThat(eventResult).isNotNull(); + assertThat(eventResult.getData()).isNotNull(); + assertThat(eventResult.getMoreAvailable()).isFalse(); + assertThat(eventResult.getNextStreamPosition()).isNotBlank(); + assertThat(eventResult.getData().size()).isEqualTo(2); + + assertThat(eventResult.getData().get(0).getEventId()).isNotBlank(); + assertThat(eventResult.getData().get(0).getUserId()).isNotNull(); + assertThat(eventResult.getData().get(0).getRequestUserId()).isNotNull(); + assertThat(eventResult.getData().get(0).getAccessTokenName()).isNotBlank(); + assertThat(eventResult.getData().get(0).getEventTimestamp()).isNotNull(); + assertThat(eventResult.getData().get(0).getSource().name()).isEqualTo(EventSource.WEB_APP.name()); + assertThat(eventResult.getData().get(0).getObjectType().name()).isEqualTo(EventObjectType.ACCESS_TOKEN.name()); + assertThat(eventResult.getData().get(1).getEventId()).isNotBlank(); + assertThat(eventResult.getData().get(1).getUserId()).isNotNull(); + assertThat(eventResult.getData().get(1).getRequestUserId()).isNotNull(); + assertThat(eventResult.getData().get(1).getAccessTokenName()).isNotBlank(); + assertThat(eventResult.getData().get(1).getEventTimestamp()).isNotNull(); + assertThat(eventResult.getData().get(1).getSource().name()).isEqualTo(EventSource.WEB_APP.name()); + assertThat(eventResult.getData().get(1).getObjectType().name()).isEqualTo(EventObjectType.ACCESS_TOKEN.name()); } @Test void testListEvents_exception() { + String since = "2023-01-30T11:42:30Z"; + Integer maxCount = 5; + Boolean numericDates = true; + server.setStatus(400); + server.setResponseBody("{\"errorCode\":1032,\"message\":\"Something went wrong\"}"); + assertThatThrownBy(() -> { + eventResources.listEvents(since, null, maxCount, numericDates); + }) + .isInstanceOf(InvalidRequestException.class) + .hasMessage("Something went wrong"); } } diff --git a/src/test/resources/listEvents.json b/src/test/resources/listEvents.json new file mode 100644 index 00000000..e8b484ff --- /dev/null +++ b/src/test/resources/listEvents.json @@ -0,0 +1,28 @@ +{ + "data": [ + { + "accessTokenName": "token name 1", + "action": "CREATE", + "eventId": "12345", + "eventTimestamp": "2023-01-30T11:42:30Z", + "objectId": "23o42843u2kwkhrejwrhkehrw", + "objectType": "ACCESS_TOKEN", + "requestUserId": 548798, + "source": "WEB_APP", + "userId": 548798 + }, + { + "accessTokenName": "token name 2", + "action": "UPDATE", + "eventId": "12345", + "eventTimestamp": "2023-8-28T11:42:30Z", + "objectId": "90348029kwwrkwri2094892", + "objectType": "ACCESS_TOKEN", + "requestUserId": 123456, + "source": "WEB_APP", + "userId": 123456 + } + ], + "moreAvailable": false, + "nextStreamPosition": "0" +} \ No newline at end of file