Skip to content

Commit

Permalink
Added BC Parks section endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
agordon-vivid committed Dec 12, 2024
1 parent acf7ce0 commit 2896e73
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BCParksOrgUnitEntity, BCParksSectionCodeModel> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -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<BCParksSectionCodeModel> {
private Integer orgUnitId;
private Date effectiveDate;
private Date expiryDate;
private String bcParksOrgUnitTypeCode;
private String parentOrgUnitId;
private String orgUnitName;
private Integer integerAlias;
private String characterAlias;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -72,6 +76,7 @@ public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAre
this.forestDistrictCodeResourceAssembler = forestDistrictUnitCodeResourceAssembler;
this.bcParksOrgUnitCodeRepository = bcParksOrgUnitCodeRepository;
this.bcParksRegionCodeResourceAssembler = bcParksRegionCodeResourceAssembler;
this.bcParksSectionCodeResourceAssembler = bcParksSectionCodeResourceAssembler;
}

/**
Expand Down Expand Up @@ -204,4 +209,21 @@ public BCParksRegionCodeModel getBCParksRegionCodeById(Integer number) {
throw new ServiceException(e.getLocalizedMessage(), e);
}
}

public CollectionModel<BCParksSectionCodeModel> getAllBCParksSectionCodes() {
try {
List<BCParksOrgUnitEntity> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BCParksSectionCodeModel> 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());
}

}
Loading

0 comments on commit 2896e73

Please sign in to comment.