Skip to content

Commit

Permalink
Added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
agordon-vivid committed Dec 19, 2024
1 parent 93a4bc9 commit 3271c82
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public ProjectModel updateProject(ProjectModel resource) throws ServiceException
ProjectEntity existingEntity = projectRepository.findById(guid)
.orElseThrow(() -> new EntityNotFoundException("Project not found: " + resource.getProjectGuid()));

existingEntity = projectResourceAssembler.updateEntity(resource, existingEntity);
assignAssociatedEntities(resource, existingEntity);
ProjectEntity entity = projectResourceAssembler.updateEntity(resource, existingEntity);
assignAssociatedEntities(resource, entity);

ProjectEntity savedEntity = projectRepository.saveAndFlush(existingEntity);
ProjectEntity savedEntity = projectRepository.saveAndFlush(entity);
return projectResourceAssembler.toModel(savedEntity);
} catch (EntityNotFoundException e) {
throw new ServiceException("Invalid reference data: " + e.getMessage(), e);
Expand Down Expand Up @@ -149,11 +149,13 @@ private void assignAssociatedEntities(ProjectModel resource, ProjectEntity entit
entity.setGeneralScopeCode(loadGeneralScopeCode(resource.getGeneralScopeCode().getGeneralScopeCode()));
}

String projectStatusCode1 = resource.getProjectStatusCode() != null
? resource.getProjectStatusCode().getProjectStatusCode()
: null;
ProjectStatusCodeEntity projectStatusCode = loadOrSetDefaultProjectStatusCode(
projectStatusCode1);
entity.setProjectStatusCode(
loadOrSetDefaultProjectStatusCode(
resource.getProjectStatusCode() != null
? resource.getProjectStatusCode().getProjectStatusCode()
: null));
projectStatusCode);
}

private ForestAreaCodeEntity loadForestAreaCode(String forestAreaCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,152 @@ void test_update_existing_project() throws ServiceException {
assertEquals(existingGuid, capturedModel.getProjectGuid());
}

@Test
void test_updateProject_entityNotFoundException() {
// Given
String nonExistentGuid = UUID.randomUUID().toString();
ProjectModel inputModel = ProjectModel.builder()
.projectGuid(nonExistentGuid)
.build();

when(projectRepository.findById(UUID.fromString(nonExistentGuid)))
.thenReturn(Optional.empty()); // Simulate project not found

// When / Then
ServiceException exception = assertThrows(
ServiceException.class,
() -> projectService.updateProject(inputModel)
);

assertTrue(exception.getMessage().contains("Project not found"));
verify(projectRepository).findById(UUID.fromString(nonExistentGuid));
verify(projectRepository, never()).saveAndFlush(any());
}

@Test
void test_updateProject_dataIntegrityViolationException() {
// Given
String existingGuid = UUID.randomUUID().toString();
ProjectModel inputModel = ProjectModel.builder()
.projectGuid(existingGuid)
.projectName("Updated Project")
.build();

ProjectEntity existingEntity = new ProjectEntity();
existingEntity.setProjectGuid(UUID.fromString(existingGuid));
existingEntity.setProjectName("Old Project");

when(projectRepository.findById(UUID.fromString(existingGuid)))
.thenReturn(Optional.of(existingEntity));

when(projectRepository.saveAndFlush(any(ProjectEntity.class)))
.thenThrow(new DataIntegrityViolationException("Data integrity violation"));

when(projectStatusCodeRepository.findById("ACTIVE"))
.thenReturn(Optional.of(ProjectStatusCodeEntity.builder().projectStatusCode("ACTIVE").build()));

when(projectResourceAssembler.updateEntity(any(ProjectModel.class), any(ProjectEntity.class)))
.thenReturn(existingEntity);

// When / Then
DataIntegrityViolationException exception = assertThrows(
DataIntegrityViolationException.class,
() -> projectService.updateProject(inputModel)
);

assertTrue(exception.getMessage().contains("Data integrity violation"));
verify(projectRepository).saveAndFlush(any(ProjectEntity.class));
}

@Test
void test_updateProject_constraintViolationException() {
// Given
String existingGuid = UUID.randomUUID().toString();
ProjectStatusCodeModel activeStatusModel = ProjectStatusCodeModel.builder()
.projectStatusCode("ACTIVE")
.build();

ProjectStatusCodeEntity activeStatusEntity = ProjectStatusCodeEntity.builder()
.projectStatusCode("ACTIVE")
.build();
ProjectModel inputModel = ProjectModel.builder()
.projectGuid(existingGuid)
.projectName("Updated Project")
.projectStatusCode(activeStatusModel)
.build();

ProjectEntity existingEntity = new ProjectEntity();
existingEntity.setProjectGuid(UUID.fromString(existingGuid));
existingEntity.setProjectName("Old Project");

when(projectStatusCodeRepository.findById("ACTIVE"))
.thenReturn(Optional.of(activeStatusEntity));

when(projectRepository.findById(UUID.fromString(existingGuid)))
.thenReturn(Optional.of(existingEntity));

when(projectResourceAssembler.updateEntity(any(ProjectModel.class), any(ProjectEntity.class)))
.thenReturn(existingEntity);

Set<ConstraintViolation<?>> violations = new HashSet<>();
ConstraintViolation<?> violation = mock(ConstraintViolation.class);
when(violation.getMessage()).thenReturn("Constraint violation occurred");
violations.add(violation);



when(projectRepository.saveAndFlush(any(ProjectEntity.class)))
.thenThrow(new ConstraintViolationException("Constraint violation occurred", violations));

// When / Then
ConstraintViolationException exception = assertThrows(
ConstraintViolationException.class,
() -> projectService.updateProject(inputModel)
);

assertTrue(exception.getMessage().contains("Constraint violation occurred"));
verify(projectRepository).saveAndFlush(any(ProjectEntity.class));
}

@Test
void test_updateProject_generalException() {
// Given
String existingGuid = UUID.randomUUID().toString();
ProjectStatusCodeModel activeStatusModel = ProjectStatusCodeModel.builder()
.projectStatusCode("ACTIVE")
.build();
ProjectModel inputModel = ProjectModel.builder()
.projectGuid(existingGuid)
.projectStatusCode(activeStatusModel)
.projectName("Updated Project")
.build();

ProjectEntity existingEntity = new ProjectEntity();
existingEntity.setProjectGuid(UUID.fromString(existingGuid));
existingEntity.setProjectName("Old Project");

when(projectRepository.findById(UUID.fromString(existingGuid)))
.thenReturn(Optional.of(existingEntity));

when(projectResourceAssembler.updateEntity(any(ProjectModel.class), any(ProjectEntity.class)))
.thenReturn(existingEntity);

when(projectStatusCodeRepository.findById("ACTIVE"))
.thenReturn(Optional.of(ProjectStatusCodeEntity.builder().projectStatusCode("ACTIVE").build()));

when(projectRepository.saveAndFlush(any(ProjectEntity.class)))
.thenThrow(new RuntimeException("Unexpected error"));

// When / Then
ServiceException exception = assertThrows(
ServiceException.class,
() -> projectService.updateProject(inputModel)
);

assertEquals("Unexpected error", exception.getMessage());
verify(projectRepository).saveAndFlush(any(ProjectEntity.class));
}

@Test
void test_create_project_forest_area_code_entity_not_found() {
// Given
Expand Down

0 comments on commit 3271c82

Please sign in to comment.