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 b4326102..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 @@ -7,4 +7,6 @@ public class CodeTables { public static final String PROGRAM_AREA_CODE = "programAreaCodes"; 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 51fc61ee..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 @@ -70,6 +70,12 @@ public ResponseEntity getCodes(@PathVariable("codeTable") String codeTable) { case CodeTables.FOREST_DISTRICT_CODE -> { response = ok(codesService.getAllForestDistrictCodes()); } + case CodeTables.BC_PARKS_REGION_CODE -> { + response = ok(codesService.getAllBCParksRegionCodes()); + } + case CodeTables.BC_PARKS_SECTION_CODE -> { + response = ok(codesService.getAllBCParksSectionCodes()); + } default -> { response = internalServerError(); @@ -118,6 +124,12 @@ 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)); + } + 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 new file mode 100644 index 00000000..456ae411 --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssembler.java @@ -0,0 +1,40 @@ +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 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 BCParksRegionCodeResourceAssembler extends RepresentationModelAssemblerSupport { + + public BCParksRegionCodeResourceAssembler() { + super(CodesController.class, BCParksRegionCodeModel.class); + } + + + @Override + public BCParksRegionCodeModel toModel(BCParksOrgUnitEntity entity) { + BCParksRegionCodeModel resource = instantiateModel(entity); + resource.add(linkTo( + methodOn(CodesController.class) + .getCodeById(CodeTables.BC_PARKS_REGION_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/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/entities/BCParksOrgUnitEntity.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/entities/BCParksOrgUnitEntity.java new file mode 100644 index 00000000..5ece2f55 --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/entities/BCParksOrgUnitEntity.java @@ -0,0 +1,84 @@ +package ca.bc.gov.nrs.wfprev.data.entities; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; + +import java.io.Serializable; +import java.util.Date; + +@Entity +@Table(name = "bc_parks_org_unit") +@JsonIgnoreProperties(ignoreUnknown = false) +@Data +@EqualsAndHashCode(callSuper = false) +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class BCParksOrgUnitEntity implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "org_unit_identifier") + @NotNull + private Integer orgUnitIdentifier; + + @NotNull + @Column(name = "effective_date") + private Date effectiveDate; + + @NotNull + @Column(name = "expiry_date") + private Date expiryDate; + + @NotNull + @Column(name = "bc_parks_org_unit_type_code") + private String bcParksOrgUnitTypeCode; + + @Column(name = "parent_org_unit_identifier") + private Integer parentOrgUnitIdentifier; + + @NotNull + @Column(name = "org_unit_name") + private String orgUnitName; + + @Column(name = "integer_alias") + private Integer integerAlias; + + @Column(name = "character_alias") + private String characterAlias; + + @CreatedBy + @NotNull + @Column(name = "create_user", length = 64) + private String createUser; + + @CreatedDate + @NotNull + @Column(name = "create_date") + private Date createDate; + + @LastModifiedBy + @NotNull + @Column(name = "update_user", length = 64) + private String updateUser; + + @LastModifiedDate + @NotNull + @Column(name = "update_date") + private Date updateDate; + + +} diff --git a/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksRegionCodeModel.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksRegionCodeModel.java new file mode 100644 index 00000000..08460e44 --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/models/BCParksRegionCodeModel.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 = "bcParksRegionCode") +@Relation(collectionRelation = "bcParksRegionCode") +@JsonInclude(JsonInclude.Include.NON_NULL) +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class BCParksRegionCodeModel 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/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/data/repositories/BCParksOrgUnitCodeRepository.java b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/repositories/BCParksOrgUnitCodeRepository.java new file mode 100644 index 00000000..1601bb82 --- /dev/null +++ b/server/wfprev-api/src/main/java/ca/bc/gov/nrs/wfprev/data/repositories/BCParksOrgUnitCodeRepository.java @@ -0,0 +1,13 @@ +package ca.bc.gov.nrs.wfprev.data.repositories; + +import ca.bc.gov.nrs.wfprev.common.repository.CommonRepository; +import ca.bc.gov.nrs.wfprev.data.entities.BCParksOrgUnitEntity; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +import java.util.List; + +@RepositoryRestResource(exported = false) +public interface BCParksOrgUnitCodeRepository extends CommonRepository{ + List findByBcParksOrgUnitTypeCode(String bcParksOrgUnitTypeCode); + +} 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 f3c49a9a..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 @@ -2,23 +2,29 @@ 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; import ca.bc.gov.nrs.wfprev.data.assemblers.GeneralScopeCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ProgramAreaResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ProjectTypeCodeResourceAssembler; +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.entities.ForestOrgUnitCodeEntity; import ca.bc.gov.nrs.wfprev.data.entities.GeneralScopeCodeEntity; 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; import ca.bc.gov.nrs.wfprev.data.models.GeneralScopeCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ProgramAreaModel; import ca.bc.gov.nrs.wfprev.data.models.ProjectTypeCodeModel; +import ca.bc.gov.nrs.wfprev.data.repositories.BCParksOrgUnitCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.ForestAreaCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.ForestOrgUnitCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.GeneralScopeCodeRepository; @@ -29,11 +35,13 @@ import org.springframework.stereotype.Component; import java.util.List; +import java.util.Optional; import java.util.UUID; @Slf4j @Component public class CodesService implements CommonService { + public static final String BC_PARKS_REGION_ORG_UNIT_TYPE_CODE = "REGION"; private ForestAreaCodeRepository forestAreaCodeRepository; private ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler; private GeneralScopeCodeRepository generalScopeCodeRepository; @@ -45,11 +53,16 @@ public class CodesService implements CommonService { private ForestOrgUnitCodeRepository forestOrgUnitCodeRepository; 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) { + ForestOrgUnitCodeRepository forestRegionCodeRepository, ForestRegionUnitCodeResourceAssembler forestRegionCodeResourceAssembler, ForestDistrictUnitCodeResourceAssembler forestDistrictUnitCodeResourceAssembler, + BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository, BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler, BCParksSectionCodeResourceAssembler bcParksSectionCodeResourceAssembler) { this.forestAreaCodeRepository = forestAreaCodeRepository; this.forestAreaCodeResourceAssembler = forestAreaCodeResourceAssembler; this.generalScopeCodeRepository = generalScopeCodeRepository; @@ -61,6 +74,9 @@ public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAre this.forestOrgUnitCodeRepository = forestRegionCodeRepository; this.forestRegionCodeResourceAssembler = forestRegionCodeResourceAssembler; this.forestDistrictCodeResourceAssembler = forestDistrictUnitCodeResourceAssembler; + this.bcParksOrgUnitCodeRepository = bcParksOrgUnitCodeRepository; + this.bcParksRegionCodeResourceAssembler = bcParksRegionCodeResourceAssembler; + this.bcParksSectionCodeResourceAssembler = bcParksSectionCodeResourceAssembler; } /** @@ -143,7 +159,7 @@ public ProgramAreaModel getProgramAreaCodeById(String id) { public CollectionModel getAllForestRegionCodes() { try { - List entities = forestOrgUnitCodeRepository.findByForestOrgUnitTypeCode("REGION"); + List entities = forestOrgUnitCodeRepository.findByForestOrgUnitTypeCode(BC_PARKS_REGION_ORG_UNIT_TYPE_CODE); return forestRegionCodeResourceAssembler.toCollectionModel(entities); } catch (Exception e) { throw new ServiceException(e.getLocalizedMessage(), e); @@ -175,4 +191,39 @@ public ForestDistrictUnitCodeModel getForestDistrictCodeById(Integer forestDistr throw new ServiceException(e.getLocalizedMessage(), e); } } + + public CollectionModel getAllBCParksRegionCodes() { + try { + List entities = bcParksOrgUnitCodeRepository.findByBcParksOrgUnitTypeCode(BC_PARKS_REGION_ORG_UNIT_TYPE_CODE); + return bcParksRegionCodeResourceAssembler.toCollectionModel(entities); + } catch (Exception e) { + 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); + } + } + + 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 e83536b6..e65520f9 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 @@ -1,10 +1,13 @@ package ca.bc.gov.nrs.wfprev; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; 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; @@ -16,8 +19,10 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import ca.bc.gov.nrs.wfprev.common.enums.CodeTables; @@ -26,6 +31,7 @@ import ca.bc.gov.nrs.wfprev.data.models.GeneralScopeCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ProjectTypeCodeModel; import ca.bc.gov.nrs.wfprev.services.CodesService; +import org.springframework.test.web.servlet.ResultActions; @WebMvcTest(CodesController.class) @Import({SecurityConfig.class, TestcontainersConfiguration.class}) @@ -40,69 +46,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 projectTypeCodeModel1 = new ProjectTypeCodeModel(); + projectTypeCodeModel1.setProjectTypeCode(exampleId1); - ProjectTypeCodeModel fac2 = new ProjectTypeCodeModel(); - fac2.setProjectTypeCode(exampleId2); + ProjectTypeCodeModel projectTypeCodeModel2 = new ProjectTypeCodeModel(); + projectTypeCodeModel2.setProjectTypeCode(exampleId2); - List facList = Arrays.asList(fac1, fac2); - CollectionModel facModel = CollectionModel.of(facList); + List projectTypeCodeModelList = Arrays.asList(projectTypeCodeModel1, projectTypeCodeModel2); + CollectionModel projectTypeCodeModels = CollectionModel.of(projectTypeCodeModelList); - when(codesService.getAllProjectTypeCodes()).thenReturn(facModel); + when(codesService.getAllProjectTypeCodes()).thenReturn(projectTypeCodeModels); - 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 @@ -115,7 +121,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(); @@ -125,7 +131,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(); @@ -135,7 +141,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()); } @@ -292,9 +298,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); } @@ -337,4 +343,175 @@ void testGetForestDistrictCodes() throws Exception { verify(codesService, times(1)).getAllForestDistrictCodes(); verifyNoMoreInteractions(codesService); } + + @Test + @WithMockUser + void testGetBCParksRegionCodes() 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); + + CollectionModel bcparksRegionCodeModelCollection = CollectionModel.of(Arrays.asList(bcparksRegionCodeModel)); + when(codesService.getAllBCParksRegionCodes()).thenReturn(bcparksRegionCodeModelCollection); + + // WHEN + mockMvc.perform(get("/codes/{codeTable}", CodeTables.BC_PARKS_REGION_CODE) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + // THEN + .andExpect(jsonPath("$._embedded.bcParksRegionCode[0].bcParksOrgUnitTypeCode").value("REGION")) + .andExpect(jsonPath("$._embedded.bcParksRegionCode[0].orgUnitName").value("Region 1")) + .andExpect(jsonPath("$._embedded.bcParksRegionCode[0].orgUnitId").value(1)) + .andExpect(jsonPath("$._embedded.bcParksRegionCode[0].characterAlias").value("R1")) + .andExpect(jsonPath("$._embedded.bcParksRegionCode[0].integerAlias").value(1)); + + 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()); + } + + @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("$._embedded.bcParksSectionCode[0].bcParksOrgUnitTypeCode").value("SECTION")) + .andExpect(jsonPath("$._embedded.bcParksSectionCode[0].orgUnitName").value("Section 1")) + .andExpect(jsonPath("$._embedded.bcParksSectionCode[0].orgUnitId").value(1)) + .andExpect(jsonPath("$._embedded.bcParksSectionCode[0].characterAlias").value("S1")) + .andExpect(jsonPath("$._embedded.bcParksSectionCode[0].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/data/assemblers/BCParksRegionCodeResourceAssemblerTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssemblerTest.java new file mode 100644 index 00000000..f626b476 --- /dev/null +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/data/assemblers/BCParksRegionCodeResourceAssemblerTest.java @@ -0,0 +1,56 @@ +package ca.bc.gov.nrs.wfprev.data.assemblers; + +import ca.bc.gov.nrs.wfprev.data.entities.BCParksOrgUnitEntity; +import ca.bc.gov.nrs.wfprev.data.models.BCParksRegionCodeModel; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Date; + +import static org.junit.Assert.assertEquals; + +public class BCParksRegionCodeResourceAssemblerTest { + + public static final Integer ORG_UNIT_IDENTIFIER = 1; + public static final Integer PARENT_ORG_UNIT_IDENTIFIER = 2; + public static final Date EFFECTIVE_DATE = new Date(); + public static final Date EXPIRY_DATE = new Date(); + public static final String BC_PARKS_ORG_UNIT_TYPE_CODE = "bcParksOrgUnitTypeCode"; + public static final String ORG_UNIT_NAME = "orgUnitName"; + public static final int INTEGER_ALIAS = 1; + public static final String CHARACTER_ALIAS = "characterAlias"; + private BCParksRegionCodeResourceAssembler assembler; + + @BeforeEach + void setUp() { + assembler = new BCParksRegionCodeResourceAssembler(); + } + + @Test + void toModel_ShouldConvertAllFields() { + // Given + BCParksOrgUnitEntity entity = new BCParksOrgUnitEntity(); + entity.setOrgUnitIdentifier(ORG_UNIT_IDENTIFIER); + entity.setEffectiveDate(EFFECTIVE_DATE); + entity.setExpiryDate(EXPIRY_DATE); + entity.setBcParksOrgUnitTypeCode(BC_PARKS_ORG_UNIT_TYPE_CODE); + entity.setParentOrgUnitIdentifier(PARENT_ORG_UNIT_IDENTIFIER); + entity.setOrgUnitName(ORG_UNIT_NAME); + entity.setIntegerAlias(INTEGER_ALIAS); + entity.setCharacterAlias(CHARACTER_ALIAS); + + // When + BCParksRegionCodeModel model = assembler.toModel(entity); + + // Then + assertEquals(ORG_UNIT_IDENTIFIER, model.getOrgUnitId()); + assertEquals(EFFECTIVE_DATE, model.getEffectiveDate()); + assertEquals(EFFECTIVE_DATE, model.getExpiryDate()); + assertEquals(BC_PARKS_ORG_UNIT_TYPE_CODE, model.getBcParksOrgUnitTypeCode()); + assertEquals(PARENT_ORG_UNIT_IDENTIFIER.toString(), model.getParentOrgUnitId()); + assertEquals(ORG_UNIT_NAME, model.getOrgUnitName()); + assertEquals(ORG_UNIT_IDENTIFIER, model.getIntegerAlias()); + assertEquals(CHARACTER_ALIAS, model.getCharacterAlias()); + + } +} 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 b8f996fe..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 @@ -1,22 +1,28 @@ package ca.bc.gov.nrs.wfprev.services; 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; import ca.bc.gov.nrs.wfprev.data.assemblers.GeneralScopeCodeResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ProgramAreaResourceAssembler; import ca.bc.gov.nrs.wfprev.data.assemblers.ProjectTypeCodeResourceAssembler; +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.entities.GeneralScopeCodeEntity; 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; import ca.bc.gov.nrs.wfprev.data.models.GeneralScopeCodeModel; import ca.bc.gov.nrs.wfprev.data.models.ProgramAreaModel; import ca.bc.gov.nrs.wfprev.data.models.ProjectTypeCodeModel; +import ca.bc.gov.nrs.wfprev.data.repositories.BCParksOrgUnitCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.ForestAreaCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.ForestOrgUnitCodeRepository; import ca.bc.gov.nrs.wfprev.data.repositories.GeneralScopeCodeRepository; @@ -50,6 +56,9 @@ class CodesServiceTest { private ForestOrgUnitCodeRepository forestOrgUnitCodeRepository; private ForestRegionUnitCodeResourceAssembler forestRegionUnitCodeResourceAssembler; private ForestDistrictUnitCodeResourceAssembler forestDistrictUnitCodeResourceAssembler; + private BCParksOrgUnitCodeRepository bcParksOrgUnitCodeRepository; + private BCParksRegionCodeResourceAssembler bcParksRegionCodeResourceAssembler; + private BCParksSectionCodeResourceAssembler bcParksSectionCodeResourceAssembler; @BeforeEach void setup() { @@ -64,11 +73,15 @@ void setup() { forestOrgUnitCodeRepository = mock(ForestOrgUnitCodeRepository.class); forestRegionUnitCodeResourceAssembler = mock(ForestRegionUnitCodeResourceAssembler.class); 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); + forestOrgUnitCodeRepository, forestRegionUnitCodeResourceAssembler, forestDistrictUnitCodeResourceAssembler, bcParksOrgUnitCodeRepository, bcParksRegionCodeResourceAssembler, + bcParksSectionCodeResourceAssembler); } @Test @@ -99,7 +112,7 @@ void testGetAllForestAreaCodes_Exception() { ServiceException.class, () -> codesService.getAllForestAreaCodes() ); - assertEquals("Error fetching forest area codes",exception.getMessage()); + assertEquals("Error fetching forest area codes", exception.getMessage()); } @Test @@ -254,7 +267,7 @@ void testGetAllGeneralScopeCodes_Exception() { ServiceException.class, () -> codesService.getAllGeneralScopeCodes() ); - assertEquals("Error fetching general scope codes",exception.getMessage()); + assertEquals("Error fetching general scope codes", exception.getMessage()); } @Test @@ -567,4 +580,174 @@ void testGetForestRegionCodeById_Success() { verifyNoMoreInteractions(forestOrgUnitCodeRepository, forestRegionUnitCodeResourceAssembler); } + + @Test + void testGetAllBCParksRegionCodes_Success() { + // Given + List entities = List.of(new BCParksOrgUnitEntity(), new BCParksOrgUnitEntity()); + BCParksRegionCodeModel expectedModel = new BCParksRegionCodeModel(); + 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(bcParksRegionCodeResourceAssembler.toCollectionModel(entities)).thenReturn(expectedModelCollection); + + // When + CollectionModel result = codesService.getAllBCParksRegionCodes(); + + // Then + assertEquals(expectedModelCollection, result); + verify(bcParksOrgUnitCodeRepository, times(1)).findByBcParksOrgUnitTypeCode("REGION"); + 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); + } + + @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