Skip to content

Commit

Permalink
fix: Fix base path in API Catalog (#3297)
Browse files Browse the repository at this point in the history
  • Loading branch information
pj892031 authored Jan 31, 2024
1 parent 9c781d5 commit 309aac0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +103,7 @@ private boolean isServiceTypeMatch(Map.Entry<String, RoutedService> serviceEntry
}

private boolean isMatchingApiRoute(String serviceUrl, String routeServiceUrl) {
return routeServiceUrl.startsWith(serviceUrl.toLowerCase());
return StringUtils.startsWithIgnoreCase(serviceUrl, routeServiceUrl);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions config/local/api-defs-http/staticclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions config/local/api-defs/staticclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 309aac0

Please sign in to comment.