From 2896e734487b2c5d3b124bee5f56ea95771f6239 Mon Sep 17 00:00:00 2001 From: Aidan Gordon Date: Wed, 11 Dec 2024 17:17:53 -0800 Subject: [PATCH] Added BC Parks section endpoint --- .../nrs/wfprev/common/enums/CodeTables.java | 1 + .../wfprev/controllers/CodesController.java | 6 ++ .../BCParksRegionCodeResourceAssembler.java | 3 - .../BCParksSectionCodeResourceAssembler.java | 39 ++++++++ .../data/models/BCParksSectionCodeModel.java | 32 +++++++ .../gov/nrs/wfprev/services/CodesService.java | 24 ++++- .../gov/nrs/wfprev/CodesControllerTest.java | 87 ++++++++++++++++++ .../nrs/wfprev/services/CodesServiceTest.java | 92 ++++++++++++++++++- 8 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksSectionCodeResourceAssembler.java create mode 100644 server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksSectionCodeModel.java diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/common/enums/CodeTables.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/common/enums/CodeTables.java index af731b95..3f7be971 100644 --- a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/common/enums/CodeTables.java +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/common/enums/CodeTables.java @@ -8,4 +8,5 @@ public class CodeTables { public static final String FOREST_REGION_CODE = "forestRegionCodes"; public static final String FOREST_DISTRICT_CODE = "forestDistrictCodes"; public static final String BC_PARKS_REGION_CODE = "bcParksRegionCodes"; + public static final String BC_PARKS_SECTION_CODE = "bcParksSectionCodes"; } 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 8c6b1cf4..d5008fac 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 @@ -73,6 +73,9 @@ public ResponseEntity getCodes(@PathVariable("codeTable") String codeTable) { case CodeTables.BC_PARKS_REGION_CODE -> { response = ok(codesService.getAllBCParksRegionCodes()); } + case CodeTables.BC_PARKS_SECTION_CODE -> { + response = ok(codesService.getAllBCParksSectionCodes()); + } default -> { response = internalServerError(); @@ -124,6 +127,9 @@ public ResponseEntity getCodeById(@PathVariable("codeTable") String codeTable, @ case CodeTables.BC_PARKS_REGION_CODE -> { resource = codesService.getBCParksRegionCodeById(Integer.parseInt(id)); } + case CodeTables.BC_PARKS_SECTION_CODE -> { + resource = codesService.getBCParksSectionCodeById(Integer.parseInt(id)); + } default -> { resource = null; } diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssembler.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssembler.java index 6928a777..456ae411 100644 --- a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssembler.java +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssembler.java @@ -3,10 +3,7 @@ import ca.bc.gov.nrs.wfprev.common.enums.CodeTables; import ca.bc.gov.nrs.wfprev.controllers.CodesController; import ca.bc.gov.nrs.wfprev.data.entities.BCParksOrgUnitEntity; -import ca.bc.gov.nrs.wfprev.data.entities.ForestAreaCodeEntity; import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; -import ca.bc.gov.nrs.wfprev.data.models.ForestAreaCodeModel; -import ca.bc.gov.nrs.wfprev.data.models.ForestDistrictUnitCodeModel; import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport; import org.springframework.stereotype.Component; diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksSectionCodeResourceAssembler.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksSectionCodeResourceAssembler.java new file mode 100644 index 00000000..8999fa1e --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksSectionCodeResourceAssembler.java @@ -0,0 +1,39 @@ +package ca.bc.gov.nrs.wfprev.data.assemblers; + +import ca.bc.gov.nrs.wfprev.common.enums.CodeTables; +import ca.bc.gov.nrs.wfprev.controllers.CodesController; +import ca.bc.gov.nrs.wfprev.data.entities.BCParksOrgUnitEntity; +import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.BCParksSectionCodeModel; +import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport; +import org.springframework.stereotype.Component; + +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; + +@Component +public class BCParksSectionCodeResourceAssembler extends RepresentationModelAssemblerSupport { + + public BCParksSectionCodeResourceAssembler() { + super(CodesController.class, BCParksSectionCodeModel.class); + } + @Override + public BCParksSectionCodeModel toModel(BCParksOrgUnitEntity entity) { + BCParksSectionCodeModel resource = instantiateModel(entity); + resource.add(linkTo( + methodOn(CodesController.class) + .getCodeById(CodeTables.BC_PARKS_SECTION_CODE, entity.getOrgUnitIdentifier().toString())) + .withSelfRel()); + resource.setOrgUnitId(entity.getOrgUnitIdentifier()); + resource.setEffectiveDate(entity.getEffectiveDate()); + resource.setExpiryDate(entity.getExpiryDate()); + resource.setBcParksOrgUnitTypeCode(entity.getBcParksOrgUnitTypeCode()); + if (entity.getParentOrgUnitIdentifier() != null) { + resource.setParentOrgUnitId(entity.getParentOrgUnitIdentifier().toString()); + } + resource.setOrgUnitName(entity.getOrgUnitName()); + resource.setIntegerAlias(entity.getIntegerAlias()); + resource.setCharacterAlias(entity.getCharacterAlias()); + return resource; + } +} diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksSectionCodeModel.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksSectionCodeModel.java new file mode 100644 index 00000000..a59e1a16 --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksSectionCodeModel.java @@ -0,0 +1,32 @@ +package ca.bc.gov.nrs.wfprev.data.models; + +import ca.bc.gov.nrs.wfprev.common.entities.CommonModel; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonRootName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.springframework.hateoas.server.core.Relation; + +import java.util.Date; + +@Data +@EqualsAndHashCode(callSuper = false) +@JsonRootName(value = "bcParksSectionCode") +@Relation(collectionRelation = "bcParksSectionCode") +@JsonInclude(JsonInclude.Include.NON_NULL) +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class BCParksSectionCodeModel extends CommonModel { + private Integer orgUnitId; + private Date effectiveDate; + private Date expiryDate; + private String bcParksOrgUnitTypeCode; + private String parentOrgUnitId; + private String orgUnitName; + private Integer integerAlias; + private String characterAlias; +} 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 606a4c83..723fc411 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 @@ -3,6 +3,7 @@ import ca.bc.gov.nrs.wfone.common.service.api.ServiceException; import ca.bc.gov.nrs.wfprev.common.services.CommonService; import ca.bc.gov.nrs.wfprev.data.assemblers.BCParksRegionCodeResourceAssembler; +import ca.bc.gov.nrs.wfprev.data.assemblers.BCParksSectionCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestAreaCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestDistrictUnitCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestRegionUnitCodeResourceAssembler; @@ -16,6 +17,7 @@ import ca.bc.gov.nrs.wfprev.data.entities.ProgramAreaEntity; import ca.bc.gov.nrs.wfprev.data.entities.ProjectTypeCodeEntity; import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.BCParksSectionCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestAreaCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestDistrictUnitCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestRegionUnitCodeModel; @@ -52,13 +54,15 @@ public class CodesService implements CommonService { private ForestRegionUnitCodeResourceAssembler forestRegionCodeResourceAssembler; private final ForestDistrictUnitCodeResourceAssembler forestDistrictCodeResourceAssembler; private final BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler; + private final BCParksSectionCodeResourceAssembler bcParksSectionCodeResourceAssembler; private final BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository; + public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler, GeneralScopeCodeRepository generalScopeCodeRepository, GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler, ProjectTypeCodeRepository projectTypeCodeRepository, ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler, ProgramAreaRepository programAreaRepository, ProgramAreaResourceAssembler programAreaResourceAssembler, ForestOrgUnitCodeRepository forestRegionCodeRepository, ForestRegionUnitCodeResourceAssembler forestRegionCodeResourceAssembler, ForestDistrictUnitCodeResourceAssembler forestDistrictUnitCodeResourceAssembler, - BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository, BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler) { + BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository, BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler, BCParksSectionCodeResourceAssembler bcParksSectionCodeResourceAssembler) { this.forestAreaCodeRepository = forestAreaCodeRepository; this.forestAreaCodeResourceAssembler = forestAreaCodeResourceAssembler; this.generalScopeCodeRepository = generalScopeCodeRepository; @@ -72,6 +76,7 @@ public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAre this.forestDistrictCodeResourceAssembler = forestDistrictUnitCodeResourceAssembler; this.bcParksOrgUnitCodeRepository = bcParksOrgUnitCodeRepository; this.bcParksRegionCodeResourceAssembler = bcParksRegionCodeResourceAssembler; + this.bcParksSectionCodeResourceAssembler = bcParksSectionCodeResourceAssembler; } /** @@ -204,4 +209,21 @@ public BCParksRegionCodeModel getBCParksRegionCodeById(Integer number) { throw new ServiceException(e.getLocalizedMessage(), e); } } + + public CollectionModel getAllBCParksSectionCodes() { + try { + List entities = bcParksOrgUnitCodeRepository.findByBcParksOrgUnitTypeCode("SECTION"); + return bcParksSectionCodeResourceAssembler.toCollectionModel(entities); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } + } + + public BCParksSectionCodeModel getBCParksSectionCodeById(Integer bcParksSectionId) { + try { + return bcParksOrgUnitCodeRepository.findById(bcParksSectionId).map(bcParksSectionCodeResourceAssembler::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 1964a7f7..f7496e24 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 @@ -7,6 +7,7 @@ import ca.bc.gov.nrs.wfone.common.service.api.ServiceException; import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.BCParksSectionCodeModel; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -426,4 +427,90 @@ void testGetBCParksRegionCodesById_ServiceException() throws Exception { // Then .andExpect(status().isInternalServerError()); } + + @Test + @WithMockUser + void getBCParksSectionCodes() throws Exception { + // GIVEN + BCParksSectionCodeModel bcparksSectionCodeModel = new BCParksSectionCodeModel(); + bcparksSectionCodeModel.setBcParksOrgUnitTypeCode("SECTION"); + bcparksSectionCodeModel.setOrgUnitName("Section 1"); + bcparksSectionCodeModel.setOrgUnitId(1); + bcparksSectionCodeModel.setEffectiveDate(new Date()); + bcparksSectionCodeModel.setExpiryDate(new Date()); + bcparksSectionCodeModel.setCharacterAlias("S1"); + bcparksSectionCodeModel.setIntegerAlias(1); + + CollectionModel bcparksSectionCodeCollectionModel = CollectionModel.of(Arrays.asList(bcparksSectionCodeModel)); + + + when(codesService.getAllBCParksSectionCodes()).thenReturn(bcparksSectionCodeCollectionModel); + + // WHEN + mockMvc.perform(get("/codes/{codeTable}", CodeTables.BC_PARKS_SECTION_CODE) + .contentType(MediaType.APPLICATION_JSON)) + // THEN + .andExpect(status().isOk()) + .andExpect(jsonPath("$.bcParksOrgUnitTypeCode").value("SECTION")) + .andExpect(jsonPath("$.orgUnitName").value("Section 1")) + .andExpect(jsonPath("$.orgUnitId").value(1)) + .andExpect(jsonPath("$.characterAlias").value("S1")) + .andExpect(jsonPath("$.integerAlias").value(1)); + verify(codesService, times(1)).getAllBCParksSectionCodes(); + verifyNoMoreInteractions(codesService); + } + + @Test + @WithMockUser + void testGetBCParksSectionCodesById_Success() throws Exception { + // Given + BCParksSectionCodeModel bcparksSectionCodeModel = new BCParksSectionCodeModel(); + bcparksSectionCodeModel.setBcParksOrgUnitTypeCode("SECTION"); + bcparksSectionCodeModel.setOrgUnitName("Section 1"); + bcparksSectionCodeModel.setOrgUnitId(1); + bcparksSectionCodeModel.setEffectiveDate(new Date()); + bcparksSectionCodeModel.setExpiryDate(new Date()); + bcparksSectionCodeModel.setCharacterAlias("S1"); + bcparksSectionCodeModel.setIntegerAlias(1); + + when(codesService.getBCParksSectionCodeById(anyInt())).thenReturn(bcparksSectionCodeModel); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_SECTION_CODE, "1") + .contentType(MediaType.APPLICATION_JSON)) + // Then + .andExpect(status().isOk()) + .andExpect(jsonPath("$.bcParksOrgUnitTypeCode").value("SECTION")) + .andExpect(jsonPath("$.orgUnitName").value("Section 1")) + .andExpect(jsonPath("$.orgUnitId").value(1)) + .andExpect(jsonPath("$.characterAlias").value("S1")) + .andExpect(jsonPath("$.integerAlias").value(1)); + } + + @Test + @WithMockUser + void testGetBCParksSectionCodesById_NotFound() throws Exception { + // Given + when(codesService.getBCParksSectionCodeById(anyInt())).thenReturn(null); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_SECTION_CODE, "1") + .contentType(MediaType.APPLICATION_JSON)) + // Then + .andExpect(status().isNotFound()); + } + + @Test + @WithMockUser + void testGetBCParksSectionCodesById_ServiceException() throws Exception { + // Given + when(codesService.getBCParksSectionCodeById(anyInt())).thenThrow(new ServiceException("Service error")); + + // When + mockMvc.perform(get("/codes/{codeTable}/{id}", CodeTables.BC_PARKS_SECTION_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 61179607..d1693233 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 @@ -2,6 +2,7 @@ import ca.bc.gov.nrs.wfone.common.service.api.ServiceException; import ca.bc.gov.nrs.wfprev.data.assemblers.BCParksRegionCodeResourceAssembler; +import ca.bc.gov.nrs.wfprev.data.assemblers.BCParksSectionCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestAreaCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestDistrictUnitCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ForestRegionUnitCodeResourceAssembler; @@ -14,6 +15,7 @@ import ca.bc.gov.nrs.wfprev.data.entities.ProgramAreaEntity; import ca.bc.gov.nrs.wfprev.data.entities.ProjectTypeCodeEntity; import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.BCParksSectionCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestAreaCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestDistrictUnitCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ForestRegionUnitCodeModel; @@ -56,6 +58,7 @@ class CodesServiceTest { private ForestDistrictUnitCodeResourceAssembler forestDistrictUnitCodeResourceAssembler; private BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository; private BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler; + private BCParksSectionCodeResourceAssembler bcParksSectionCodeResourceAssembler; @BeforeEach void setup() { @@ -72,11 +75,13 @@ void setup() { forestDistrictUnitCodeResourceAssembler = mock(ForestDistrictUnitCodeResourceAssembler.class); bcParksOrgUnitCodeRepository = mock(BCParksOrgUnitCodeRepository.class); bcParksRegionCodeResourceAssembler = mock(BCParksRegionCodeResourceAssembler.class); + bcParksSectionCodeResourceAssembler = mock(BCParksSectionCodeResourceAssembler.class); codesService = new CodesService(forestAreaCodeRepository, forestAreaCodeResourceAssembler, generalScopeCodeRepository, generalScopeCodeResourceAssembler, projectTypeCodeRepository, projectTypeCodeResourceAssembler, programAreaRepository, programAreaResourceAssembler, - forestOrgUnitCodeRepository, forestRegionUnitCodeResourceAssembler, forestDistrictUnitCodeResourceAssembler, bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler); + forestOrgUnitCodeRepository, forestRegionUnitCodeResourceAssembler, forestDistrictUnitCodeResourceAssembler, bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler, + bcParksSectionCodeResourceAssembler); } @Test @@ -660,4 +665,89 @@ void testGetBCParksRegionCodeById_Exception() { verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksRegionId); verifyNoInteractions(bcParksRegionCodeResourceAssembler); } + + @Test + void testGetAllBCParksSectionCodes_Success() { + // Given + List entities = List.of(new BCParksOrgUnitEntity(), new BCParksOrgUnitEntity()); + BCParksSectionCodeModel expectedModel = new BCParksSectionCodeModel(); + expectedModel.setBcParksOrgUnitTypeCode("1"); + expectedModel.setEffectiveDate(new Date()); + expectedModel.setExpiryDate(new Date()); + expectedModel.setOrgUnitId(1); + expectedModel.setOrgUnitName("orgUnitName"); + expectedModel.setParentOrgUnitId("1"); + expectedModel.setIntegerAlias(1); + expectedModel.setCharacterAlias("characterAlias"); + + CollectionModel expectedModelCollection = CollectionModel.of(List.of(expectedModel)); + + when(bcParksOrgUnitCodeRepository.findByBcParksOrgUnitTypeCode(anyString())).thenReturn(entities); + when(bcParksSectionCodeResourceAssembler.toCollectionModel(entities)).thenReturn(expectedModelCollection); + + // When + CollectionModel result = codesService.getAllBCParksSectionCodes(); + + // Then + assertEquals(expectedModelCollection, result); + verify(bcParksOrgUnitCodeRepository, times(1)).findByBcParksOrgUnitTypeCode("SECTION"); + verify(bcParksSectionCodeResourceAssembler, times(1)).toCollectionModel(entities); + verifyNoMoreInteractions(bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler); + } + + @Test + void testGetAllBCParksSectionCodeById_Success() { + // Given + Integer bcParksSectionId = 1; + BCParksOrgUnitEntity entity = new BCParksOrgUnitEntity(); + BCParksSectionCodeModel expectedModel = new BCParksSectionCodeModel(); + expectedModel.setOrgUnitId(bcParksSectionId); + expectedModel.setEffectiveDate(new Date()); + expectedModel.setExpiryDate(new Date()); + expectedModel.setBcParksOrgUnitTypeCode("SECTION"); + expectedModel.setOrgUnitName("orgUnitName"); + expectedModel.setParentOrgUnitId("1"); + expectedModel.setIntegerAlias(1); + expectedModel.setCharacterAlias("characterAlias"); + + when(bcParksOrgUnitCodeRepository.findById(bcParksSectionId)).thenReturn(Optional.of(entity)); + when(bcParksSectionCodeResourceAssembler.toModel(entity)).thenReturn(expectedModel); + + // When + BCParksSectionCodeModel result = codesService.getBCParksSectionCodeById(bcParksSectionId); + + // Then + assertEquals(expectedModel, result); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksSectionId); + verify(bcParksSectionCodeResourceAssembler, times(1)).toModel(entity); + verifyNoMoreInteractions(bcParksOrgUnitCodeRepository, bcParksSectionCodeResourceAssembler); + } + + @Test + void testGetBCParksSectionCodeById_NotFound() { + // Arrange + Integer bcParksSectionId = 1; + when(bcParksOrgUnitCodeRepository.findById(bcParksSectionId)).thenReturn(Optional.empty()); + + // Act + BCParksSectionCodeModel result = codesService.getBCParksSectionCodeById(bcParksSectionId); + + // Assert + assertNull(result); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksSectionId); + verifyNoInteractions(bcParksSectionCodeResourceAssembler); + } + + @Test + void testGetBCParksSectionCodeById_Exception() { + // Arrange + Integer bcParksSectionId = 1; + when(bcParksOrgUnitCodeRepository.findById(bcParksSectionId)).thenThrow(new RuntimeException("Database error")); + + // Act & Assert + ServiceException exception = assertThrows(ServiceException.class, () -> codesService.getBCParksSectionCodeById(bcParksSectionId)); + assertEquals("Database error", exception.getLocalizedMessage()); + verify(bcParksOrgUnitCodeRepository, times(1)).findById(bcParksSectionId); + verifyNoInteractions(bcParksSectionCodeResourceAssembler); + } } \ No newline at end of file