Skip to content

Commit

Permalink
Added getByID to BC Parks Regions
Browse files Browse the repository at this point in the history
  • Loading branch information
agordon-vivid committed Dec 12, 2024
1 parent 889d9d1 commit acf7ce0
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
Expand Down Expand Up @@ -194,4 +195,13 @@ public CollectionModel<BCParksRegionCodeModel> getAllBCParksRegionCodes() {
throw new ServiceException(e.getLocalizedMessage(), e);
}
}

public BCParksRegionCodeModel getBCParksRegionCodeById(Integer number) {
try {
Optional<BCParksOrgUnitEntity> byId = bcParksOrgUnitCodeRepository.findById(number);
return byId.map(bcParksRegionCodeResourceAssembler::toModel).orElse(null);
} catch (Exception e) {
throw new ServiceException(e.getLocalizedMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ForestAreaCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<ForestAreaCodeModel> facModel = CollectionModel.of(facList);
List<ForestAreaCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<ForestAreaCodeModel> 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<GeneralScopeCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<GeneralScopeCodeModel> facModel = CollectionModel.of(facList);
List<GeneralScopeCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<GeneralScopeCodeModel> 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<ProjectTypeCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<ProjectTypeCodeModel> facModel = CollectionModel.of(facList);
List<ProjectTypeCodeModel> facList = Arrays.asList(fac1, fac2);
CollectionModel<ProjectTypeCodeModel> 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
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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());
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit acf7ce0

Please sign in to comment.