Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
- Create generic TestTemplateInvocationContext
- Only change var when env change
  • Loading branch information
ntqdinh-axonivy committed Jan 7, 2025
1 parent 015c8d0 commit 6298926
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

public class CustomInvocationContextProvider implements TestTemplateInvocationContextProvider {
import com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants;

public class MultiEnvironmentContextProvider implements TestTemplateInvocationContextProvider {

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
return Stream.of(new MockServerInvocationContext(), new RestCallInvocationContext());
return Stream.of(new TestEnironmentInvocationContext(OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME),
new TestEnironmentInvocationContext(OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME));
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.axonivy.connector.openweather.test.context;

import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME;

import java.util.Collections;
import java.util.List;

Expand All @@ -12,10 +10,17 @@
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;;

public class MockServerInvocationContext implements TestTemplateInvocationContext {
public class TestEnironmentInvocationContext implements TestTemplateInvocationContext {
private String contextDisplayName;

public TestEnironmentInvocationContext(String contextDisplayName) {
super();
this.contextDisplayName = contextDisplayName;
}

@Override
public String getDisplayName(int invocationIndex) {
return MOCK_SERVER_CONTEXT_DISPLAY_NAME;
return contextDisplayName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.extension.ExtensionContext;

import com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants;

import ch.ivyteam.ivy.application.IApplication;
import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess;
import ch.ivyteam.ivy.bpm.engine.client.sub.SubRequestBuilder;
import ch.ivyteam.ivy.environment.AppFixture;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.rest.client.RestClient;
import ch.ivyteam.ivy.rest.client.RestClients;
import ch.ivyteam.ivy.server.internal.test.AppFixtureJu5Context;

public class OpenWeatherUtils {
private static final String APP_ID_KEY = "appId";
private static final String FEATURE_SUFFIX = ".Features";
private static final String REST_CLIENT_PREFIX = "RestClients.";
private static final String WEATHER_GEO_URL_KEY = "weatherGeoUrl";
private static final String APP_ID_PROP_KEY = "AUTH." + APP_ID_KEY;
private static final String WEATHER_DATA_URL_KEY = "weatherDataUrl";
private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties";
private static final String OPEN_WEATHER_CONNECTOR_PREFIX = "openWeatherConnector.";
Expand All @@ -34,43 +28,33 @@ public class OpenWeatherUtils {
private static final String WEATHER_DATA_MOCK_ENDPOINT = "{ivy.app.baseurl}/api/weatherDataMock";
private static final String WEATHER_GEO_MOCK_ENDPOINT = "{ivy.app.baseurl}/api/weatherGeoMock";

@SuppressWarnings("restriction")
public static void setUpConfigForMockServer(ExtensionContext context) {
AppFixture fixture = AppFixtureJu5Context.get(context).getFixture();
public static void setUpConfigForMockServer(AppFixture fixture) {
// Disable OAuth feature for mock rest service
fixture.config(REST_CLIENT_PREFIX + WEATHER_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, List.of(JSON_FEATURES));
fixture.config(REST_CLIENT_PREFIX + GEO_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, List.of(JSON_FEATURES));

fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_DATA_URL_KEY, WEATHER_DATA_MOCK_ENDPOINT);
fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_GEO_URL_KEY, WEATHER_GEO_MOCK_ENDPOINT);
}

public static void setUpConfigForContext(ExtensionContext context) {
switch (context.getDisplayName()) {
public static void setUpConfigForContext(String contextName, AppFixture fixture) {
switch (contextName) {
case OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME:
setUpConfigForRestCallTest();
setUpConfigForApiTest(fixture);
break;
case OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME:
setUpConfigForMockServer(context);
setUpConfigForMockServer(fixture);
break;
default:
break;
}
}

private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri,
String appIdValue) {
RestClient client = clients.find(clientName);
System.out.println("client == null: "+client == null);
client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appIdValue).toRestClient();
clients.set(client);
}

public static void setUpConfigForRestCallTest() {
public static void setUpConfigForApiTest(AppFixture fixture) {
String appId = System.getProperty(APP_ID_KEY);
String weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY);
String weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY);


// Local setup for testing
if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) {
try (var in = OpenWeatherUtils.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) {
Expand All @@ -86,12 +70,13 @@ public static void setUpConfigForRestCallTest() {
}
}

RestClients clients = RestClients.of(IApplication.current());
setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl, appId);
setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl, appId);
fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_DATA_URL_KEY, weatherDataUrl);
fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_GEO_URL_KEY, weatherGeoUrl);
fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + APP_ID_KEY, appId);
}

public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient client,String subProcessPath, String subProcessName) {
public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient client, String subProcessPath,
String subProcessName) {
return client.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openweathermap.api.data2_5.client.AirPollution;

import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider;
import com.axonivy.connector.openweather.test.context.MultiEnvironmentContextProvider;
import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils;

import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult;
import ch.ivyteam.ivy.bpm.error.BpmError;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.environment.AppFixture;

@IvyProcessTest(enableWebServer = true)
@ExtendWith(CustomInvocationContextProvider.class)
@ExtendWith(MultiEnvironmentContextProvider.class)
public class AirPollutionProcessTest {
private final Double TEST_LON_VALUE = 40.7484;
private final Double TEST_LAT_VALUE = -73.9967;

@BeforeEach
void beforeEach(ExtensionContext context) {
OpenWeatherUtils.setUpConfigForContext(context);
void beforeEach(ExtensionContext context, AppFixture fixture) {
OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture);
}

@TestTemplate
void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException {
ExecutionResult result = OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE)
.execute(TEST_LON_VALUE, TEST_LAT_VALUE);
Ivy.log().fatal("result " + result);
var object = result.data().last().get(RESULT_KEY);
assertThat(object).isInstanceOf(AirPollution.class);
}
Expand All @@ -60,19 +59,17 @@ void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws No

@TestTemplate
void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException {
ExecutionResult result = OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE)
.execute(TEST_LON_VALUE, TEST_LAT_VALUE);
ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH,
GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE);
var object = result.data().last().get(RESULT_KEY);
assertThat(object).isInstanceOf(AirPollution.class);
}

@TestTemplate
void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException {
try {
OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE)
.execute(null, null);
OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH,
GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null);
} catch (BpmError e) {
assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
}
Expand All @@ -83,14 +80,16 @@ void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client
OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2));
ExecutionResult result = OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE)
.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH,
GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE)
.execute(TEST_LON_VALUE, TEST_LAT_VALUE, now, twoDaysLater);
var object = result.data().last().get(RESULT_KEY);
assertThat(object).isInstanceOf(AirPollution.class);
}

@TestTemplate
void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient client) throws NoSuchFieldException {
void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient client)
throws NoSuchFieldException {
OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2));
try {
Expand All @@ -102,7 +101,8 @@ void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClien
}

@TestTemplate
void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient client) throws NoSuchFieldException {
void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient client)
throws NoSuchFieldException {
OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openweathermap.api.data2_5.client.Current;

import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider;
import com.axonivy.connector.openweather.test.context.MultiEnvironmentContextProvider;
import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils;

import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult;
import ch.ivyteam.ivy.bpm.error.BpmError;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.AppFixture;

@IvyProcessTest(enableWebServer = true)
@ExtendWith(CustomInvocationContextProvider.class)
@ExtendWith(MultiEnvironmentContextProvider.class)
public class CurrentWeatherProcessTest {

private final Double TEST_LON_VALUE = 40.7484;
private final Double TEST_LAT_VALUE = -73.9967;

@BeforeEach
void beforeEach(ExtensionContext context) {
OpenWeatherUtils.setUpConfigForContext(context);
void beforeEach(ExtensionContext context, AppFixture fixture) {
OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture);
}

@TestTemplate
public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient client) throws NoSuchFieldException {
ExecutionResult result = OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_CURRENT_WEATHER_PROCESS_PATH, GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE)
.getSubProcessWithNameAndPath(client, GET_CURRENT_WEATHER_PROCESS_PATH,
GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE)
.execute(TEST_LON_VALUE, TEST_LAT_VALUE, StringUtils.EMPTY, StringUtils.EMPTY);
var object = result.data().last().get(RESULT_KEY);
assertThat(object).isInstanceOf(Current.class);
Expand All @@ -45,9 +47,8 @@ public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient clien
@TestTemplate
public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException {
try {
OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_CURRENT_WEATHER_PROCESS_PATH, GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE)
.execute(null, null, StringUtils.EMPTY, StringUtils.EMPTY);
OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_CURRENT_WEATHER_PROCESS_PATH,
GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE).execute(null, null, StringUtils.EMPTY, StringUtils.EMPTY);
} catch (BpmError e) {
assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openweathermap.api.data2_5.client.Forecast;

import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider;
import com.axonivy.connector.openweather.test.context.MultiEnvironmentContextProvider;
import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils;

import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult;
import ch.ivyteam.ivy.bpm.error.BpmError;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.AppFixture;

@IvyProcessTest(enableWebServer = true)
@ExtendWith(CustomInvocationContextProvider.class)
@ExtendWith(MultiEnvironmentContextProvider.class)
public class ForecastWeatherProcessTest {
private final Double TEST_LON_VALUE = 40.7484;
private final Double TEST_LAT_VALUE = -73.9967;

@BeforeEach
void beforeEach(ExtensionContext context) {
OpenWeatherUtils.setUpConfigForContext(context);
void beforeEach(ExtensionContext context, AppFixture fixture) {
OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture);
}

@TestTemplate
Expand All @@ -44,7 +45,8 @@ public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient client) th
@TestTemplate
public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException {
try {
OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE)
OpenWeatherUtils
.getSubProcessWithNameAndPath(client, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE)
.execute(null, null, 1, StringUtils.EMPTY, StringUtils.EMPTY);
} catch (BpmError e) {
assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
Expand Down
Loading

0 comments on commit 6298926

Please sign in to comment.