From 309aac068c06b15f1fc1b61f1abfa3b7cad16bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jare=C5=A1?= <58428711+pj892031@users.noreply.github.com> Date: Wed, 31 Jan 2024 22:28:11 +0100 Subject: [PATCH] fix: Fix base path in API Catalog (#3297) --- .../transform/TransformServiceTest.java | 30 +++++++++++++++++-- .../apiml/product/routing/RoutedServices.java | 3 +- .../product/routing/RoutedServicesTest.java | 4 +-- config/local/api-defs-http/staticclient.yml | 12 ++++---- config/local/api-defs/staticclient.yml | 14 ++++----- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/apiml-common/src/test/java/org/zowe/apiml/product/routing/transform/TransformServiceTest.java b/apiml-common/src/test/java/org/zowe/apiml/product/routing/transform/TransformServiceTest.java index 0bdf694f2e..7208e0fcd2 100644 --- a/apiml-common/src/test/java/org/zowe/apiml/product/routing/transform/TransformServiceTest.java +++ b/apiml-common/src/test/java/org/zowe/apiml/product/routing/transform/TransformServiceTest.java @@ -12,6 +12,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.zowe.apiml.product.gateway.GatewayClient; import org.zowe.apiml.product.gateway.GatewayConfigProperties; import org.zowe.apiml.product.routing.RoutedService; @@ -233,7 +235,7 @@ void givenUrlContainingPathAndQuery_whenTransform_thenKeepQueryPartInTheNewUrl() @Test void givenServiceAndApiRoute_whenGetApiBasePath_thenReturnApiPath() throws URLTransformationException { - String url = "https://localhost:8080/" + SERVICE_ID; + String url = "https://localhost:8080/" + SERVICE_ID + "/" + API_PREFIX + "/v1"; String serviceUrl = String.format("/%s/%s", SERVICE_ID, API_PREFIX); RoutedServices routedServices = new RoutedServices(); @@ -251,7 +253,7 @@ void givenServiceAndApiRoute_whenGetApiBasePath_thenReturnApiPath() throws URLTr @Test void givenServiceAndApiRouteWithVersion_whenGetApiBasePath_thenReturnApiPath() throws URLTransformationException { - String url = "https://localhost:8080/" + SERVICE_ID; + String url = "https://localhost:8080/" + SERVICE_ID + "/" + API_PREFIX + "/v1"; String serviceUrl = String.format("/%s/%s/%s", SERVICE_ID, API_PREFIX, "v1"); RoutedServices routedServices = new RoutedServices(); @@ -295,4 +297,28 @@ void givenNoRoutes_whenGetApiBasePath_thenThrowError() { assertEquals("Not able to select API base path for the service " + SERVICE_ID + ". Original url used.", exception.getMessage()); } + @ParameterizedTest + @CsvSource({ + "service,api/v1,api/v1,api/v1,/service/api/{api-version}", + "srv,api/v1/home/page.html,api/v1,api/v1,/srv/api/{api-version}", + "srv,wrong/url,api/v1,api/v1,", + "srv,apiV1/home/page.html,api/v1,apiV1,/srv/api/{api-version}" + }) + void testRetrieveApiBasePath(String serviceId, String url, String gatewayUrl, String serviceUrl, String expectedBasePath) { + RoutedService route = new RoutedService("api", gatewayUrl, serviceUrl); + + RoutedServices routedServices = new RoutedServices(); + routedServices.addRoutedService(route); + + TransformService transformService = new TransformService(null); + String basePath; + try { + basePath = transformService.retrieveApiBasePath(serviceId, url, routedServices); + } catch (URLTransformationException e) { + basePath = null; + } + + assertEquals(expectedBasePath, basePath); + } + } diff --git a/common-service-core/src/main/java/org/zowe/apiml/product/routing/RoutedServices.java b/common-service-core/src/main/java/org/zowe/apiml/product/routing/RoutedServices.java index b838ca9d8a..be3d52ae27 100644 --- a/common-service-core/src/main/java/org/zowe/apiml/product/routing/RoutedServices.java +++ b/common-service-core/src/main/java/org/zowe/apiml/product/routing/RoutedServices.java @@ -10,6 +10,7 @@ package org.zowe.apiml.product.routing; +import org.apache.commons.lang3.StringUtils; import org.zowe.apiml.util.UrlUtils; import java.util.HashMap; @@ -102,7 +103,7 @@ private boolean isServiceTypeMatch(Map.Entry serviceEntry } private boolean isMatchingApiRoute(String serviceUrl, String routeServiceUrl) { - return routeServiceUrl.startsWith(serviceUrl.toLowerCase()); + return StringUtils.startsWithIgnoreCase(serviceUrl, routeServiceUrl); } @Override diff --git a/common-service-core/src/test/java/org/zowe/apiml/product/routing/RoutedServicesTest.java b/common-service-core/src/test/java/org/zowe/apiml/product/routing/RoutedServicesTest.java index bee4b06fb9..6f8d27586b 100644 --- a/common-service-core/src/test/java/org/zowe/apiml/product/routing/RoutedServicesTest.java +++ b/common-service-core/src/test/java/org/zowe/apiml/product/routing/RoutedServicesTest.java @@ -112,13 +112,11 @@ void testBestMatchingApiUrl() { routedServices.addRoutedService(routedService1); routedServices.addRoutedService(routedService2); - routedService = routedServices.getBestMatchingApiUrl("/test"); + routedService = routedServices.getBestMatchingApiUrl("/test2/api/v2"); assertEquals("api_v2", routedService.getSubServiceId()); assertEquals("api/v2", routedService.getGatewayUrl()); assertEquals("/test2/api/v2", routedService.getServiceUrl()); - - } @Test diff --git a/config/local/api-defs-http/staticclient.yml b/config/local/api-defs-http/staticclient.yml index 96bdacf5b7..22a5c0ad86 100644 --- a/config/local/api-defs-http/staticclient.yml +++ b/config/local/api-defs-http/staticclient.yml @@ -11,7 +11,7 @@ services: description: Sample to demonstrate how to add an API service with Swagger to API Catalog using a static YAML definition # Description of the service in the API catalog instanceBaseUrls: # list of base URLs for each instance - http://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: / # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -36,7 +36,7 @@ services: description: Sample to demonstrate how to add an API service without Swagger documentation to API Catalog using a static YAML definition # Description of the service in the API catalog instanceBaseUrls: # list of base URLs for each instance - http://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -61,7 +61,7 @@ services: description: Define service to test by pass authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - http://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -80,7 +80,7 @@ services: description: Define service to test passTicket authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - http://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -100,7 +100,7 @@ services: description: Define service to test safIdt authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - http://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -120,7 +120,7 @@ services: description: Define service to test zosmf authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: diff --git a/config/local/api-defs/staticclient.yml b/config/local/api-defs/staticclient.yml index 0fdd2ffca5..1a01bc94ae 100644 --- a/config/local/api-defs/staticclient.yml +++ b/config/local/api-defs/staticclient.yml @@ -11,7 +11,7 @@ services: description: Sample to demonstrate how to add an API service with Swagger to API Catalog using a static YAML definition # Description of the service in the API catalog instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: / # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -36,7 +36,7 @@ services: description: Sample to demonstrate how to add an API service without Swagger documentation to API Catalog using a static YAML definition # Description of the service in the API catalog instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -61,7 +61,7 @@ services: description: Sample to demonstrate how to add an API service without Swagger documentation to API Catalog using a static YAML definition # Description of the service in the API catalog instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -84,7 +84,7 @@ services: description: Define service to test by pass authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -103,7 +103,7 @@ services: description: Define service to test passTicket authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -123,7 +123,7 @@ services: description: Define service to test safIdt authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: @@ -143,7 +143,7 @@ services: description: Define service to test zosmf authentication schema for integration tests. instanceBaseUrls: # list of base URLs for each instance - https://localhost:10012/discoverableclient # scheme://hostname:port/contextPath - homePageRelativeUrl: # Normally used for informational purposes for other services to use it as a landing page + homePageRelativeUrl: /api/v1 # Normally used for informational purposes for other services to use it as a landing page statusPageRelativeUrl: /application/info # Appended to the instanceBaseUrl healthCheckRelativeUrl: /application/health # Appended to the instanceBaseUrl routes: