Skip to content

Commit 0b2de66

Browse files
authored
Merge pull request #13 from objectcomputing/comments-tests
added tests for thank you screens
2 parents ca06301 + 07f1e25 commit 0b2de66

2 files changed

Lines changed: 100 additions & 14 deletions

File tree

send-surveys/src/main/java/com/objectcomputing/pulsesurvey/receive/responses/SurveyResponseController.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,10 @@ public HttpResponse sendThankYouWithCommentBlock (String userComments, String su
114114
LOG.info("The user has commented: " + userComments);
115115
LOG.info("With surveyKey: " + surveyKey);
116116
// put comment into the db using the survey key
117-
// first check to see if there is currently a comment
118-
boolean alreadyIsComment = checkForComment(surveyKey);
119-
if (!alreadyIsComment) {
120-
saveUserComment(surveyKey, userComments);
121-
return HttpResponse.ok();
122-
} else {
123-
return HttpResponse.ok("This key has already been used.");
124-
}
117+
118+
saveUserComment(surveyKey, userComments);
119+
120+
return HttpResponse.ok();
125121
}
126122

127123
@Get("thanks")
@@ -177,12 +173,18 @@ private boolean checkForComment(String surveyKey) {
177173

178174
}
179175

180-
private void saveUserComment(String surveyKey, String commentText) {
176+
void saveUserComment(String surveyKey, String comments) {
181177

178+
boolean commentsAdded = false;
182179
UserComments userComments = new UserComments();
183-
userComments.setCommentText(commentText);
180+
userComments.setCommentText(comments);
184181
userComments.setResponseKey(UUID.fromString(surveyKey));
185182
userComments = userCommentsRepo.save(userComments);
183+
184+
LOG.info("Adding comment: " + comments);
185+
186+
if (userComments.getCommentId() != null) commentsAdded = true;
187+
186188
}
187189

188190
ResponseKey markKeyAsUsed(String surveyKey) {

send-surveys/src/test/java/com/objectcomputing/pulsesurvey/receive/responses/SurveyResponseControllerTest.java

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import com.objectcomputing.pulsesurvey.email.manager.GmailSender;
44
import com.objectcomputing.pulsesurvey.model.Response;
55
import com.objectcomputing.pulsesurvey.model.ResponseKey;
6+
import com.objectcomputing.pulsesurvey.model.UserComments;
67
import com.objectcomputing.pulsesurvey.repositories.ResponseKeyRepository;
78
import com.objectcomputing.pulsesurvey.repositories.ResponseRepository;
9+
import com.objectcomputing.pulsesurvey.repositories.UserCommentsRepository;
810
import com.objectcomputing.pulsesurvey.send.surveys.SurveysControllerTest;
9-
import com.objectcomputing.pulsesurvey.template.manager.SurveyTemplateManager;
1011
import io.micronaut.core.io.buffer.ByteBuffer;
1112
import io.micronaut.http.HttpRequest;
1213
import io.micronaut.http.HttpResponse;
1314
import io.micronaut.http.HttpStatus;
15+
import io.micronaut.http.MediaType;
1416
import io.micronaut.http.client.RxHttpClient;
1517
import io.micronaut.http.client.annotation.Client;
1618
import io.micronaut.test.annotation.MicronautTest;
@@ -22,16 +24,21 @@
2224
import org.slf4j.LoggerFactory;
2325

2426
import javax.inject.Inject;
25-
2627
import java.time.LocalDateTime;
2728
import java.time.Month;
29+
import java.util.HashMap;
30+
import java.util.Map;
2831
import java.util.Optional;
2932
import java.util.UUID;
3033

31-
import static org.junit.jupiter.api.Assertions.*;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
3237
import static org.mockito.ArgumentMatchers.any;
3338
import static org.mockito.Mockito.mock;
3439
import static org.mockito.Mockito.reset;
40+
import static org.mockito.Mockito.times;
41+
import static org.mockito.Mockito.verify;
3542
import static org.mockito.Mockito.when;
3643

3744
@ExtendWith(SystemPropertyExtension.class)
@@ -49,7 +56,7 @@ class SurveyResponseControllerTest {
4956

5057
ResponseKeyRepository mockResponseKeyRepository = mock(ResponseKeyRepository.class);
5158

52-
SurveyTemplateManager mockTemplateManager = mock(SurveyTemplateManager.class);
59+
UserCommentsRepository mockUserCommentsRepository = mock(UserCommentsRepository.class);
5360

5461
GmailSender mockGmailSender = mock(GmailSender.class);
5562

@@ -59,6 +66,7 @@ class SurveyResponseControllerTest {
5966
void setupTest() {
6067
itemUnderTest.setResponseRepo(mockResponseRepository);
6168
itemUnderTest.setResponseKeyRepo(mockResponseKeyRepository);
69+
itemUnderTest.setUserCommentsRepo(mockUserCommentsRepository);
6270
// itemUnderTest.setGmailApi(gmailApiMock);
6371
// itemUnderTest.setTemplateManager(mockTemplateManager);
6472
// itemUnderTest.setGmailSender(mockGmailSender);
@@ -67,6 +75,7 @@ void setupTest() {
6775
reset(mockResponseKeyRepository);
6876
reset(mockGmailSender);
6977
reset(mockResponseRepository);
78+
reset(mockUserCommentsRepository);
7079
}
7180

7281
@Test
@@ -205,4 +214,79 @@ void testMarkKeyAsUsed() {
205214
assertTrue(fakeResponseKey.isUsed());
206215
}
207216

217+
@Test
218+
void testDisplayComments() {
219+
220+
String surveyKey = "123";
221+
222+
HttpResponse<ByteBuffer> response = httpClient
223+
.exchange(HttpRequest.GET(String.format("/happiness/comment?&surveyKey=%s", surveyKey)))
224+
.blockingFirst();
225+
226+
assertEquals(HttpStatus.OK, response.getStatus());
227+
228+
}
229+
230+
@Test
231+
void testSaveUserComment() {
232+
233+
String fakeComments = "Here are some fake comments - Test";
234+
String surveyKey = "12345678-9123-4567-abcd-123456789abc";
235+
String fakeResponseKey = "98765432-9876-9876-9876-987654321234";
236+
237+
UserComments fakeUserComments = new UserComments();
238+
fakeUserComments.setCommentId(UUID.fromString(fakeResponseKey));
239+
fakeUserComments.setResponseKey(UUID.fromString(surveyKey));
240+
fakeUserComments.setCommentText(fakeComments);
241+
fakeUserComments.setCreatedOn(LocalDateTime.of(2020, Month.JANUARY, 27, 1, 1));
242+
243+
when(mockUserCommentsRepository.save(any())).thenReturn(fakeUserComments);
244+
245+
itemUnderTest.saveUserComment(surveyKey, fakeComments);
246+
verify(mockUserCommentsRepository, times(1)).save(any(UserComments.class));
247+
248+
}
249+
250+
@Test
251+
void testSendThankYouWithCommentBlock() {
252+
253+
String userComments = "fake user comments";
254+
String fakeComments = "Here are some fake comments - Test";
255+
String surveyKey = "12345678-9123-4567-abcd-123456789abc";
256+
String fakeResponseKey = "98765432-9876-9876-9876-987654321234";
257+
258+
UserComments fakeUserComments = new UserComments();
259+
fakeUserComments.setCommentId(UUID.fromString(fakeResponseKey));
260+
fakeUserComments.setResponseKey(UUID.fromString(surveyKey));
261+
fakeUserComments.setCommentText(fakeComments);
262+
fakeUserComments.setCreatedOn(LocalDateTime.of(2020, Month.JANUARY, 27, 1, 1));
263+
Map<String, String> fakeBody = new HashMap<String, String>() {{
264+
put("userComments",userComments);
265+
put("surveyKey", surveyKey);
266+
}};
267+
268+
when(mockUserCommentsRepository.save(any())).thenReturn(fakeUserComments);
269+
270+
HttpResponse response = httpClient
271+
.exchange(HttpRequest.POST("/happiness/userComments", fakeBody)
272+
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
273+
.blockingFirst();
274+
275+
assertEquals(HttpStatus.OK, response.getStatus());
276+
277+
}
278+
279+
@Test
280+
void testSendThankYou() {
281+
282+
String surveyKey = "123";
283+
284+
HttpResponse<ByteBuffer> response = httpClient
285+
.exchange(HttpRequest.GET(String.format("/happiness/thanks")))
286+
.blockingFirst();
287+
288+
assertEquals(HttpStatus.OK, response.getStatus());
289+
290+
}
291+
208292
}

0 commit comments

Comments
 (0)