From 111064e0642fcf4d8c41be316f8973beac004e2a Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 14:10:42 +0700 Subject: [PATCH 01/14] update test case with real call --- .github/workflows/ci.yml | 2 + .../org.eclipse.core.resources.prefs | 2 + open-weather-connector-demo/pom.xml | 1 + .../org.eclipse.core.resources.prefs | 2 + open-weather-connector-test/pom.xml | 12 +++ .../openweather/test/OpenWeatherDataMock.java | 87 ------------------- .../openweather/test/OpenWeatherGeoMock.java | 63 -------------- .../test/json/air-pollution-result.json | 24 ----- .../test/json/current-weather-result.json | 44 ---------- .../test/json/end-must-be-after-start.json | 4 - .../test/json/forecast-result.json | 56 ------------ .../test/json/geo-direct-result.json | 39 --------- .../openweather/test/json/geo-not-found.json | 4 - .../openweather/test/json/geo-nothing.json | 4 - .../test/json/geo-reverse-result.json | 39 --------- .../openweather/test/json/geo-zip-result.json | 7 -- .../test/AirPollutionProcessTest.java | 40 ++++----- .../openweather/test/BaseProcessTest.java | 64 ++++++++++++-- .../test/CurrentWeatherProcessTest.java | 19 ++-- .../test/ForecastWeatherProcessTest.java | 16 ++-- .../test/GeocodingLocationProcessTest.java | 36 ++++---- .../org.eclipse.core.resources.prefs | 2 + open-weather-connector/config/variables.yaml | 8 +- open-weather-connector/pom.xml | 1 + 24 files changed, 132 insertions(+), 444 deletions(-) create mode 100644 open-weather-connector-demo/.settings/org.eclipse.core.resources.prefs create mode 100644 open-weather-connector-test/.settings/org.eclipse.core.resources.prefs delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json create mode 100644 open-weather-connector/.settings/org.eclipse.core.resources.prefs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd4b9c4..c2ce012 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,3 +10,5 @@ on: jobs: build: uses: axonivy-market/github-workflows/.github/workflows/ci.yml@v5 + secrets: + mvnArgs: -Dopenweather.appId=${{ secrets.APP_ID }} -Dopenweather.weatherDataUrl=${{ secrets.WEATHER_DATA_URL }} -Dopenweather.weatherGeoUrl=${{ secrets.WEATHER_GEO_URL }} diff --git a/open-weather-connector-demo/.settings/org.eclipse.core.resources.prefs b/open-weather-connector-demo/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/open-weather-connector-demo/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/open-weather-connector-demo/pom.xml b/open-weather-connector-demo/pom.xml index d6c4ea5..438e438 100644 --- a/open-weather-connector-demo/pom.xml +++ b/open-weather-connector-demo/pom.xml @@ -7,6 +7,7 @@ iar 12.0.0 + UTF-8 diff --git a/open-weather-connector-test/.settings/org.eclipse.core.resources.prefs b/open-weather-connector-test/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/open-weather-connector-test/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/open-weather-connector-test/pom.xml b/open-weather-connector-test/pom.xml index 1da7b93..f3beb3a 100644 --- a/open-weather-connector-test/pom.xml +++ b/open-weather-connector-test/pom.xml @@ -7,6 +7,7 @@ iar 12.0.0 + UTF-8 12.0.0 @@ -55,6 +56,17 @@ src_test + + maven-surefire-plugin + 3.0.0-M5 + + + ${openweather.appId} + ${openweather.weatherDataUrl} + ${openweather.weatherGeoUrl} + + + com.axonivy.ivy.ci project-build-plugin diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java deleted file mode 100644 index 6861749..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.axonivy.connector.openweather.test; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - -import javax.annotation.security.PermitAll; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.ObjectUtils; - -import io.swagger.v3.oas.annotations.Hidden; - -@Path("weatherDataMock") -@PermitAll -@Hidden -public class OpenWeatherDataMock { - @GET - @Path("air_pollution") - @Produces(MediaType.APPLICATION_JSON) - public Response getAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/air-pollution-result.json")).build(); - } - - @GET - @Path("air_pollution/forecast") - @Produces(MediaType.APPLICATION_JSON) - public Response getForeacastAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/air-pollution-result.json")).build(); - } - - @GET - @Path("air_pollution/history") - @Produces(MediaType.APPLICATION_JSON) - public Response getHistoryAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon, - @QueryParam("start") long start, @QueryParam("end") long end) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - if (start > end) { - return Response.status(400).entity(load("json/end-must-be-after-start.json")).build(); - } - return Response.status(200).entity(load("json/air-pollution-result.json")).build(); - } - - @GET - @Path("weather") - @Produces(MediaType.APPLICATION_JSON) - public Response getWeather(@QueryParam("lat") Double lat, @QueryParam("lon") Double lon, - @QueryParam("lang") String lang, @QueryParam("units") String units) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/current-weather-result.json")).build(); - } - - @GET - @Path("forecast") - @Produces(MediaType.APPLICATION_JSON) - public Response getForecast(@QueryParam("lat") double lat, @QueryParam("lon") double lon, - @QueryParam("lang") String lang, @QueryParam("units") String units, @QueryParam("cnt") int cnt) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/forecast-result.json")).build(); - } - - private static String load(String path) { - try (InputStream is = OpenWeatherDataMock.class.getResourceAsStream(path)) { - return IOUtils.toString(is, StandardCharsets.UTF_8); - } catch (IOException ex) { - throw new RuntimeException("Failed to read resource: " + path); - } - } -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java deleted file mode 100644 index 5494212..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.axonivy.connector.openweather.test; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - -import javax.annotation.security.PermitAll; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.StringUtils; - -import io.swagger.v3.oas.annotations.Hidden; - -@Path("weatherGeoMock") -@PermitAll -@Hidden -public class OpenWeatherGeoMock { - @GET - @Path("direct") - @Produces(MediaType.APPLICATION_JSON) - public Response getGeoByCoordinates(@QueryParam("q") String q, @QueryParam("limit") int limit) { - if (StringUtils.isBlank(q)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/geo-direct-result.json")).build(); - } - - @GET - @Path("zip") - @Produces(MediaType.APPLICATION_JSON) - public Response getGeoByZip(@QueryParam("zip") String zip) { - if (StringUtils.isBlank(zip)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/geo-zip-result.json")).build(); - } - - @GET - @Path("reverse") - @Produces(MediaType.APPLICATION_JSON) - public Response getLocationInformation(@QueryParam("lat") double lat, @QueryParam("lon") double lon, - @QueryParam("limit") int limit) { - if (ObjectUtils.anyNull(lat, lon)) { - return Response.status(400).entity(load("json/geo-nothing.json")).build(); - } - return Response.status(200).entity(load("json/geo-reverse-result.json")).build(); - } - - private static String load(String path) { - try (InputStream is = OpenWeatherGeoMock.class.getResourceAsStream(path)) { - return IOUtils.toString(is, StandardCharsets.UTF_8); - } catch (IOException ex) { - throw new RuntimeException("Failed to read resource: " + path); - } - } -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json deleted file mode 100644 index 8daaf18..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "coord": { - "lon": -73.9967, - "lat": 40.7484 - }, - "list": [ - { - "main": { - "aqi": 3 - }, - "components": { - "co": 907.9, - "no": 82.25, - "no2": 45.24, - "o3": 4.11, - "so2": 9.18, - "pm2_5": 32.24, - "pm10": 44.75, - "nh3": 4.12 - }, - "dt": 1709560558 - } - ] -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json deleted file mode 100644 index a434234..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "coord": { - "lon": -73.9967, - "lat": 40.7484 - }, - "weather": [ - { - "id": 804, - "main": "Clouds", - "description": "overcast clouds", - "icon": "04d" - } - ], - "base": "stations", - "main": { - "temp": 283.38, - "feels_like": 282.5, - "temp_min": 281.16, - "temp_max": 285.07, - "pressure": 1030, - "humidity": 78 - }, - "visibility": 10000, - "wind": { - "speed": 5.36, - "deg": 53, - "gust": 5.81 - }, - "clouds": { - "all": 100 - }, - "dt": 1709560480, - "sys": { - "type": 2, - "id": 2008101, - "country": "US", - "sunrise": 1709551486, - "sunset": 1709592643 - }, - "timezone": -18000, - "id": 5099133, - "name": "Hoboken", - "cod": 200 -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json deleted file mode 100644 index 5e3c522..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "cod": "400", - "message": "end must be after start" -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json deleted file mode 100644 index 52489d8..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "cod": "200", - "message": 0, - "cnt": 1, - "list": [ - { - "dt": 1709564400, - "main": { - "temp": 283.35, - "feels_like": 282.49, - "temp_min": 283.35, - "temp_max": 285.34, - "pressure": 1030, - "sea_level": 1030, - "grnd_level": 1027, - "humidity": 79, - "temp_kf": -1.99 - }, - "weather": [ - { - "id": 804, - "main": "Clouds", - "description": "overcast clouds", - "icon": "04d" - } - ], - "clouds": { - "all": 100 - }, - "wind": { - "speed": 2.78, - "deg": 87, - "gust": 3.92 - }, - "visibility": 10000, - "pop": 0, - "sys": { - "pod": "d" - }, - "dt_txt": "2024-03-04 15:00:00" - } - ], - "city": { - "id": 5099133, - "name": "Hoboken", - "coord": { - "lat": 40.7484, - "lon": -73.9967 - }, - "country": "US", - "population": 50005, - "timezone": -18000, - "sunrise": 1709551486, - "sunset": 1709592643 - } -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json deleted file mode 100644 index 5100175..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json +++ /dev/null @@ -1,39 +0,0 @@ -[ - { - "name": "New York County", - "local_names": { - "uk": "Нью-Йорк", - "es": "Nueva York", - "pl": "Nowy Jork", - "ko": "뉴욕", - "cy": "Efrog Newydd", - "pt": "Nova Iorque", - "cs": "New York", - "is": "Nýja Jórvík", - "el": "Νέα Υόρκη", - "kn": "ನ್ಯೂಯೊರ್ಕ್", - "te": "న్యూయొర్క్", - "vi": "New York", - "hi": "न्यूयॊर्क्", - "gl": "Nova York", - "he": "ניו יורק", - "eo": "Novjorko", - "ja": "ニューヨーク", - "ar": "نيويورك", - "it": "New York", - "ru": "Нью-Йорк", - "de": "New York", - "fr": "New York", - "oc": "Nòva York", - "zh": "纽约/紐約", - "en": "New York", - "be": "Нью-Ёрк", - "fa": "نیویورک", - "ca": "Nova York" - }, - "lat": 40.7127281, - "lon": -74.0060152, - "country": "US", - "state": "New York" - } -] diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json deleted file mode 100644 index 104c73f..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "cod": "404", - "message": "not found" -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json deleted file mode 100644 index 5f6cbb4..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "cod": "400", - "message": "Nothing to geocode" -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json deleted file mode 100644 index c7ed3ca..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json +++ /dev/null @@ -1,39 +0,0 @@ -[ - { - "name": "New York County", - "local_names": { - "ca": "Nova York", - "is": "Nýja Jórvík", - "en": "New York", - "ko": "뉴욕", - "he": "ניו יורק", - "es": "Nueva York", - "cy": "Efrog Newydd", - "kn": "ನ್ಯೂಯೊರ್ಕ್", - "cs": "New York", - "fr": "New York", - "pt": "Nova Iorque", - "vi": "New York", - "zh": "纽约/紐約", - "it": "New York", - "uk": "Нью-Йорк", - "fa": "نیویورک", - "be": "Нью-Ёрк", - "ru": "Нью-Йорк", - "hi": "न्यूयॊर्क्", - "te": "న్యూయొర్క్", - "ar": "نيويورك", - "ja": "ニューヨーク", - "gl": "Nova York", - "pl": "Nowy Jork", - "oc": "Nòva York", - "eo": "Novjorko", - "el": "Νέα Υόρκη", - "de": "New York" - }, - "lat": 40.7127281, - "lon": -74.0060152, - "country": "US", - "state": "New York" - } -] diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json deleted file mode 100644 index c7bc53b..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "zip": "10001", - "name": "New York", - "lat": 40.7484, - "lon": -73.9967, - "country": "US" -} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 50f9208..0f9905d 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -10,23 +10,18 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class AirPollutionProcessTest extends BaseProcessTest { - private static final BpmProcess GET_AIR_POLLUTION_PROCESS = BpmProcess.path("connector/AirPollution"); - private static final BpmElement GET_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getCurrentAirPollution(Double,Double)"); - private static final BpmElement GET_FORECAST_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getForecastAirPollution(Double,Double)"); - private static final BpmElement GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"); + private static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; + private static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; + private static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; + private static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; @Test public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, - -74.0060152); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -34,7 +29,8 @@ public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient @Test() public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(null, null); + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -43,8 +39,8 @@ public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) @Test public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, - -74.0060152); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -53,7 +49,8 @@ public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient b public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(null, null); + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -64,8 +61,8 @@ public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - ExecutionResult result = bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE) - .execute(40.7127281, -74.0060152, now, twoDaysLater); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152, now, twoDaysLater); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -76,8 +73,8 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(B OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(null, null, now, - twoDaysLater); + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -89,8 +86,9 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreTh OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, -74.0060152, - twoDaysLater, now); + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, twoDaysLater, now); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java index 3b019fe..55ee292 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java @@ -1,21 +1,69 @@ package com.axonivy.connector.openweather.test; +import java.io.IOException; +import java.util.Properties; + +import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; +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.bpm.exec.client.IvyProcessTest; import ch.ivyteam.ivy.environment.AppFixture; +import ch.ivyteam.ivy.rest.client.RestClient; +import ch.ivyteam.ivy.rest.client.RestClients; @IvyProcessTest(enableWebServer = true) public class BaseProcessTest { + private static final String APP_ID_KEY = "appId"; + 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 WEATHER_GEO_URL_KEY = "weatherGeoUrl"; + private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; + private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; + private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + private static String appId; + private static String weatherDataUrl; + private static String weatherGeoUrl; @BeforeEach - void beforeEach(AppFixture fixture) { - // Disable OAuth feature for mock rest service - fixture.config("RestClients.WeatherData (Openweathermap weather API).Features", - "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); - fixture.config("RestClients.GeocodingCoordinates (Openweathermap geocoding API).Features", - "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); - fixture.var("openWeatherConnector.weatherDataUrl", "{ivy.app.baseurl}/api/weatherDataMock"); - fixture.var("openWeatherConnector.weatherGeoUrl", "{ivy.app.baseurl}/api/weatherGeoMock"); + void beforeAll(AppFixture fixture, IApplication app) throws IOException { + setupConfig(fixture, app); + } + + private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri) { + RestClient client = clients.find(clientName); + client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appId).toRestClient(); + clients.set(client); + } + + protected SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, + String subProcessName) { + return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); + } + + static void setupConfig(AppFixture fixture, IApplication app) throws IOException { + appId = System.getProperty(APP_ID_KEY); + weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); + weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); + + // Local setup for testing + if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { + try (var in = BaseProcessTest.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { + if (in != null) { + Properties props = new Properties(); + props.load(in); + appId = (String) props.get(APP_ID_KEY); + weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); + weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); + } + } + } + + RestClients clients = RestClients.of(app); + setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl); + setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl); } } \ No newline at end of file diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 7326ea6..87eaca4 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -8,20 +8,17 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class CurrentWeatherProcessTest extends BaseProcessTest { - private static final BpmProcess GET_CURRENT_WEATHER_PROCESS = BpmProcess.path("connector/CurrentWeather"); - private static final BpmElement GET_CURRENT_WEATHER_BY_GEOCODE = GET_CURRENT_WEATHER_PROCESS - .elementName("getCurrentWeather(Double,Double,String,String)"); + private static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; + private static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; @Test - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) - throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(40.7127281, - -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, + GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(Current.class); } @@ -29,8 +26,8 @@ public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmCl @Test() public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(null, null, StringUtils.EMPTY, - StringUtils.EMPTY); + getSubProcessWithNameAndPath(bpmClient, 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(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 5cb324a..d23875c 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -8,20 +8,18 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class ForecastWeatherProcessTest extends BaseProcessTest { - private static final BpmProcess GET_FORECAST_PROCESS = BpmProcess.path("connector/ForecastWeather"); - private static final BpmElement GET_FORECAST_BY_GEOCODE = GET_FORECAST_PROCESS - .elementName("getForecastWeather(Double,Double,Integer,String,String)"); + private static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; + private static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; @Test public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(40.7127281, -74.0060152, 1, - StringUtils.EMPTY, StringUtils.EMPTY); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, + GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, 1, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(Forecast.class); } @@ -29,8 +27,8 @@ public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) @Test() public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(null, null, 1, StringUtils.EMPTY, - StringUtils.EMPTY); + getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(null, null, 1, StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index 706d18d..a3a8b2c 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -11,24 +11,19 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class GeocodingLocationProcessTest extends BaseProcessTest { - private static final BpmProcess GEOCODING_LOCATION_PROCESS = BpmProcess.path("connector/GeocodingLocation"); - private static final BpmElement GEOCODING_LOCATION_BY_NAME = GEOCODING_LOCATION_PROCESS - .elementName("getCoordinatesByLocationName(String,String,String,Integer)"); - private static final BpmElement GEOCODING_LOCATION_BY_ZIP_CODE = GEOCODING_LOCATION_PROCESS - .elementName("getCoordinatesByZipCode(String,String)"); - private static final BpmElement GEOCODING_LOCATION_REVERSE = GEOCODING_LOCATION_PROCESS - .elementName("reverse(Double,Double,Integer)"); + private static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; + private static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; + private static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; + private static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; @Test public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute("New York", - StringUtils.EMPTY, StringUtils.EMPTY, 1); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE).execute("New York", StringUtils.EMPTY, StringUtils.EMPTY, 1); var object = result.data().last().get("results"); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -39,8 +34,9 @@ public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) t @Test() public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute(StringUtils.EMPTY, StringUtils.EMPTY, - StringUtils.EMPTY, 1); + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -48,8 +44,8 @@ public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws N @Test public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute("10001", - StringUtils.EMPTY); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute("10001", StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(GeoLocation.class); } @@ -57,7 +53,8 @@ public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws No @Test() public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute(StringUtils.EMPTY, StringUtils.EMPTY); + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -65,7 +62,8 @@ public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws No @Test public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(40.7484, -73.9967, 1); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(40.7484, -73.9967, 1); var object = result.data().last().get("results"); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -76,10 +74,10 @@ public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws No @Test() public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(null, null, 1); + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } } - } diff --git a/open-weather-connector/.settings/org.eclipse.core.resources.prefs b/open-weather-connector/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/open-weather-connector/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/open-weather-connector/config/variables.yaml b/open-weather-connector/config/variables.yaml index 3d905c1..a6aa737 100644 --- a/open-weather-connector/config/variables.yaml +++ b/open-weather-connector/config/variables.yaml @@ -2,10 +2,8 @@ Variables: openWeatherConnector: # Open Weather Data endpoint - weatherDataUrl: 'https://api.openweathermap.org/data/2.5' - + weatherDataUrl: "" # Open Weather Geo endpoint - weatherGeoUrl: 'https://api.openweathermap.org/geo/1.0' - + weatherGeoUrl: "" # Your Open Weather connector Application Client ID - appId: '' \ No newline at end of file + appId: "" diff --git a/open-weather-connector/pom.xml b/open-weather-connector/pom.xml index ffa3022..0483585 100644 --- a/open-weather-connector/pom.xml +++ b/open-weather-connector/pom.xml @@ -7,6 +7,7 @@ iar 12.0.0 + UTF-8 From addcc19e27d658f49c400a30feed1cf9e8a4ba22 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 14:11:45 +0700 Subject: [PATCH 02/14] provide mvn arg for dev build --- .github/workflows/dev.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index ae5e6c3..c30230c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -9,3 +9,5 @@ on: jobs: build: uses: axonivy-market/github-workflows/.github/workflows/dev.yml@v5 + secrets: + mvnArgs: -Dopenweather.appId=${{ secrets.APP_ID }} -Dopenweather.weatherDataUrl=${{ secrets.WEATHER_DATA_URL }} -Dopenweather.weatherGeoUrl=${{ secrets.WEATHER_GEO_URL }} From 2f41401e06e55d2bb37e60e5c1683b75833240c1 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 15:15:24 +0700 Subject: [PATCH 03/14] change package name of it test --- .../openweather/test/OpenWeatherDataMock.java | 87 +++++++++++++++++ .../openweather/test/OpenWeatherGeoMock.java | 63 ++++++++++++ .../test/json/air-pollution-result.json | 24 +++++ .../test/json/current-weather-result.json | 44 +++++++++ .../test/json/end-must-be-after-start.json | 4 + .../test/json/forecast-result.json | 56 +++++++++++ .../test/json/geo-direct-result.json | 39 ++++++++ .../openweather/test/json/geo-not-found.json | 4 + .../openweather/test/json/geo-nothing.json | 4 + .../test/json/geo-reverse-result.json | 39 ++++++++ .../openweather/test/json/geo-zip-result.json | 7 ++ .../test/AirPollutionProcessTest.java | 40 ++++---- .../openweather/test/BaseProcessTest.java | 64 ++----------- .../test/CurrentWeatherProcessTest.java | 19 ++-- .../test/ForecastWeatherProcessTest.java | 16 ++-- .../test/GeocodingLocationProcessTest.java | 36 +++---- .../test/it/AirPollutionProcessTest.java | 96 +++++++++++++++++++ .../openweather/test/it/BaseProcessTest.java | 69 +++++++++++++ .../test/it/CurrentWeatherProcessTest.java | 35 +++++++ .../test/it/ForecastWeatherProcessTest.java | 36 +++++++ .../test/it/GeocodingLocationProcessTest.java | 85 ++++++++++++++++ 21 files changed, 760 insertions(+), 107 deletions(-) create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json create mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java create mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java create mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java create mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java create mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java new file mode 100644 index 0000000..6861749 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherDataMock.java @@ -0,0 +1,87 @@ +package com.axonivy.connector.openweather.test; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import javax.annotation.security.PermitAll; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.ObjectUtils; + +import io.swagger.v3.oas.annotations.Hidden; + +@Path("weatherDataMock") +@PermitAll +@Hidden +public class OpenWeatherDataMock { + @GET + @Path("air_pollution") + @Produces(MediaType.APPLICATION_JSON) + public Response getAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/air-pollution-result.json")).build(); + } + + @GET + @Path("air_pollution/forecast") + @Produces(MediaType.APPLICATION_JSON) + public Response getForeacastAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/air-pollution-result.json")).build(); + } + + @GET + @Path("air_pollution/history") + @Produces(MediaType.APPLICATION_JSON) + public Response getHistoryAirPollution(@QueryParam("lat") double lat, @QueryParam("lon") double lon, + @QueryParam("start") long start, @QueryParam("end") long end) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + if (start > end) { + return Response.status(400).entity(load("json/end-must-be-after-start.json")).build(); + } + return Response.status(200).entity(load("json/air-pollution-result.json")).build(); + } + + @GET + @Path("weather") + @Produces(MediaType.APPLICATION_JSON) + public Response getWeather(@QueryParam("lat") Double lat, @QueryParam("lon") Double lon, + @QueryParam("lang") String lang, @QueryParam("units") String units) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/current-weather-result.json")).build(); + } + + @GET + @Path("forecast") + @Produces(MediaType.APPLICATION_JSON) + public Response getForecast(@QueryParam("lat") double lat, @QueryParam("lon") double lon, + @QueryParam("lang") String lang, @QueryParam("units") String units, @QueryParam("cnt") int cnt) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/forecast-result.json")).build(); + } + + private static String load(String path) { + try (InputStream is = OpenWeatherDataMock.class.getResourceAsStream(path)) { + return IOUtils.toString(is, StandardCharsets.UTF_8); + } catch (IOException ex) { + throw new RuntimeException("Failed to read resource: " + path); + } + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java new file mode 100644 index 0000000..5494212 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/OpenWeatherGeoMock.java @@ -0,0 +1,63 @@ +package com.axonivy.connector.openweather.test; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import javax.annotation.security.PermitAll; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; + +import io.swagger.v3.oas.annotations.Hidden; + +@Path("weatherGeoMock") +@PermitAll +@Hidden +public class OpenWeatherGeoMock { + @GET + @Path("direct") + @Produces(MediaType.APPLICATION_JSON) + public Response getGeoByCoordinates(@QueryParam("q") String q, @QueryParam("limit") int limit) { + if (StringUtils.isBlank(q)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/geo-direct-result.json")).build(); + } + + @GET + @Path("zip") + @Produces(MediaType.APPLICATION_JSON) + public Response getGeoByZip(@QueryParam("zip") String zip) { + if (StringUtils.isBlank(zip)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/geo-zip-result.json")).build(); + } + + @GET + @Path("reverse") + @Produces(MediaType.APPLICATION_JSON) + public Response getLocationInformation(@QueryParam("lat") double lat, @QueryParam("lon") double lon, + @QueryParam("limit") int limit) { + if (ObjectUtils.anyNull(lat, lon)) { + return Response.status(400).entity(load("json/geo-nothing.json")).build(); + } + return Response.status(200).entity(load("json/geo-reverse-result.json")).build(); + } + + private static String load(String path) { + try (InputStream is = OpenWeatherGeoMock.class.getResourceAsStream(path)) { + return IOUtils.toString(is, StandardCharsets.UTF_8); + } catch (IOException ex) { + throw new RuntimeException("Failed to read resource: " + path); + } + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json new file mode 100644 index 0000000..8daaf18 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/air-pollution-result.json @@ -0,0 +1,24 @@ +{ + "coord": { + "lon": -73.9967, + "lat": 40.7484 + }, + "list": [ + { + "main": { + "aqi": 3 + }, + "components": { + "co": 907.9, + "no": 82.25, + "no2": 45.24, + "o3": 4.11, + "so2": 9.18, + "pm2_5": 32.24, + "pm10": 44.75, + "nh3": 4.12 + }, + "dt": 1709560558 + } + ] +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json new file mode 100644 index 0000000..a434234 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/current-weather-result.json @@ -0,0 +1,44 @@ +{ + "coord": { + "lon": -73.9967, + "lat": 40.7484 + }, + "weather": [ + { + "id": 804, + "main": "Clouds", + "description": "overcast clouds", + "icon": "04d" + } + ], + "base": "stations", + "main": { + "temp": 283.38, + "feels_like": 282.5, + "temp_min": 281.16, + "temp_max": 285.07, + "pressure": 1030, + "humidity": 78 + }, + "visibility": 10000, + "wind": { + "speed": 5.36, + "deg": 53, + "gust": 5.81 + }, + "clouds": { + "all": 100 + }, + "dt": 1709560480, + "sys": { + "type": 2, + "id": 2008101, + "country": "US", + "sunrise": 1709551486, + "sunset": 1709592643 + }, + "timezone": -18000, + "id": 5099133, + "name": "Hoboken", + "cod": 200 +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json new file mode 100644 index 0000000..5e3c522 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/end-must-be-after-start.json @@ -0,0 +1,4 @@ +{ + "cod": "400", + "message": "end must be after start" +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json new file mode 100644 index 0000000..52489d8 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/forecast-result.json @@ -0,0 +1,56 @@ +{ + "cod": "200", + "message": 0, + "cnt": 1, + "list": [ + { + "dt": 1709564400, + "main": { + "temp": 283.35, + "feels_like": 282.49, + "temp_min": 283.35, + "temp_max": 285.34, + "pressure": 1030, + "sea_level": 1030, + "grnd_level": 1027, + "humidity": 79, + "temp_kf": -1.99 + }, + "weather": [ + { + "id": 804, + "main": "Clouds", + "description": "overcast clouds", + "icon": "04d" + } + ], + "clouds": { + "all": 100 + }, + "wind": { + "speed": 2.78, + "deg": 87, + "gust": 3.92 + }, + "visibility": 10000, + "pop": 0, + "sys": { + "pod": "d" + }, + "dt_txt": "2024-03-04 15:00:00" + } + ], + "city": { + "id": 5099133, + "name": "Hoboken", + "coord": { + "lat": 40.7484, + "lon": -73.9967 + }, + "country": "US", + "population": 50005, + "timezone": -18000, + "sunrise": 1709551486, + "sunset": 1709592643 + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json new file mode 100644 index 0000000..5100175 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-direct-result.json @@ -0,0 +1,39 @@ +[ + { + "name": "New York County", + "local_names": { + "uk": "Нью-Йорк", + "es": "Nueva York", + "pl": "Nowy Jork", + "ko": "뉴욕", + "cy": "Efrog Newydd", + "pt": "Nova Iorque", + "cs": "New York", + "is": "Nýja Jórvík", + "el": "Νέα Υόρκη", + "kn": "ನ್ಯೂಯೊರ್ಕ್", + "te": "న్యూయొర్క్", + "vi": "New York", + "hi": "न्यूयॊर्क्", + "gl": "Nova York", + "he": "ניו יורק", + "eo": "Novjorko", + "ja": "ニューヨーク", + "ar": "نيويورك", + "it": "New York", + "ru": "Нью-Йорк", + "de": "New York", + "fr": "New York", + "oc": "Nòva York", + "zh": "纽约/紐約", + "en": "New York", + "be": "Нью-Ёрк", + "fa": "نیویورک", + "ca": "Nova York" + }, + "lat": 40.7127281, + "lon": -74.0060152, + "country": "US", + "state": "New York" + } +] diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json new file mode 100644 index 0000000..104c73f --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-not-found.json @@ -0,0 +1,4 @@ +{ + "cod": "404", + "message": "not found" +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json new file mode 100644 index 0000000..5f6cbb4 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-nothing.json @@ -0,0 +1,4 @@ +{ + "cod": "400", + "message": "Nothing to geocode" +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json new file mode 100644 index 0000000..c7ed3ca --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-reverse-result.json @@ -0,0 +1,39 @@ +[ + { + "name": "New York County", + "local_names": { + "ca": "Nova York", + "is": "Nýja Jórvík", + "en": "New York", + "ko": "뉴욕", + "he": "ניו יורק", + "es": "Nueva York", + "cy": "Efrog Newydd", + "kn": "ನ್ಯೂಯೊರ್ಕ್", + "cs": "New York", + "fr": "New York", + "pt": "Nova Iorque", + "vi": "New York", + "zh": "纽约/紐約", + "it": "New York", + "uk": "Нью-Йорк", + "fa": "نیویورک", + "be": "Нью-Ёрк", + "ru": "Нью-Йорк", + "hi": "न्यूयॊर्क्", + "te": "న్యూయొర్క్", + "ar": "نيويورك", + "ja": "ニューヨーク", + "gl": "Nova York", + "pl": "Nowy Jork", + "oc": "Nòva York", + "eo": "Novjorko", + "el": "Νέα Υόρκη", + "de": "New York" + }, + "lat": 40.7127281, + "lon": -74.0060152, + "country": "US", + "state": "New York" + } +] diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json new file mode 100644 index 0000000..c7bc53b --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/json/geo-zip-result.json @@ -0,0 +1,7 @@ +{ + "zip": "10001", + "name": "New York", + "lat": 40.7484, + "lon": -73.9967, + "country": "US" +} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 0f9905d..50f9208 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -10,18 +10,23 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class AirPollutionProcessTest extends BaseProcessTest { - private static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; - private static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; - private static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; - private static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; + private static final BpmProcess GET_AIR_POLLUTION_PROCESS = BpmProcess.path("connector/AirPollution"); + private static final BpmElement GET_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS + .elementName("getCurrentAirPollution(Double,Double)"); + private static final BpmElement GET_FORECAST_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS + .elementName("getForecastAirPollution(Double,Double)"); + private static final BpmElement GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS + .elementName("getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"); @Test public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); + ExecutionResult result = bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, + -74.0060152); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -29,8 +34,7 @@ public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient @Test() public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); + bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -39,8 +43,8 @@ public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) @Test public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); + ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, + -74.0060152); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -49,8 +53,7 @@ public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient b public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); + bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -61,8 +64,8 @@ public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152, now, twoDaysLater); + ExecutionResult result = bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE) + .execute(40.7127281, -74.0060152, now, twoDaysLater); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(AirPollution.class); } @@ -73,8 +76,8 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(B OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); + bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(null, null, now, + twoDaysLater); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -86,9 +89,8 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreTh OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, twoDaysLater, now); + bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, -74.0060152, + twoDaysLater, now); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java index 55ee292..3b019fe 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java @@ -1,69 +1,21 @@ package com.axonivy.connector.openweather.test; -import java.io.IOException; -import java.util.Properties; - -import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; -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.bpm.exec.client.IvyProcessTest; import ch.ivyteam.ivy.environment.AppFixture; -import ch.ivyteam.ivy.rest.client.RestClient; -import ch.ivyteam.ivy.rest.client.RestClients; @IvyProcessTest(enableWebServer = true) public class BaseProcessTest { - private static final String APP_ID_KEY = "appId"; - 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 WEATHER_GEO_URL_KEY = "weatherGeoUrl"; - private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; - private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; - private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; - private static String appId; - private static String weatherDataUrl; - private static String weatherGeoUrl; @BeforeEach - void beforeAll(AppFixture fixture, IApplication app) throws IOException { - setupConfig(fixture, app); - } - - private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri) { - RestClient client = clients.find(clientName); - client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appId).toRestClient(); - clients.set(client); - } - - protected SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, - String subProcessName) { - return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); - } - - static void setupConfig(AppFixture fixture, IApplication app) throws IOException { - appId = System.getProperty(APP_ID_KEY); - weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); - weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); - - // Local setup for testing - if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { - try (var in = BaseProcessTest.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { - if (in != null) { - Properties props = new Properties(); - props.load(in); - appId = (String) props.get(APP_ID_KEY); - weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); - weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); - } - } - } - - RestClients clients = RestClients.of(app); - setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl); - setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl); + void beforeEach(AppFixture fixture) { + // Disable OAuth feature for mock rest service + fixture.config("RestClients.WeatherData (Openweathermap weather API).Features", + "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); + fixture.config("RestClients.GeocodingCoordinates (Openweathermap geocoding API).Features", + "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); + fixture.var("openWeatherConnector.weatherDataUrl", "{ivy.app.baseurl}/api/weatherDataMock"); + fixture.var("openWeatherConnector.weatherGeoUrl", "{ivy.app.baseurl}/api/weatherGeoMock"); } } \ No newline at end of file diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 87eaca4..7326ea6 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -8,17 +8,20 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class CurrentWeatherProcessTest extends BaseProcessTest { - private static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; - private static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; + private static final BpmProcess GET_CURRENT_WEATHER_PROCESS = BpmProcess.path("connector/CurrentWeather"); + private static final BpmElement GET_CURRENT_WEATHER_BY_GEOCODE = GET_CURRENT_WEATHER_PROCESS + .elementName("getCurrentWeather(Double,Double,String,String)"); @Test - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, - GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) + throws NoSuchFieldException { + ExecutionResult result = bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(40.7127281, + -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(Current.class); } @@ -26,8 +29,8 @@ public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmCl @Test() public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, - GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE).execute(null, null, StringUtils.EMPTY, StringUtils.EMPTY); + bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(null, null, StringUtils.EMPTY, + StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index d23875c..5cb324a 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -8,18 +8,20 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class ForecastWeatherProcessTest extends BaseProcessTest { - private static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; - private static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + private static final BpmProcess GET_FORECAST_PROCESS = BpmProcess.path("connector/ForecastWeather"); + private static final BpmElement GET_FORECAST_BY_GEOCODE = GET_FORECAST_PROCESS + .elementName("getForecastWeather(Double,Double,Integer,String,String)"); @Test public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, - GET_FORECAST_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, 1, StringUtils.EMPTY, StringUtils.EMPTY); + ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(40.7127281, -74.0060152, 1, + StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(Forecast.class); } @@ -27,8 +29,8 @@ public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) @Test() public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) - .execute(null, null, 1, StringUtils.EMPTY, StringUtils.EMPTY); + bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(null, null, 1, StringUtils.EMPTY, + StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index a3a8b2c..706d18d 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -11,19 +11,24 @@ import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; +import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; public class GeocodingLocationProcessTest extends BaseProcessTest { - private static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; - private static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; - private static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; - private static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + private static final BpmProcess GEOCODING_LOCATION_PROCESS = BpmProcess.path("connector/GeocodingLocation"); + private static final BpmElement GEOCODING_LOCATION_BY_NAME = GEOCODING_LOCATION_PROCESS + .elementName("getCoordinatesByLocationName(String,String,String,Integer)"); + private static final BpmElement GEOCODING_LOCATION_BY_ZIP_CODE = GEOCODING_LOCATION_PROCESS + .elementName("getCoordinatesByZipCode(String,String)"); + private static final BpmElement GEOCODING_LOCATION_REVERSE = GEOCODING_LOCATION_PROCESS + .elementName("reverse(Double,Double,Integer)"); @Test public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE).execute("New York", StringUtils.EMPTY, StringUtils.EMPTY, 1); + ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute("New York", + StringUtils.EMPTY, StringUtils.EMPTY, 1); var object = result.data().last().get("results"); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -34,9 +39,8 @@ public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) t @Test() public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE) - .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); + bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute(StringUtils.EMPTY, StringUtils.EMPTY, + StringUtils.EMPTY, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -44,8 +48,8 @@ public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws N @Test public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute("10001", StringUtils.EMPTY); + ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute("10001", + StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(GeoLocation.class); } @@ -53,8 +57,7 @@ public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws No @Test() public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); + bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } @@ -62,8 +65,7 @@ public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws No @Test public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(40.7484, -73.9967, 1); + ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(40.7484, -73.9967, 1); var object = result.data().last().get("results"); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -74,10 +76,10 @@ public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws No @Test() public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); + bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(null, null, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(400); } } + } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java new file mode 100644 index 0000000..1d45f00 --- /dev/null +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java @@ -0,0 +1,96 @@ +package com.axonivy.connector.openweather.test.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; +import java.time.OffsetDateTime; + +import org.junit.jupiter.api.Test; +import org.openweathermap.api.data2_5.client.AirPollution; + +import ch.ivyteam.ivy.bpm.engine.client.BpmClient; +import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.error.BpmError; + +public class AirPollutionProcessTest extends BaseProcessTest { + private static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; + private static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; + private static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; + private static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; + + @Test + public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(AirPollution.class); + } + + @Test() + public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } + + @Test + public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) + throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(AirPollution.class); + } + + @Test() + public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) + throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } + + @Test + public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) + throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152, now, twoDaysLater); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(AirPollution.class); + } + + @Test() + public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient bpmClient) + throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } + + @Test() + public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient bpmClient) + throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, twoDaysLater, now); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } +} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java new file mode 100644 index 0000000..9877cf3 --- /dev/null +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java @@ -0,0 +1,69 @@ +package com.axonivy.connector.openweather.test.it; + +import java.io.IOException; +import java.util.Properties; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; + +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.bpm.exec.client.IvyProcessTest; +import ch.ivyteam.ivy.environment.AppFixture; +import ch.ivyteam.ivy.rest.client.RestClient; +import ch.ivyteam.ivy.rest.client.RestClients; + +@IvyProcessTest(enableWebServer = true) +public class BaseProcessTest { + private static final String APP_ID_KEY = "appId"; + 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 WEATHER_GEO_URL_KEY = "weatherGeoUrl"; + private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; + private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; + private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + private static String appId; + private static String weatherDataUrl; + private static String weatherGeoUrl; + + @BeforeEach + void beforeAll(AppFixture fixture, IApplication app) throws IOException { + setupConfig(fixture, app); + } + + private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri) { + RestClient client = clients.find(clientName); + client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appId).toRestClient(); + clients.set(client); + } + + protected SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, + String subProcessName) { + return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); + } + + static void setupConfig(AppFixture fixture, IApplication app) throws IOException { + appId = System.getProperty(APP_ID_KEY); + weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); + weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); + + // Local setup for testing + if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { + try (var in = BaseProcessTest.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { + if (in != null) { + Properties props = new Properties(); + props.load(in); + appId = (String) props.get(APP_ID_KEY); + weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); + weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); + } + } + } + + RestClients clients = RestClients.of(app); + setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl); + setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl); + } +} \ No newline at end of file diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java new file mode 100644 index 0000000..e516450 --- /dev/null +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java @@ -0,0 +1,35 @@ +package com.axonivy.connector.openweather.test.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.openweathermap.api.data2_5.client.Current; + +import ch.ivyteam.ivy.bpm.engine.client.BpmClient; +import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.error.BpmError; + +public class CurrentWeatherProcessTest extends BaseProcessTest { + private static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; + private static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; + + @Test + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, + GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(Current.class); + } + + @Test() + public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, 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(400); + } + } +} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java new file mode 100644 index 0000000..505c8d1 --- /dev/null +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java @@ -0,0 +1,36 @@ +package com.axonivy.connector.openweather.test.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.openweathermap.api.data2_5.client.Forecast; + +import ch.ivyteam.ivy.bpm.engine.client.BpmClient; +import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; +import ch.ivyteam.ivy.bpm.error.BpmError; + +public class ForecastWeatherProcessTest extends BaseProcessTest { + + private static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; + private static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + + @Test + public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, + GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(40.7127281, -74.0060152, 1, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(Forecast.class); + } + + @Test() + public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(null, null, 1, StringUtils.EMPTY, StringUtils.EMPTY); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } +} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java new file mode 100644 index 0000000..f2af5ea --- /dev/null +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java @@ -0,0 +1,85 @@ +package com.axonivy.connector.openweather.test.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.openweathermap.api.geo1_0.client.GeoLocation; + +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.environment.Ivy; + +public class GeocodingLocationProcessTest extends BaseProcessTest { + + private static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; + private static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; + private static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; + private static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + + @Test + public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE).execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); + var object = result.data().last().get("results"); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + Ivy.log().warn(objects.get(0).toString()); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } + + @Test() + public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } + + @Test + public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute("10001", StringUtils.EMPTY); + var object = result.data().last().get("result"); + assertThat(object).isInstanceOf(GeoLocation.class); + } + + @Test() + public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } + + @Test + public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { + ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(40.7484, -73.9967, 1); + var object = result.data().last().get("results"); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } + + @Test() + public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { + try { + getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(400); + } + } +} From ea1a2262c56370aad2bee031426c700f66ce84f6 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 15:20:03 +0700 Subject: [PATCH 04/14] remove result log from test case --- .../openweather/test/it/GeocodingLocationProcessTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java index f2af5ea..0b5bc21 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java @@ -12,7 +12,6 @@ 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.environment.Ivy; public class GeocodingLocationProcessTest extends BaseProcessTest { @@ -29,7 +28,6 @@ public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) t assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; assertThat(objects).isNotEmpty(); - Ivy.log().warn(objects.get(0).toString()); assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); } From 3ac46cfebfe89a65e189bc86204dc4cf31d4861f Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 15:41:13 +0700 Subject: [PATCH 05/14] remove had text from test case --- .../test/it/AirPollutionProcessTest.java | 24 ++++++++++--------- .../openweather/test/it/BaseProcessTest.java | 5 ++++ .../test/it/CurrentWeatherProcessTest.java | 7 +++--- .../test/it/ForecastWeatherProcessTest.java | 7 +++--- .../test/it/GeocodingLocationProcessTest.java | 17 ++++++------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java index 1d45f00..8d03a45 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java @@ -5,6 +5,7 @@ import java.time.Duration; import java.time.OffsetDateTime; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.AirPollution; @@ -21,8 +22,8 @@ public class AirPollutionProcessTest extends BaseProcessTest { @Test public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); - var object = result.data().last().get("result"); + GET_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); } @@ -32,7 +33,7 @@ public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } @@ -40,8 +41,8 @@ public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152); - var object = result.data().last().get("result"); + 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); } @@ -52,7 +53,7 @@ public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bp getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } @@ -62,8 +63,9 @@ public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(40.7127281, -74.0060152, now, twoDaysLater); - var object = result.data().last().get("result"); + 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); } @@ -76,7 +78,7 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(B getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } @@ -88,9 +90,9 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreTh try { getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, twoDaysLater, now); + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java index 9877cf3..3de064d 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java @@ -24,6 +24,11 @@ public class BaseProcessTest { private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + protected static final String RESULT_KEY = "result"; + protected static final String RESULTS_KEY = "results"; + protected static final Double TEST_LON_VALUE = 40.7484; + protected static final Double TEST_LAT_VALUE = -73.9967; + protected static final String TEST_ZIPCODE_VALUE = "10001"; private static String appId; private static String weatherDataUrl; private static String weatherGeoUrl; diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java index e516450..a7939e3 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Current; @@ -18,8 +19,8 @@ public class CurrentWeatherProcessTest extends BaseProcessTest { public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get("result"); + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(Current.class); } @@ -29,7 +30,7 @@ public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClien getSubProcessWithNameAndPath(bpmClient, 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(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java index 505c8d1..1a6fa3a 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Forecast; @@ -19,8 +20,8 @@ public class ForecastWeatherProcessTest extends BaseProcessTest { public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) - .execute(40.7127281, -74.0060152, 1, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get("result"); + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(Forecast.class); } @@ -30,7 +31,7 @@ public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) thr getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) .execute(null, null, 1, StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java index 0b5bc21..4e1a1bf 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java @@ -6,6 +6,7 @@ import java.util.List; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.openweathermap.api.geo1_0.client.GeoLocation; @@ -24,7 +25,7 @@ public class GeocodingLocationProcessTest extends BaseProcessTest { public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE).execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); - var object = result.data().last().get("results"); + var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; assertThat(objects).isNotEmpty(); @@ -38,15 +39,15 @@ public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws N GEOCODING_LOCATION_BY_NAME_SIGNATURE) .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } @Test public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute("10001", StringUtils.EMPTY); - var object = result.data().last().get("result"); + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(GeoLocation.class); } @@ -56,15 +57,15 @@ public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws No getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } @Test public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(40.7484, -73.9967, 1); - var object = result.data().last().get("results"); + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); + var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; assertThat(objects).isNotEmpty(); @@ -77,7 +78,7 @@ public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFie getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } } } From 9edec0e2d9d36a8df39734444c23c540257c73ca Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 3 Jan 2025 01:43:50 +0700 Subject: [PATCH 06/14] Handle feedback Unify @beforeEach to composition instead of inheritance hierarchy --- .settings/org.eclipse.core.resources.prefs | 2 + .../test/utils/OpenWeatherUtils.java} | 65 +++++++++---------- .../test/AirPollutionProcessTest.java | 13 +++- .../openweather/test/BaseProcessTest.java | 21 ------ .../test/CurrentWeatherProcessTest.java | 16 ++++- .../test/ForecastWeatherProcessTest.java | 17 ++++- .../test/GeocodingLocationProcessTest.java | 13 +++- .../test/it/AirPollutionProcessTest.java | 50 +++++++++----- .../test/it/CurrentWeatherProcessTest.java | 32 ++++++--- .../test/it/ForecastWeatherProcessTest.java | 29 +++++++-- .../test/it/GeocodingLocationProcessTest.java | 53 ++++++++++----- 11 files changed, 198 insertions(+), 113 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs rename open-weather-connector-test/{src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java => src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java} (53%) delete mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java similarity index 53% rename from open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java rename to open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index 3de064d..a2daf88 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/BaseProcessTest.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -1,62 +1,52 @@ -package com.axonivy.connector.openweather.test.it; +package com.axonivy.connector.openweather.test.utils; import java.io.IOException; import java.util.Properties; - import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.BeforeEach; - 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.bpm.exec.client.IvyProcessTest; import ch.ivyteam.ivy.environment.AppFixture; import ch.ivyteam.ivy.rest.client.RestClient; import ch.ivyteam.ivy.rest.client.RestClients; -@IvyProcessTest(enableWebServer = true) -public class BaseProcessTest { +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 WEATHER_GEO_URL_KEY = "weatherGeoUrl"; private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; + private static final String OPEN_WEATHER_CONNECTOR_PREFIX = "openWeatherConnector."; + private static final String JSON_FEATURES = "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"; private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; - protected static final String RESULT_KEY = "result"; - protected static final String RESULTS_KEY = "results"; - protected static final Double TEST_LON_VALUE = 40.7484; - protected static final Double TEST_LAT_VALUE = -73.9967; - protected static final String TEST_ZIPCODE_VALUE = "10001"; - private static String appId; - private static String weatherDataUrl; - private static String weatherGeoUrl; - @BeforeEach - void beforeAll(AppFixture fixture, IApplication app) throws IOException { - setupConfig(fixture, app); + 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, JSON_FEATURES); + fixture.config(REST_CLIENT_PREFIX + GEO_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, JSON_FEATURES); + fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_DATA_URL_KEY, "{ivy.app.baseurl}/api/weatherDataMock"); + fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_GEO_URL_KEY, "{ivy.app.baseurl}/api/weatherGeoMock"); } - private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri) { + private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri, + String appIdValue) { RestClient client = clients.find(clientName); - client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appId).toRestClient(); + client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appIdValue).toRestClient(); clients.set(client); } - protected SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, - String subProcessName) { - return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); - } - - static void setupConfig(AppFixture fixture, IApplication app) throws IOException { - appId = System.getProperty(APP_ID_KEY); - weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); - weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); + public static void setUpConfigForRestCallTest() throws IOException { + 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 = BaseProcessTest.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { + try (var in = OpenWeatherUtils.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { if (in != null) { Properties props = new Properties(); props.load(in); @@ -67,8 +57,13 @@ static void setupConfig(AppFixture fixture, IApplication app) throws IOException } } - RestClients clients = RestClients.of(app); - setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl); - setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl); + RestClients clients = RestClients.of(IApplication.current()); + setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl, appId); + setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl, appId); + } + + public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, + String subProcessName) { + return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); } -} \ No newline at end of file +} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 50f9208..3ae9920 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -5,16 +5,22 @@ import java.time.Duration; import java.time.OffsetDateTime; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.AirPollution; +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.engine.client.element.BpmElement; import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; +import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; +import ch.ivyteam.ivy.environment.AppFixture; -public class AirPollutionProcessTest extends BaseProcessTest { +@IvyProcessTest(enableWebServer = true) +public class AirPollutionProcessTest { private static final BpmProcess GET_AIR_POLLUTION_PROCESS = BpmProcess.path("connector/AirPollution"); private static final BpmElement GET_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS .elementName("getCurrentAirPollution(Double,Double)"); @@ -23,6 +29,11 @@ public class AirPollutionProcessTest extends BaseProcessTest { private static final BpmElement GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS .elementName("getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"); + @BeforeEach + void beforeEach(AppFixture fixture) { + OpenWeatherUtils.setUpConfigForMockServer(fixture); + } + @Test public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java deleted file mode 100644 index 3b019fe..0000000 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/BaseProcessTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.axonivy.connector.openweather.test; - -import org.junit.jupiter.api.BeforeEach; - -import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.AppFixture; - -@IvyProcessTest(enableWebServer = true) -public class BaseProcessTest { - - @BeforeEach - void beforeEach(AppFixture fixture) { - // Disable OAuth feature for mock rest service - fixture.config("RestClients.WeatherData (Openweathermap weather API).Features", - "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); - fixture.config("RestClients.GeocodingCoordinates (Openweathermap geocoding API).Features", - "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"); - fixture.var("openWeatherConnector.weatherDataUrl", "{ivy.app.baseurl}/api/weatherDataMock"); - fixture.var("openWeatherConnector.weatherGeoUrl", "{ivy.app.baseurl}/api/weatherGeoMock"); - } -} \ No newline at end of file diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 7326ea6..92d93ac 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -3,23 +3,33 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Current; +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.engine.client.element.BpmElement; import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; +import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; +import ch.ivyteam.ivy.environment.AppFixture; -public class CurrentWeatherProcessTest extends BaseProcessTest { +@IvyProcessTest(enableWebServer = true) +public class CurrentWeatherProcessTest { private static final BpmProcess GET_CURRENT_WEATHER_PROCESS = BpmProcess.path("connector/CurrentWeather"); private static final BpmElement GET_CURRENT_WEATHER_BY_GEOCODE = GET_CURRENT_WEATHER_PROCESS .elementName("getCurrentWeather(Double,Double,String,String)"); + @BeforeEach + void beforeEach(AppFixture fixture) { + OpenWeatherUtils.setUpConfigForMockServer(fixture); + } + @Test - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) - throws NoSuchFieldException { + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(40.7127281, -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 5cb324a..6cdf36a 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -3,25 +3,36 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Forecast; +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.engine.client.element.BpmElement; import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; +import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; +import ch.ivyteam.ivy.environment.AppFixture; -public class ForecastWeatherProcessTest extends BaseProcessTest { +@IvyProcessTest(enableWebServer = true) +public class ForecastWeatherProcessTest { private static final BpmProcess GET_FORECAST_PROCESS = BpmProcess.path("connector/ForecastWeather"); private static final BpmElement GET_FORECAST_BY_GEOCODE = GET_FORECAST_PROCESS .elementName("getForecastWeather(Double,Double,Integer,String,String)"); + @BeforeEach + void beforeEach(AppFixture fixture) { + OpenWeatherUtils.setUpConfigForMockServer(fixture); + } + @Test public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(40.7127281, -74.0060152, 1, - StringUtils.EMPTY, StringUtils.EMPTY); + ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(40.7127281, -74.0060152, + 1, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get("result"); assertThat(object).isInstanceOf(Forecast.class); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index 706d18d..8ad4bf7 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -6,16 +6,22 @@ import java.util.List; import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.geo1_0.client.GeoLocation; +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.engine.client.element.BpmElement; import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; +import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; +import ch.ivyteam.ivy.environment.AppFixture; -public class GeocodingLocationProcessTest extends BaseProcessTest { +@IvyProcessTest(enableWebServer = true) +public class GeocodingLocationProcessTest { private static final BpmProcess GEOCODING_LOCATION_PROCESS = BpmProcess.path("connector/GeocodingLocation"); private static final BpmElement GEOCODING_LOCATION_BY_NAME = GEOCODING_LOCATION_PROCESS @@ -25,6 +31,11 @@ public class GeocodingLocationProcessTest extends BaseProcessTest { private static final BpmElement GEOCODING_LOCATION_REVERSE = GEOCODING_LOCATION_PROCESS .elementName("reverse(Double,Double,Integer)"); + @BeforeEach + void beforeEach(AppFixture fixture) { + OpenWeatherUtils.setUpConfigForMockServer(fixture); + } + @Test public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute("New York", diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java index 8d03a45..4bbc29b 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java @@ -2,27 +2,42 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.io.IOException; import java.time.Duration; import java.time.OffsetDateTime; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.AirPollution; +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; + +@IvyProcessTest +public class AirPollutionProcessTest { + private final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; + private final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; + private final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; + private final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; + private final String RESULT_KEY = "result"; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; -public class AirPollutionProcessTest extends BaseProcessTest { - private static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; - private static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; - private static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; - private static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; + @BeforeEach + void beforeEach() throws IOException { + OpenWeatherUtils.setUpConfigForRestCallTest(); + } @Test public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE); + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, + GET_AIR_POLLUTION_PROCESS_PATH, GET_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); } @@ -30,7 +45,7 @@ public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient @Test() public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -40,8 +55,9 @@ public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) @Test public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE); + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, + 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); } @@ -50,7 +66,7 @@ public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient b public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, 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); @@ -62,8 +78,9 @@ public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, 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); @@ -75,7 +92,7 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(B OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -88,8 +105,9 @@ public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreTh OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java index a7939e3..597fc0e 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java @@ -1,24 +1,36 @@ package com.axonivy.connector.openweather.test.it; import static org.assertj.core.api.Assertions.assertThat; - +import java.io.IOException; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Current; - +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; -public class CurrentWeatherProcessTest extends BaseProcessTest { - private static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; - private static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; +@IvyProcessTest +public class CurrentWeatherProcessTest { + private final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; + private final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; + private final String RESULT_KEY = "result"; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + void beforeEach() throws IOException { + OpenWeatherUtils.setUpConfigForRestCallTest(); + } @Test public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, - GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE) + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, 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); @@ -27,8 +39,10 @@ public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmCl @Test() public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_CURRENT_WEATHER_PROCESS_PATH, - GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE).execute(null, null, StringUtils.EMPTY, StringUtils.EMPTY); + OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, 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); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java index 1a6fa3a..fb4faf0 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java @@ -2,24 +2,39 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.io.IOException; + import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.data2_5.client.Forecast; +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; -public class ForecastWeatherProcessTest extends BaseProcessTest { +@IvyProcessTest +public class ForecastWeatherProcessTest { - private static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; - private static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + private final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; + private final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + private final String RESULT_KEY = "result"; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + void beforeEach() throws IOException { + OpenWeatherUtils.setUpConfigForRestCallTest(); + } @Test public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, - GET_FORECAST_BY_GEOCODE_SIGNATURE) + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(Forecast.class); @@ -28,7 +43,9 @@ public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) @Test() public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, 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); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java index 4e1a1bf..e2b2155 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java @@ -1,30 +1,44 @@ package com.axonivy.connector.openweather.test.it; import static org.assertj.core.api.Assertions.assertThat; - +import java.io.IOException; import java.util.ArrayList; import java.util.List; - import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openweathermap.api.geo1_0.client.GeoLocation; - +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; -public class GeocodingLocationProcessTest extends BaseProcessTest { +@IvyProcessTest +public class GeocodingLocationProcessTest { - private static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; - private static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; - private static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; - private static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + private final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; + private final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; + private final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; + private final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + private final String RESULTS_KEY = "results"; + private final String RESULT_KEY = "result"; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + private final String TEST_ZIPCODE_VALUE = "10001"; + + @BeforeEach + void beforeEach() throws IOException { + OpenWeatherUtils.setUpConfigForRestCallTest(); + } @Test public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE).execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -35,8 +49,9 @@ public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) t @Test() public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE) + OpenWeatherUtils + .getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -45,8 +60,9 @@ public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws N @Test public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, + GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) + .execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(GeoLocation.class); } @@ -54,7 +70,7 @@ public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws No @Test() public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -63,8 +79,9 @@ public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws No @Test public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, + GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); var objects = (ArrayList) object; @@ -75,7 +92,7 @@ public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws No @Test() public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { try { - getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); From 278f77aeaee8926d2199138cca303017b0c65095 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Mon, 6 Jan 2025 16:34:53 +0700 Subject: [PATCH 07/14] Refactor test case with multiple env --- open-weather-connector-test/.classpath | 3 +- .../constant/OpenWeatherCommonConstants.java | 20 ++ .../CustomInvocationContextProvider.java | 20 ++ .../context/MockServerInvocationContext.java | 37 ++++ .../context/RestCallInvocationContext.java | 43 +++++ .../test/utils/OpenWeatherUtils.java | 126 ++++++++----- .../test/AirPollutionProcessTest.java | 175 +++++++++--------- .../test/CurrentWeatherProcessTest.java | 69 +++---- .../test/ForecastWeatherProcessTest.java | 70 +++---- .../test/GeocodingLocationProcessTest.java | 152 +++++++-------- .../test/it/AirPollutionProcessTest.java | 116 ------------ .../test/it/CurrentWeatherProcessTest.java | 50 ----- .../test/it/ForecastWeatherProcessTest.java | 54 ------ .../test/it/GeocodingLocationProcessTest.java | 101 ---------- 14 files changed, 440 insertions(+), 596 deletions(-) create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java create mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java delete mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java delete mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java delete mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java delete mode 100644 open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java diff --git a/open-weather-connector-test/.classpath b/open-weather-connector-test/.classpath index a7c4476..85832ee 100644 --- a/open-weather-connector-test/.classpath +++ b/open-weather-connector-test/.classpath @@ -5,7 +5,7 @@ - + @@ -31,5 +31,6 @@ + diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java new file mode 100644 index 0000000..44221b7 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java @@ -0,0 +1,20 @@ +package com.axonivy.connector.openweather.test.constant; + +public class OpenWeatherCommonConstants { + public static final String RESULT_KEY = "result"; + public static final String RESULTS_KEY = "results"; + public static final String REAL_CALL_CONTEXT_DISPLAY_NAME = "Real Server Test"; + public static final String MOCK_SERVER_CONTEXT_DISPLAY_NAME = "Mock Server Test"; + public static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; + public static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; + public static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; + public static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; + public static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + public static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; + public static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; + public static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; + public static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; + public static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + public static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; + public static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java new file mode 100644 index 0000000..d7b02f0 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java @@ -0,0 +1,20 @@ +package com.axonivy.connector.openweather.test.context; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestTemplateInvocationContext; +import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; + +public class CustomInvocationContextProvider implements TestTemplateInvocationContextProvider { + + @Override + public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { + return Stream.of(new MockServerInvocationContext(), new RestCallInvocationContext()); + } + + @Override + public boolean supportsTestTemplate(ExtensionContext context) { + return true; + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java new file mode 100644 index 0000000..7ad6370 --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java @@ -0,0 +1,37 @@ +package com.axonivy.connector.openweather.test.context; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.extension.Extension; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.jupiter.api.extension.TestTemplateInvocationContext; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME;; + +public class MockServerInvocationContext implements TestTemplateInvocationContext { + @Override + public String getDisplayName(int invocationIndex) { + return MOCK_SERVER_CONTEXT_DISPLAY_NAME; + } + + @Override + public List getAdditionalExtensions() { + return Collections.singletonList(new ParameterResolver() { + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return extensionContext; + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return true; + } + }); + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java new file mode 100644 index 0000000..c3172ca --- /dev/null +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java @@ -0,0 +1,43 @@ +package com.axonivy.connector.openweather.test.context; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.extension.Extension; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.jupiter.api.extension.TestTemplateInvocationContext; +import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME; + +public class RestCallInvocationContext implements TestTemplateInvocationContext { + + @Override + public String getDisplayName(int invocationIndex) { + return REAL_CALL_CONTEXT_DISPLAY_NAME; + } + + public void setUp() throws IOException { + OpenWeatherUtils.setUpConfigForRestCallTest(); + } + + @Override + public List getAdditionalExtensions() { + return Collections.singletonList(new ParameterResolver() { + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return extensionContext; + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return true; + } + }); + } +} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index a2daf88..44da410 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -3,67 +3,97 @@ import java.io.IOException; 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.ExecContext; 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."; - private static final String JSON_FEATURES = "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"; - private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; - private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + 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."; + private static final String JSON_FEATURES = "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"; + private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; + private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + 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"; + private static BpmClient testClient = new BpmClient(new ExecContext(IApplication.current())); - 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, JSON_FEATURES); - fixture.config(REST_CLIENT_PREFIX + GEO_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, JSON_FEATURES); - fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_DATA_URL_KEY, "{ivy.app.baseurl}/api/weatherDataMock"); - fixture.var(OPEN_WEATHER_CONNECTOR_PREFIX + WEATHER_GEO_URL_KEY, "{ivy.app.baseurl}/api/weatherGeoMock"); - } + public static BpmClient getTestBpmClient() { + return testClient; + } - private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri, - String appIdValue) { - RestClient client = clients.find(clientName); - client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appIdValue).toRestClient(); - clients.set(client); - } + @SuppressWarnings("restriction") + public static void setUpConfigForMockServer(ExtensionContext context) { + AppFixture fixture = AppFixtureJu5Context.get(context).getFixture(); + // Disable OAuth feature for mock rest service + fixture.config(REST_CLIENT_PREFIX + WEATHER_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, JSON_FEATURES); + fixture.config(REST_CLIENT_PREFIX + GEO_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, 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 setUpConfigForRestCallTest() throws IOException { - String appId = System.getProperty(APP_ID_KEY); - String weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); - String weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); + public static void setUpConfigForContext(ExtensionContext context) { + switch (context.getDisplayName()) { + case OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME: + setUpConfigForRestCallTest(); + break; + case OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME: + setUpConfigForMockServer(context); + break; + default: + break; + } + } - // Local setup for testing - if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { - try (var in = OpenWeatherUtils.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { - if (in != null) { - Properties props = new Properties(); - props.load(in); - appId = (String) props.get(APP_ID_KEY); - weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); - weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); - } - } - } + private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri, + String appIdValue) { + RestClient client = clients.find(clientName); + Ivy.log().warn("client == null: "+client == null); + client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appIdValue).toRestClient(); + clients.set(client); + } - RestClients clients = RestClients.of(IApplication.current()); - setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl, appId); - setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl, appId); - } + public static void setUpConfigForRestCallTest() { + String appId = System.getProperty(APP_ID_KEY); + String weatherDataUrl = System.getProperty(WEATHER_DATA_URL_KEY); + String weatherGeoUrl = System.getProperty(WEATHER_GEO_URL_KEY); - public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient bpmClient, String subProcessPath, - String subProcessName) { - return bpmClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); - } + + // Local setup for testing + if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { + try (var in = OpenWeatherUtils.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { + if (in != null) { + Properties props = new Properties(); + props.load(in); + appId = (String) props.get(APP_ID_KEY); + weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); + weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); + } + } catch (IOException e) { + Ivy.log().warn("Can't get credential from local file with path: {0}", LOCAL_CREDENTIALS_FILE_PATH); + } + } + + RestClients clients = RestClients.of(IApplication.current()); + setupClientWithNameAndUrl(clients, WEATHER_DATA_REST_CLIENT_NAME, weatherDataUrl, appId); + setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl, appId); + } + + public static SubRequestBuilder getSubProcessWithNameAndPath(String subProcessPath, String subProcessName) { + return testClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 3ae9920..6764c8b 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -4,106 +4,111 @@ import java.time.Duration; import java.time.OffsetDateTime; - +import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +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.utils.OpenWeatherUtils; - -import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.AppFixture; +import ch.ivyteam.ivy.environment.Ivy; + +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; @IvyProcessTest(enableWebServer = true) +@ExtendWith(CustomInvocationContextProvider.class) public class AirPollutionProcessTest { - private static final BpmProcess GET_AIR_POLLUTION_PROCESS = BpmProcess.path("connector/AirPollution"); - private static final BpmElement GET_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getCurrentAirPollution(Double,Double)"); - private static final BpmElement GET_FORECAST_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getForecastAirPollution(Double,Double)"); - private static final BpmElement GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE = GET_AIR_POLLUTION_PROCESS - .elementName("getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"); + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; - @BeforeEach - void beforeEach(AppFixture fixture) { - OpenWeatherUtils.setUpConfigForMockServer(fixture); - } + @BeforeEach + void beforeEach(ExtensionContext context) { + OpenWeatherUtils.setUpConfigForContext(context); + } - @Test - public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, - -74.0060152); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(AirPollution.class); - } + @TestTemplate + void testGetAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } - @Test() - public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GET_AIR_POLLUTION_BY_GEOCODE).execute(null, null); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + void testGetAirPollutionByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .execute(null, null); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @Test - public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) - throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, - -74.0060152); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(AirPollution.class); - } + @TestTemplate + void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } - @Test() - public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) - throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GET_FORECAST_AIR_POLLUTION_BY_GEOCODE).execute(null, null); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + void testGetForecastAirPollutionByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } + } - @Test - public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - ExecutionResult result = bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE) - .execute(40.7127281, -74.0060152, now, twoDaysLater); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(AirPollution.class); - } + @TestTemplate + void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } - @Test() - public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(null, null, now, - twoDaysLater); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo() throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @Test() - public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - bpmClient.start().subProcess(GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE).execute(40.7127281, -74.0060152, - twoDaysLater, now); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd() throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 92d93ac..d226e90 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -1,48 +1,51 @@ package com.axonivy.connector.openweather.test; import static org.assertj.core.api.Assertions.assertThat; - import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +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.utils.OpenWeatherUtils; - -import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.AppFixture; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE; @IvyProcessTest(enableWebServer = true) +@ExtendWith(CustomInvocationContextProvider.class) public class CurrentWeatherProcessTest { - private static final BpmProcess GET_CURRENT_WEATHER_PROCESS = BpmProcess.path("connector/CurrentWeather"); - private static final BpmElement GET_CURRENT_WEATHER_BY_GEOCODE = GET_CURRENT_WEATHER_PROCESS - .elementName("getCurrentWeather(Double,Double,String,String)"); - - @BeforeEach - void beforeEach(AppFixture fixture) { - OpenWeatherUtils.setUpConfigForMockServer(fixture); - } - - @Test - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(40.7127281, - -74.0060152, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(Current.class); - } - @Test() - public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GET_CURRENT_WEATHER_BY_GEOCODE).execute(null, null, StringUtils.EMPTY, - StringUtils.EMPTY); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + void beforeEach(ExtensionContext context) { + OpenWeatherUtils.setUpConfigForContext(context); + } + + @TestTemplate + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } + + @TestTemplate + public void testGetCurrentWeatherByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(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); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 6cdf36a..f43c708 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -1,49 +1,49 @@ package com.axonivy.connector.openweather.test; import static org.assertj.core.api.Assertions.assertThat; - import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +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.utils.OpenWeatherUtils; - -import ch.ivyteam.ivy.bpm.engine.client.BpmClient; import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.AppFixture; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; @IvyProcessTest(enableWebServer = true) +@ExtendWith(CustomInvocationContextProvider.class) public class ForecastWeatherProcessTest { - - private static final BpmProcess GET_FORECAST_PROCESS = BpmProcess.path("connector/ForecastWeather"); - private static final BpmElement GET_FORECAST_BY_GEOCODE = GET_FORECAST_PROCESS - .elementName("getForecastWeather(Double,Double,Integer,String,String)"); - - @BeforeEach - void beforeEach(AppFixture fixture) { - OpenWeatherUtils.setUpConfigForMockServer(fixture); - } - - @Test - public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(40.7127281, -74.0060152, - 1, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(Forecast.class); - } - - @Test() - public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GET_FORECAST_BY_GEOCODE).execute(null, null, 1, StringUtils.EMPTY, - StringUtils.EMPTY); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + void beforeEach(ExtensionContext context) { + OpenWeatherUtils.setUpConfigForContext(context); + } + + @TestTemplate + public void testGetForecastWeatherByGeoCode_ReturnsForecast() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(Forecast.class); + } + + @TestTemplate + public void testGetForecastByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(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); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index 8ad4bf7..a0d1ce0 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -1,96 +1,102 @@ package com.axonivy.connector.openweather.test; import static org.assertj.core.api.Assertions.assertThat; - import java.util.ArrayList; import java.util.List; - import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; import org.openweathermap.api.geo1_0.client.GeoLocation; - +import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; 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.engine.client.element.BpmElement; -import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import ch.ivyteam.ivy.environment.AppFixture; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_NAME_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_REVERSE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULTS_KEY; @IvyProcessTest(enableWebServer = true) +@ExtendWith(CustomInvocationContextProvider.class) public class GeocodingLocationProcessTest { + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + private final String TEST_ZIPCODE_VALUE = "10001"; - private static final BpmProcess GEOCODING_LOCATION_PROCESS = BpmProcess.path("connector/GeocodingLocation"); - private static final BpmElement GEOCODING_LOCATION_BY_NAME = GEOCODING_LOCATION_PROCESS - .elementName("getCoordinatesByLocationName(String,String,String,Integer)"); - private static final BpmElement GEOCODING_LOCATION_BY_ZIP_CODE = GEOCODING_LOCATION_PROCESS - .elementName("getCoordinatesByZipCode(String,String)"); - private static final BpmElement GEOCODING_LOCATION_REVERSE = GEOCODING_LOCATION_PROCESS - .elementName("reverse(Double,Double,Integer)"); - - @BeforeEach - void beforeEach(AppFixture fixture) { - OpenWeatherUtils.setUpConfigForMockServer(fixture); - } - - @Test - public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute("New York", - StringUtils.EMPTY, StringUtils.EMPTY, 1); - var object = result.data().last().get("results"); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } + @BeforeEach + void beforeEach(ExtensionContext context) { + OpenWeatherUtils.setUpConfigForContext(context); + } - @Test() - public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GEOCODING_LOCATION_BY_NAME).execute(StringUtils.EMPTY, StringUtils.EMPTY, - StringUtils.EMPTY, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + public void testGeocodingByName_ReturnsListOfGeoLocations() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); + var object = result.data().last().get(RESULTS_KEY); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } - @Test - public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute("10001", - StringUtils.EMPTY); - var object = result.data().last().get("result"); - assertThat(object).isInstanceOf(GeoLocation.class); - } + @TestTemplate + public void testGeocodingByName_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @Test() - public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GEOCODING_LOCATION_BY_ZIP_CODE).execute(StringUtils.EMPTY, StringUtils.EMPTY); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + public void testGeocodingByZip_ReturnsGeoLocation() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) + .execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(GeoLocation.class); + } - @Test - public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(40.7484, -73.9967, 1); - var object = result.data().last().get("results"); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } + @TestTemplate + public void testGeocodingByZip_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) + .execute(StringUtils.EMPTY, StringUtils.EMPTY); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @Test() - public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - bpmClient.start().subProcess(GEOCODING_LOCATION_REVERSE).execute(null, null, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(400); - } - } + @TestTemplate + public void testReverse_ReturnsListOfGeoLocations() throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); + var object = result.data().last().get(RESULTS_KEY); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } + @TestTemplate + public void testReverse_ThrowsBpmException() throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) + .execute(null, null, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java deleted file mode 100644 index 4bbc29b..0000000 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/AirPollutionProcessTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.axonivy.connector.openweather.test.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; -import java.time.Duration; -import java.time.OffsetDateTime; - -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openweathermap.api.data2_5.client.AirPollution; - -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; - -@IvyProcessTest -public class AirPollutionProcessTest { - private final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; - private final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; - private final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; - private final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; - private final String RESULT_KEY = "result"; - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; - - @BeforeEach - void beforeEach() throws IOException { - OpenWeatherUtils.setUpConfigForRestCallTest(); - } - - @Test - public void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, - GET_AIR_POLLUTION_PROCESS_PATH, GET_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); - } - - @Test() - public void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } - - @Test - public void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) - throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, - 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); - } - - @Test() - public void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient bpmClient) - throws NoSuchFieldException { - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, 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); - } - } - - @Test - public void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, 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); - } - - @Test() - public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } - - @Test() - public void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient bpmClient) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } -} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java deleted file mode 100644 index 597fc0e..0000000 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/CurrentWeatherProcessTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.axonivy.connector.openweather.test.it; - -import static org.assertj.core.api.Assertions.assertThat; -import java.io.IOException; -import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openweathermap.api.data2_5.client.Current; -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; - -@IvyProcessTest -public class CurrentWeatherProcessTest { - private final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; - private final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; - private final String RESULT_KEY = "result"; - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; - - @BeforeEach - void beforeEach() throws IOException { - OpenWeatherUtils.setUpConfigForRestCallTest(); - } - - @Test - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, 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); - } - - @Test() - public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, 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); - } - } -} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java deleted file mode 100644 index fb4faf0..0000000 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/ForecastWeatherProcessTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.axonivy.connector.openweather.test.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; - -import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openweathermap.api.data2_5.client.Forecast; - -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; - -@IvyProcessTest -public class ForecastWeatherProcessTest { - - private final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; - private final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; - private final String RESULT_KEY = "result"; - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; - - @BeforeEach - void beforeEach() throws IOException { - OpenWeatherUtils.setUpConfigForRestCallTest(); - } - - @Test - public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(Forecast.class); - } - - @Test() - public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, 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); - } - } -} diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java deleted file mode 100644 index e2b2155..0000000 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/it/GeocodingLocationProcessTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.axonivy.connector.openweather.test.it; - -import static org.assertj.core.api.Assertions.assertThat; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpStatus; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openweathermap.api.geo1_0.client.GeoLocation; -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; - -@IvyProcessTest -public class GeocodingLocationProcessTest { - - private final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; - private final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; - private final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; - private final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; - private final String RESULTS_KEY = "results"; - private final String RESULT_KEY = "result"; - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; - private final String TEST_ZIPCODE_VALUE = "10001"; - - @BeforeEach - void beforeEach() throws IOException { - OpenWeatherUtils.setUpConfigForRestCallTest(); - } - - @Test - public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE) - .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); - var object = result.data().last().get(RESULTS_KEY); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } - - @Test() - public void testGeocodingByName_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_NAME_SIGNATURE) - .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } - - @Test - public void testGeocodingByZip_ReturnsGeoLocation(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, - GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) - .execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(GeoLocation.class); - } - - @Test() - public void testGeocodingByZip_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } - - @Test - public void testReverse_ReturnsListOfGeoLocations(BpmClient bpmClient) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, - GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); - var object = result.data().last().get(RESULTS_KEY); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } - - @Test() - public void testReverse_ThrowsBpmException(BpmClient bpmClient) throws NoSuchFieldException { - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(bpmClient, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } -} From 9802bcc616240db79c18b1e7420a0f8251030341 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Mon, 6 Jan 2025 17:11:26 +0700 Subject: [PATCH 08/14] Fix failed in compiling --- open-weather-connector-test/.classpath | 1 - open-weather-connector-test/pom.xml | 1 - .../context/MockServerInvocationContext.java | 5 +++-- .../test/context/RestCallInvocationContext.java | 5 ++++- .../openweather/test/utils/OpenWeatherUtils.java | 3 +++ .../test/AirPollutionProcessTest.java | 14 ++++++++------ .../test/CurrentWeatherProcessTest.java | 9 ++++++--- .../test/ForecastWeatherProcessTest.java | 9 ++++++--- .../test/GeocodingLocationProcessTest.java | 16 ++++++++++------ 9 files changed, 40 insertions(+), 23 deletions(-) diff --git a/open-weather-connector-test/.classpath b/open-weather-connector-test/.classpath index 85832ee..106dc80 100644 --- a/open-weather-connector-test/.classpath +++ b/open-weather-connector-test/.classpath @@ -31,6 +31,5 @@ - diff --git a/open-weather-connector-test/pom.xml b/open-weather-connector-test/pom.xml index f3beb3a..5280ecd 100644 --- a/open-weather-connector-test/pom.xml +++ b/open-weather-connector-test/pom.xml @@ -39,7 +39,6 @@ com.axonivy.ivy.test unit-tester ${tester.version} - test diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java index 7ad6370..0616fb6 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java @@ -1,5 +1,7 @@ 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; @@ -8,8 +10,7 @@ import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME;; +import org.junit.jupiter.api.extension.TestTemplateInvocationContext;; public class MockServerInvocationContext implements TestTemplateInvocationContext { @Override diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java index c3172ca..4ae87f9 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java @@ -1,16 +1,19 @@ package com.axonivy.connector.openweather.test.context; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME; + import java.io.IOException; import java.util.Collections; import java.util.List; + import org.junit.jupiter.api.extension.Extension; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.jupiter.api.extension.TestTemplateInvocationContext; + import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME; public class RestCallInvocationContext implements TestTemplateInvocationContext { diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index 44da410..7adc369 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -2,9 +2,12 @@ import java.io.IOException; 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.ExecContext; diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 6764c8b..fc19817 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -1,28 +1,30 @@ package com.axonivy.connector.openweather.test; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; import static org.assertj.core.api.Assertions.assertThat; import java.time.Duration; import java.time.OffsetDateTime; + import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; 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.utils.OpenWeatherUtils; + 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 static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_PROCESS_PATH; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; - @IvyProcessTest(enableWebServer = true) @ExtendWith(CustomInvocationContextProvider.class) public class AirPollutionProcessTest { diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index d226e90..fad6a97 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -1,6 +1,10 @@ package com.axonivy.connector.openweather.test; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; import static org.assertj.core.api.Assertions.assertThat; + import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; @@ -8,14 +12,13 @@ import org.junit.jupiter.api.extension.ExtendWith; 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.utils.OpenWeatherUtils; + import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_PROCESS_PATH; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE; @IvyProcessTest(enableWebServer = true) @ExtendWith(CustomInvocationContextProvider.class) diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index f43c708..4bb5cdb 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -1,6 +1,10 @@ package com.axonivy.connector.openweather.test; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_BY_GEOCODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; import static org.assertj.core.api.Assertions.assertThat; + import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; @@ -8,14 +12,13 @@ import org.junit.jupiter.api.extension.ExtendWith; 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.utils.OpenWeatherUtils; + import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_PROCESS_PATH; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GET_FORECAST_BY_GEOCODE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; @IvyProcessTest(enableWebServer = true) @ExtendWith(CustomInvocationContextProvider.class) diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index a0d1ce0..ce6c3f7 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -1,8 +1,16 @@ package com.axonivy.connector.openweather.test; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_NAME_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_PROCESS_PATH; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_REVERSE_SIGNATURE; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULTS_KEY; +import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; import static org.assertj.core.api.Assertions.assertThat; + import java.util.ArrayList; import java.util.List; + import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; @@ -10,17 +18,13 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; import org.openweathermap.api.geo1_0.client.GeoLocation; + import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils; + import ch.ivyteam.ivy.bpm.engine.client.ExecutionResult; import ch.ivyteam.ivy.bpm.error.BpmError; import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_PROCESS_PATH; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_NAME_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.GEOCODING_LOCATION_REVERSE_SIGNATURE; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULT_KEY; -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.RESULTS_KEY; @IvyProcessTest(enableWebServer = true) @ExtendWith(CustomInvocationContextProvider.class) From ed0d10b198fd3a9d644912983153a662abc27c37 Mon Sep 17 00:00:00 2001 From: Reguel Wermelinger Date: Mon, 6 Jan 2025 13:52:10 +0100 Subject: [PATCH 09/14] fix: app.yaml config corrupt in test, due to feature setup --- .../connector/openweather/test/utils/OpenWeatherUtils.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index 7adc369..a15634c 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -1,6 +1,7 @@ package com.axonivy.connector.openweather.test.utils; import java.io.IOException; +import java.util.List; import java.util.Properties; import org.apache.commons.lang3.StringUtils; @@ -43,8 +44,8 @@ public static BpmClient getTestBpmClient() { public static void setUpConfigForMockServer(ExtensionContext context) { AppFixture fixture = AppFixtureJu5Context.get(context).getFixture(); // Disable OAuth feature for mock rest service - fixture.config(REST_CLIENT_PREFIX + WEATHER_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, JSON_FEATURES); - fixture.config(REST_CLIENT_PREFIX + GEO_DATA_REST_CLIENT_NAME + FEATURE_SUFFIX, JSON_FEATURES); + 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); } @@ -65,7 +66,7 @@ public static void setUpConfigForContext(ExtensionContext context) { private static void setupClientWithNameAndUrl(RestClients clients, String clientName, String clientDefaultUri, String appIdValue) { RestClient client = clients.find(clientName); - Ivy.log().warn("client == null: "+client == null); + System.out.println("client == null: "+client == null); client = client.toBuilder().uri(clientDefaultUri).property(APP_ID_PROP_KEY, appIdValue).toRestClient(); clients.set(client); } From 015c8d013db353c41fa7d1f9fdae3516e361cbf9 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Mon, 6 Jan 2025 23:16:21 +0700 Subject: [PATCH 10/14] Update parameter resolver --- .../context/MockServerInvocationContext.java | 4 +-- .../context/RestCallInvocationContext.java | 2 +- .../test/utils/OpenWeatherUtils.java | 10 ++----- .../test/AirPollutionProcessTest.java | 29 ++++++++++--------- .../test/CurrentWeatherProcessTest.java | 9 +++--- .../test/ForecastWeatherProcessTest.java | 9 +++--- .../test/GeocodingLocationProcessTest.java | 25 ++++++++-------- 7 files changed, 43 insertions(+), 45 deletions(-) diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java index 0616fb6..8b4998e 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java @@ -30,8 +30,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return true; + throws ParameterResolutionException { + return ExtensionContext.class == parameterContext.getParameter().getType(); } }); } diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java index 4ae87f9..495ace4 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java @@ -39,7 +39,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { - return true; + return ExtensionContext.class == parameterContext.getParameter().getType(); } }); } diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index a15634c..f3be313 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -11,7 +11,6 @@ import ch.ivyteam.ivy.application.IApplication; import ch.ivyteam.ivy.bpm.engine.client.BpmClient; -import ch.ivyteam.ivy.bpm.engine.client.ExecContext; import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess; import ch.ivyteam.ivy.bpm.engine.client.sub.SubRequestBuilder; import ch.ivyteam.ivy.environment.AppFixture; @@ -34,11 +33,6 @@ public class OpenWeatherUtils { private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; 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"; - private static BpmClient testClient = new BpmClient(new ExecContext(IApplication.current())); - - public static BpmClient getTestBpmClient() { - return testClient; - } @SuppressWarnings("restriction") public static void setUpConfigForMockServer(ExtensionContext context) { @@ -97,7 +91,7 @@ public static void setUpConfigForRestCallTest() { setupClientWithNameAndUrl(clients, GEO_DATA_REST_CLIENT_NAME, weatherGeoUrl, appId); } - public static SubRequestBuilder getSubProcessWithNameAndPath(String subProcessPath, String subProcessName) { - return testClient.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); + public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient client,String subProcessPath, String subProcessName) { + return client.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index fc19817..bc72004 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -20,6 +20,7 @@ import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; 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; @@ -37,9 +38,9 @@ void beforeEach(ExtensionContext context) { } @TestTemplate - void testGetAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .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); @@ -47,10 +48,10 @@ void testGetAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldExcept } @TestTemplate - void testGetAirPollutionByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) .execute(null, null); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -58,19 +59,19 @@ void testGetAirPollutionByGeoCode_ThrowsBpmException() throws NoSuchFieldExcepti } @TestTemplate - void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .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() throws NoSuchFieldException { + void testGetForecastAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .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); @@ -78,22 +79,22 @@ void testGetForecastAirPollutionByGeoCode_ThrowsBpmException() throws NoSuchFiel } @TestTemplate - void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution() throws NoSuchFieldException { + void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(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() throws NoSuchFieldException { + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient client) throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { - OpenWeatherUtils.getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -101,12 +102,12 @@ void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo() throws } @TestTemplate - void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd() throws NoSuchFieldException { + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient client) throws NoSuchFieldException { OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_AIR_POLLUTION_PROCESS_PATH, + .getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); } catch (BpmError e) { diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index fad6a97..3879dd8 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -16,6 +16,7 @@ import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; 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; @@ -33,19 +34,19 @@ void beforeEach(ExtensionContext context) { } @TestTemplate - public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather() throws NoSuchFieldException { + public void testGetCurrentWeatherByGeoCode_ReturnsCurrentWeather(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(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); } @TestTemplate - public void testGetCurrentWeatherByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + public void testGetCurrentWeatherByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(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(null, null, StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 4bb5cdb..0de43c0 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -16,6 +16,7 @@ import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; 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; @@ -32,18 +33,18 @@ void beforeEach(ExtensionContext context) { } @TestTemplate - public void testGetForecastWeatherByGeoCode_ReturnsForecast() throws NoSuchFieldException { + public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(Forecast.class); } @TestTemplate - public void testGetForecastByGeoCode_ThrowsBpmException() throws NoSuchFieldException { + public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { - OpenWeatherUtils.getSubProcessWithNameAndPath(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); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index ce6c3f7..c53e1e4 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -22,6 +22,7 @@ import com.axonivy.connector.openweather.test.context.CustomInvocationContextProvider; 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; @@ -39,9 +40,9 @@ void beforeEach(ExtensionContext context) { } @TestTemplate - public void testGeocodingByName_ReturnsListOfGeoLocations() throws NoSuchFieldException { + public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); @@ -51,10 +52,10 @@ public void testGeocodingByName_ReturnsListOfGeoLocations() throws NoSuchFieldEx } @TestTemplate - public void testGeocodingByName_ThrowsBpmException() throws NoSuchFieldException { + public void testGeocodingByName_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -62,19 +63,19 @@ public void testGeocodingByName_ThrowsBpmException() throws NoSuchFieldException } @TestTemplate - public void testGeocodingByZip_ReturnsGeoLocation() throws NoSuchFieldException { + public void testGeocodingByZip_ReturnsGeoLocation(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) .execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); var object = result.data().last().get(RESULT_KEY); assertThat(object).isInstanceOf(GeoLocation.class); } @TestTemplate - public void testGeocodingByZip_ThrowsBpmException() throws NoSuchFieldException { + public void testGeocodingByZip_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) .execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); @@ -82,9 +83,9 @@ public void testGeocodingByZip_ThrowsBpmException() throws NoSuchFieldException } @TestTemplate - public void testReverse_ReturnsListOfGeoLocations() throws NoSuchFieldException { + public void testReverse_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); var object = result.data().last().get(RESULTS_KEY); assertThat(object).isInstanceOf(List.class); @@ -94,10 +95,10 @@ public void testReverse_ReturnsListOfGeoLocations() throws NoSuchFieldException } @TestTemplate - public void testReverse_ThrowsBpmException() throws NoSuchFieldException { + public void testReverse_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { OpenWeatherUtils - .getSubProcessWithNameAndPath(GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) .execute(null, null, 1); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); From 62989267734a7a6dadf67c4e4c2d0db3234c1717 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Tue, 7 Jan 2025 14:59:17 +0700 Subject: [PATCH 11/14] refactor test code - Create generic TestTemplateInvocationContext - Only change var when env change --- ...a => MultiEnvironmentContextProvider.java} | 7 ++- .../context/RestCallInvocationContext.java | 46 ------------------- ...a => TestEnironmentInvocationContext.java} | 13 ++++-- .../test/utils/OpenWeatherUtils.java | 39 +++++----------- .../test/AirPollutionProcessTest.java | 30 ++++++------ .../test/CurrentWeatherProcessTest.java | 17 +++---- .../test/ForecastWeatherProcessTest.java | 12 +++-- .../test/GeocodingLocationProcessTest.java | 14 +++--- 8 files changed, 64 insertions(+), 114 deletions(-) rename open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/{CustomInvocationContextProvider.java => MultiEnvironmentContextProvider.java} (60%) delete mode 100644 open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java rename open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/{MockServerInvocationContext.java => TestEnironmentInvocationContext.java} (79%) diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java similarity index 60% rename from open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java rename to open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java index d7b02f0..1cf1116 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/CustomInvocationContextProvider.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java @@ -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 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 diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java deleted file mode 100644 index 495ace4..0000000 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/RestCallInvocationContext.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.axonivy.connector.openweather.test.context; - -import static com.axonivy.connector.openweather.test.constant.OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME; - -import java.io.IOException; -import java.util.Collections; -import java.util.List; - -import org.junit.jupiter.api.extension.Extension; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.jupiter.api.extension.ParameterResolutionException; -import org.junit.jupiter.api.extension.ParameterResolver; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; - -import com.axonivy.connector.openweather.test.utils.OpenWeatherUtils; - -public class RestCallInvocationContext implements TestTemplateInvocationContext { - - @Override - public String getDisplayName(int invocationIndex) { - return REAL_CALL_CONTEXT_DISPLAY_NAME; - } - - public void setUp() throws IOException { - OpenWeatherUtils.setUpConfigForRestCallTest(); - } - - @Override - public List getAdditionalExtensions() { - return Collections.singletonList(new ParameterResolver() { - - @Override - public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return extensionContext; - } - - @Override - public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return ExtensionContext.class == parameterContext.getParameter().getType(); - } - }); - } -} diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java similarity index 79% rename from open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java rename to open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java index 8b4998e..8765572 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MockServerInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java @@ -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; @@ -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 diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index f3be313..0ecc946 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -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."; @@ -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)) { @@ -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)); } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index bc72004..58ac535 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -17,24 +17,24 @@ 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 @@ -42,7 +42,6 @@ void testGetAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws N 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); } @@ -60,9 +59,8 @@ 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); } @@ -70,9 +68,8 @@ void testGetForecastAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) @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); } @@ -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 { @@ -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 { diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 3879dd8..6fa79ab 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -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); @@ -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); } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 0de43c0..001bec7 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -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 @@ -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); diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index c53e1e4..fe07371 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -19,24 +19,25 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.openweathermap.api.geo1_0.client.GeoLocation; -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 GeocodingLocationProcessTest { private final Double TEST_LON_VALUE = 40.7484; private final Double TEST_LAT_VALUE = -73.9967; private final String TEST_ZIPCODE_VALUE = "10001"; @BeforeEach - void beforeEach(ExtensionContext context) { - OpenWeatherUtils.setUpConfigForContext(context); + void beforeEach(ExtensionContext context, AppFixture fixture) { + OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); } @TestTemplate @@ -74,9 +75,8 @@ public void testGeocodingByZip_ReturnsGeoLocation(BpmClient client) throws NoSuc @TestTemplate public void testGeocodingByZip_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) - .execute(StringUtils.EMPTY, StringUtils.EMPTY); + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); } catch (BpmError e) { assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); } From a0b14f98f19ff5b65335fd4fdd8ab52a86446bc3 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Tue, 7 Jan 2025 15:09:10 +0700 Subject: [PATCH 12/14] return default variable value + format code --- open-weather-connector-demo/pom.xml | 2 +- open-weather-connector-test/pom.xml | 2 +- open-weather-connector/config/variables.yaml | 6 +++--- open-weather-connector/pom.xml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/open-weather-connector-demo/pom.xml b/open-weather-connector-demo/pom.xml index 438e438..fc635b5 100644 --- a/open-weather-connector-demo/pom.xml +++ b/open-weather-connector-demo/pom.xml @@ -7,7 +7,7 @@ iar 12.0.0 - UTF-8 + UTF-8 diff --git a/open-weather-connector-test/pom.xml b/open-weather-connector-test/pom.xml index 5280ecd..2962f9b 100644 --- a/open-weather-connector-test/pom.xml +++ b/open-weather-connector-test/pom.xml @@ -7,7 +7,7 @@ iar 12.0.0 - UTF-8 + UTF-8 12.0.0 diff --git a/open-weather-connector/config/variables.yaml b/open-weather-connector/config/variables.yaml index a6aa737..2601806 100644 --- a/open-weather-connector/config/variables.yaml +++ b/open-weather-connector/config/variables.yaml @@ -2,8 +2,8 @@ Variables: openWeatherConnector: # Open Weather Data endpoint - weatherDataUrl: "" + weatherDataUrl: 'https://api.openweathermap.org/data/2.5' # Open Weather Geo endpoint - weatherGeoUrl: "" + weatherGeoUrl: 'https://api.openweathermap.org/geo/1.0' # Your Open Weather connector Application Client ID - appId: "" + appId: '' diff --git a/open-weather-connector/pom.xml b/open-weather-connector/pom.xml index 0483585..f6e37d6 100644 --- a/open-weather-connector/pom.xml +++ b/open-weather-connector/pom.xml @@ -7,7 +7,7 @@ iar 12.0.0 - UTF-8 + UTF-8 From 57e86509cf150444f3d9e7da36cc0cd0f57dea80 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Wed, 8 Jan 2025 14:11:53 +0700 Subject: [PATCH 13/14] format code + handle feedback --- .../constant/OpenWeatherCommonConstants.java | 32 ++-- .../MultiEnvironmentContextProvider.java | 18 +-- .../TestEnironmentInvocationContext.java | 58 +++---- .../test/AirPollutionProcessTest.java | 152 +++++++++--------- .../test/CurrentWeatherProcessTest.java | 56 +++---- .../test/ForecastWeatherProcessTest.java | 54 +++---- .../test/GeocodingLocationProcessTest.java | 131 ++++++++------- 7 files changed, 250 insertions(+), 251 deletions(-) diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java index 44221b7..0510b1a 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/constant/OpenWeatherCommonConstants.java @@ -1,20 +1,20 @@ package com.axonivy.connector.openweather.test.constant; public class OpenWeatherCommonConstants { - public static final String RESULT_KEY = "result"; - public static final String RESULTS_KEY = "results"; - public static final String REAL_CALL_CONTEXT_DISPLAY_NAME = "Real Server Test"; - public static final String MOCK_SERVER_CONTEXT_DISPLAY_NAME = "Mock Server Test"; - public static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; - public static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; - public static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; - public static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; - public static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; - public static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; - public static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; - public static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; - public static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; - public static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; - public static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; - public static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; + public static final String RESULT_KEY = "result"; + public static final String RESULTS_KEY = "results"; + public static final String REAL_CALL_CONTEXT_DISPLAY_NAME = "Real Server Test"; + public static final String MOCK_SERVER_CONTEXT_DISPLAY_NAME = "Mock Server Test"; + public static final String GET_FORECAST_PROCESS_PATH = "connector/ForecastWeather"; + public static final String GET_AIR_POLLUTION_PROCESS_PATH = "connector/AirPollution"; + public static final String GET_CURRENT_WEATHER_PROCESS_PATH = "connector/CurrentWeather"; + public static final String GEOCODING_LOCATION_PROCESS_PATH = "connector/GeocodingLocation"; + public static final String GEOCODING_LOCATION_REVERSE_SIGNATURE = "reverse(Double,Double,Integer)"; + public static final String GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getCurrentAirPollution(Double,Double)"; + public static final String GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE = "getCoordinatesByZipCode(String,String)"; + public static final String GET_FORECAST_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getForecastAirPollution(Double,Double)"; + public static final String GET_CURRENT_WEATHER_BY_GEOCODE_SIGNATURE = "getCurrentWeather(Double,Double,String,String)"; + public static final String GET_FORECAST_BY_GEOCODE_SIGNATURE = "getForecastWeather(Double,Double,Integer,String,String)"; + public static final String GEOCODING_LOCATION_BY_NAME_SIGNATURE = "getCoordinatesByLocationName(String,String,String,Integer)"; + public static final String GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE = "getHistoricalAirPollution(Double,Double,OffsetDateTime,OffsetDateTime)"; } diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java index 1cf1116..8c91037 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/MultiEnvironmentContextProvider.java @@ -10,14 +10,14 @@ public class MultiEnvironmentContextProvider implements TestTemplateInvocationContextProvider { - @Override - public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { - return Stream.of(new TestEnironmentInvocationContext(OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME), - new TestEnironmentInvocationContext(OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME)); - } + @Override + public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { + return Stream.of(new TestEnironmentInvocationContext(OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME), + new TestEnironmentInvocationContext(OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME)); + } - @Override - public boolean supportsTestTemplate(ExtensionContext context) { - return true; - } + @Override + public boolean supportsTestTemplate(ExtensionContext context) { + return true; + } } diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java index 8765572..699a15d 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/context/TestEnironmentInvocationContext.java @@ -11,33 +11,33 @@ import org.junit.jupiter.api.extension.TestTemplateInvocationContext;; public class TestEnironmentInvocationContext implements TestTemplateInvocationContext { - private String contextDisplayName; - - public TestEnironmentInvocationContext(String contextDisplayName) { - super(); - this.contextDisplayName = contextDisplayName; - } - - @Override - public String getDisplayName(int invocationIndex) { - return contextDisplayName; - } - - @Override - public List getAdditionalExtensions() { - return Collections.singletonList(new ParameterResolver() { - - @Override - public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return extensionContext; - } - - @Override - public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return ExtensionContext.class == parameterContext.getParameter().getType(); - } - }); - } + private String contextDisplayName; + + public TestEnironmentInvocationContext(String contextDisplayName) { + super(); + this.contextDisplayName = contextDisplayName; + } + + @Override + public String getDisplayName(int invocationIndex) { + return contextDisplayName; + } + + @Override + public List getAdditionalExtensions() { + return Collections.singletonList(new ParameterResolver() { + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return extensionContext; + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return ExtensionContext.class == parameterContext.getParameter().getType(); + } + }); + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java index 58ac535..686f0a5 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/AirPollutionProcessTest.java @@ -29,89 +29,87 @@ @IvyProcessTest(enableWebServer = true) @ExtendWith(MultiEnvironmentContextProvider.class) public class AirPollutionProcessTest { - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; - @BeforeEach - void beforeEach(ExtensionContext context, AppFixture fixture) { - OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); - } + @BeforeEach + 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); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(AirPollution.class); - } + @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); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(AirPollution.class); + } - @TestTemplate - void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) - .execute(null, null); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + void testGetAirPollutionByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, + GET_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @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); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(AirPollution.class); - } + @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); + 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); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @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); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @TestTemplate - void testGetHistoricalAirPollutionByGeoCode_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException { - 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) - .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_ReturnsAirPollution(BpmClient client) throws NoSuchFieldException { + 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) + .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 { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionCanNotGeo(BpmClient client) + throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE).execute(null, null, now, twoDaysLater); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @TestTemplate - void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient client) - throws NoSuchFieldException { - OffsetDateTime now = OffsetDateTime.now(); - OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, - GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + void testGetHistoricalAirPollutionByGeoCode_ThrowsBpmExceptionStartMoreThanEnd(BpmClient client) + throws NoSuchFieldException { + OffsetDateTime now = OffsetDateTime.now(); + OffsetDateTime twoDaysLater = now.plus(Duration.ofDays(2)); + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(client, GET_AIR_POLLUTION_PROCESS_PATH, + GET_HISTORICAL_AIR_POLLUTION_BY_GEOCODE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, twoDaysLater, now); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java index 6fa79ab..41f4da7 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/CurrentWeatherProcessTest.java @@ -26,31 +26,33 @@ @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, 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) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(Current.class); - } - - @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); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + 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) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(Current.class); + } + + @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); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java index 001bec7..1906dd8 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/ForecastWeatherProcessTest.java @@ -25,31 +25,31 @@ @IvyProcessTest(enableWebServer = true) @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, AppFixture fixture) { - OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); - } - - @TestTemplate - public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient client) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(Forecast.class); - } - - @TestTemplate - public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { - try { - 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); - } - } + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + + @BeforeEach + void beforeEach(ExtensionContext context, AppFixture fixture) { + OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); + } + + @TestTemplate + public void testGetForecastWeatherByGeoCode_ReturnsForecast(BpmClient client) throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(client, GET_FORECAST_PROCESS_PATH, GET_FORECAST_BY_GEOCODE_SIGNATURE) + .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1, StringUtils.EMPTY, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(Forecast.class); + } + + @TestTemplate + public void testGetForecastByGeoCode_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { + try { + 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); + } + } } diff --git a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java index fe07371..6e839fa 100644 --- a/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java +++ b/open-weather-connector-test/src_test/com/axonivy/connector/openweather/test/GeocodingLocationProcessTest.java @@ -31,77 +31,76 @@ @IvyProcessTest(enableWebServer = true) @ExtendWith(MultiEnvironmentContextProvider.class) public class GeocodingLocationProcessTest { - private final Double TEST_LON_VALUE = 40.7484; - private final Double TEST_LAT_VALUE = -73.9967; - private final String TEST_ZIPCODE_VALUE = "10001"; + private final Double TEST_LON_VALUE = 40.7484; + private final Double TEST_LAT_VALUE = -73.9967; + private final String TEST_ZIPCODE_VALUE = "10001"; - @BeforeEach - void beforeEach(ExtensionContext context, AppFixture fixture) { - OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); - } + @BeforeEach + void beforeEach(ExtensionContext context, AppFixture fixture) { + OpenWeatherUtils.setUpConfigForContext(context.getDisplayName(), fixture); + } - @TestTemplate - public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) - .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); - var object = result.data().last().get(RESULTS_KEY); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } + @TestTemplate + public void testGeocodingByName_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute("London", StringUtils.EMPTY, StringUtils.EMPTY, 1); + var object = result.data().last().get(RESULTS_KEY); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } - @TestTemplate - public void testGeocodingByName_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_NAME_SIGNATURE) - .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + public void testGeocodingByName_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { + try { + OpenWeatherUtils + .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_NAME_SIGNATURE) + .execute(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @TestTemplate - public void testGeocodingByZip_ReturnsGeoLocation(BpmClient client) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE) - .execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); - var object = result.data().last().get(RESULT_KEY); - assertThat(object).isInstanceOf(GeoLocation.class); - } + @TestTemplate + public void testGeocodingByZip_ReturnsGeoLocation(BpmClient client) throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(TEST_ZIPCODE_VALUE, StringUtils.EMPTY); + var object = result.data().last().get(RESULT_KEY); + assertThat(object).isInstanceOf(GeoLocation.class); + } - @TestTemplate - public void testGeocodingByZip_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { - try { - OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, - GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + public void testGeocodingByZip_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_BY_ZIP_CODE_SIGNATURE).execute(StringUtils.EMPTY, StringUtils.EMPTY); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } - @TestTemplate - public void testReverse_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { - ExecutionResult result = OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) - .execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); - var object = result.data().last().get(RESULTS_KEY); - assertThat(object).isInstanceOf(List.class); - var objects = (ArrayList) object; - assertThat(objects).isNotEmpty(); - assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); - } + @TestTemplate + public void testReverse_ReturnsListOfGeoLocations(BpmClient client) throws NoSuchFieldException { + ExecutionResult result = OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(TEST_LON_VALUE, TEST_LAT_VALUE, 1); + var object = result.data().last().get(RESULTS_KEY); + assertThat(object).isInstanceOf(List.class); + var objects = (ArrayList) object; + assertThat(objects).isNotEmpty(); + assertThat(objects.get(0)).isInstanceOf(GeoLocation.class); + } - @TestTemplate - public void testReverse_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { - try { - OpenWeatherUtils - .getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, GEOCODING_LOCATION_REVERSE_SIGNATURE) - .execute(null, null, 1); - } catch (BpmError e) { - assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); - } - } + @TestTemplate + public void testReverse_ThrowsBpmException(BpmClient client) throws NoSuchFieldException { + try { + OpenWeatherUtils.getSubProcessWithNameAndPath(client, GEOCODING_LOCATION_PROCESS_PATH, + GEOCODING_LOCATION_REVERSE_SIGNATURE).execute(null, null, 1); + } catch (BpmError e) { + assertThat(e.getHttpStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + } } From 008deccdf36aca89000f8b69f8d34c39889d54ea Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Wed, 8 Jan 2025 14:12:52 +0700 Subject: [PATCH 14/14] format code --- .../test/utils/OpenWeatherUtils.java | 114 +++++++++--------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java index 0ecc946..7cc4955 100644 --- a/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java +++ b/open-weather-connector-test/src/com/axonivy/connector/openweather/test/utils/OpenWeatherUtils.java @@ -15,68 +15,68 @@ import ch.ivyteam.ivy.environment.Ivy; 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 WEATHER_DATA_URL_KEY = "weatherDataUrl"; - private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; - private static final String OPEN_WEATHER_CONNECTOR_PREFIX = "openWeatherConnector."; - private static final String JSON_FEATURES = "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"; - private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; - private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; - 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"; + 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 WEATHER_DATA_URL_KEY = "weatherDataUrl"; + private static final String LOCAL_CREDENTIALS_FILE_PATH = "credentials.properties"; + private static final String OPEN_WEATHER_CONNECTOR_PREFIX = "openWeatherConnector."; + private static final String JSON_FEATURES = "ch.ivyteam.ivy.rest.client.mapper.JsonFeature"; + private static final String WEATHER_DATA_REST_CLIENT_NAME = "WeatherData (Openweathermap weather API)"; + private static final String GEO_DATA_REST_CLIENT_NAME = "GeocodingCoordinates (Openweathermap geocoding API)"; + 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"; - 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)); + 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); - } + 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(String contextName, AppFixture fixture) { - switch (contextName) { - case OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME: - setUpConfigForApiTest(fixture); - break; - case OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME: - setUpConfigForMockServer(fixture); - break; - default: - break; - } - } + public static void setUpConfigForContext(String contextName, AppFixture fixture) { + switch (contextName) { + case OpenWeatherCommonConstants.REAL_CALL_CONTEXT_DISPLAY_NAME: + setUpConfigForApiTest(fixture); + break; + case OpenWeatherCommonConstants.MOCK_SERVER_CONTEXT_DISPLAY_NAME: + setUpConfigForMockServer(fixture); + break; + default: + break; + } + } - 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); + 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)) { - if (in != null) { - Properties props = new Properties(); - props.load(in); - appId = (String) props.get(APP_ID_KEY); - weatherDataUrl = (String) props.get(WEATHER_DATA_URL_KEY); - weatherGeoUrl = (String) props.get(WEATHER_GEO_URL_KEY); - } - } catch (IOException e) { - Ivy.log().warn("Can't get credential from local file with path: {0}", LOCAL_CREDENTIALS_FILE_PATH); - } - } + // Local setup for testing + if (StringUtils.isAnyBlank(new String[] { appId, weatherDataUrl, weatherGeoUrl })) { + try (var in = OpenWeatherUtils.class.getResourceAsStream(LOCAL_CREDENTIALS_FILE_PATH)) { + if (in != null) { + Properties props = new Properties(); + props.load(in); + appId = props.getProperty(APP_ID_KEY); + weatherDataUrl = props.getProperty(WEATHER_DATA_URL_KEY); + weatherGeoUrl = props.getProperty(WEATHER_GEO_URL_KEY); + } + } catch (IOException e) { + Ivy.log().warn("Can't get credential from local file with path: {0}", LOCAL_CREDENTIALS_FILE_PATH); + } + } - 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); - } + 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) { - return client.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); - } + public static SubRequestBuilder getSubProcessWithNameAndPath(BpmClient client, String subProcessPath, + String subProcessName) { + return client.start().subProcess(BpmProcess.path(subProcessPath).elementName(subProcessName)); + } }