Skip to content

Commit

Permalink
added tests for EventResourcesImplTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyanka Lakhe committed Sep 12, 2023
1 parent e68dfdd commit ef47946
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

}
28 changes: 28 additions & 0 deletions src/test/resources/listEvents.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit ef47946

Please sign in to comment.