From 2f41401e06e55d2bb37e60e5c1683b75833240c1 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Fri, 27 Dec 2024 15:15:24 +0700 Subject: [PATCH] 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); + } + } +}