Skip to content

Commit

Permalink
feat: 1435 tests for different parent tree gw values (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigyu authored Aug 28, 2024
1 parent 1371338 commit a5b5b1f
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 555 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,11 +40,6 @@ default Optional<OrchardSpuDto> findOrchardParentTreeGeneticQualityData(
return Optional.empty();
}

default List<SameSpeciesTreeDto> findParentTreesByVegCode(
String vegCode, Map<String, String> orchardSpuMap) {
return List.of();
}

default List<GeospatialOracleResDto> getPtGeospatialDataByIdList(List<Long> ptIds) {
return List.of();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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<SameSpeciesTreeDto> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, String> testMap = new HashMap<>();

// List<SameSpeciesTreeDto> 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<String, String> 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<OrchardDto> 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() {
Expand Down Expand Up @@ -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<OrchardDto> 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);
});
}
}
Loading

0 comments on commit a5b5b1f

Please sign in to comment.