11package com .objectcomputing .pulsesurvey .receive .responses ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
35import com .objectcomputing .pulsesurvey .email .manager .GmailSender ;
46import com .objectcomputing .pulsesurvey .model .Response ;
57import com .objectcomputing .pulsesurvey .model .ResponseKey ;
8+ import com .objectcomputing .pulsesurvey .model .UserComments ;
69import com .objectcomputing .pulsesurvey .repositories .ResponseKeyRepository ;
710import com .objectcomputing .pulsesurvey .repositories .ResponseRepository ;
11+ import com .objectcomputing .pulsesurvey .repositories .UserCommentsRepository ;
812import com .objectcomputing .pulsesurvey .send .surveys .SurveysControllerTest ;
913import com .objectcomputing .pulsesurvey .template .manager .SurveyTemplateManager ;
1014import io .micronaut .core .io .buffer .ByteBuffer ;
15+ import io .micronaut .core .util .CollectionUtils ;
1116import io .micronaut .http .HttpRequest ;
1217import io .micronaut .http .HttpResponse ;
1318import io .micronaut .http .HttpStatus ;
19+ import io .micronaut .http .MediaType ;
1420import io .micronaut .http .client .RxHttpClient ;
1521import io .micronaut .http .client .annotation .Client ;
1622import io .micronaut .test .annotation .MicronautTest ;
2531
2632import java .time .LocalDateTime ;
2733import java .time .Month ;
34+ import java .util .HashMap ;
35+ import java .util .Map ;
2836import java .util .Optional ;
2937import 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