Skip to content

Commit

Permalink
chore: test adjust (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Oct 28, 2024
1 parent a85ab86 commit 1333fb2
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.bc.gov.restapi.results.common.endpoint;

import ca.bc.gov.restapi.results.common.service.RestService;
import ca.bc.gov.restapi.results.common.service.OpenMapsService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -15,7 +15,7 @@
@AllArgsConstructor
public class FeatureServiceEndpoint {

private final RestService restService;
private final OpenMapsService openMapsService;

/**
* Fetch Opening data from WFS.
Expand All @@ -27,6 +27,6 @@ public class FeatureServiceEndpoint {
public Object getOpeningPolygonAndProperties(
@PathVariable
String openingId) {
return restService.getOpeningPolygonAndProperties(openingId);
return openMapsService.getOpeningPolygonAndProperties(openingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class DashboardExtractionService {
/**
* Service for extracting data from oracle and adding into Postgres.
*
* @param months Optional number of months.
* @param debug Optional debug mode enabled.
* @param months Optional number of months.
* @param debug Optional debug mode enabled.
* @param manuallyTriggered Optional option.
*/
@Async
Expand All @@ -47,18 +47,11 @@ public void extractDataForTheDashboard(Integer months, Boolean debug, Boolean ma
dashboardInsertionService.loadDashboardData(extractionDto, startDateTime, params);
}

private OracleExtractionParamsDto getParams(
Integer months, Boolean debug, Boolean manuallyTriggered) {
if (Objects.isNull(months)) {
months = 24;
}
if (Objects.isNull(debug)) {
debug = Boolean.FALSE;
}
if (Objects.isNull(manuallyTriggered)) {
manuallyTriggered = Boolean.FALSE;
}

return new OracleExtractionParamsDto(months, debug, manuallyTriggered);
}
private OracleExtractionParamsDto getParams(Integer months, Boolean debug, Boolean manuallyTriggered) {
return new OracleExtractionParamsDto(
Objects.requireNonNullElse(months, 24),
Objects.requireNonNullElse(debug, Boolean.FALSE),
Objects.requireNonNullElse(manuallyTriggered, Boolean.FALSE)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import ca.bc.gov.restapi.results.common.dto.ForestClientDto;
import ca.bc.gov.restapi.results.common.provider.ForestClientApiProvider;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

/** This service contains methods for interacting with Forest Client API. */
/**
* This service contains methods for interacting with Forest Client API.
*/
@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -30,17 +31,11 @@ public Optional<ForestClientDto> getClientByNumber(String clientNumber) {
if (!fixedNumber.equals(clientNumber)) {
log.info("Fixed client number to fetch {}", fixedNumber);
}

try {
return forestClientApiProvider.fetchClientByNumber(fixedNumber);
} catch (HttpClientErrorException.NotFound e) {
log.info(String.format("Client %s not found", clientNumber), e);
return Optional.empty();
}
return forestClientApiProvider.fetchClientByNumber(fixedNumber);
}

private String checkClientNumber(String clientNumber) {
if (Objects.isNull(clientNumber)) {
if (StringUtils.isEmpty(clientNumber)) {
return "00000000";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
@Slf4j
@Service
public class RestService {
public class OpenMapsService {

private final RestClient restClient;

public RestService(@Qualifier("openMapsApi") RestClient openMapsApi) {
public OpenMapsService(@Qualifier("openMapsApi") RestClient openMapsApi) {
this.restClient = openMapsApi;
}

Expand All @@ -40,14 +40,14 @@ public Object getOpeningPolygonAndProperties(String openingId) {
.queryParam("SrsName", "EPSG:4326")
.queryParam("PROPERTYNAME",
"OPENING_ID,"
+ "GEOMETRY,"
+ "REGION_NAME,"
+ "REGION_CODE,"
+ "DISTRICT_NAME,"
+ "DISTRICT_CODE,"
+ "CLIENT_NAME,"
+ "CLIENT_NUMBER,"
+ "OPENING_WHEN_CREATED"
+ "GEOMETRY,"
+ "REGION_NAME,"
+ "REGION_CODE,"
+ "DISTRICT_NAME,"
+ "DISTRICT_CODE,"
+ "CLIENT_NAME,"
+ "CLIENT_NUMBER,"
+ "OPENING_WHEN_CREATED"
)
.queryParam("CQL_FILTER", "OPENING_ID=" + openingId)
.build(Map.of())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/** This class contains useful methods for parsing and handling timestamps. */
/**
* This class contains useful methods for parsing and handling timestamps.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TimestampUtil {

private TimestampUtil() {}

/**
* Parses a date string to a {@link LocalDateTime} instance. Format: yyyy-MM-dd.
Expand All @@ -18,13 +24,34 @@ private TimestampUtil() {}
* @return LocalDateTime parsed or null if a null value is found.
*/
public static LocalDateTime parseDateString(String dateStr) {
if (Objects.isNull(dateStr)) {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
return parseDateString(LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

/**
* Parses a LocalDate to a LocalDateTime instance with the time set to LocalTime.MIN.
*
* @param localDate The LocalDate to be parsed
* @return LocalDateTime parsed or null if a null value is found.
*/
public static LocalDateTime parseDateString(LocalDate localDate) {
return parseDateString(localDate, LocalTime.MIN);
}

LocalDate entryDateStartLd =
LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return entryDateStartLd.atStartOfDay();
/**
* Parses a LocalDate to a LocalDateTime instance with the specified time.
*
* @param localDate The LocalDate to be parsed
* @param time The LocalTime to be set
* @return LocalDateTime parsed or null if a null value is found.
*/
public static LocalDateTime parseDateString(LocalDate localDate, LocalTime time) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.atTime(time);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import ca.bc.gov.restapi.results.common.service.RestService;
import ca.bc.gov.restapi.results.common.service.OpenMapsService;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -24,7 +24,8 @@ class FeatureServiceEndpointTest {

@Autowired private MockMvc mockMvc;

@MockBean RestService restService;
@MockBean
OpenMapsService openMapsService;

@Test
@DisplayName("Get opening polygon and properties happy path should succeed")
Expand Down Expand Up @@ -67,7 +68,7 @@ void getOpeningPolygonAndProperties_happyPath_shouldSucceed() throws Exception {
}
""";

when(restService.getOpeningPolygonAndProperties(openingId))
when(openMapsService.getOpeningPolygonAndProperties(openingId))
.thenReturn(ResponseEntity.of(Optional.of(response)));

mockMvc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
import ca.bc.gov.restapi.results.oracle.service.OracleExtractionService;
import ca.bc.gov.restapi.results.postgres.service.DashboardInsertionService;
import java.util.ArrayList;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@DisplayName("Unit Test | Dashboard Extraction Service")
class DashboardExtractionServiceTest {

@Mock OracleExtractionService oracleExtractionService;
@Mock
OracleExtractionService oracleExtractionService;

@Mock DashboardInsertionService dashboardInsertionService;
@Mock
DashboardInsertionService dashboardInsertionService;

private DashboardExtractionService dashboardExtractionService;

Expand All @@ -32,19 +38,50 @@ void setup() {
new DashboardExtractionService(oracleExtractionService, dashboardInsertionService);
}

@Test
@ParameterizedTest
@MethodSource("basicValues")
@DisplayName("extract data for the dashboard happy path should succeed")
void extractDataForTheDashboard_happyPath_shouldSucceed() {
OracleExtractionParamsDto params = new OracleExtractionParamsDto(24, false, false);
void extractDataForTheDashboard_happyPath_shouldSucceed(
Integer months,
Boolean debug,
Boolean manuallyTriggered
) {
OracleExtractionParamsDto params = new OracleExtractionParamsDto(
24,
false,
false
);
OracleExtractionDto extractionDto =
new OracleExtractionDto(null, null, null, null, null, null, new ArrayList<>());
new OracleExtractionDto(
null,
null,
null,
null,
null,
null,
new ArrayList<>()
);

when(oracleExtractionService.getOpeningActivities(params)).thenReturn(extractionDto);

doNothing().when(dashboardInsertionService).loadDashboardData(any(), any(), any());

dashboardExtractionService.extractDataForTheDashboard(null, null, null);
dashboardExtractionService.extractDataForTheDashboard(months, debug, manuallyTriggered);

Assertions.assertFalse(extractionDto.logMessages().isEmpty());
}

private static Stream<Arguments> basicValues() {
return Stream.of(
Arguments.of(24, false, false),
Arguments.of(24, false, null),
Arguments.of(24, null, false),
Arguments.of(24, null, null),
Arguments.of(null, false, false),
Arguments.of(null, null, false),
Arguments.of(null, null, null),
Arguments.of(null, false, null)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@DisplayName("Unit Test | Forest Client Service")
class ForestClientServiceTest {

@Mock ForestClientApiProvider forestClientApiProvider;
Expand Down Expand Up @@ -107,4 +111,19 @@ void getClientByNumber_notFound_shouldSucceed() {

Assertions.assertTrue(clientDto.isEmpty());
}

@ParameterizedTest
@ValueSource(strings = {"", " ", " ", " "})
@NullAndEmptySource
@DisplayName("No client number should return empty")
void shouldGetEmptyWhenNoClientNumber(String clientNumber) {

when(forestClientApiProvider.fetchClientByNumber("00000000"))
.thenReturn(Optional.empty());

Optional<ForestClientDto> clientDtoOptional =
forestClientService.getClientByNumber(clientNumber);

Assertions.assertTrue(clientDtoOptional.isEmpty());
}
}
Loading

0 comments on commit 1333fb2

Please sign in to comment.