11package com .objectcomputing .pulsesurvey .send .surveys ;
22
3+ import com .objectcomputing .pulsesurvey .email .manager .GmailSender ;
34import com .objectcomputing .pulsesurvey .model .SendSurveysCommand ;
5+ import com .objectcomputing .pulsesurvey .repositories .ResponseKeyRepository ;
6+ import com .objectcomputing .pulsesurvey .template .manager .SurveyTemplateManager ;
47import io .micronaut .test .annotation .MicronautTest ;
58import com .objectcomputing .pulsesurvey .model .ResponseKey ;
69import net .bytebuddy .utility .RandomString ;
912import org .junit .jupiter .api .Test ;
1013import org .junit .jupiter .api .extension .ExtendWith ;
1114import org .junitpioneer .jupiter .SystemPropertyExtension ;
15+ import org .mockito .ArgumentMatchers ;
1216import org .slf4j .Logger ;
1317import org .slf4j .LoggerFactory ;
1418
1519import javax .inject .Inject ;
1620
21+ import java .io .IOException ;
1722import java .time .LocalDateTime ;
1823import java .util .ArrayList ;
1924import java .util .HashMap ;
2631import static org .junit .jupiter .api .Assertions .assertTrue ;
2732import static org .hamcrest .MatcherAssert .assertThat ;
2833import static org .hamcrest .CoreMatchers .containsString ;
34+ import static org .mockito .ArgumentMatchers .any ;
2935import static org .mockito .Mockito .mock ;
3036import static org .mockito .Mockito .reset ;
37+ //import static org.mockito.Mockito.times;
38+ import static org .mockito .Mockito .verify ;
3139import static org .mockito .Mockito .when ;
40+ import static org .mockito .internal .verification .VerificationModeFactory .times ;
3241
3342@ ExtendWith (SystemPropertyExtension .class )
3443@ MicronautTest
@@ -41,46 +50,78 @@ public class SurveysControllerTest {
4150
4251 SurveysController .GmailApi gmailApiMock = mock (SurveysController .GmailApi .class );
4352
53+ ResponseKeyRepository mockRepository = mock (ResponseKeyRepository .class );
54+
55+ SurveyTemplateManager mockTemplateManager = mock (SurveyTemplateManager .class );
56+
57+ GmailSender mockGmailSender = mock (GmailSender .class );
58+
4459 @ BeforeEach
4560 void setupTest () {
4661 itemUnderTest .setGmailApi (gmailApiMock );
62+ itemUnderTest .setResponseKeyRepo (mockRepository );
63+ itemUnderTest .setTemplateManager (mockTemplateManager );
64+ itemUnderTest .setGmailSender (mockGmailSender );
4765 reset (gmailApiMock );
66+ reset (mockRepository );
67+ reset (mockTemplateManager );
68+ reset (mockGmailSender );
4869 }
4970
50- List <String > generateEmails (int size ) {
71+ List <String > generateFakeEmails (int size ) {
5172 List <String > toReturn = new ArrayList <>(size );
5273 for (int count = 0 ; count < size ; count ++) {
5374 toReturn .add (RandomString .make (8 ) + "@" + RandomString .make (10 ) + ".com" );
5475 }
5576 return toReturn ;
5677 }
5778
58- List <ResponseKey > generateResponseKeys (int size ) {
79+ List <ResponseKey > generateFakeResponseKeys (int size ) {
5980 List <ResponseKey > toReturn = new ArrayList <>(size );
6081 for (int count = 0 ; count < size ; count ++) {
6182 toReturn .add (new ResponseKey (UUID .randomUUID (), LocalDateTime .now ()));
6283 }
6384 return toReturn ;
6485 }
6586
66- static final int PERCENT_OF_EMAILS = 10 ;
87+ // static final int PERCENT_OF_EMAILS = 10;
6788
6889 @ Test
6990 void testSendEmails_ReportsCorrectNumber () {
7091 String percentOfEmails = "10" ;
7192 double doublePercentOfEmails = parseDouble (percentOfEmails );
7293 LOG .info ("Using doublePercentOfEmails: " + doublePercentOfEmails );
73- List <String > fakeEmails = generateEmails ((int )(Math .random ()*100 )%50 );
94+ List <String > fakeEmails = generateFakeEmails ((int )(Math .random ()*100 )%50 );
7495 LOG .info ("Using addresses: " + fakeEmails .size ());
7596 final int numberOfEmailsToBeSent = (int ) Math
7697 .ceil (fakeEmails .size () * parseDouble (percentOfEmails ) / 100.0 );
98+ final int numkeys = (int )(Math .random ()*100 )%100 ;
7799 LOG .info ("Using numberOfEmailsToBeSent: " + numberOfEmailsToBeSent );
100+
78101 when (gmailApiMock .getEmails ()).thenReturn (fakeEmails );
79102
103+ List <ResponseKey > fakeKeys = generateFakeResponseKeys (numkeys );
104+ when (mockRepository .saveAll (any ())).thenReturn (fakeKeys );
105+
106+ String fakeTemplateName = "not a real template" ;
107+ Map <String , String > fakeEmailKeyMap = new HashMap <>();
108+ fakeEmailKeyMap .put ("one@fake.email" , "fake key 1" );
109+ fakeEmailKeyMap .put ("two@fake.email" , "fake key 2" );
110+ Map <String , String > fakeEmailBodiesMap = new HashMap <>();
111+ fakeEmailBodiesMap .put ("one@fake.email" , "fake email body 1" );
112+ fakeEmailBodiesMap .put ("two@fake.email" , "fake email body 2" );
113+
114+ try {
115+ when (mockTemplateManager .populateEmails (fakeTemplateName , fakeEmailKeyMap )).thenReturn (fakeEmailBodiesMap );
116+ } catch (IOException e ) {
117+ e .printStackTrace ();
118+ }
119+
80120 SendSurveysCommand sendSurveysCommand = new SendSurveysCommand ();
81121 sendSurveysCommand .setTemplateName ("emailTemplate" );
82122 sendSurveysCommand .setPercentOfEmails (percentOfEmails );
83123 SendSurveys sent = itemUnderTest .sendEmails (sendSurveysCommand );
124+
84125 assertThat (sent .getName (), containsString ("Sent surveys:" ));
85126 assertThat (sent .getName (), containsString ("Sent surveys: " +numberOfEmailsToBeSent ));
86127 }
@@ -91,7 +132,7 @@ void testSendEmails_ReportsCorrectNumber() {
91132 **/
92133 @ Test
93134 void testGetTotalNumberOfAvailableEmailAddresses () {
94- List <String > fakeEmails = generateEmails ((int )(Math .random ()*100 )%50 );
135+ List <String > fakeEmails = generateFakeEmails ((int )(Math .random ()*100 )%50 );
95136 LOG .info ("Using addresses: " + fakeEmails .size ());
96137 when (gmailApiMock .getEmails ()).thenReturn (fakeEmails );
97138 assertEquals (fakeEmails .size (), itemUnderTest .getTotalNumberOfAvailableEmailAddresses ());
@@ -103,7 +144,7 @@ void testGetTotalNumberOfAvailableEmailAddresses() {
103144 @ Test
104145 void testGetRandomEmailAddresses_CorrectNumber () {
105146 final int percentOfEmailsNeeded = 10 ;
106- List <String > fakeEmails = generateEmails ((int )(Math .random ()*100 )%50 );
147+ List <String > fakeEmails = generateFakeEmails ((int )(Math .random ()*100 )%50 );
107148 LOG .info ("Using addresses: " + fakeEmails .size ());
108149 final long numberOfEmailsToBeSent = (long ) Math
109150 .ceil (fakeEmails .size () * (double ) percentOfEmailsNeeded / 100.0 );
@@ -118,7 +159,7 @@ void testGetRandomEmailAddresses_CorrectNumber() {
118159 @ Test
119160 void testGetRandomEmailAddresses_UniqueAddresses () {
120161 final int percentOfEmailsNeeded = 10 ;
121- List <String > fakeEmails = generateEmails ((int )(Math .random ()*100 )%50 );
162+ List <String > fakeEmails = generateFakeEmails ((int )(Math .random ()*100 )%50 );
122163 LOG .info ("Using addresses: " + fakeEmails .size ());
123164 final long numberOfEmailsToBeSent = (long ) Math
124165 .ceil (fakeEmails .size () * (double ) percentOfEmailsNeeded / 100.0 );
@@ -134,7 +175,7 @@ void testGetRandomEmailAddresses_UniqueAddresses() {
134175 // contains("a", "b", "c"));
135176 @ Test
136177 void testGetRandomEmailAddresses_SubsetOfOriginal () {
137- List <String > fakeEmails = generateEmails ((int )(Math .random ()*100 )%50 );
178+ List <String > fakeEmails = generateFakeEmails ((int )(Math .random ()*100 )%50 );
138179 LOG .info ("Using addresses: " + fakeEmails .size ());
139180 final int percentOfEmailsNeeded = 10 ;
140181
@@ -152,6 +193,10 @@ void testGenerateAndSaveKeys() {
152193 final int numkeys = (int )(Math .random ()*100 )%100 ;
153194 LOG .info ("Generating keys: " + numkeys );
154195
196+ List <ResponseKey > fakeKeys = generateFakeResponseKeys (numkeys );
197+
198+ when (mockRepository .saveAll (any ())).thenReturn (fakeKeys );
199+
155200 List <ResponseKey > actual = itemUnderTest .generateAndSaveKeys (numkeys );
156201 assertEquals (numkeys , actual .size ());
157202 }
@@ -162,8 +207,8 @@ void testGenerateAndSaveKeys() {
162207 void testMapEmailsToKeys_CorrectNumber () {
163208 int numberOfEmails = (int )(Math .random ()*100 )%50 ;
164209 LOG .info ("Mapping emails: " + numberOfEmails );
165- List <String > fakeEmails = generateEmails (numberOfEmails );
166- List <ResponseKey > fakeKeys = generateResponseKeys (numberOfEmails );
210+ List <String > fakeEmails = generateFakeEmails (numberOfEmails );
211+ List <ResponseKey > fakeKeys = generateFakeResponseKeys (numberOfEmails );
167212
168213 when (gmailApiMock .getEmails ()).thenReturn (fakeEmails );
169214
@@ -178,8 +223,8 @@ void testMapEmailsToKeys_CorrectNumber() {
178223 void testMapEmailsToKeys_ContainsCorrectKeys () {
179224 int numberOfEmails = (int )(Math .random ()*100 )%50 ;
180225 LOG .info ("Mapping emails: " + numberOfEmails );
181- List <String > fakeEmails = generateEmails (numberOfEmails );
182- List <ResponseKey > fakeKeys = generateResponseKeys (numberOfEmails );
226+ List <String > fakeEmails = generateFakeEmails (numberOfEmails );
227+ List <ResponseKey > fakeKeys = generateFakeResponseKeys (numberOfEmails );
183228
184229 when (gmailApiMock .getEmails ()).thenReturn (fakeEmails );
185230
@@ -193,13 +238,16 @@ void testMapEmailsToKeys_ContainsCorrectKeys() {
193238 // * no idea how to test this one *
194239 @ Test
195240 void testSendTheEmails () {
196- // * no idea how to test this one *
241+ //todo mock gmailsender
197242 Map <String , String > fakeEmailMap = new HashMap <String , String >();
198243 fakeEmailMap .put ("a@dnc.com" ,"a bunch of fake html" );
199244 fakeEmailMap .put ("b@dnc.com" ,"more fake html" );
200- //call sendTheEmails with the email body map <emailaddress, emailbody>
245+
201246 itemUnderTest .sendTheEmails (fakeEmailMap );
202247
248+ verify (mockGmailSender ,
249+ times (fakeEmailMap .size ())).sendEmail (any (String .class ), any (String .class ), any (String .class ));
250+
203251 }
204252
205253}
0 commit comments