diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/LocationController.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/LocationController.java index 9be30f376f9..7db7db9bd5e 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/LocationController.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/controller/LocationController.java @@ -230,6 +230,23 @@ public ResponseWrapper getImmediateChildrenByLocCodeAndLang return responseWrapper; } + /** + * + * @param locationCode location code + * @param languageCodes language codes + * @return list of location hierarchies + */ + @ResponseFilter + @GetMapping(value = "/immediatechildren/{locationcode}") + public ResponseWrapper getImmediateChildrenByLocCode( + @PathVariable("locationcode") String locationCode, @RequestParam("languageCodes") List languageCodes) { + + ResponseWrapper responseWrapper = new ResponseWrapper<>(); + responseWrapper + .setResponse(locationHierarchyService.getImmediateChildrenByLocCode(locationCode, languageCodes)); + return responseWrapper; + } + /** * checks whether the given location name is valid or not * diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java index c47876bd38b..419735474e2 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java @@ -160,4 +160,7 @@ List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, Stri @Query(value = "FROM Location l where l.langCode=?1 and l.hierarchyLevel=?2") List getAllLocationsByLangCodeWithHierarchyLevel(String langCode, Short level); + @Query(value = "FROM Location l WHERE parentLocCode = ?1 AND l.langCode IN (?2) AND (l.isDeleted IS NULL OR l.isDeleted = false) AND l.isActive = true") + List findLocationHierarchyByParentLocCode(String parentLocCode, List languageCodes); + } diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/LocationService.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/LocationService.java index 74f52bc059c..e2c8a0d0452 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/LocationService.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/LocationService.java @@ -137,4 +137,5 @@ public interface LocationService { public FilterResponseCodeDto locFilterValues(FilterValueDto filterValueDto); + LocationResponseDto getImmediateChildrenByLocCode(String locationCode, List languageCodes); } diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java index 7cad5c2ea24..d3dd6c8fd76 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java @@ -5,10 +5,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.TreeMap; -import java.util.stream.Collectors; import io.mosip.kernel.masterdata.dto.response.*; import org.springframework.beans.factory.annotation.Autowired; @@ -958,4 +956,26 @@ public FilterResponseCodeDto locFilterValues(FilterValueDto filterValueDto) { } return filterResponseDto; } + + + @Override + public LocationResponseDto getImmediateChildrenByLocCode(String locationCode, List languageCodes) { + List locationlist = null; + LocationResponseDto locationHierarchyResponseDto = new LocationResponseDto(); + try { + locationlist = locationRepository.findLocationHierarchyByParentLocCode(locationCode, languageCodes); + + } catch (DataAccessException | DataAccessLayerException e) { + throw new MasterDataServiceException(LocationErrorCode.LOCATION_FETCH_EXCEPTION.getErrorCode(), + LocationErrorCode.LOCATION_FETCH_EXCEPTION.getErrorMessage() + ExceptionUtils.parseException(e)); + } + + if (locationlist.isEmpty()) { + throw new DataNotFoundException(LocationErrorCode.LOCATION_NOT_FOUND_EXCEPTION.getErrorCode(), + LocationErrorCode.LOCATION_NOT_FOUND_EXCEPTION.getErrorMessage()); + } + List locationDtoList = MapperUtils.mapAll(locationlist, LocationDto.class); + locationHierarchyResponseDto.setLocations(locationDtoList); + return locationHierarchyResponseDto; + } } diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/LocationControllerTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/LocationControllerTest.java index eb693eddf38..ed32cf0b380 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/LocationControllerTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/controller/LocationControllerTest.java @@ -674,4 +674,12 @@ public void t021updateLocationStatusFailTest() throws Exception { } + @Test + @WithUserDetails("global-admin") + public void t021getImmediateChildrenByLocCode() throws Exception { + + MasterDataTest + .checkResponse(mockMvc.perform(MockMvcRequestBuilders.get("/locations/immediatechildren/RSK?languageCodes=eng,tam")).andReturn(), null); + } + } diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/MasterDataServiceTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/MasterDataServiceTest.java index 4a5600b55a2..4d21559491d 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/MasterDataServiceTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/MasterDataServiceTest.java @@ -12,7 +12,6 @@ import java.time.Month; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import io.mosip.kernel.core.exception.ServiceError; @@ -3105,4 +3104,28 @@ public void validateRegCenterUpdateTest() { Assert.assertEquals("KER-MSD-259", serviceErrors.get(4).getErrorCode()); } + @Test + public void getImmediateChildrenByLocCodeTest() { + Mockito.when(locationHierarchyRepository + .findLocationHierarchyByParentLocCode(Mockito.anyString(), Mockito.anyList())) + .thenReturn(locationHierarchies); + Assert.assertEquals("IND", locationHierarchyService.getImmediateChildrenByLocCode("KAR", List.of("eng")).getLocations().get(0).getCode()); + } + + @Test(expected = MasterDataServiceException.class) + public void getImmediateChildrenByLocCodeTestExceptionTest() { + Mockito.when(locationHierarchyRepository + .findLocationHierarchyByParentLocCode(Mockito.anyString(), Mockito.anyList())) + .thenThrow(DataRetrievalFailureException.class); + locationHierarchyService.getImmediateChildrenByLocCode("KAR", List.of("eng")); + } + + @Test(expected = DataNotFoundException.class) + public void getImmediateChildrenByLocCodeTestDataExceptionTest() { + Mockito.when(locationHierarchyRepository + .findLocationHierarchyByParentLocCode(Mockito.anyString(), Mockito.anyList())) + .thenReturn(new ArrayList()); + locationHierarchyService.getImmediateChildrenByLocCode("KAR", List.of("eng")); + } + }