From 4f44e35c6f7f3816c228dcd3a0f08d5f4266ac48 Mon Sep 17 00:00:00 2001 From: Amanda D'Errico <40764145+AmandaDErrico@users.noreply.github.com> Date: Wed, 30 Mar 2022 10:00:29 -0400 Subject: [PATCH] fix: zaas client supports v2 compatibility with both gateway endpoint base Url paths (#2227) * zaas client supports v2 compatibility with updated path Signed-off-by: Amanda D'Errico * addressed code review Signed-off-by: Amanda D'Errico --- .../zaasclient/config/ConfigProperties.java | 28 +++++++++++ .../internal/ZaasClientApimlBaseUrlTest.java | 47 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java diff --git a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java index 350b413e12..89010adf51 100644 --- a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java +++ b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java @@ -13,6 +13,8 @@ import lombok.Data; import lombok.experimental.Tolerate; +import java.util.Objects; + @Data @Builder public class ConfigProperties { @@ -29,6 +31,8 @@ public class ConfigProperties { private boolean httpOnly; private boolean nonStrictVerifySslCertificatesOfServices; + private static final String GATEWAY_SERVICE_ID = "gateway"; + @Tolerate public ConfigProperties() { // no args constructor @@ -47,4 +51,28 @@ public ConfigProperties withoutKeyStore() { .build(); } + public void setApimlBaseUrl(String baseUrl) { + if (baseUrl == null) { // default path + apimlBaseUrl = "/gateway/api/v1/auth"; + } + else if (baseUrl.contains("/") && baseUrl.contains(GATEWAY_SERVICE_ID)) { + String[] baseUrlParts = baseUrl.split("/"); + if (Objects.equals(baseUrlParts[2], GATEWAY_SERVICE_ID)) { + apimlBaseUrl = "/gateway/" + baseUrlParts[0] + "/" + baseUrlParts[1] + "/auth"; + } + else if (Objects.equals(baseUrlParts[3], GATEWAY_SERVICE_ID)) { + apimlBaseUrl = "/gateway/" + baseUrlParts[1] + "/" + baseUrlParts[2] + "/auth"; + } + else if (!baseUrl.startsWith("/")) { // starts with gateway/.. + apimlBaseUrl = "/" + baseUrl; + } + else { + apimlBaseUrl = baseUrl; + } + } + else { + apimlBaseUrl = baseUrl; + } + } + } diff --git a/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java b/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java new file mode 100644 index 0000000000..5f695e00c4 --- /dev/null +++ b/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java @@ -0,0 +1,47 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ + +package org.zowe.apiml.zaasclient.service.internal; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.zowe.apiml.zaasclient.config.ConfigProperties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +class ZaasClientApimlBaseUrlTest { + + private static final String ZOWE_V2_BASE_URL = "/gateway/api/v1/auth"; + + @ParameterizedTest + @ValueSource(strings = {"/api/v1/gateway/auth", "api/v1/gateway/auth", "/gateway/api/v1/auth", "gateway/api/v1/auth"}) + void givenBaseUrl_thenTransformToOrDontChangeZoweV2BaseUrl(String baseUrl) { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(baseUrl); + assertThat(configProperties.getApimlBaseUrl(), is(ZOWE_V2_BASE_URL)); + } + + @ParameterizedTest + @ValueSource(strings = {"/api/v1/zaasClient/auth", "api.v1.zaasClient.auth"}) + void givenBaseUrl_thenDontChangeBaseUrl(String baseUrl) { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(baseUrl); + assertThat(configProperties.getApimlBaseUrl(), is(baseUrl)); + } + + @Test + void givenBaseUrlIsNull_thenTransformToZoweV2BaseUrl() { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(null); + assertThat(configProperties.getApimlBaseUrl(), is(ZOWE_V2_BASE_URL)); + } +}