From a5b5b1f310999e33cb5f40cb6a7b3156c7ef68a1 Mon Sep 17 00:00:00 2001 From: Craig Yu Date: Wed, 28 Aug 2024 12:21:57 -0700 Subject: [PATCH] feat: 1435 tests for different parent tree gw values (#1548) --- .../gov/backendstartapi/dto/OrchardDto.java | 4 + .../backendstartapi/provider/Provider.java | 6 - .../endpoint/OrchardEndpointTest.java | 104 +++--- .../provider/OracleApiProviderTest.java | 160 +++----- .../service/OrchardServiceTest.java | 83 +++-- .../service/SeedlotFormPutTest.java | 347 +++++++++--------- .../gov/oracleapi/service/OrchardService.java | 80 ---- .../oracleapi/service/ParentTreeService.java | 6 +- .../endpoint/OrchardEndpointTest.java | 55 --- .../endpoint/ParentTreeEndpointTest.java | 44 +++ .../oracleapi/service/OrchardServiceTest.java | 48 --- .../service/ParentTreeServiceTest.java | 69 +++- 12 files changed, 451 insertions(+), 555 deletions(-) diff --git a/backend/src/main/java/ca/bc/gov/backendstartapi/dto/OrchardDto.java b/backend/src/main/java/ca/bc/gov/backendstartapi/dto/OrchardDto.java index 255ff7ab7..d6696612f 100644 --- a/backend/src/main/java/ca/bc/gov/backendstartapi/dto/OrchardDto.java +++ b/backend/src/main/java/ca/bc/gov/backendstartapi/dto/OrchardDto.java @@ -1,7 +1,9 @@ package ca.bc.gov.backendstartapi.dto; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; /** @@ -10,6 +12,8 @@ @Schema(description = "Represents an Orchard object received from oracle-api except spuId.") @Getter @Setter +@AllArgsConstructor +@NoArgsConstructor public class OrchardDto { @Schema( description = diff --git a/backend/src/main/java/ca/bc/gov/backendstartapi/provider/Provider.java b/backend/src/main/java/ca/bc/gov/backendstartapi/provider/Provider.java index cc0def51d..95582386a 100644 --- a/backend/src/main/java/ca/bc/gov/backendstartapi/provider/Provider.java +++ b/backend/src/main/java/ca/bc/gov/backendstartapi/provider/Provider.java @@ -5,7 +5,6 @@ import ca.bc.gov.backendstartapi.dto.GeospatialOracleResDto; import ca.bc.gov.backendstartapi.dto.OrchardDto; import ca.bc.gov.backendstartapi.dto.OrchardSpuDto; -import ca.bc.gov.backendstartapi.dto.SameSpeciesTreeDto; import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseDto; import ca.bc.gov.backendstartapi.dto.oracle.SpuDto; import java.util.HashMap; @@ -41,11 +40,6 @@ default Optional findOrchardParentTreeGeneticQualityData( return Optional.empty(); } - default List findParentTreesByVegCode( - String vegCode, Map orchardSpuMap) { - return List.of(); - } - default List getPtGeospatialDataByIdList(List ptIds) { return List.of(); } diff --git a/backend/src/test/java/ca/bc/gov/backendstartapi/endpoint/OrchardEndpointTest.java b/backend/src/test/java/ca/bc/gov/backendstartapi/endpoint/OrchardEndpointTest.java index cb959c42e..1402a57f9 100644 --- a/backend/src/test/java/ca/bc/gov/backendstartapi/endpoint/OrchardEndpointTest.java +++ b/backend/src/test/java/ca/bc/gov/backendstartapi/endpoint/OrchardEndpointTest.java @@ -1,11 +1,13 @@ package ca.bc.gov.backendstartapi.endpoint; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import ca.bc.gov.backendstartapi.dto.OrchardDto; import ca.bc.gov.backendstartapi.dto.OrchardSpuDto; import ca.bc.gov.backendstartapi.exception.NoParentTreeDataException; import ca.bc.gov.backendstartapi.exception.NoSpuForOrchardException; @@ -16,9 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.server.ResponseStatusException; @WebMvcTest(OrchardEndpoint.class) @WithMockUser(username = "SPARTest", roles = "SPAR_NONMINISTRY_ORCHARD") @@ -88,53 +92,55 @@ void getParentTreeGeneticQualityDataNoSpuTest() throws Exception { .andReturn(); } - // TODO - // @Test - // @DisplayName("getAllParentTreeByVegCodeTest") - // void getAllParentTreeByVegCodeTest() throws Exception { - // String vegCode = "PLI"; - - // SameSpeciesTreeDto firstDto = - // new SameSpeciesTreeDto(Long.valueOf(123), "1000", "1", Long.valueOf(7), List.of()); - // SameSpeciesTreeDto secondDto = - // new SameSpeciesTreeDto(Long.valueOf(456), "2000", "1", Long.valueOf(7), List.of()); - - // List testList = List.of(firstDto, secondDto); - - // when(orchardService.findParentTreesByVegCode(vegCode)).thenReturn(testList); - - // mockMvc - // .perform( - // get("/api/orchards/parent-trees/vegetation-codes/{vegCode}", vegCode) - // .with(csrf().asHeader()) - // .header("Content-Type", "application/json") - // .accept(MediaType.APPLICATION_JSON)) - // .andExpect(status().isOk()) - // .andExpect(jsonPath("$[0].parentTreeId").value(firstDto.getParentTreeId())) - // .andExpect(jsonPath("$[0].parentTreeNumber").value(firstDto.getParentTreeNumber())) - // .andExpect(jsonPath("$[1].parentTreeId").value(secondDto.getParentTreeId())) - // .andExpect(jsonPath("$[1].parentTreeNumber").value(secondDto.getParentTreeNumber())) - // .andReturn(); - // } - - // @Test - // @DisplayName("getAllParentTreeByVegCodeErrorTest") - // void getAllParentTreeByVegCodeErrorTest() throws Exception { - // String vegCode = "LAMB"; - - // HttpStatus errCode = HttpStatus.BAD_REQUEST; - // String errMsg = "LAMB is not a veg code."; - - // when(orchardService.findParentTreesByVegCode(vegCode)) - // .thenThrow(new ResponseStatusException(errCode, errMsg)); - // mockMvc - // .perform( - // get("/api/orchards/parent-trees/vegetation-codes/{vegCode}", vegCode) - // .with(csrf().asHeader()) - // .header("Content-Type", "application/json") - // .accept(MediaType.APPLICATION_JSON)) - // .andExpect(status().isBadRequest()) - // .andExpect(status().reason(errMsg)) - // .andReturn(); - // } + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - valid vegCode") + void getOrchardByVegCode_validVegCode_shouldReturnData() throws Exception { + OrchardDto dto = new OrchardDto(); + dto.setId("1"); + dto.setName("Primary Orchard"); + dto.setVegetationCode("BV"); + dto.setLotTypeCode('S'); + dto.setLotTypeDescription("Seed Lot"); + dto.setStageCode("PRD"); + dto.setBecZoneCode("SBS"); + dto.setBecZoneDescription("Sub-Boreal Spruce"); + dto.setBecSubzoneCode("mk"); + dto.setVariant('1'); + dto.setBecVersionId(5); + dto.setSpuId(7); + + when(orchardService.findAllOrchardsByVegCode("BV")).thenReturn(List.of(dto)); + + mockMvc + .perform(get("/api/orchards/vegetation-codes/BV").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value("1")) + .andExpect(jsonPath("$[0].name").value("Primary Orchard")) + .andExpect(jsonPath("$[0].vegetationCode").value("BV")) + .andExpect(jsonPath("$[0].lotTypeCode").value("S")) + .andExpect(jsonPath("$[0].stageCode").value("PRD")); + } + + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - invalid vegCode") + void getOrchardByVegCode_invalidVegCode_shouldReturnEmpty() throws Exception { + when(orchardService.findAllOrchardsByVegCode("INVALID")).thenReturn(List.of()); + + mockMvc + .perform( + get("/api/orchards/vegetation-codes/INVALID").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isEmpty()); + } + + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - empty vegCode") + void getOrchardByVegCode_emptyVegCode_shouldReturnError() throws Exception { + when(orchardService.findAllOrchardsByVegCode(any())) + .thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND)); + + mockMvc + .perform(get("/api/orchards/vegetation-codes/").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } } diff --git a/backend/src/test/java/ca/bc/gov/backendstartapi/provider/OracleApiProviderTest.java b/backend/src/test/java/ca/bc/gov/backendstartapi/provider/OracleApiProviderTest.java index 1d0d628ae..450395856 100644 --- a/backend/src/test/java/ca/bc/gov/backendstartapi/provider/OracleApiProviderTest.java +++ b/backend/src/test/java/ca/bc/gov/backendstartapi/provider/OracleApiProviderTest.java @@ -1,14 +1,17 @@ package ca.bc.gov.backendstartapi.provider; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.when; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; import ca.bc.gov.backendstartapi.config.ProvidersConfig; +import ca.bc.gov.backendstartapi.dto.OrchardDto; import ca.bc.gov.backendstartapi.dto.OrchardSpuDto; import ca.bc.gov.backendstartapi.dto.oracle.SpuDto; import ca.bc.gov.backendstartapi.security.LoggedUserService; +import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -19,6 +22,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.server.ResponseStatusException; @RestClientTest(OracleApiProvider.class) class OracleApiProviderTest { @@ -77,108 +81,6 @@ void findOrchardParentTreeGeneticQualityDataErrorProviderTest() { Assertions.assertFalse(orchardSpuDto.isPresent()); } - // TODO - // @Test - // @DisplayName("findParentTreesByVegCodeTest") - // void findParentTreesByVegCodeTest() { - // when(loggedUserService.getLoggedUserToken()).thenReturn("1f7a4k5e8t9o5k6e9n8h5e2r6e"); - - // String vegCode = "FDC"; - // String url = "/null/api/orchards/parent-trees/vegetation-codes/" + vegCode; - - // String json = - // """ - // [ - // { - // "parentTreeId": 1003477, - // "parentTreeNumber": "34", - // "orchardId": "1", - // "spu": 0, - // "parentTreeGeneticQualities": [ - // { - // "geneticTypeCode": "BV", - // "geneticWorthCode": "GVO", - // "geneticQualityValue": 18 - // } - // ] - // } - // ] - // """; - - // mockRestServiceServer - // .expect(requestTo(url)) - // .andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); - - // Map testMap = new HashMap<>(); - - // List parentTreeDtoList = - // oracleApiProvider.findParentTreesByVegCode(vegCode, testMap); - - // Assertions.assertFalse(parentTreeDtoList.isEmpty()); - // Assertions.assertEquals("1003477", parentTreeDtoList.get(0).getParentTreeId().toString()); - // Assertions.assertEquals("34", parentTreeDtoList.get(0).getParentTreeNumber()); - // } - - // TODO - // @Test - // @DisplayName("findParentTreesByVegCodeErrorTest") - // void findParentTreesByVegCodeErrorTest() throws Exception { - // when(loggedUserService.getLoggedUserToken()).thenReturn(""); - - // String vegCode = "LAMB"; - // String url = "/null/api/orchards/parent-trees/vegetation-codes/" + vegCode; - - // mockRestServiceServer.expect(requestTo(url)).andRespond(withStatus(HttpStatus.BAD_REQUEST)); - - // Map testMap = new HashMap<>(); - - // ResponseStatusException httpExc = - // Assertions.assertThrows( - // ResponseStatusException.class, - // () -> { - // oracleApiProvider.findParentTreesByVegCode(vegCode, testMap); - // }); - - // Assertions.assertEquals(HttpStatus.BAD_REQUEST, httpExc.getStatusCode()); - // } - - // TODO - // @Test - // @DisplayName("Find Orchard with ID success test.") - // void findOrchardById_shouldSucceed() { - // when(loggedUserService.getLoggedUserToken()).thenReturn("1f7a4k5e8t9o5k6e9n8h5e2r6e"); - - // String orchardId = "339"; - // String url = "/null/api/orchards/" + orchardId; - - // String json = - // """ - // { - // "id": "339", - // "name": "EAGLEROCK", - // "vegetationCode": "PLI", - // "lotTypeCode": "S", - // "lotTypeDescription": "Seed Lot", - // "stageCode": "PRD", - // "becZoneCode": "CWH", - // "becZoneDescription": "Coastal Western Hemlock", - // "becSubzoneCode": "dm", - // "variant": null, - // "becVersionId": 5 - // } - // """; - - // mockRestServiceServer - // .expect(requestTo(url)) - // .andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); - - // Optional orchardDtoOpt = oracleApiProvider.findOrchardById(orchardId); - - // Assertions.assertFalse(orchardDtoOpt.isEmpty()); - // Assertions.assertEquals(orchardId, orchardDtoOpt.get().id()); - // Assertions.assertEquals("Coastal Western Hemlock", orchardDtoOpt.get().becZoneDescription()); - // } - @Test @DisplayName("Get SPU with ID success test") void getSpuById_shouldSucceed() { @@ -231,4 +133,58 @@ void getSpuById_shouldReturnEmpty_onError() { Assertions.assertTrue(optSpuDto.isEmpty()); } + + @Test + @DisplayName("findOrchardsByVegCode - valid vegCode should return orchard list") + void findOrchardsByVegCode_validVegCode_shouldReturnOrchardList() { + String vegCode = "BV"; + String url = "/null/api/orchards/vegetation-codes/BV"; + String json = "[{ \"id\": \"1\", \"name\": \"Primary Orchard\", \"vegetationCode\": \"BV\" }]"; + + when(loggedUserService.getLoggedUserToken()).thenReturn("sample-token"); + + mockRestServiceServer + .expect(requestTo(url)) + .andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); + + List result = oracleApiProvider.findOrchardsByVegCode(vegCode); + + Assertions.assertFalse(result.isEmpty()); + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals("Primary Orchard", result.get(0).getName()); + } + + @Test + @DisplayName("findOrchardsByVegCode - invalid vegCode should return empty list") + void findOrchardsByVegCode_invalidVegCode_shouldReturnEmptyList() { + String vegCode = "INVALID"; + String url = "/null/api/orchards/vegetation-codes/INVALID"; + + when(loggedUserService.getLoggedUserToken()).thenReturn("sample-token"); + + mockRestServiceServer.expect(requestTo(url)).andRespond(withStatus(HttpStatus.NOT_FOUND)); + + assertThrows( + ResponseStatusException.class, + () -> { + oracleApiProvider.findOrchardsByVegCode(vegCode); + }); + } + + @Test + @DisplayName("findOrchardsByVegCode - empty vegCode should return empty list") + void findOrchardsByVegCode_emptyVegCode_shouldReturnEmptyList() { + String vegCode = ""; + String url = "/null/api/orchards/vegetation-codes/"; + + when(loggedUserService.getLoggedUserToken()).thenReturn("sample-token"); + + mockRestServiceServer.expect(requestTo(url)).andRespond(withStatus(HttpStatus.NOT_FOUND)); + + assertThrows( + ResponseStatusException.class, + () -> { + oracleApiProvider.findOrchardsByVegCode(vegCode); + }); + } } diff --git a/backend/src/test/java/ca/bc/gov/backendstartapi/service/OrchardServiceTest.java b/backend/src/test/java/ca/bc/gov/backendstartapi/service/OrchardServiceTest.java index c58726c77..24de6cbf7 100644 --- a/backend/src/test/java/ca/bc/gov/backendstartapi/service/OrchardServiceTest.java +++ b/backend/src/test/java/ca/bc/gov/backendstartapi/service/OrchardServiceTest.java @@ -3,6 +3,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import ca.bc.gov.backendstartapi.dto.OrchardDto; import ca.bc.gov.backendstartapi.dto.OrchardSpuDto; import ca.bc.gov.backendstartapi.dto.ParentTreeGeneticInfoDto; import ca.bc.gov.backendstartapi.dto.ParentTreeGeneticQualityDto; @@ -149,34 +150,6 @@ void findParentTreeGeneticQualityDataEmptyServiceTest() { "404 NOT_FOUND \"No active SPU for the given Orchard ID!\"", exc.getMessage()); } - // TODO - // @Test - // @DisplayName("findParentTreesByVegCodeTest") - // void findParentTreesByVegCodeTest() { - // String vegCode = "FDI"; - - // SameSpeciesTreeDto firstDto = - // new SameSpeciesTreeDto(Long.valueOf(123), "1000", "1", Long.valueOf(7), List.of()); - // SameSpeciesTreeDto secondDto = - // new SameSpeciesTreeDto(Long.valueOf(456), "2000", "1", Long.valueOf(7), List.of()); - - // List testList = List.of(firstDto, secondDto); - - // Map testMap = new HashMap<>(); - // testMap.put("1", "1"); - // when(oracleApiProvider.findParentTreesByVegCode(vegCode, testMap)).thenReturn(testList); - - // ActiveOrchardSpuEntity activeOrchardSpu = createOrchardSpu("1", true); - // when(orchardService.findAllSpu(true)).thenReturn(List.of(activeOrchardSpu)); - - // List responseFromService = - // orchardService.findParentTreesByVegCode(vegCode); - - // Assertions.assertNotNull(responseFromService); - // Assertions.assertEquals(testList.size(), responseFromService.size()); - // Assertions.assertEquals(testList, responseFromService); - // } - @Test @DisplayName("Find all spu happy path should succeed") void findAllSpu_happyPath_shouldSucceed() { @@ -191,4 +164,58 @@ void findAllSpu_happyPath_shouldSucceed() { Assertions.assertFalse(list.isEmpty()); Assertions.assertEquals(1, list.size()); } + + @Test + @DisplayName("findAllOrchardsByVegCode - valid vegCode should return orchard list") + void findAllOrchardsByVegCode_validVegCode_shouldReturnOrchardList() { + String vegCode = "BV"; + OrchardDto orchardDto = new OrchardDto(); + orchardDto.setId("1"); + orchardDto.setName("Primary Orchard"); + orchardDto.setVegetationCode(vegCode); + + when(oracleApiProvider.findOrchardsByVegCode(vegCode)).thenReturn(List.of(orchardDto)); + + List result = orchardService.findAllOrchardsByVegCode(vegCode); + + Assertions.assertFalse(result.isEmpty()); + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals("Primary Orchard", result.get(0).getName()); + } + + @Test + @DisplayName("findAllOrchardsByVegCode - invalid vegCode should return empty list") + void findAllOrchardsByVegCode_invalidVegCode_shouldReturnEmptyList() { + String vegCode = "INVALID"; + + when(oracleApiProvider.findOrchardsByVegCode(vegCode)).thenReturn(List.of()); + + List result = orchardService.findAllOrchardsByVegCode(vegCode); + + Assertions.assertTrue(result.isEmpty()); + } + + @Test + @DisplayName("findAllOrchardsByVegCode - empty vegCode should return empty list") + void findAllOrchardsByVegCode_emptyVegCode_shouldReturnEmptyList() { + String vegCode = ""; + + when(oracleApiProvider.findOrchardsByVegCode(vegCode)).thenReturn(List.of()); + + List result = orchardService.findAllOrchardsByVegCode(vegCode); + + Assertions.assertTrue(result.isEmpty()); + } + + @Test + @DisplayName("findAllOrchardsByVegCode - null vegCode should return empty list") + void findAllOrchardsByVegCode_nullVegCode_shouldReturnEmptyList() { + String vegCode = null; + + when(oracleApiProvider.findOrchardsByVegCode(vegCode)).thenReturn(List.of()); + + List result = orchardService.findAllOrchardsByVegCode(vegCode); + + Assertions.assertTrue(result.isEmpty()); + } } diff --git a/backend/src/test/java/ca/bc/gov/backendstartapi/service/SeedlotFormPutTest.java b/backend/src/test/java/ca/bc/gov/backendstartapi/service/SeedlotFormPutTest.java index 1a36b5b4b..9d55eaa92 100644 --- a/backend/src/test/java/ca/bc/gov/backendstartapi/service/SeedlotFormPutTest.java +++ b/backend/src/test/java/ca/bc/gov/backendstartapi/service/SeedlotFormPutTest.java @@ -1,12 +1,18 @@ package ca.bc.gov.backendstartapi.service; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; +import ca.bc.gov.backendstartapi.dto.CalculatedParentTreeValsDto; +import ca.bc.gov.backendstartapi.dto.GeospatialRespondDto; +import ca.bc.gov.backendstartapi.dto.OrchardDto; import ca.bc.gov.backendstartapi.dto.ParentTreeGeneticQualityDto; +import ca.bc.gov.backendstartapi.dto.PtCalculationResDto; import ca.bc.gov.backendstartapi.dto.SeedlotFormCollectionDto; import ca.bc.gov.backendstartapi.dto.SeedlotFormExtractionDto; import ca.bc.gov.backendstartapi.dto.SeedlotFormInterimDto; @@ -14,6 +20,13 @@ import ca.bc.gov.backendstartapi.dto.SeedlotFormOwnershipDto; import ca.bc.gov.backendstartapi.dto.SeedlotFormParentTreeSmpDto; import ca.bc.gov.backendstartapi.dto.SeedlotFormSubmissionDto; +import ca.bc.gov.backendstartapi.dto.SeedlotStatusResponseDto; +import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseDto; +import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseSpuGeoDto; +import ca.bc.gov.backendstartapi.dto.oracle.SpzDto; +import ca.bc.gov.backendstartapi.entity.ActiveOrchardSpuEntity; +import ca.bc.gov.backendstartapi.entity.GeneticClassEntity; +import ca.bc.gov.backendstartapi.entity.SeedlotSourceEntity; import ca.bc.gov.backendstartapi.entity.SeedlotStatusEntity; import ca.bc.gov.backendstartapi.entity.seedlot.Seedlot; import ca.bc.gov.backendstartapi.exception.ConeCollectionMethodNotFoundException; @@ -31,6 +44,7 @@ import java.math.BigDecimal; import java.time.Clock; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Assertions; @@ -361,174 +375,167 @@ void submitSeedlotForm_facilityDescNotFound_shouldFail() { }); } - // TODO - // @Test - // @DisplayName("Seedlot form submit - Success") - // void submitSeedlotForm_happyPath_shouldSucceed() { - // Seedlot seedlot = new Seedlot("5432"); - // SeedlotStatusEntity seedlotStatus = new SeedlotStatusEntity(); - // seedlotStatus.setSeedlotStatusCode("PND"); - // seedlot.setSeedlotStatus(seedlotStatus); - - // SeedlotSourceEntity seedSource = new SeedlotSourceEntity(); - // seedSource.setSeedlotSourceCode("UNT"); - // seedlot.setSeedlotSource(seedSource); - // when(seedlotRepository.findById("5432")).thenReturn(Optional.of(seedlot)); - - // doNothing() - // .when(seedlotCollectionMethodService) - // .saveSeedlotFormStep1(any(), any(), anyBoolean()); - // when(seedlotOwnerQuantityService.saveSeedlotFormStep2(any(), any(), anyBoolean())) - // .thenReturn(List.of()); - // doNothing().when(seedlotOrchardService).saveSeedlotFormStep4(any(), any(), anyBoolean()); - // when(seedlotParentTreeService.saveSeedlotFormStep5(any(), any(), anyBoolean())) - // .thenReturn(List.of()); - // doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), - // any()); - // when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of()); - // doNothing().when(smpMixGeneticQualityService).saveSeedlotFormStep5(any(), any()); - // doNothing() - // .when(seedlotParentTreeSmpMixService) - // .saveSeedlotFormStep5(any(), any(), anyBoolean()); - - // SeedlotStatusEntity ssEntity = new SeedlotStatusEntity(); - // ssEntity.setSeedlotStatusCode("SUB"); - // when(seedlotStatusService.getValidSeedlotStatus(any())).thenReturn(Optional.of(ssEntity)); - - // // Parent tree contribution mock - // CalculatedParentTreeValsDto caculatedParentTreeValsDto = new CalculatedParentTreeValsDto(); - // caculatedParentTreeValsDto.setNeValue(BigDecimal.valueOf(0)); - // GeospatialRespondDto geospatialRespondDto = - // new GeospatialRespondDto( - // 120, 12, 0, 23, 4, 0, BigDecimal.valueOf(120.22), BigDecimal.valueOf(23.44), 750); - // caculatedParentTreeValsDto.setGeospatialData(geospatialRespondDto); - // PtCalculationResDto ptCalculationResDto = - // new PtCalculationResDto(List.of(), caculatedParentTreeValsDto, geospatialRespondDto); - // when(parentTreeService.calculatePtVals(any())).thenReturn(ptCalculationResDto); - - // SeedlotFormSubmissionDto mockedForm = mockSeedlotFormDto(null, null); - - // // Set area of use mocks - // int activeSpuId = 3; - // String primaryOrchardId = mockedForm.seedlotFormOrchardDto().primaryOrchardId(); - // Optional activeSpuOptional = - // Optional.of(new ActiveOrchardSpuEntity(primaryOrchardId, activeSpuId, true, false, - // false)); - // when(orchardService.findSpuIdByOrchardWithActive(primaryOrchardId, true)) - // .thenReturn(activeSpuOptional); - // when(orchardService.findSpuIdByOrchard(primaryOrchardId)).thenReturn(activeSpuOptional); - - // AreaOfUseDto areaOfUseDto = new AreaOfUseDto(); - // AreaOfUseSpuGeoDto areaOfUseSpuGeoDto = new AreaOfUseSpuGeoDto(1, 100, null, null, 3, 5); - // areaOfUseDto.setAreaOfUseSpuGeoDto(areaOfUseSpuGeoDto); - - // SpzDto spzDto1 = new SpzDto("GL", "Georgia Lowlands", false); - // SpzDto spzDto2 = new SpzDto("M", "Maritime", true); - // List spzList = List.of(spzDto1, spzDto2); - // areaOfUseDto.setSpzList(spzList); - - // OrchardDto oracleOrchardRet = - // new OrchardDto( - // primaryOrchardId, - // "Primary Orchard", - // seedlot.getVegetationCode(), - // 'S', - // "Seed Lot", - // "PRD", - // "SBS", - // "Sub-Boreal Spruce", - // "mk", - // '1', - // 5); - // when(oracleApiProvider.findOrchardById(primaryOrchardId)) - // .thenReturn(Optional.of(oracleOrchardRet)); - - // - // when(oracleApiProvider.getAreaOfUseData(activeSpuId)).thenReturn(Optional.of(areaOfUseDto)); - - // Optional genClassOptional = Optional.of(new GeneticClassEntity()); - // when(geneticClassRepository.findById("A")).thenReturn(genClassOptional); - - // when(loggedUserService.getLoggedUserId()).thenReturn("meatball@Pasta"); - - // when(seedlotSeedPlanZoneRepository.saveAll(any())).thenReturn(List.of()); - - // SeedlotStatusResponseDto scDto = - // seedlotService.updateSeedlotWithForm("5432", mockedForm, false, true, "SUB"); - - // Assertions.assertNotNull(scDto); - // Assertions.assertEquals("5432", scDto.seedlotNumber()); - // Assertions.assertEquals("SUB", scDto.seedlotStatusCode()); - - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermStrgClientNumber(), - // seedlot.getInterimStorageClientNumber()); - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermStrgLocnCode(), - // seedlot.getInterimStorageLocationCode()); - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermStrgStDate(), - // seedlot.getInterimStorageStartDate()); - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermStrgEndDate(), - // seedlot.getInterimStorageEndDate()); - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermOtherFacilityDesc(), - // seedlot.getInterimStorageOtherFacilityDesc()); - // Assertions.assertEquals( - // mockedForm.seedlotFormInterimDto().intermFacilityCode(), - // seedlot.getInterimStorageFacilityCode()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().extractoryClientNumber(), - // seedlot.getExtractionClientNumber()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().extractoryLocnCode(), - // seedlot.getExtractionLocationCode()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().extractionStDate(), - // seedlot.getExtractionStartDate()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().extractionEndDate(), - // seedlot.getExtractionEndDate()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().storageClientNumber(), - // seedlot.getStorageClientNumber()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().storageLocnCode(), - // seedlot.getStorageLocationCode()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().temporaryStrgStartDate(), - // seedlot.getTemporaryStorageStartDate()); - // Assertions.assertEquals( - // mockedForm.seedlotFormExtractionDto().temporaryStrgEndDate(), - // seedlot.getTemporaryStorageEndDate()); - // // Area of use test - // assertEquals(areaOfUseSpuGeoDto.getElevationMax(), seedlot.getElevationMax()); - // assertEquals(areaOfUseSpuGeoDto.getElevationMin(), seedlot.getElevationMin()); - // assertEquals(geospatialRespondDto.getMeanLatitudeDegree(), seedlot.getLatitudeDegMax()); - // assertEquals(geospatialRespondDto.getMeanLatitudeDegree(), seedlot.getLatitudeDegMin()); - // assertEquals(areaOfUseSpuGeoDto.getLatitudeMinutesMax(), seedlot.getLatitudeMinMax()); - // assertEquals(areaOfUseSpuGeoDto.getLatitudeMinutesMin(), seedlot.getLatitudeMinMin()); - // assertEquals(0, seedlot.getLatitudeSecMax()); - // assertEquals(0, seedlot.getLatitudeSecMin()); - // assertEquals(geospatialRespondDto.getMeanLongitudeDegree(), seedlot.getLongitudeDegMax()); - // assertEquals(geospatialRespondDto.getMeanLongitudeDegree(), seedlot.getLongitudeDegMin()); - // assertEquals(geospatialRespondDto.getMeanLongitudeMinute(), seedlot.getLongitudeMinMax()); - // assertEquals(geospatialRespondDto.getMeanLongitudeMinute(), seedlot.getLongitudeMinMin()); - // assertEquals(0, seedlot.getLongitudeSecMax()); - // assertEquals(0, seedlot.getLongitudeSecMin()); - // // BEC values - // assertEquals(oracleOrchardRet.becZoneCode(), seedlot.getBgcZoneCode()); - // assertEquals(oracleOrchardRet.becZoneDescription(), seedlot.getBgcZoneDescription()); - // assertEquals(oracleOrchardRet.becSubzoneCode(), seedlot.getBgcSubzoneCode()); - // assertEquals(oracleOrchardRet.variant(), seedlot.getVariant()); - // assertEquals(oracleOrchardRet.becVersionId(), seedlot.getBecVersionId()); - // // Declared Seedlot Value - // assertEquals( - // loggedUserService.getLoggedUserId(), seedlot.getDeclarationOfTrueInformationUserId()); - // assertTrue( - // LocalDateTime.now(Clock.systemUTC()) - // .minusSeconds(15L) - // .isBefore(seedlot.getDeclarationOfTrueInformationTimestamp())); - // } + @Test + @DisplayName("Seedlot form submit - Success") + void submitSeedlotForm_happyPath_shouldSucceed() { + Seedlot seedlot = new Seedlot("5432"); + SeedlotStatusEntity seedlotStatus = new SeedlotStatusEntity(); + seedlotStatus.setSeedlotStatusCode("PND"); + seedlot.setSeedlotStatus(seedlotStatus); + + SeedlotSourceEntity seedSource = new SeedlotSourceEntity(); + seedSource.setSeedlotSourceCode("UNT"); + seedlot.setSeedlotSource(seedSource); + when(seedlotRepository.findById("5432")).thenReturn(Optional.of(seedlot)); + + doNothing() + .when(seedlotCollectionMethodService) + .saveSeedlotFormStep1(any(), any(), anyBoolean()); + when(seedlotOwnerQuantityService.saveSeedlotFormStep2(any(), any(), anyBoolean())) + .thenReturn(List.of()); + doNothing().when(seedlotOrchardService).saveSeedlotFormStep4(any(), any(), anyBoolean()); + when(seedlotParentTreeService.saveSeedlotFormStep5(any(), any(), anyBoolean())) + .thenReturn(List.of()); + doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), any()); + when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of()); + doNothing().when(smpMixGeneticQualityService).saveSeedlotFormStep5(any(), any()); + doNothing() + .when(seedlotParentTreeSmpMixService) + .saveSeedlotFormStep5(any(), any(), anyBoolean()); + + SeedlotStatusEntity ssEntity = new SeedlotStatusEntity(); + ssEntity.setSeedlotStatusCode("SUB"); + when(seedlotStatusService.getValidSeedlotStatus(any())).thenReturn(Optional.of(ssEntity)); + + // Parent tree contribution mock + CalculatedParentTreeValsDto caculatedParentTreeValsDto = new CalculatedParentTreeValsDto(); + caculatedParentTreeValsDto.setNeValue(BigDecimal.valueOf(0)); + GeospatialRespondDto geospatialRespondDto = + new GeospatialRespondDto( + 120, 12, 0, 23, 4, 0, BigDecimal.valueOf(120.22), BigDecimal.valueOf(23.44), 750); + caculatedParentTreeValsDto.setGeospatialData(geospatialRespondDto); + PtCalculationResDto ptCalculationResDto = + new PtCalculationResDto(List.of(), caculatedParentTreeValsDto, geospatialRespondDto); + when(parentTreeService.calculatePtVals(any())).thenReturn(ptCalculationResDto); + + SeedlotFormSubmissionDto mockedForm = mockSeedlotFormDto(null, null); + + // Set area of use mocks + int activeSpuId = 3; + String primaryOrchardId = mockedForm.seedlotFormOrchardDto().primaryOrchardId(); + Optional activeSpuOptional = + Optional.of(new ActiveOrchardSpuEntity(primaryOrchardId, activeSpuId, true, false, false)); + when(orchardService.findSpuIdByOrchardWithActive(primaryOrchardId, true)) + .thenReturn(activeSpuOptional); + when(orchardService.findSpuIdByOrchard(primaryOrchardId)).thenReturn(activeSpuOptional); + + AreaOfUseDto areaOfUseDto = new AreaOfUseDto(); + AreaOfUseSpuGeoDto areaOfUseSpuGeoDto = new AreaOfUseSpuGeoDto(1, 100, null, null, 3, 5); + areaOfUseDto.setAreaOfUseSpuGeoDto(areaOfUseSpuGeoDto); + + SpzDto spzDto1 = new SpzDto("GL", "Georgia Lowlands", false); + SpzDto spzDto2 = new SpzDto("M", "Maritime", true); + List spzList = List.of(spzDto1, spzDto2); + areaOfUseDto.setSpzList(spzList); + + OrchardDto oracleOrchardRet = + new OrchardDto( + primaryOrchardId, + "Primary Orchard", + seedlot.getVegetationCode(), + 'S', + "Seed Lot", + "PRD", + "SBS", + "Sub-Boreal Spruce", + "mk", + '1', + 5, + 7); + when(oracleApiProvider.findOrchardById(primaryOrchardId)) + .thenReturn(Optional.of(oracleOrchardRet)); + + when(oracleApiProvider.getAreaOfUseData(activeSpuId)).thenReturn(Optional.of(areaOfUseDto)); + + Optional genClassOptional = Optional.of(new GeneticClassEntity()); + when(geneticClassRepository.findById("A")).thenReturn(genClassOptional); + + when(loggedUserService.getLoggedUserId()).thenReturn("meatball@Pasta"); + + when(seedlotSeedPlanZoneRepository.saveAll(any())).thenReturn(List.of()); + + SeedlotStatusResponseDto scDto = + seedlotService.updateSeedlotWithForm("5432", mockedForm, false, true, "SUB"); + + Assertions.assertNotNull(scDto); + Assertions.assertEquals("5432", scDto.seedlotNumber()); + Assertions.assertEquals("SUB", scDto.seedlotStatusCode()); + + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermStrgClientNumber(), + seedlot.getInterimStorageClientNumber()); + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermStrgLocnCode(), + seedlot.getInterimStorageLocationCode()); + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermStrgStDate(), + seedlot.getInterimStorageStartDate()); + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermStrgEndDate(), seedlot.getInterimStorageEndDate()); + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermOtherFacilityDesc(), + seedlot.getInterimStorageOtherFacilityDesc()); + Assertions.assertEquals( + mockedForm.seedlotFormInterimDto().intermFacilityCode(), + seedlot.getInterimStorageFacilityCode()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().extractoryClientNumber(), + seedlot.getExtractionClientNumber()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().extractoryLocnCode(), + seedlot.getExtractionLocationCode()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().extractionStDate(), seedlot.getExtractionStartDate()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().extractionEndDate(), seedlot.getExtractionEndDate()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().storageClientNumber(), + seedlot.getStorageClientNumber()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().storageLocnCode(), seedlot.getStorageLocationCode()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().temporaryStrgStartDate(), + seedlot.getTemporaryStorageStartDate()); + Assertions.assertEquals( + mockedForm.seedlotFormExtractionDto().temporaryStrgEndDate(), + seedlot.getTemporaryStorageEndDate()); + // Area of use test + assertEquals(areaOfUseSpuGeoDto.getElevationMax(), seedlot.getElevationMax()); + assertEquals(areaOfUseSpuGeoDto.getElevationMin(), seedlot.getElevationMin()); + assertEquals(geospatialRespondDto.getMeanLatitudeDegree(), seedlot.getLatitudeDegMax()); + assertEquals(geospatialRespondDto.getMeanLatitudeDegree(), seedlot.getLatitudeDegMin()); + assertEquals(areaOfUseSpuGeoDto.getLatitudeMinutesMax(), seedlot.getLatitudeMinMax()); + assertEquals(areaOfUseSpuGeoDto.getLatitudeMinutesMin(), seedlot.getLatitudeMinMin()); + assertEquals(0, seedlot.getLatitudeSecMax()); + assertEquals(0, seedlot.getLatitudeSecMin()); + assertEquals(geospatialRespondDto.getMeanLongitudeDegree(), seedlot.getLongitudeDegMax()); + assertEquals(geospatialRespondDto.getMeanLongitudeDegree(), seedlot.getLongitudeDegMin()); + assertEquals(geospatialRespondDto.getMeanLongitudeMinute(), seedlot.getLongitudeMinMax()); + assertEquals(geospatialRespondDto.getMeanLongitudeMinute(), seedlot.getLongitudeMinMin()); + assertEquals(0, seedlot.getLongitudeSecMax()); + assertEquals(0, seedlot.getLongitudeSecMin()); + // BEC values + assertEquals(oracleOrchardRet.getBecZoneCode(), seedlot.getBgcZoneCode()); + assertEquals(oracleOrchardRet.getBecZoneDescription(), seedlot.getBgcZoneDescription()); + assertEquals(oracleOrchardRet.getBecSubzoneCode(), seedlot.getBgcSubzoneCode()); + assertEquals(oracleOrchardRet.getVariant(), seedlot.getVariant()); + assertEquals(oracleOrchardRet.getBecVersionId(), seedlot.getBecVersionId()); + // Declared Seedlot Value + assertEquals( + loggedUserService.getLoggedUserId(), seedlot.getDeclarationOfTrueInformationUserId()); + assertTrue( + LocalDateTime.now(Clock.systemUTC()) + .minusSeconds(15L) + .isBefore(seedlot.getDeclarationOfTrueInformationTimestamp())); + } } diff --git a/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/OrchardService.java b/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/OrchardService.java index e2d045209..5250c5526 100644 --- a/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/OrchardService.java +++ b/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/OrchardService.java @@ -5,14 +5,12 @@ import ca.bc.gov.oracleapi.dto.OrchardParentTreeDto; import ca.bc.gov.oracleapi.dto.ParentTreeGeneticInfoDto; import ca.bc.gov.oracleapi.dto.ParentTreeGeneticQualityDto; -import ca.bc.gov.oracleapi.dto.SameSpeciesTreeDto; import ca.bc.gov.oracleapi.entity.OrchardEntity; import ca.bc.gov.oracleapi.entity.OrchardLotTypeCode; import ca.bc.gov.oracleapi.entity.ParentTreeEntity; import ca.bc.gov.oracleapi.entity.ParentTreeGeneticQuality; import ca.bc.gov.oracleapi.entity.ParentTreeOrchard; import ca.bc.gov.oracleapi.entity.VegetationCode; -import ca.bc.gov.oracleapi.entity.projection.ParentTreeProj; import ca.bc.gov.oracleapi.repository.OrchardRepository; import ca.bc.gov.oracleapi.repository.ParentTreeGeneticQualityRepository; import ca.bc.gov.oracleapi.repository.ParentTreeOrchardRepository; @@ -191,84 +189,6 @@ public Optional> findNotRetOrchardsByVegCode(String vegCode) { return Optional.of(resultList); } - /** Find all parent trees under a vegCode. */ - public List findParentTreesWithVegCode( - String vegCode, Map orchardSpuMap) { - SparLog.info("Finding all parent trees under VegCode: {}", vegCode); - - // Step 1: Get all the parent trees under a species - List parentTrees = parentTreeRepository.findAllParentTreeWithVegCode(vegCode); - - // Step 2: Convert projections to Dto - List resultList = - parentTrees.stream() - .map( - parentTree -> { - SameSpeciesTreeDto treeDto = new SameSpeciesTreeDto(); - treeDto.setOrchardId(parentTree.getOrchardId()); - treeDto.setParentTreeId(parentTree.getParentTreeId()); - treeDto.setParentTreeNumber(parentTree.getParentTreeNumber()); - treeDto.setSpu(parentTree.getSpu()); - return treeDto; - }) - .toList(); - - // Step 3 fill gen worth values - fillGenInfoForEachTree(resultList); - - // Step 4 filter with orchard spu map - // The first filter makes sure that the tree has a valid orchard id that exist in the - // orchard-spu table - resultList = - resultList.stream() - .filter(treeDto -> orchardSpuMap.containsKey(treeDto.getOrchardId())) - .filter( - treeDto -> - treeDto - .getSpu() - .equals(Long.valueOf(orchardSpuMap.get(treeDto.getOrchardId())))) - .toList(); - - SparLog.info("{} parent trees found under VegCode: {}", resultList.size(), vegCode); - return resultList; - } - - private void fillGenInfoForEachTree(List resultList) { - List parentTreeIds = - resultList.stream() - .map( - (treeDto) -> { - return treeDto.getParentTreeId(); - }) - .toList(); - - List spuList = - resultList.stream() - .map( - (treeDto) -> { - return treeDto.getSpu(); - }) - .toList(); - - List ptgqList = - parentTreeGeneticQualityRepository.findAllByListOfSpuAndId( - true, "BV", spuList, parentTreeIds); - - resultList.stream() - .forEach( - (treeDto) -> { - List matchedPtgqs = - ptgqList.stream() - .filter( - ptgq -> - ptgq.getParentTreeId().equals(treeDto.getParentTreeId()) - && ptgq.getSeedPlanningUnitId().equals(treeDto.getSpu())) - .map(ptgq -> ModelMapper.convert(ptgq, ParentTreeGeneticQualityDto.class)) - .toList(); - treeDto.setParentTreeGeneticQualities(matchedPtgqs); - }); - } - private List findAllParentTree( String orchardId, Long spuId, long milli) { diff --git a/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/ParentTreeService.java b/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/ParentTreeService.java index e93eac252..70b9cea36 100644 --- a/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/ParentTreeService.java +++ b/oracle-api/src/main/java/ca/bc/gov/oracleapi/service/ParentTreeService.java @@ -100,7 +100,7 @@ private Map checkParentTreeHierarchy( SparLog.debug("Hierarchy level {}", i); List testList = new ArrayList<>(); - + // Loop through all ParentTree records, getting their female and male parents for the ones // that has no elevation data for (ParentTreeEntity ptEntity : ptEntityList) { @@ -135,7 +135,7 @@ private Map checkParentTreeHierarchy( // If 'femaleAndMaleParentsIds' contains the current parent tree id, it means that the // current parent tree id is one of the parents, it could be either the female or male. if (femaleAndMaleParentsIds.contains(ptEntity.getId())) { - + // If resultMap doesn't have 'sonParentTreeId' key, it means this is not the first level // and it should look at the 'parentTreeRelationMap' who has ALL the parent tree // 'son-parents' relation @@ -148,7 +148,7 @@ private Map checkParentTreeHierarchy( } } } - + // Get female parent tree data and connect with son. Female is always the first Long femaleParentTreeId = femaleAndMaleParentsIds.get(0); if (femaleParentTreeId.equals(ptEntity.getId())) { diff --git a/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/OrchardEndpointTest.java b/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/OrchardEndpointTest.java index 5deaeef11..c19c20f2c 100644 --- a/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/OrchardEndpointTest.java +++ b/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/OrchardEndpointTest.java @@ -257,59 +257,4 @@ void findOrchardsWithVegCodeNotFoundEndpointTest() throws Exception { .andExpect(status().isNotFound()) .andReturn(); } - - // TODO - // @Test - // @DisplayName("getAllParentTreeByVegCodeTest") - // void getAllParentTreeByVegCodeTest() throws Exception { - - // SameSpeciesTreeDto firstDto = - // new SameSpeciesTreeDto(Long.valueOf(123), "1000", "1", Long.valueOf(7), List.of()); - // SameSpeciesTreeDto secondDto = - // new SameSpeciesTreeDto(Long.valueOf(456), "2000", "1", Long.valueOf(7), List.of()); - - // List testList = List.of(firstDto, secondDto); - - // String vegCode = "PLI"; - // Map testMap = new HashMap<>(); - - // when(orchardService.findParentTreesWithVegCode(vegCode, testMap)).thenReturn(testList); - - // mockMvc - // .perform( - // post("/api/orchards/parent-trees/vegetation-codes/{vegCode}", vegCode) - // .with(csrf().asHeader()) - // .content(testMap.toString()) - // .contentType(MediaType.APPLICATION_JSON) - // .header("Content-Type", "application/json") - // .accept(MediaType.APPLICATION_JSON)) - // .andExpect(status().isOk()) - // .andExpect(jsonPath("$[0].parentTreeId").value(firstDto.getParentTreeId())) - // .andExpect(jsonPath("$[0].parentTreeNumber").value(firstDto.getParentTreeNumber())) - // .andExpect(jsonPath("$[1].parentTreeId").value(secondDto.getParentTreeId())) - // .andExpect(jsonPath("$[1].parentTreeNumber").value(secondDto.getParentTreeNumber())) - // .andReturn(); - // } - - // TODO - // @Test - // @DisplayName("getAllParentTreeByVegCodeErrorTest") - // void getAllParentTreeByVegCodeErrorTest() throws Exception { - // String vegCode = "FDI"; - - // Map testMap = new HashMap<>(); - // when(orchardService.findParentTreesWithVegCode(vegCode, testMap)) - // .thenThrow(new DataRetrievalFailureException("")); - - // mockMvc - // .perform( - // post("/api/orchards/parent-trees/vegetation-codes/{vegCode}", vegCode) - // .with(csrf().asHeader()) - // .content(testMap.toString()) - // .contentType(MediaType.APPLICATION_JSON) - // .header("Content-Type", "application/json") - // .accept(MediaType.APPLICATION_JSON)) - // .andExpect(status().isInternalServerError()) - // .andReturn(); - // } } diff --git a/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/ParentTreeEndpointTest.java b/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/ParentTreeEndpointTest.java index eda9354a0..5dedc220b 100644 --- a/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/ParentTreeEndpointTest.java +++ b/oracle-api/src/test/java/ca/bc/gov/oracleapi/endpoint/ParentTreeEndpointTest.java @@ -2,6 +2,7 @@ import static org.mockito.Mockito.when; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -9,9 +10,11 @@ import ca.bc.gov.oracleapi.dto.GeospatialRequestDto; import ca.bc.gov.oracleapi.dto.GeospatialRespondDto; +import ca.bc.gov.oracleapi.dto.ParentTreeByVegCodeDto; import ca.bc.gov.oracleapi.service.ParentTreeService; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -85,4 +88,45 @@ void getPtGeoSpatialDataEmptyTest() throws Exception { .andExpect(content().string("[]")) .andReturn(); } + + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - valid vegCode") + void getParentTreeByVegCode_validVegCode_shouldReturnData() throws Exception { + ParentTreeByVegCodeDto dto = new ParentTreeByVegCodeDto(); + dto.setParentTreeId(1L); + dto.setTestedInd(true); + dto.setOrchardIds(List.of("211")); + dto.setGeneticQualitiesBySpu(Map.of(68L, List.of())); + + when(parentTreeService.findParentTreesWithVegCode("BV")).thenReturn(Map.of("29", dto)); + + mockMvc + .perform( + get("/api/parent-trees/vegetation-codes/BV").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.29.parentTreeId").value(1L)) + .andExpect(jsonPath("$.29.orchardIds[0]").value("211")) + .andExpect(jsonPath("$.29.testedInd").value(true)); + } + + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - invalid vegCode") + void getParentTreeByVegCode_invalidVegCode_shouldReturnEmpty() throws Exception { + when(parentTreeService.findParentTreesWithVegCode("INVALID")).thenReturn(Map.of()); + + mockMvc + .perform( + get("/api/parent-trees/vegetation-codes/INVALID") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isEmpty()); + } + + @Test + @DisplayName("GET /vegetation-codes/{vegCode} - empty vegCode") + void getParentTreeByVegCode_emptyVegCode_shouldReturnError() throws Exception { + mockMvc + .perform(get("/api/parent-trees/vegetation-codes/").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } } diff --git a/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/OrchardServiceTest.java b/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/OrchardServiceTest.java index 5a9b196c7..64ac3a964 100644 --- a/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/OrchardServiceTest.java +++ b/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/OrchardServiceTest.java @@ -7,14 +7,12 @@ import ca.bc.gov.oracleapi.dto.OrchardParentTreeDto; import ca.bc.gov.oracleapi.dto.ParentTreeGeneticInfoDto; import ca.bc.gov.oracleapi.dto.ParentTreeGeneticQualityDto; -import ca.bc.gov.oracleapi.dto.SameSpeciesTreeDto; import ca.bc.gov.oracleapi.entity.OrchardEntity; import ca.bc.gov.oracleapi.entity.OrchardLotTypeCode; import ca.bc.gov.oracleapi.entity.ParentTreeEntity; import ca.bc.gov.oracleapi.entity.ParentTreeGeneticQuality; import ca.bc.gov.oracleapi.entity.ParentTreeOrchard; import ca.bc.gov.oracleapi.entity.idclass.ParentTreeOrchardId; -import ca.bc.gov.oracleapi.entity.projection.ParentTreeProj; import ca.bc.gov.oracleapi.repository.OrchardRepository; import ca.bc.gov.oracleapi.repository.ParentTreeGeneticQualityRepository; import ca.bc.gov.oracleapi.repository.ParentTreeOrchardRepository; @@ -34,8 +32,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.projection.ProjectionFactory; -import org.springframework.data.projection.SpelAwareProxyProjectionFactory; @ExtendWith(MockitoExtension.class) class OrchardServiceTest { @@ -330,48 +326,4 @@ void findNotRetOrchardsByVegCodeSuccessServiceTest() { Assertions.assertFalse(testObj.id().isEmpty()); }); } - - @Test - @DisplayName("findParentTreesWithVegCodeServiceTest") - void findParentTreesWithVegCodeServiceTest() { - - ProjectionFactory factory = new SpelAwareProxyProjectionFactory(); - ParentTreeProj firstProj = factory.createProjection(ParentTreeProj.class); - - firstProj.setParentTreeId(Long.valueOf(12345)); - firstProj.setParentTreeNumber("456"); - firstProj.setOrchardId("1"); - firstProj.setSpu(Long.valueOf(7)); - - ParentTreeProj secondProj = factory.createProjection(ParentTreeProj.class); - secondProj.setParentTreeId(Long.valueOf(45678)); - secondProj.setParentTreeNumber("678"); - secondProj.setOrchardId("1"); - secondProj.setSpu(Long.valueOf(7)); - - List repoResult = List.of(firstProj, secondProj); - - String vegCode = "PLI"; - - when(parentTreeRepository.findAllParentTreeWithVegCode(vegCode)).thenReturn(repoResult); - - Map testMap = new HashMap<>(); - - testMap.put("1", "7"); - - List listToTest = - orchardService.findParentTreesWithVegCode(vegCode, testMap); - - Assertions.assertEquals(repoResult.size(), listToTest.size()); - - Assertions.assertEquals( - repoResult.get(0).getParentTreeId(), listToTest.get(0).getParentTreeId()); - Assertions.assertEquals( - repoResult.get(0).getParentTreeNumber(), listToTest.get(0).getParentTreeNumber()); - - Assertions.assertEquals( - repoResult.get(1).getParentTreeId(), listToTest.get(1).getParentTreeId()); - Assertions.assertEquals( - repoResult.get(1).getParentTreeNumber(), listToTest.get(1).getParentTreeNumber()); - } } diff --git a/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/ParentTreeServiceTest.java b/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/ParentTreeServiceTest.java index 946f04c46..18c9e0765 100644 --- a/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/ParentTreeServiceTest.java +++ b/oracle-api/src/test/java/ca/bc/gov/oracleapi/service/ParentTreeServiceTest.java @@ -146,21 +146,25 @@ void getPtGeoSpatialData_emptyTest() { } @Test - @DisplayName("Find parent trees with veg code happy path should succeed") + @DisplayName("findParentTreesWithVegCode happy path, should succeed") void findParentTreesWithVegCode_happyPath_shouldSucceed() { - String vegCode = "SX"; - - ParentTreeProj parentProj = mockParentTreeProj(); - - when(parentTreeRepository.findAllParentTreeWithVegCode(vegCode)) - .thenReturn(List.of(parentProj)); - - Map response = - parentTreeService.findParentTreesWithVegCode(vegCode); - - Assertions.assertNotNull(response); - Assertions.assertEquals(1, response.size()); - Assertions.assertTrue(response.containsKey("29")); + List mockProjections = List.of(mockParentTreeProj()); + when(parentTreeRepository.findAllParentTreeWithVegCode("BV")).thenReturn(mockProjections); + + Map result = parentTreeService.findParentTreesWithVegCode("BV"); + + Assertions.assertEquals(1, result.size()); + ParentTreeByVegCodeDto dto = result.get("29"); + Assertions.assertNotNull(dto); + Assertions.assertEquals(20012L, dto.getParentTreeId()); + Assertions.assertEquals("211", dto.getOrchardIds().get(0)); + Assertions.assertEquals( + "BV", dto.getGeneticQualitiesBySpu().get(68L).get(0).getGeneticTypeCode()); + Assertions.assertEquals( + "GVO", dto.getGeneticQualitiesBySpu().get(68L).get(0).getGeneticWorthCode()); + Assertions.assertEquals( + new BigDecimal("15"), + dto.getGeneticQualitiesBySpu().get(68L).get(0).getGeneticQualityValue()); } private ParentTreeEntity mock( @@ -357,4 +361,41 @@ void getPtGeoSpatialData_hierarchyTree_shouldSucceed() { Assertions.assertEquals(2, dtoList.get(4).longitudeMinute()); Assertions.assertEquals(30, dtoList.get(4).longitudeSecond()); } + + @Test + @DisplayName("findParentTreesWithVegCode - empty vegetation code list, should return empty map") + void findParentTreesWithVegCode_emptyVegetationCodeList_shouldReturnEmptyMap() { + + when(parentTreeRepository.findAllParentTreeWithVegCode("")).thenReturn(new ArrayList<>()); + + Map result = parentTreeService.findParentTreesWithVegCode(""); + + Assertions.assertTrue(result.isEmpty()); + } + + + @Test + @DisplayName("findParentTreesWithVegCode - invalid vegetation code, should return empty map") + void findParentTreesWithVegCode_invalidVegetationCode_shouldReturnEmptyMap() { + + when(parentTreeRepository.findAllParentTreeWithVegCode("INVALID")) + .thenReturn(new ArrayList<>()); + + Map result = + parentTreeService.findParentTreesWithVegCode("INVALID"); + + Assertions.assertTrue(result.isEmpty()); + } + + @Test + @DisplayName( + "findParentTreesWithVegCode - repository returns empty list, should return empty map") + void findParentTreesWithVegCode_repositoryReturnsEmptyList_shouldReturnEmptyMap() { + + when(parentTreeRepository.findAllParentTreeWithVegCode("BV")).thenReturn(new ArrayList<>()); + + Map result = parentTreeService.findParentTreesWithVegCode("BV"); + + Assertions.assertTrue(result.isEmpty()); + } }