From 111064e0642fcf4d8c41be316f8973beac004e2a Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 14:10:42 +0700 Subject: [PATCH] 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