33import com .objectcomputing .pulsesurvey .email .manager .GmailSender ;
44import com .objectcomputing .pulsesurvey .model .Response ;
55import com .objectcomputing .pulsesurvey .model .ResponseKey ;
6+ import com .objectcomputing .pulsesurvey .model .UserComments ;
67import com .objectcomputing .pulsesurvey .repositories .ResponseKeyRepository ;
78import com .objectcomputing .pulsesurvey .repositories .ResponseRepository ;
9+ import com .objectcomputing .pulsesurvey .repositories .UserCommentsRepository ;
810import com .objectcomputing .pulsesurvey .send .surveys .SurveysControllerTest ;
9- import com .objectcomputing .pulsesurvey .template .manager .SurveyTemplateManager ;
1011import io .micronaut .core .io .buffer .ByteBuffer ;
1112import io .micronaut .http .HttpRequest ;
1213import io .micronaut .http .HttpResponse ;
1314import io .micronaut .http .HttpStatus ;
15+ import io .micronaut .http .MediaType ;
1416import io .micronaut .http .client .RxHttpClient ;
1517import io .micronaut .http .client .annotation .Client ;
1618import io .micronaut .test .annotation .MicronautTest ;
2224import org .slf4j .LoggerFactory ;
2325
2426import javax .inject .Inject ;
25-
2627import java .time .LocalDateTime ;
2728import java .time .Month ;
29+ import java .util .HashMap ;
30+ import java .util .Map ;
2831import java .util .Optional ;
2932import 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 ;
3237import static org .mockito .ArgumentMatchers .any ;
3338import static org .mockito .Mockito .mock ;
3439import static org .mockito .Mockito .reset ;
40+ import static org .mockito .Mockito .times ;
41+ import static org .mockito .Mockito .verify ;
3542import 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