-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathIdentityControllerTest.java
More file actions
407 lines (362 loc) · 17.2 KB
/
IdentityControllerTest.java
File metadata and controls
407 lines (362 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.identity.controller;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.annotation.Description;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.iemr.common.identity.domain.Address;
import com.iemr.common.identity.dto.BenFamilyDTO;
import com.iemr.common.identity.dto.BenIdentityDTO;
import com.iemr.common.identity.dto.BenServiceDTO;
import com.iemr.common.identity.dto.BeneficiariesDTO;
import com.iemr.common.identity.dto.IdentityEditDTO;
import com.iemr.common.identity.dto.IdentitySearchDTO;
import com.iemr.common.identity.dto.ReserveIdentityDTO;
import com.iemr.common.identity.dto.SearchSyncDTO;
import com.iemr.common.identity.exception.MissingMandatoryFieldsException;
import com.iemr.common.identity.service.IdentityService;
import jakarta.persistence.NoResultException;
import jakarta.persistence.QueryTimeoutException;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
@ExtendWith(MockitoExtension.class)
class IdentityControllerTest {
@InjectMocks
IdentityController identityController;
@Mock
IdentityService svc;
@Test
@Description("Tests successful retrieval of all beneficiaries. (TC_GetBeneficiaries_Success_001)")
void testGetBeneficiaries() throws NoResultException, QueryTimeoutException, Exception {
IdentitySearchDTO searchParams = new IdentitySearchDTO();
BeneficiariesDTO a1 = new BeneficiariesDTO();
makeBeneficiariesDTO(a1);
List<BeneficiariesDTO> bdList = Lists.newArrayList();
a1.setBenId(new BigInteger("1"));
bdList.add(a1);
String req = new Gson().toJson(searchParams);
// when(svc.getBeneficiaries(searchParams)).thenReturn(any()).thenReturn(bdList);
String resp = identityController.getBeneficiaries(req);
Assertions.assertNotNull(resp);
}
private void makeBeneficiariesDTO(BeneficiariesDTO dto) {
dto.setAbhaDetails(null);
dto.setAccountNo(null);
dto.setAgeAtMarriage(null);
dto.setBankName(null);
dto.setBenAccountID(null);
dto.setBeneficiaryAge(null);
dto.setBeneficiaryDetails(null);
List<BenFamilyDTO> tags = new ArrayList<>();
BenFamilyDTO benFamilyDTO = new BenFamilyDTO();
benFamilyDTO.setAssociatedBenRegId(null);
benFamilyDTO.setBenFamilyMapId(null);
benFamilyDTO.setCreatedBy(null);
benFamilyDTO.setCreatedDate(null);
benFamilyDTO.setDeleted(null);
benFamilyDTO.setIsEmergencyContact(true);
benFamilyDTO.setLastModDate(null);
benFamilyDTO.setModifiedBy(null);
benFamilyDTO.setParkingPlaceID(null);
benFamilyDTO.setRelationshipID(null);
benFamilyDTO.setRelationshipToSelf(null);
benFamilyDTO.setVanID(null);
tags.add(benFamilyDTO);
dto.setBeneficiaryFamilyTags(tags);
List<BenIdentityDTO> identity = new ArrayList<>();
BenIdentityDTO benIdentityDTO = new BenIdentityDTO();
benIdentityDTO.toString();
identity.add(benIdentityDTO);
dto.setBeneficiaryIdentites(identity);
List<BenServiceDTO> service = new ArrayList<>();
BenServiceDTO benServiceDTO = new BenServiceDTO();
benServiceDTO.toString();
service.add(benServiceDTO);
dto.setBeneficiaryServiceMap(service);
dto.setBenId(null);
dto.setBenMapId(null);
dto.setBenRegId(null);
dto.setBranchName(null);
dto.setContacts(null);
dto.setCreatedBy(null);
dto.setCreatedDate(null);
Address address = new Address();
address.toString();
address.equals(address);
dto.setCurrentAddress(null);
dto.setDeleted(null);
dto.setEmail(null);
dto.setEmergencyAddress(address);
dto.setEmergencyContactNum(null);
dto.setEmergencyContactTyp(null);
dto.setIfscCode(null);
dto.setIncomeStatus(null);
dto.setIsHIVPos(null);
dto.setLastModDate(null);
dto.setLiteracyStatus(null);
dto.setMarriageDate(null);
dto.setModifiedBy(null);
dto.setMonthlyFamilyIncome(null);
dto.setMotherName(null);
dto.setOccupationName(null);
dto.setOccupationId(null);
dto.setPermanentAddress(address);
dto.setPreferredEmailId(null);
dto.setPreferredPhoneNum(null);
dto.setPreferredPhoneTyp(null);
dto.setPreferredSMSPhoneNum(null);
dto.setPreferredSMSPhoneTyp(null);
dto.setReligion(null);
dto.setReligionId(null);
dto.setSourceOfInformation(null);
dto.toString();
BeneficiariesDTO beneficiariesDTO = new BeneficiariesDTO();
dto.equals(beneficiariesDTO);
}
@Test
@Description("Tests successful retrieval of beneficiaries by beneficiary registration ID. (TC_GetBeneficiariesByBeneficiaryRegId_Success_001)")
void testGetBeneficiariesByBeneficiaryRegId() {
String resp = identityController.getBeneficiariesByBeneficiaryRegId("123");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the getBeneficiariesByBeneficiaryRegId method. (TC_GetBeneficiariesByBeneficiaryRegId_Exception_002)")
void testGetBeneficiariesByBeneficiaryRegIdCatchBlock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.getBeneficiariesByBenRegId(any())).thenThrow(NoResultException.class);
String resp = identityController.getBeneficiariesByBeneficiaryRegId("123");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by beneficiary ID. (TC_GetBeneficiariesByBeneficiaryId_Success_001)")
void testGetBeneficiariesByBeneficiaryId() {
String resp = identityController.getBeneficiariesByBeneficiaryId("987");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the getBeneficiariesByBeneficiaryId method. (TC_GetBeneficiariesByBeneficiaryId_Exception_002)")
void testGetBeneficiariesByBeneficiaryIdCatchBlock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.getBeneficiariesByBenId(any())).thenThrow(NoResultException.class);
String resp = identityController.getBeneficiariesByBeneficiaryId("987");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by phone number. (TC_GetBeneficiariesByPhoneNum_Success_001)")
void testGetBeneficiariesByPhoneNum() {
String resp = identityController.getBeneficiariesByPhoneNum("9988776655");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the getBeneficiariesByPhoneNum method. (TC_GetBeneficiariesByPhoneNum_Exception_002)")
void testGetBeneficiariesByPhoneNumCatchblock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.getBeneficiariesByPhoneNum(any())).thenThrow(NoResultException.class);
String resp = identityController.getBeneficiariesByPhoneNum("9988776655");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by ABHA address. (TC_SearhBeneficiaryByABHAAddress_Success_001)")
void testSearhBeneficiaryByABHAAddress() {
String resp = identityController.searhBeneficiaryByABHAAddress("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the searchBeneficiaryByABHAAddress method. (TC_SearhBeneficiaryByABHAAddress_Exception_002)")
void testSearhBeneficiaryByABHAAddressCatchblock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.getBeneficiaryByHealthIDAbhaAddress(any())).thenThrow(NoResultException.class);
String resp = identityController.searhBeneficiaryByABHAAddress("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by ABHA ID. (TC_SearhBeneficiaryByABHAIdNo_Success_001)")
void testSearhBeneficiaryByABHAIdNo() {
String resp = identityController.searhBeneficiaryByABHAIdNo("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the searchBeneficiaryByABHAIdNo method. (TC_SearhBeneficiaryByABHAIdNo_Exception_002)")
void testSearhBeneficiaryByABHAIdNoCatchblock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.getBeneficiaryByHealthIDNoAbhaIdNo(any())).thenThrow(NoResultException.class);
String resp = identityController.searhBeneficiaryByABHAIdNo("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by family ID. (TC_SearhBeneficiaryByFamilyId_Success_001)")
void testSearhBeneficiaryByFamilyId() {
String resp = identityController.searhBeneficiaryByFamilyId("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests exception handling within the searchBeneficiaryByFamilyId method. (TC_SearhBeneficiaryByFamilyId_Exception_002)")
void testSearhBeneficiaryByFamilyIdCatchblock() throws NoResultException, QueryTimeoutException, Exception {
when(svc.searhBeneficiaryByFamilyId(any())).thenThrow(NoResultException.class);
String resp = identityController.searhBeneficiaryByFamilyId("9876");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of beneficiaries by block ID and last modified date. (TC_SearchBeneficiaryByBlockIdAndLastModDate_Success_001)")
void testSearchBeneficiaryByBlockIdAndLastModDate() {
BeneficiariesDTO a1 = new BeneficiariesDTO();
List<BeneficiariesDTO> bdList = Lists.newArrayList();
a1.setBenId(new BigInteger("1"));
bdList.add(a1);
SearchSyncDTO searchSyncDTO = new SearchSyncDTO();
String req = new Gson().toJson(searchSyncDTO);
// when(svc.searchBeneficiaryByBlockIdAndLastModifyDate(any(),
// any())).thenReturn(bdList);
String resp = identityController.searchBeneficiaryByVillageIdAndLastModDate(req);
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests the behavior of the reserveIdentity method when an empty identity object is provided. (TC_ReserveIdentityEmptyIdentity_001)")
void testReserveIdentityEmptyIdentity() throws ParseException {
String resp = identityController.reserveIdentity("String");
String status = getData(resp, "statusMessage");
Assertions.assertNotEquals("Null/Empty Identity Create Data.", status);
}
@Test
@Description("Tests successful reservation of an identity. (TC_ReserveIdentity_Success_002)")
void testReserveIdentity() throws ParseException {
ReserveIdentityDTO reserveIdentityDTO = new ReserveIdentityDTO();
String req = new Gson().toJson(reserveIdentityDTO);
String resp = identityController.reserveIdentity(req);
String status = getData(resp, "statusMessage");
Assertions.assertEquals("success", status);
}
@Test
@Description("Tests the behavior of the unreserveIdentity method when an empty identity object is provided. (TC_UnreserveIdentityEmptyIdentity_001)")
void testUnreserveIdentityEmptyIdentity() throws ParseException {
String resp = identityController.unreserveIdentity("String");
String status = getData(resp, "statusMessage");
Assertions.assertNotEquals("Null/Empty Identity Create Data.", status);
}
@Test
@Description("Tests successful unreservation of an identity. (TC_UnreserveIdentity_Success_002)")
void testUnreserveIdentity() throws ParseException {
ReserveIdentityDTO reserveIdentityDTO = new ReserveIdentityDTO();
String req = new Gson().toJson(reserveIdentityDTO);
String resp = identityController.unreserveIdentity(req);
String status = getData(resp, "statusMessage");
Assertions.assertEquals("success", status);
}
@Test
@Description("Tests exception handling within the getFiniteBeneficiaries method when an invalid JSON is provided. (TC_GetFiniteBeneficiariesCatchblockIfInvalidJSON_001)")
void testGetFiniteBeneficiariesCatchblockIfInvalidJSON() {
String resp = identityController.getFiniteBeneficiaries("String");
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful retrieval of a finite number of beneficiaries. (TC_GetFiniteBeneficiaries_Success_002)")
void testGetFiniteBeneficiaries() throws ParseException {
IdentitySearchDTO identitySearchDTO = new IdentitySearchDTO();
String req = new Gson().toJson(identitySearchDTO);
String resp = identityController.getFiniteBeneficiaries(req);
String status = getData(resp, "statusMessage");
Assertions.assertEquals("success", status);
}
@Test
@Description("Tests the handling of a NoResultException within the getFiniteBeneficiaries method. (TC_GetFiniteBeneficiariesNoResultException_003)")
void testGetFiniteBeneficiariesNoResultException() throws NoResultException, QueryTimeoutException, Exception {
IdentitySearchDTO identitySearchDTO = new IdentitySearchDTO();
String req = new Gson().toJson(identitySearchDTO);
when(svc.getBeneficiaries(identitySearchDTO)).thenThrow(NoResultException.class);
String resp = identityController.getFiniteBeneficiaries(req);
String status = getData(resp, "statusMessage");
Assertions.assertEquals("failure", status);
}
@Test
@Description("Tests the handling of a QueryTimeoutException within the getFiniteBeneficiaries method. (TC_GetFiniteBeneficiariesQueryTimeoutException_004)")
void testGetFiniteBeneficiariesQueryTimeoutException() throws NoResultException, QueryTimeoutException, Exception {
IdentitySearchDTO identitySearchDTO = new IdentitySearchDTO();
String req = new Gson().toJson(identitySearchDTO);
when(svc.getBeneficiaries(identitySearchDTO)).thenThrow(QueryTimeoutException.class);
String resp = identityController.getFiniteBeneficiaries(req);
String status = getData(resp, "statusMessage");
Assertions.assertEquals("failure", status);
}
@Test
@Description("Tests successful retrieval of beneficiary image by beneficiary registration ID. (TC_GetBeneficiaryImageByBenRegID_Success_001)")
void testGetBeneficiaryImageByBenRegID() {
String resp = identityController.getBeneficiaryImageByBenRegID("String");
Assertions.assertNull(resp);
}
@Test
@Description("Tests successful editing of identity information related to education or community details. (TC_EditIdentityEducationOrCommunity_Success_001)")
void testEditIdentityEducationOrCommunity() throws ParseException {
IdentityEditDTO identityEditDTO = new IdentityEditDTO();
String req = new Gson().toJson(identityEditDTO);
String resp = identityController.editIdentityEducationOrCommunity(req);
String actualresp = getData(resp, "data");
Assertions.assertEquals("Updated successfully", actualresp);
}
private String getData(String resp, String status) throws ParseException {
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(resp);
JSONObject object = (JSONObject) json.get("response");
String actualresp = object.getAsString(status);
return actualresp;
}
@Test
@Description("Tests exception handling within the editIdentityEducationOrCommunity method. (TC_EditIdentityEducationOrCommunity_Exception_002)")
void testEditIdentityEducationOrCommunityCatchblock() throws MissingMandatoryFieldsException, ParseException {
IdentityEditDTO identityEditDTO = new IdentityEditDTO();
String req = new Gson().toJson(identityEditDTO);
doThrow(MissingMandatoryFieldsException.class).when(svc).editIdentityEducationOrCommunity(identityEditDTO);
svc.editIdentityEducationOrCommunity(any());
String resp = identityController.editIdentityEducationOrCommunity(req);
String actualresp = getData(resp, "data");
Assertions.assertNotEquals("Updated successfully", actualresp);
}
@Test
@Description("Tests the availability of a beneficiary ID on the local server. (TC_CheckAvailablBenIDLocalServer_Success_001)")
void testCheckAvailablBenIDLocalServer() {
String resp = identityController.checkAvailablBenIDLocalServer();
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests successful saving of a generated beneficiary ID to the local server. (TC_SaveGeneratedBenIDToLocalServer_Success_001)")
void testSaveGeneratedBenIDToLocalServer() {
String resp = identityController.saveGeneratedBenIDToLocalServer(null);
Assertions.assertNotNull(resp);
}
@Test
@Description("Tests counting beneficiaries based on village ID and last modified date. (TC_countBeneficiaryByVillageIdAndLastModDate_Success_001)")
public void testcountBeneficiaryByVillageIdAndLastModDate() {
SearchSyncDTO searchSyncDTO = new SearchSyncDTO();
searchSyncDTO.setLastModifiedDate(9l);
String json = new Gson().toJson(searchSyncDTO);
when(svc.countBeneficiaryByVillageIdAndLastModifyDate(any(), any())).thenReturn(9l);
String resp = identityController.countBeneficiaryByVillageIdAndLastModDate(json);
Assertions.assertNotNull(resp);
}
}