From acf7ce0cd863922e5afc2cc861db75cadfcec38d Mon Sep 17 00:00:00 2001 From: Aidan Gordon Date: Wed, 11 Dec 2024 16:36:37 -0800 Subject: [PATCH] Added getByID to BC Parks Regions --- .../wfprev/controllers/CodesController.java | 3 + .../gov/nrs/wfprev/services/CodesService.java | 10 ++ .../gov/nrs/wfprev/CodesControllerTest.java | 143 ++++++++++++------ .../nrs/wfprev/services/CodesServiceTest.java | 56 +++++++ 4 files changed, 167 insertions(+), 45 deletions(-) diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/controllers/CodesController.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/controllers/CodesController.java index 91880f9ce..8c6b1cf46 100644 --- a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/controllers/CodesController.java +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/controllers/CodesController.java @@ -121,6 +121,9 @@ public ResponseEntity getCodeById(@PathVariable("codeTable") String codeTable, @ case CodeTables.FOREST_DISTRICT_CODE -> { resource = codesService.getForestDistrictCodeById(Integer.parseInt(id)); } + case CodeTables.BC_PARKS_REGION_CODE -> { + resource = codesService.getBCParksRegionCodeById(Integer.parseInt(id)); + } default -> { resource = null; } diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/services/CodesService.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/services/CodesService.java index 3597a1cbe..606a4c833 100644 --- a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/services/CodesService.java +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/services/CodesService.java @@ -33,6 +33,7 @@ import org.springframework.stereotype.Component; import java.util.List; +import java.util.Optional; import java.util.UUID; @Slf4j @@ -194,4 +195,13 @@ public CollectionModel getAllBCParksRegionCodes() { throw new ServiceException(e.getLocalizedMessage(), e); } } + + public BCParksRegionCodeModel getBCParksRegionCodeById(Integer number) { + try { + Optional byId = bcParksOrgUnitCodeRepository.findById(number); + return byId.map(bcParksRegionCodeResourceAssembler::toModel).orElse(null); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } + } } diff --git a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/CodesControllerTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/CodesControllerTest.java index 28eff44ad..1964a7f72 100644 --- a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/CodesControllerTest.java +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/CodesControllerTest.java @@ -45,69 +45,69 @@ class CodesControllerTest { @Test @WithMockUser void testGetAllCodes() throws Exception { - testGetForestAreaCodes(); - testGetGeneralScopeCodes(); - testGetProjectTypeCodes(); + testGetForestAreaCodes(); + testGetGeneralScopeCodes(); + testGetProjectTypeCodes(); } void testGetForestAreaCodes() throws Exception { - String exampleId1 = UUID.randomUUID().toString(); - String exampleId2 = UUID.randomUUID().toString(); + String exampleId1 = UUID.randomUUID().toString(); + String exampleId2 = UUID.randomUUID().toString(); - ForestAreaCodeModel fac1 = new ForestAreaCodeModel(); - fac1.setForestAreaCode(exampleId1); + ForestAreaCodeModel fac1 = new ForestAreaCodeModel(); + fac1.setForestAreaCode(exampleId1); - ForestAreaCodeModel fac2 = new ForestAreaCodeModel(); - fac2.setForestAreaCode(exampleId2); + ForestAreaCodeModel fac2 = new ForestAreaCodeModel(); + fac2.setForestAreaCode(exampleId2); - List facList = Arrays.asList(fac1, fac2); - CollectionModel facModel = CollectionModel.of(facList); + List facList = Arrays.asList(fac1, fac2); + CollectionModel facModel = CollectionModel.of(facList); - when(codesService.getAllForestAreaCodes()).thenReturn(facModel); + when(codesService.getAllForestAreaCodes()).thenReturn(facModel); - mockMvc.perform(get("/codes/" + CodeTables.FOREST_AREA_CODE) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + mockMvc.perform(get("/codes/" + CodeTables.FOREST_AREA_CODE) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); } void testGetGeneralScopeCodes() throws Exception { - String exampleId1 = UUID.randomUUID().toString(); - String exampleId2 = UUID.randomUUID().toString(); + String exampleId1 = UUID.randomUUID().toString(); + String exampleId2 = UUID.randomUUID().toString(); - GeneralScopeCodeModel fac1 = new GeneralScopeCodeModel(); - fac1.setGeneralScopeCode(exampleId1); + GeneralScopeCodeModel fac1 = new GeneralScopeCodeModel(); + fac1.setGeneralScopeCode(exampleId1); - GeneralScopeCodeModel fac2 = new GeneralScopeCodeModel(); - fac2.setGeneralScopeCode(exampleId2); + GeneralScopeCodeModel fac2 = new GeneralScopeCodeModel(); + fac2.setGeneralScopeCode(exampleId2); - List facList = Arrays.asList(fac1, fac2); - CollectionModel facModel = CollectionModel.of(facList); + List facList = Arrays.asList(fac1, fac2); + CollectionModel facModel = CollectionModel.of(facList); - when(codesService.getAllGeneralScopeCodes()).thenReturn(facModel); + when(codesService.getAllGeneralScopeCodes()).thenReturn(facModel); - mockMvc.perform(get("/codes/" + CodeTables.GENERAL_SCOPE_CODE) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + mockMvc.perform(get("/codes/" + CodeTables.GENERAL_SCOPE_CODE) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); } void testGetProjectTypeCodes() throws Exception { - String exampleId1 = UUID.randomUUID().toString(); - String exampleId2 = UUID.randomUUID().toString(); + String exampleId1 = UUID.randomUUID().toString(); + String exampleId2 = UUID.randomUUID().toString(); - ProjectTypeCodeModel fac1 = new ProjectTypeCodeModel(); - fac1.setProjectTypeCode(exampleId1); + ProjectTypeCodeModel fac1 = new ProjectTypeCodeModel(); + fac1.setProjectTypeCode(exampleId1); - ProjectTypeCodeModel fac2 = new ProjectTypeCodeModel(); - fac2.setProjectTypeCode(exampleId2); + ProjectTypeCodeModel fac2 = new ProjectTypeCodeModel(); + fac2.setProjectTypeCode(exampleId2); - List facList = Arrays.asList(fac1, fac2); - CollectionModel facModel = CollectionModel.of(facList); + List facList = Arrays.asList(fac1, fac2); + CollectionModel facModel = CollectionModel.of(facList); - when(codesService.getAllProjectTypeCodes()).thenReturn(facModel); + when(codesService.getAllProjectTypeCodes()).thenReturn(facModel); - mockMvc.perform(get("/codes/" + CodeTables.PROJECT_TYPE_CODE) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + mockMvc.perform(get("/codes/" + CodeTables.PROJECT_TYPE_CODE) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); } @Test @@ -120,7 +120,7 @@ void testGetCodesById() throws Exception { when(codesService.getProjectTypeCodeById(ptID)).thenReturn(projectTypeCode); mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.PROJECT_TYPE_CODE, ptID) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); String gsID = UUID.randomUUID().toString(); @@ -130,7 +130,7 @@ void testGetCodesById() throws Exception { when(codesService.getGeneralScopeCodeById(gsID)).thenReturn(generalScopeCode); mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.GENERAL_SCOPE_CODE, gsID) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); String faID = UUID.randomUUID().toString(); @@ -140,7 +140,7 @@ void testGetCodesById() throws Exception { when(codesService.getForestAreaCodeById(faID)).thenReturn(forestAreaCode); mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.FOREST_AREA_CODE, faID) - .contentType(MediaType.APPLICATION_JSON)) + .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); } @@ -297,9 +297,9 @@ void testGetProgramAreaCodeById_VerifyServiceCall() throws Exception { @WithMockUser void testGetForestRegionCodes() throws Exception { when(codesService.getAllForestRegionCodes()).thenReturn(CollectionModel.empty()); - mockMvc.perform(get("/codes/{codeTable}", CodeTables.FOREST_REGION_CODE) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + mockMvc.perform(get("/codes/{codeTable}", CodeTables.FOREST_REGION_CODE) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); verify(codesService, times(1)).getAllForestRegionCodes(); verifyNoMoreInteractions(codesService); } @@ -373,4 +373,57 @@ void testGetBCParksRegionCodes() throws Exception { verify(codesService, times(1)).getAllBCParksRegionCodes(); verifyNoMoreInteractions(codesService); } + + @Test + @WithMockUser + void testGetBCParksRegionCodesById_Success() throws Exception { + // Given + BCParksRegionCodeModel bcparksRegionCodeModel = new BCParksRegionCodeModel(); + bcparksRegionCodeModel.setBcParksOrgUnitTypeCode("REGION"); + bcparksRegionCodeModel.setOrgUnitName("Region 1"); + bcparksRegionCodeModel.setOrgUnitId(1); + bcparksRegionCodeModel.setEffectiveDate(new Date()); + bcparksRegionCodeModel.setExpiryDate(new Date()); + bcparksRegionCodeModel.setCharacterAlias("R1"); + bcparksRegionCodeModel.setIntegerAlias(1); + + when(codesService.getBCParksRegionCodeById(anyInt())).thenReturn(bcparksRegionCodeModel); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_REGION_CODE, "1") + .contentType(MediaType.APPLICATION_JSON)) + // Then + .andExpect(status().isOk()) + .andExpect(jsonPath("$.bcParksOrgUnitTypeCode").value("REGION")) + .andExpect(jsonPath("$.orgUnitName").value("Region 1")) + .andExpect(jsonPath("$.orgUnitId").value(1)) + .andExpect(jsonPath("$.characterAlias").value("R1")) + .andExpect(jsonPath("$.integerAlias").value(1)); + } + + @Test + @WithMockUser + void testGetBCParksRegionCodesById_NotFound() throws Exception { + // Given + when(codesService.getBCParksRegionCodeById(anyInt())).thenReturn(null); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_REGION_CODE, "1") + .contentType(MediaType.APPLICATION_JSON)) + // Then + .andExpect(status().isNotFound()); + } + + @Test + @WithMockUser + void testGetBCParksRegionCodesById_ServiceException() throws Exception { + // Given + when(codesService.getBCParksRegionCodeById(anyInt())).thenThrow(new ServiceException("Service error")); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_REGION_CODE, "1") + .contentType(MediaType.APPLICATION_JSON)) + // Then + .andExpect(status().isInternalServerError()); + } } diff --git a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java index 18aaac41b..611796076 100644 --- a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java @@ -604,4 +604,60 @@ void testGetAllBCParksRegionCodes_Success() { verify(bcParksRegionCodeResourceAssembler, times(1)).toCollectionModel(entities); verifyNoMoreInteractions(bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler); } + + @Test + void testGetAllBCParksRegionCodeById_Success() { + // Given + Integer bcParksRegionId = 1; + BCParksOrgUnitEntity entity = new BCParksOrgUnitEntity(); + BCParksRegionCodeModel expectedModel = new BCParksRegionCodeModel(); + expectedModel.setOrgUnitId(bcParksRegionId); + expectedModel.setEffectiveDate(new Date()); + expectedModel.setExpiryDate(new Date()); + expectedModel.setBcParksOrgUnitTypeCode("REGION"); + expectedModel.setOrgUnitName("orgUnitName"); + expectedModel.setParentOrgUnitId("1"); + expectedModel.setIntegerAlias(1); + expectedModel.setCharacterAlias("characterAlias"); + + when(bcParksOrgUnitCodeRepository.findById(bcParksRegionId)).thenReturn(Optional.of(entity)); + when(bcParksRegionCodeResourceAssembler.toModel(entity)).thenReturn(expectedModel); + + // When + BCParksRegionCodeModel result = codesService.getBCParksRegionCodeById(bcParksRegionId); + + // Then + assertEquals(expectedModel, result); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksRegionId); + verify(bcParksRegionCodeResourceAssembler, times(1)).toModel(entity); + verifyNoMoreInteractions(bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler); + } + + @Test + void testGetBCParksRegionCodeById_NotFound() { + // Arrange + Integer bcParksRegionId = 1; + when(bcParksOrgUnitCodeRepository.findById(bcParksRegionId)).thenReturn(Optional.empty()); + + // Act + BCParksRegionCodeModel result = codesService.getBCParksRegionCodeById(bcParksRegionId); + + // Assert + assertNull(result); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksRegionId); + verifyNoInteractions(bcParksRegionCodeResourceAssembler); + } + + @Test + void testGetBCParksRegionCodeById_Exception() { + // Arrange + Integer bcParksRegionId = 1; + when(bcParksOrgUnitCodeRepository.findById(bcParksRegionId)).thenThrow(new RuntimeException("Database error")); + + // Act & Assert + ServiceException exception = assertThrows(ServiceException.class, () -> codesService.getBCParksRegionCodeById(bcParksRegionId)); + assertEquals("Database error", exception.getLocalizedMessage()); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksRegionId); + verifyNoInteractions(bcParksRegionCodeResourceAssembler); + } } \ No newline at end of file