Skip to content

Commit 349baf2

Browse files
author
Holly Williams
committed
added tests for thank you screens
1 parent caed785 commit 349baf2

2 files changed

Lines changed: 100 additions & 4 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public HttpResponse displayComments(String surveyKey) {
117117
LOG.info("The user has commented: " + userComments);
118118
LOG.info("With surveyKey: " + surveyKey);
119119
// put comment into the db using the survey key
120-
saveUserComment(surveyKey, userComments);
120+
boolean commentSaved = saveUserComment(surveyKey, userComments);
121+
LOG.info("commentSaved: " + commentSaved);
121122

122123
return HttpResponse.ok();
123124
}
@@ -164,12 +165,19 @@ boolean saveResponse(String currentEmotion, String surveyKey) {
164165
return responseAdded;
165166
}
166167

167-
private void saveUserComment(String surveyKey, String comments) {
168+
boolean saveUserComment(String surveyKey, String comments) {
168169

170+
boolean commentsAdded = false;
169171
UserComments userComments = new UserComments();
170172
userComments.setCommentText(comments);
171173
userComments.setResponseKey(UUID.fromString(surveyKey));
172-
userCommentsRepo.save(userComments);
174+
userComments = userCommentsRepo.save(userComments);
175+
176+
LOG.info("Adding comment: " + comments);
177+
178+
if (userComments.getCommentId() != null) commentsAdded = true;
179+
180+
return commentsAdded;
173181
}
174182

175183
ResponseKey markKeyAsUsed(String surveyKey) {

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.objectcomputing.pulsesurvey.receive.responses;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.objectcomputing.pulsesurvey.email.manager.GmailSender;
46
import com.objectcomputing.pulsesurvey.model.Response;
57
import com.objectcomputing.pulsesurvey.model.ResponseKey;
8+
import com.objectcomputing.pulsesurvey.model.UserComments;
69
import com.objectcomputing.pulsesurvey.repositories.ResponseKeyRepository;
710
import com.objectcomputing.pulsesurvey.repositories.ResponseRepository;
11+
import com.objectcomputing.pulsesurvey.repositories.UserCommentsRepository;
812
import com.objectcomputing.pulsesurvey.send.surveys.SurveysControllerTest;
913
import com.objectcomputing.pulsesurvey.template.manager.SurveyTemplateManager;
1014
import io.micronaut.core.io.buffer.ByteBuffer;
15+
import io.micronaut.core.util.CollectionUtils;
1116
import io.micronaut.http.HttpRequest;
1217
import io.micronaut.http.HttpResponse;
1318
import io.micronaut.http.HttpStatus;
19+
import io.micronaut.http.MediaType;
1420
import io.micronaut.http.client.RxHttpClient;
1521
import io.micronaut.http.client.annotation.Client;
1622
import io.micronaut.test.annotation.MicronautTest;
@@ -25,6 +31,8 @@
2531

2632
import java.time.LocalDateTime;
2733
import java.time.Month;
34+
import java.util.HashMap;
35+
import java.util.Map;
2836
import java.util.Optional;
2937
import java.util.UUID;
3038

@@ -49,7 +57,7 @@ class SurveyResponseControllerTest {
4957

5058
ResponseKeyRepository mockResponseKeyRepository = mock(ResponseKeyRepository.class);
5159

52-
SurveyTemplateManager mockTemplateManager = mock(SurveyTemplateManager.class);
60+
UserCommentsRepository mockUserCommentsRepository = mock(UserCommentsRepository.class);
5361

5462
GmailSender mockGmailSender = mock(GmailSender.class);
5563

@@ -59,6 +67,7 @@ class SurveyResponseControllerTest {
5967
void setupTest() {
6068
itemUnderTest.setResponseRepo(mockResponseRepository);
6169
itemUnderTest.setResponseKeyRepo(mockResponseKeyRepository);
70+
itemUnderTest.setUserCommentsRepo(mockUserCommentsRepository);
6271
// itemUnderTest.setGmailApi(gmailApiMock);
6372
// itemUnderTest.setTemplateManager(mockTemplateManager);
6473
// itemUnderTest.setGmailSender(mockGmailSender);
@@ -67,6 +76,7 @@ void setupTest() {
6776
reset(mockResponseKeyRepository);
6877
reset(mockGmailSender);
6978
reset(mockResponseRepository);
79+
reset(mockUserCommentsRepository);
7080
}
7181

7282
@Test
@@ -205,4 +215,82 @@ void testMarkKeyAsUsed() {
205215
assertTrue(fakeResponseKey.isUsed());
206216
}
207217

218+
@Test
219+
void testDisplayComments() {
220+
221+
String surveyKey = "123";
222+
223+
HttpResponse<ByteBuffer> response = httpClient
224+
.exchange(HttpRequest.GET(String.format("/happiness/comment?&surveyKey=%s", surveyKey)))
225+
.blockingFirst();
226+
227+
assertEquals(HttpStatus.OK, response.getStatus());
228+
229+
}
230+
231+
@Test
232+
void testSaveUserComment() {
233+
234+
String fakeComments = "Here are some fake comments - Test";
235+
String surveyKey = "12345678-9123-4567-abcd-123456789abc";
236+
String fakeResponseKey = "98765432-9876-9876-9876-987654321234";
237+
238+
UserComments fakeUserComments = new UserComments();
239+
fakeUserComments.setCommentId(UUID.fromString(fakeResponseKey));
240+
fakeUserComments.setResponseKey(UUID.fromString(surveyKey));
241+
fakeUserComments.setCommentText(fakeComments);
242+
fakeUserComments.setCreatedOn(LocalDateTime.of(2020, Month.JANUARY, 27, 1, 1));
243+
244+
when(mockUserCommentsRepository.save(any())).thenReturn(fakeUserComments);
245+
246+
assertTrue(itemUnderTest.saveUserComment(surveyKey, fakeComments));
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+
//todo should we just call this directly?
271+
// HttpResponse resp = itemUnderTest.sendThankYouWithCommentBlock(userComments, surveyKey);
272+
// assertEquals(HttpStatus.OK, resp.getStatus());
273+
274+
HttpResponse response = httpClient
275+
.exchange(HttpRequest.POST("/happiness/userComments", fakeBody)
276+
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
277+
.blockingFirst();
278+
279+
assertEquals(HttpStatus.OK, response.getStatus());
280+
281+
}
282+
283+
@Test
284+
void testSendThankYou() {
285+
286+
String surveyKey = "123";
287+
288+
HttpResponse<ByteBuffer> response = httpClient
289+
.exchange(HttpRequest.GET(String.format("/happiness/thanks")))
290+
.blockingFirst();
291+
292+
assertEquals(HttpStatus.OK, response.getStatus());
293+
294+
}
295+
208296
}

0 commit comments

Comments
 (0)