Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: zaas client supports v2 compatibility with both gateway endpoint base Url paths #2227

Merged
merged 2 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}