Skip to content

Commit

Permalink
getting test coverage up
Browse files Browse the repository at this point in the history
  • Loading branch information
agordon-vivid committed Nov 30, 2024
1 parent 96d82fb commit 42d571a
Show file tree
Hide file tree
Showing 6 changed files with 853 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
htmlReport/
/target
/.vscode
/.idea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,59 @@
@Component
public class ProjectResourceAssembler extends RepresentationModelAssemblerSupport<ProjectEntity, ProjectModel> {

public ProjectResourceAssembler() {
super(ProjectController.class, ProjectModel.class);
}

public ProjectEntity toEntity(ProjectModel resource) {
ProjectEntity entity = new ProjectEntity();

entity.setProjectGuid(UUID.fromString(resource.getProjectGuid()));
entity.setProjectTypeCode(toProjectTypeCodeEntity(resource.getProjectTypeCode()));
entity.setProjectNumber(resource.getProjectNumber());
entity.setSiteUnitName(resource.getSiteUnitName());
entity.setForestAreaCode(toForestAreaCodeEntity(resource.getForestAreaCode()));
entity.setGeneralScopeCode(toGeneralScopeCodeEntity(resource.getGeneralScopeCode()));
entity.setProgramAreaGuid(UUID.fromString(resource.getProgramAreaGuid()));
entity.setForestRegionOrgUnitId(resource.getForestRegionOrgUnitId());
entity.setForestDistrictOrgUnitId(resource.getForestDistrictOrgUnitId());
entity.setFireCentreOrgUnitId(resource.getFireCentreOrgUnitId());
entity.setBcParksRegionOrgUnitId(resource.getBcParksRegionOrgUnitId());
entity.setBcParksSectionOrgUnitId(resource.getBcParksSectionOrgUnitId());
entity.setProjectName(resource.getProjectName());
entity.setProjectLead(resource.getProjectLead());
entity.setProjectLeadEmailAddress(resource.getProjectLeadEmailAddress());
entity.setProjectDescription(resource.getProjectDescription());
entity.setClosestCommunityName(resource.getClosestCommunityName());
entity.setTotalFundingRequestAmount(resource.getTotalFundingRequestAmount());
entity.setTotalAllocatedAmount(resource.getTotalAllocatedAmount());
entity.setTotalPlannedProjectSizeHa(resource.getTotalPlannedProjectSizeHa());
entity.setTotalPlannedCostPerHectare(resource.getTotalPlannedCostPerHectare());
entity.setTotalActualAmount(resource.getTotalActualAmount());
entity.setTotalProjectSizeHa(resource.getTotalProjectSizeHa());
entity.setTotalCostPerHectareAmount(resource.getTotalCostPerHectareAmount());
entity.setIsMultiFiscalYearProj(resource.getIsMultiFiscalYearProj());
entity.setLatitude(resource.getLatitude());
entity.setLongitude(resource.getLongitude());
entity.setLastProgressUpdateTimestamp(resource.getLastProgressUpdateTimestamp());
entity.setRevisionCount(resource.getRevisionCount());
entity.setCreateUser(resource.getCreateUser());
entity.setCreateDate(resource.getCreateDate());
entity.setUpdateUser(resource.getUpdateUser());
entity.setUpdateDate(resource.getUpdateDate());
return entity;
}

@Override
public ProjectModel toModel(ProjectEntity entity) {
ProjectModel resource = instantiateModel(entity);
resource.add(linkTo(
methodOn(ProjectController.class)
.getById(entity.getProgramAreaGuid().toString()))
.withSelfRel());
public ProjectResourceAssembler() {
super(ProjectController.class, ProjectModel.class);
}

public ProjectEntity toEntity(ProjectModel resource) {
ProjectEntity entity = new ProjectEntity();

entity.setProjectGuid(UUID.fromString(resource.getProjectGuid()));
entity.setProjectTypeCode(toProjectTypeCodeEntity(resource.getProjectTypeCode()));
entity.setProjectNumber(resource.getProjectNumber());
entity.setSiteUnitName(resource.getSiteUnitName());
entity.setForestAreaCode(toForestAreaCodeEntity(resource.getForestAreaCode()));
entity.setGeneralScopeCode(toGeneralScopeCodeEntity(resource.getGeneralScopeCode()));
entity.setProgramAreaGuid(UUID.fromString(resource.getProgramAreaGuid()));
entity.setForestRegionOrgUnitId(resource.getForestRegionOrgUnitId());
entity.setForestDistrictOrgUnitId(resource.getForestDistrictOrgUnitId());
entity.setFireCentreOrgUnitId(resource.getFireCentreOrgUnitId());
entity.setBcParksRegionOrgUnitId(resource.getBcParksRegionOrgUnitId());
entity.setBcParksSectionOrgUnitId(resource.getBcParksSectionOrgUnitId());
entity.setProjectName(resource.getProjectName());
entity.setProjectLead(resource.getProjectLead());
entity.setProjectLeadEmailAddress(resource.getProjectLeadEmailAddress());
entity.setProjectDescription(resource.getProjectDescription());
entity.setClosestCommunityName(resource.getClosestCommunityName());
entity.setTotalFundingRequestAmount(resource.getTotalFundingRequestAmount());
entity.setTotalAllocatedAmount(resource.getTotalAllocatedAmount());
entity.setTotalPlannedProjectSizeHa(resource.getTotalPlannedProjectSizeHa());
entity.setTotalPlannedCostPerHectare(resource.getTotalPlannedCostPerHectare());
entity.setTotalActualAmount(resource.getTotalActualAmount());
entity.setTotalProjectSizeHa(resource.getTotalProjectSizeHa());
entity.setTotalCostPerHectareAmount(resource.getTotalCostPerHectareAmount());
entity.setIsMultiFiscalYearProj(resource.getIsMultiFiscalYearProj());
entity.setLatitude(resource.getLatitude());
entity.setLongitude(resource.getLongitude());
entity.setLastProgressUpdateTimestamp(resource.getLastProgressUpdateTimestamp());
entity.setRevisionCount(resource.getRevisionCount());
entity.setCreateUser(resource.getCreateUser());
entity.setCreateDate(resource.getCreateDate());
entity.setUpdateUser(resource.getUpdateUser());
entity.setUpdateDate(resource.getUpdateDate());

return entity;
}

@Override
public ProjectModel toModel(ProjectEntity entity) {
ProjectModel resource = instantiateModel(entity);

resource.add(linkTo(
methodOn(ProjectController.class)
.getById(entity.getProgramAreaGuid().toString()))
.withSelfRel());

resource.setProjectGuid(entity.getProjectGuid().toString());
resource.setProjectTypeCode(toProjectTypeCodeModel(entity.getProjectTypeCode()));
resource.setProjectNumber(entity.getProjectNumber());
Expand Down Expand Up @@ -109,52 +109,52 @@ public ProjectModel toModel(ProjectEntity entity) {
resource.setUpdateUser(entity.getUpdateUser());
resource.setUpdateDate(entity.getUpdateDate());

return resource;
}
@Override
public CollectionModel<ProjectModel> toCollectionModel(Iterable<? extends ProjectEntity> entities)
{
CollectionModel<ProjectModel> resources = super.toCollectionModel(entities);
resources.add(linkTo(methodOn(ProjectController.class).getAllProjects()).withSelfRel());
return resources;
}

private ProjectTypeCodeModel toProjectTypeCodeModel(ProjectTypeCodeEntity code) {
ProjectTypeCodeResourceAssembler ra = new ProjectTypeCodeResourceAssembler();
return ra.toModel(code);
}

private ProjectTypeCodeEntity toProjectTypeCodeEntity(ProjectTypeCodeModel code) {
ProjectTypeCodeResourceAssembler ra = new ProjectTypeCodeResourceAssembler();
return ra.toEntity(code);
}
private ForestAreaCodeModel toForestAreaCodeModel(ForestAreaCodeEntity code) {
ForestAreaCodeResourceAssembler ra = new ForestAreaCodeResourceAssembler();
return ra.toModel(code);
}

private ForestAreaCodeEntity toForestAreaCodeEntity(ForestAreaCodeModel code) {
ForestAreaCodeResourceAssembler ra = new ForestAreaCodeResourceAssembler();
return ra.toEntity(code);
}

private GeneralScopeCodeModel toGeneralScopeCodeModel(GeneralScopeCodeEntity code) {
GeneralScopeCodeResourceAssembler ra = new GeneralScopeCodeResourceAssembler();
return ra.toModel(code);
}

private GeneralScopeCodeEntity toGeneralScopeCodeEntity(GeneralScopeCodeModel code) {
GeneralScopeCodeResourceAssembler ra = new GeneralScopeCodeResourceAssembler();
return ra.toEntity(code);
}
return resource;
}

@Override
public CollectionModel<ProjectModel> toCollectionModel(Iterable<? extends ProjectEntity> entities) {
System.out.println("entities: " + entities);
CollectionModel<ProjectModel> resources = super.toCollectionModel(entities);

resources.add(linkTo(methodOn(ProjectController.class).getAllProjects()).withSelfRel());

return resources;
}

private ProjectTypeCodeModel toProjectTypeCodeModel(ProjectTypeCodeEntity code) {

ProjectTypeCodeResourceAssembler ra = new ProjectTypeCodeResourceAssembler();
return ra.toModel(code);
}

private ProjectTypeCodeEntity toProjectTypeCodeEntity(ProjectTypeCodeModel code) {

ProjectTypeCodeResourceAssembler ra = new ProjectTypeCodeResourceAssembler();
return ra.toEntity(code);
}

private ForestAreaCodeModel toForestAreaCodeModel(ForestAreaCodeEntity code) {

ForestAreaCodeResourceAssembler ra = new ForestAreaCodeResourceAssembler();
return ra.toModel(code);
}

private ForestAreaCodeEntity toForestAreaCodeEntity(ForestAreaCodeModel code) {

ForestAreaCodeResourceAssembler ra = new ForestAreaCodeResourceAssembler();
return ra.toEntity(code);
}

private GeneralScopeCodeModel toGeneralScopeCodeModel(GeneralScopeCodeEntity code) {

GeneralScopeCodeResourceAssembler ra = new GeneralScopeCodeResourceAssembler();
return ra.toModel(code);
}

private GeneralScopeCodeEntity toGeneralScopeCodeEntity(GeneralScopeCodeModel code) {

GeneralScopeCodeResourceAssembler ra = new GeneralScopeCodeResourceAssembler();
return ra.toEntity(code);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ca.bc.gov.nrs.wfprev.services;

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

import ca.bc.gov.nrs.wfprev.data.entities.ForestAreaCodeEntity;
Expand Down Expand Up @@ -47,8 +47,8 @@ public ProjectService(ProjectRepository projectRepository, ProjectResourceAssemb

public CollectionModel<ProjectModel> getAllProjects() throws ServiceException {
try {
List<ProjectEntity> entities = new ArrayList<>();
return projectResourceAssembler.toCollectionModel(entities);
List<ProjectEntity> all = projectRepository.findAll();
return projectResourceAssembler.toCollectionModel(all);
} catch(Exception e) {
throw new ServiceException(e.getLocalizedMessage(), e);
}
Expand All @@ -65,19 +65,16 @@ public ProjectModel getProjectById(String id) throws ServiceException {
@Transactional
public ProjectModel createOrUpdateProject(ProjectModel resource) throws ServiceException {
try {
// For new projects (no GUID), generate a UUID
if (resource.getProjectGuid() == null) {
resource.setCreateDate(new Date());
//TODO - Fix to use proper user
resource.setCreateUser("SYSTEM");
resource.setProjectGuid(UUID.randomUUID().toString());
resource.setCreateDate(new Date());
resource.setCreateUser("SYSTEM");
resource.setRevisionCount(0); // Initialize revision count for new records
}
// Set audit fields
if (resource.getProjectGuid() == null) {
resource.setCreateDate(new Date());
//TODO - Fix to use proper user
resource.setCreateUser("SYSTEM");
}
resource.setUpdateDate(new Date());
//TODO - Fix to use proper user
resource.setUpdateUser("SYSTEM");
Expand All @@ -87,12 +84,11 @@ public ProjectModel createOrUpdateProject(ProjectModel resource) throws ServiceE
// Load ForestAreaCode with null checks
if (resource.getForestAreaCode() != null) {
String forestAreaCode = resource.getForestAreaCode().getForestAreaCode();
if (forestAreaCode != null) {
ForestAreaCodeEntity forestAreaCodeEntity = forestAreaCodeRepository
.findById(forestAreaCode)
.orElseThrow(() -> new EntityNotFoundException(
"Forest Area Code not found: " + forestAreaCode));
entity.setForestAreaCode(forestAreaCodeEntity);
Optional<ForestAreaCodeEntity> forestAreaCodeEntityOpt = forestAreaCodeRepository.findById(forestAreaCode);
if (forestAreaCodeEntityOpt.isPresent()) {
entity.setForestAreaCode(forestAreaCodeEntityOpt.get());
} else {
throw new IllegalArgumentException("ForestAreaCode not found: " + forestAreaCode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.UUID;

import ca.bc.gov.nrs.wfprev.services.ProjectService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -31,7 +32,6 @@
import ca.bc.gov.nrs.wfprev.controllers.ProjectController;
import ca.bc.gov.nrs.wfprev.data.models.ProjectModel;
import ca.bc.gov.nrs.wfprev.data.models.ProjectTypeCodeModel;
import ca.bc.gov.nrs.wfprev.services.ProjectService;

@WebMvcTest(ProjectController.class)
@Import({TestSpringSecurity.class, TestcontainersConfiguration.class})
Expand Down
Loading

0 comments on commit 42d571a

Please sign in to comment.