From 6646bd61fa3fcdfc962d20e99207d38d7e620252 Mon Sep 17 00:00:00 2001 From: Aidan Gordon Date: Sun, 1 Dec 2024 11:56:48 -0800 Subject: [PATCH] Added CodeServicesTest --- .../gov/nrs/wfprev/services/CodesService.java | 128 ++++---- .../nrs/wfprev/services/CodesServiceTest.java | 273 ++++++++++++++++++ .../wfprev/services/ProjectServiceTest.java | 20 ++ 3 files changed, 360 insertions(+), 61 deletions(-) create mode 100644 server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java 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 a81541718..f91a64438 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 @@ -25,75 +25,81 @@ @Slf4j @Component public class CodesService implements CommonService { - private ForestAreaCodeRepository forestAreaCodeRepository; - private ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler; - private GeneralScopeCodeRepository generalScopeCodeRepository; - private GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler; - private ProjectTypeCodeRepository projectTypeCodeRepository; - private ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler; + private ForestAreaCodeRepository forestAreaCodeRepository; + private ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler; + private GeneralScopeCodeRepository generalScopeCodeRepository; + private GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler; + private ProjectTypeCodeRepository projectTypeCodeRepository; + private ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler; - public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler, - GeneralScopeCodeRepository generalScopeCodeRepository, GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler, - ProjectTypeCodeRepository projectTypeCodeRepository, ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler) { - this.forestAreaCodeRepository = forestAreaCodeRepository; - this.forestAreaCodeResourceAssembler = forestAreaCodeResourceAssembler; - this.generalScopeCodeRepository = generalScopeCodeRepository; - this.generalScopeCodeResourceAssembler = generalScopeCodeResourceAssembler; - this.projectTypeCodeRepository = projectTypeCodeRepository; - this.projectTypeCodeResourceAssembler = projectTypeCodeResourceAssembler; - } + public CodesService(ForestAreaCodeRepository forestAreaCodeRepository, ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler, + GeneralScopeCodeRepository generalScopeCodeRepository, GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler, + ProjectTypeCodeRepository projectTypeCodeRepository, ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler) { + this.forestAreaCodeRepository = forestAreaCodeRepository; + this.forestAreaCodeResourceAssembler = forestAreaCodeResourceAssembler; + this.generalScopeCodeRepository = generalScopeCodeRepository; + this.generalScopeCodeResourceAssembler = generalScopeCodeResourceAssembler; + this.projectTypeCodeRepository = projectTypeCodeRepository; + this.projectTypeCodeResourceAssembler = projectTypeCodeResourceAssembler; + } - /** FOREST AREA CODES **/ - public CollectionModel getAllForestAreaCodes() throws ServiceException { - try { - List entities = new ArrayList<>(); - return forestAreaCodeResourceAssembler.toCollectionModel(entities); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + /** + * FOREST AREA CODES + **/ + public CollectionModel getAllForestAreaCodes() throws ServiceException { + try { + List entities = forestAreaCodeRepository.findAll(); + return forestAreaCodeResourceAssembler.toCollectionModel(entities); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } } - } - - public ForestAreaCodeModel getForestAreaCodeById(String id) throws ServiceException { - try { - return forestAreaCodeRepository.findById(id).map(forestAreaCodeResourceAssembler::toModel).orElse(null); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + + public ForestAreaCodeModel getForestAreaCodeById(String id) throws ServiceException { + try { + return forestAreaCodeRepository.findById(id).map(forestAreaCodeResourceAssembler::toModel).orElse(null); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } } - } - /** GENERAL SCOPE CODES **/ - public CollectionModel getAllGeneralScopeCodes() throws ServiceException { - try { - List entities = new ArrayList<>(); - return generalScopeCodeResourceAssembler.toCollectionModel(entities); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + /** + * GENERAL SCOPE CODES + **/ + public CollectionModel getAllGeneralScopeCodes() throws ServiceException { + try { + List entities = generalScopeCodeRepository.findAll(); + return generalScopeCodeResourceAssembler.toCollectionModel(entities); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } } - } - - public GeneralScopeCodeModel getGeneralScopeCodeById(String id) throws ServiceException { - try { - return generalScopeCodeRepository.findById(id).map(generalScopeCodeResourceAssembler::toModel).orElse(null); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + + public GeneralScopeCodeModel getGeneralScopeCodeById(String id) throws ServiceException { + try { + return generalScopeCodeRepository.findById(id).map(generalScopeCodeResourceAssembler::toModel).orElse(null); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } } - } - /** PROJECT TYPE CODES **/ - public CollectionModel getAllProjectTypeCodes() throws ServiceException { - try { - List entities = new ArrayList<>(); - return projectTypeCodeResourceAssembler.toCollectionModel(entities); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + /** + * PROJECT TYPE CODES + **/ + public CollectionModel getAllProjectTypeCodes() throws ServiceException { + try { + List entities = projectTypeCodeRepository.findAll(); + return projectTypeCodeResourceAssembler.toCollectionModel(entities); + } catch (Exception e) { + throw new ServiceException(e.getLocalizedMessage(), e); + } } - } - - public ProjectTypeCodeModel getProjectTypeCodeById(String id) throws ServiceException { - try { - return projectTypeCodeRepository.findById(id).map(projectTypeCodeResourceAssembler::toModel).orElse(null); - } catch(Exception e) { - throw new ServiceException(e.getLocalizedMessage(), e); + + public ProjectTypeCodeModel getProjectTypeCodeById(String id) throws ServiceException { + try { + return projectTypeCodeRepository.findById(id).map(projectTypeCodeResourceAssembler::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/services/CodesServiceTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java new file mode 100644 index 000000000..bb2d98ccd --- /dev/null +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/CodesServiceTest.java @@ -0,0 +1,273 @@ +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.ForestAreaCodeResourceAssembler; +import ca.bc.gov.nrs.wfprev.data.assemblers.GeneralScopeCodeResourceAssembler; +import ca.bc.gov.nrs.wfprev.data.assemblers.ProjectTypeCodeResourceAssembler; +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.ProjectTypeCodeEntity; +import ca.bc.gov.nrs.wfprev.data.models.ForestAreaCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.GeneralScopeCodeModel; +import ca.bc.gov.nrs.wfprev.data.models.ProjectTypeCodeModel; +import ca.bc.gov.nrs.wfprev.data.repositories.ForestAreaCodeRepository; +import ca.bc.gov.nrs.wfprev.data.repositories.GeneralScopeCodeRepository; +import ca.bc.gov.nrs.wfprev.data.repositories.ProjectTypeCodeRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.hateoas.CollectionModel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CodesServiceTest { + private CodesService codesService; + private ForestAreaCodeRepository forestAreaCodeRepository; + private ForestAreaCodeResourceAssembler forestAreaCodeResourceAssembler; + private GeneralScopeCodeRepository generalScopeCodeRepository; + private GeneralScopeCodeResourceAssembler generalScopeCodeResourceAssembler; + private ProjectTypeCodeRepository projectTypeCodeRepository; + private ProjectTypeCodeResourceAssembler projectTypeCodeResourceAssembler; + + @BeforeEach + void setup() { + forestAreaCodeRepository = mock(ForestAreaCodeRepository.class); + forestAreaCodeResourceAssembler = mock(ForestAreaCodeResourceAssembler.class); + generalScopeCodeRepository = mock(GeneralScopeCodeRepository.class); + generalScopeCodeResourceAssembler = mock(GeneralScopeCodeResourceAssembler.class); + projectTypeCodeRepository = mock(ProjectTypeCodeRepository.class); + projectTypeCodeResourceAssembler = mock(ProjectTypeCodeResourceAssembler.class); + + codesService = new CodesService(forestAreaCodeRepository, forestAreaCodeResourceAssembler, + generalScopeCodeRepository, generalScopeCodeResourceAssembler, + projectTypeCodeRepository, projectTypeCodeResourceAssembler); + } + + @Test + void testGetAllForestAreaCodes_Success() throws ServiceException { + // Arrange + List entities = new ArrayList<>(); + entities.add(new ForestAreaCodeEntity()); + entities.add(new ForestAreaCodeEntity()); + + when(forestAreaCodeRepository.findAll()).thenReturn(entities); + when(forestAreaCodeResourceAssembler.toCollectionModel(entities)) + .thenReturn(CollectionModel.of(new ArrayList<>())); + + // Act + CollectionModel result = codesService.getAllForestAreaCodes(); + + // Assert + assertNotNull(result); + } + + @Test + void testGetForestAreaCodeById_Success() throws ServiceException { + // Arrange + String exampleId = UUID.randomUUID().toString(); + ForestAreaCodeEntity entity = new ForestAreaCodeEntity(); + when(forestAreaCodeRepository.findById(exampleId)) + .thenReturn(Optional.of(entity)); + when(forestAreaCodeResourceAssembler.toModel(entity)) + .thenReturn(new ForestAreaCodeModel()); + + // Act + ForestAreaCodeModel result = codesService.getForestAreaCodeById(exampleId); + + // Assert + assertNotNull(result); + } + + @Test + void testGetForestAreaCodeById_NotFound() throws ServiceException { + // Arrange + String nonExistentId = UUID.randomUUID().toString(); + when(forestAreaCodeRepository.findById(nonExistentId)) + .thenReturn(Optional.empty()); + + // Act + ForestAreaCodeModel result = codesService.getForestAreaCodeById(nonExistentId); + + // Assert + assertNull(result); + } + + @Test + void testGetForestAreaCodeById_Exception() { + // Arrange + String exampleId = UUID.randomUUID().toString(); + when(forestAreaCodeRepository.findById(exampleId)) + .thenThrow(new RuntimeException("Error fetching forest area code")); + + // Act & Assert + ServiceException exception = assertThrows( + ServiceException.class, + () -> codesService.getForestAreaCodeById(exampleId) + ); + assertTrue(exception.getMessage().contains("Error fetching forest area code")); + } + + @Test + public void getAllProjectTypeCodes_Success() throws ServiceException { + // Arrange + List entities = new ArrayList<>(); + entities.add(new ProjectTypeCodeEntity()); + entities.add(new ProjectTypeCodeEntity()); + + when(projectTypeCodeRepository.findAll()).thenReturn(entities); + when(projectTypeCodeResourceAssembler.toCollectionModel(entities)) + .thenReturn(CollectionModel.of(new ArrayList<>())); + + // Act + CollectionModel result = codesService.getAllProjectTypeCodes(); + + // Assert + assertNotNull(result); + } + + @Test + void testGetAllProjectTypeCodes_Exception() { + // Arrange + when(projectTypeCodeRepository.findAll()).thenThrow(new RuntimeException("Error fetching project type codes")); + + // Act & Assert + ServiceException exception = assertThrows( + ServiceException.class, + () -> codesService.getAllProjectTypeCodes() + ); + assertEquals("Error fetching project type codes", exception.getMessage()); + } + + @Test + void testGetProjectTypeCodeById_Success() throws ServiceException { + // Arrange + String exampleId = UUID.randomUUID().toString(); + ProjectTypeCodeEntity entity = new ProjectTypeCodeEntity(); + when(projectTypeCodeRepository.findById(exampleId)) + .thenReturn(Optional.of(entity)); + when(projectTypeCodeResourceAssembler.toModel(entity)) + .thenReturn(new ProjectTypeCodeModel()); + + // Act + ProjectTypeCodeModel result = codesService.getProjectTypeCodeById(exampleId); + + // Assert + assertNotNull(result); + } + + @Test + void testGetProjectTypeCodeById_NotFound() throws ServiceException { + // Arrange + String nonExistentId = UUID.randomUUID().toString(); + when(projectTypeCodeRepository.findById(nonExistentId)) + .thenReturn(Optional.empty()); + + // Act + ProjectTypeCodeModel result = codesService.getProjectTypeCodeById(nonExistentId); + + // Assert + assertNull(result); + } + + + @Test + void testProjectTypeAreaCodeById_Exception() { + // Arrange + String exampleId = UUID.randomUUID().toString(); + when(projectTypeCodeRepository.findById(exampleId)) + .thenThrow(new RuntimeException("Error fetching project type code")); + + // Act & Assert + ServiceException exception = assertThrows( + ServiceException.class, + () -> codesService.getProjectTypeCodeById(exampleId) + ); + assertTrue(exception.getMessage().contains("Error fetching project type code")); + } + + @Test + void testGetAllGeneralScopeCodes_Success() throws ServiceException { + // Arrange + List entities = new ArrayList<>(); + entities.add(new GeneralScopeCodeEntity()); + entities.add(new GeneralScopeCodeEntity()); + + when(generalScopeCodeRepository.findAll()).thenReturn(entities); + when(generalScopeCodeResourceAssembler.toCollectionModel(entities)) + .thenReturn(CollectionModel.of(new ArrayList<>())); + + // Act + CollectionModel result = codesService.getAllGeneralScopeCodes(); + + // Assert + assertNotNull(result); + } + + @Test + void testGetAllGeneralScopeCodes_Exception() { + // Arrange + when(generalScopeCodeRepository.findAll()).thenThrow(new RuntimeException("Error fetching general scope codes")); + + // Act & Assert + ServiceException exception = assertThrows( + ServiceException.class, + () -> codesService.getAllGeneralScopeCodes() + ); + assertEquals("Error fetching general scope codes",exception.getMessage()); + } + + @Test + void testGetGeneralScopeCodeById_Success() throws ServiceException { + // Arrange + String exampleId = UUID.randomUUID().toString(); + GeneralScopeCodeEntity entity = new GeneralScopeCodeEntity(); + when(generalScopeCodeRepository.findById(exampleId)) + .thenReturn(Optional.of(entity)); + when(generalScopeCodeResourceAssembler.toModel(entity)) + .thenReturn(new GeneralScopeCodeModel()); + + // Act + GeneralScopeCodeModel result = codesService.getGeneralScopeCodeById(exampleId); + + // Assert + assertNotNull(result); + } + + @Test + void testGetGeneralScopeCodeById_NotFound() throws ServiceException { + // Arrange + String nonExistentId = UUID.randomUUID().toString(); + when(generalScopeCodeRepository.findById(nonExistentId)) + .thenReturn(Optional.empty()); + + // Act + GeneralScopeCodeModel result = codesService.getGeneralScopeCodeById(nonExistentId); + + // Assert + assertNull(result); + } + + + @Test + void testGeneralScopeCodeById_Exception() { + // Arrange + String exampleId = UUID.randomUUID().toString(); + when(generalScopeCodeRepository.findById(exampleId)) + .thenThrow(new RuntimeException("Error fetching general scope code")); + + // Act & Assert + ServiceException exception = assertThrows( + ServiceException.class, + () -> codesService.getGeneralScopeCodeById(exampleId) + ); + assertTrue(exception.getMessage().contains("Error fetching general scope code")); + } + + +} \ No newline at end of file diff --git a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/ProjectServiceTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/ProjectServiceTest.java index ac0061bd0..768689c33 100644 --- a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/ProjectServiceTest.java +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/services/ProjectServiceTest.java @@ -5,6 +5,7 @@ import ca.bc.gov.nrs.wfprev.data.entities.*; import ca.bc.gov.nrs.wfprev.data.models.*; import ca.bc.gov.nrs.wfprev.data.repositories.*; +import jakarta.persistence.EntityNotFoundException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -350,6 +351,25 @@ public void test_create_project_with_exception() { assertTrue(exception.getMessage().contains("Error saving project")); } + @Test + public void test_create_project_with_service_exception () { + // Given + ProjectModel inputModel = ProjectModel.builder() + .projectName("Test Project") + .siteUnitName("Test Site") + .totalPlannedProjectSizeHa(BigDecimal.valueOf(100)) + .build(); + + when(projectResourceAssembler.toEntity(any())).thenThrow(new EntityNotFoundException("Error saving project")); + + // When/Then + ServiceException exception = assertThrows( + ServiceException.class, + () -> projectService.createOrUpdateProject(inputModel) + ); + assertTrue(exception.getMessage().contains("Error saving project")); + } + @Test public void test_delete_project_with_valid_id() { // Given