From 61cc8a8b2a85e6e0e42d407c952ceedf9cf19100 Mon Sep 17 00:00:00 2001 From: nkokkiligadda87 Date: Tue, 13 Aug 2024 22:29:50 +0530 Subject: [PATCH] junit test case --- .../controller/IdentityControllerTest.java | 86 +++++++++++++------ .../rmnch/RMNCHMobileAppControllerTest.java | 12 +++ 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/test/java/com/iemr/common/identity/controller/IdentityControllerTest.java b/src/test/java/com/iemr/common/identity/controller/IdentityControllerTest.java index 640864ee..bef23415 100644 --- a/src/test/java/com/iemr/common/identity/controller/IdentityControllerTest.java +++ b/src/test/java/com/iemr/common/identity/controller/IdentityControllerTest.java @@ -35,6 +35,7 @@ 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; @@ -56,7 +57,6 @@ import net.minidev.json.parser.JSONParser; import net.minidev.json.parser.ParseException; - @ExtendWith(MockitoExtension.class) class IdentityControllerTest { @InjectMocks @@ -65,17 +65,18 @@ class IdentityControllerTest { 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 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); + // when(svc.getBeneficiaries(searchParams)).thenReturn(any()).thenReturn(bdList); String resp = identityController.getBeneficiaries(req); Assertions.assertNotNull(resp); @@ -103,17 +104,17 @@ private void makeBeneficiariesDTO(BeneficiariesDTO dto) { benFamilyDTO.setRelationshipID(null); benFamilyDTO.setRelationshipToSelf(null); benFamilyDTO.setVanID(null); - + tags.add(benFamilyDTO); dto.setBeneficiaryFamilyTags(tags); List identity = new ArrayList<>(); - BenIdentityDTO benIdentityDTO= new BenIdentityDTO(); + BenIdentityDTO benIdentityDTO = new BenIdentityDTO(); benIdentityDTO.toString(); identity.add(benIdentityDTO); dto.setBeneficiaryIdentites(identity); - + List service = new ArrayList<>(); - BenServiceDTO benServiceDTO= new BenServiceDTO(); + BenServiceDTO benServiceDTO = new BenServiceDTO(); benServiceDTO.toString(); service.add(benServiceDTO); dto.setBeneficiaryServiceMap(service); @@ -160,12 +161,14 @@ private void makeBeneficiariesDTO(BeneficiariesDTO dto) { } @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"); @@ -173,11 +176,14 @@ void testGetBeneficiariesByBeneficiaryRegIdCatchBlock() throws NoResultException } @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"); @@ -185,34 +191,44 @@ void testGetBeneficiariesByBeneficiaryIdCatchBlock() throws NoResultException, Q } @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"); @@ -220,11 +236,14 @@ void testSearhBeneficiaryByABHAIdNoCatchblock() throws NoResultException, QueryT } @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"); @@ -232,6 +251,7 @@ void testSearhBeneficiaryByFamilyIdCatchblock() throws NoResultException, QueryT } @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 bdList = Lists.newArrayList(); @@ -239,23 +259,24 @@ void testSearchBeneficiaryByBlockIdAndLastModDate() { bdList.add(a1); SearchSyncDTO searchSyncDTO = new SearchSyncDTO(); String req = new Gson().toJson(searchSyncDTO); - //when(svc.searchBeneficiaryByBlockIdAndLastModifyDate(any(), any())).thenReturn(bdList); + // 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); @@ -265,13 +286,16 @@ void testReserveIdentity() throws ParseException { } @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); @@ -280,17 +304,15 @@ void testUnreserveIdentity() throws ParseException { Assertions.assertEquals("success", status); } - //@Test - void testGetJsonAsString() { - //identityController. - } - @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); @@ -298,8 +320,9 @@ void testGetFiniteBeneficiaries() throws ParseException { 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); @@ -308,7 +331,9 @@ void testGetFiniteBeneficiariesNoResultException() throws NoResultException, Que 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); @@ -317,55 +342,60 @@ void testGetFiniteBeneficiariesQueryTimeoutException() throws NoResultException, 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"); + 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); + + 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"); + 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); diff --git a/src/test/java/com/iemr/common/identity/controller/rmnch/RMNCHMobileAppControllerTest.java b/src/test/java/com/iemr/common/identity/controller/rmnch/RMNCHMobileAppControllerTest.java index ff95378f..8483b0eb 100644 --- a/src/test/java/com/iemr/common/identity/controller/rmnch/RMNCHMobileAppControllerTest.java +++ b/src/test/java/com/iemr/common/identity/controller/rmnch/RMNCHMobileAppControllerTest.java @@ -11,6 +11,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.annotation.Description; import com.iemr.common.identity.service.rmnch.RmnchDataSyncService; @@ -25,6 +26,7 @@ class RMNCHMobileAppControllerTest { RmnchDataSyncService rmnchDataSyncService; @Test + @Description("Tests the synchronization of data to the Amrit system. (TC_SyncDataToAmrit_Success_001)") void testSyncDataToAmrit() throws Exception { String req = "requestObj"; when(rmnchDataSyncService.syncDataToAmrit(any())).thenReturn("resp"); @@ -32,6 +34,7 @@ void testSyncDataToAmrit() throws Exception { assertNotNull(syncDataToAmrit); } @Test + @Description("Tests the behavior of the syncDataToAmrit method when provided with a null request object. (TC_SyncDataToAmritNullReq_002)") void testSyncDataToAmritNullReq() throws Exception { String req = null; String syncDataToAmrit = rmnchMobileAppController.syncDataToAmrit(req); @@ -39,6 +42,7 @@ void testSyncDataToAmritNullReq() throws Exception { assertEquals("Invalid/NULL request obj", data); } @Test + @Description("Tests the exception handling within the syncDataToAmrit method. (TC_SyncDataToAmritException_003)") void testSyncDataToAmritException() throws Exception { String req = "requestObj"; when(rmnchDataSyncService.syncDataToAmrit(any())).thenThrow(Exception.class); @@ -48,6 +52,7 @@ void testSyncDataToAmritException() throws Exception { } @Test + @Description("Tests successful retrieval of beneficiary data. (TC_GetBeneficiaryData_Success_001)") void testGetBeneficiaryData() throws Exception { String req = "requestObj"; String auth = "authorization"; @@ -56,6 +61,7 @@ void testGetBeneficiaryData() throws Exception { assertNotNull(beneficiaryData); } @Test + @Description("Tests the handling of a null response when retrieving beneficiary data. (TC_GetBeneficiaryDataNullResp_002)") void testGetBeneficiaryDataNullResp() throws Exception { String req = "requestObj"; String auth = "authorization"; @@ -65,6 +71,7 @@ void testGetBeneficiaryDataNullResp() throws Exception { assertTrue(data.contains("No record found")); } @Test + @Description("Tests the handling of a null response when retrieving beneficiary data. (TC_GetBeneficiaryDataNullResp_003)") void testGetBeneficiaryDataNullReq() throws Exception { String req = null; String auth = "authorization"; @@ -73,6 +80,7 @@ void testGetBeneficiaryDataNullReq() throws Exception { assertEquals("Invalid/NULL request obj", data); } @Test + @Description("Tests exception handling within the getBeneficiaryData method. (TC_GetBeneficiaryDataException_004)") void testGetBeneficiaryDataException() throws Exception { String req = "requestObj"; String auth = "authorization"; @@ -83,6 +91,7 @@ void testGetBeneficiaryDataException() throws Exception { } @Test + @Description("Tests successful retrieval of beneficiary data by Asha worker ID. (TC_GetBeneficiaryDataByAsha_Success_001)") void testGetBeneficiaryDataByAsha() throws Exception { String req = "requestObj"; String auth = "authorization"; @@ -92,6 +101,7 @@ void testGetBeneficiaryDataByAsha() throws Exception { } @Test + @Description("Tests the handling of a null response when retrieving beneficiary data by Asha worker ID. (TC_GetBeneficiaryDataByAshaNullResp_002)") void testGetBeneficiaryDataByAshaNullResp() throws Exception { String req = "requestObj"; String auth = "authorization"; @@ -101,6 +111,7 @@ void testGetBeneficiaryDataByAshaNullResp() throws Exception { assertTrue(data.contains("No record found")); } @Test + @Description("Tests the behavior of the getBeneficiaryDataByAsha method when a null request object is provided. (TC_GetBeneficiaryDataByAshaNullReq_003)") void testGetBeneficiaryDataByAshaNullReq() throws Exception { String req = null; String auth = "authorization"; @@ -110,6 +121,7 @@ void testGetBeneficiaryDataByAshaNullReq() throws Exception { } @Test + @Description("Tests exception handling within the getBeneficiaryDataByAsha method. (TC_GetBeneficiaryDataByAshaException_004)") void testGetBeneficiaryDataByAshaException() throws Exception { String req = "requestObj"; String auth = "authorization";