Skip to content

Commit

Permalink
fix: zaas client supports v2 compatibility with both gateway endpoint…
Browse files Browse the repository at this point in the history
… base Url paths (#2227)

* zaas client supports v2 compatibility with updated path

Signed-off-by: Amanda D'Errico <[email protected]>

* addressed code review

Signed-off-by: Amanda D'Errico <[email protected]>
  • Loading branch information
AmandaDErrico committed Mar 30, 2022
1 parent 5446fd5 commit 4f44e35
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import lombok.Data;
import lombok.experimental.Tolerate;

import java.util.Objects;

@Data
@Builder
public class ConfigProperties {
Expand All @@ -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
Expand All @@ -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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 4f44e35

Please sign in to comment.