Skip to content
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
@@ -1,5 +1,5 @@
/**
* © Copyright IBM Corporation 2020, 2024. All Rights Reserved.
* © Copyright IBM Corporation 2020, 2026. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -32,6 +32,7 @@
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Logger;

/**
* This class provides customizations to the generated Cloudant class.
Expand All @@ -40,6 +41,12 @@
*/
public abstract class CloudantBaseService extends BaseService {

private static final Logger LOGGER = Logger.getLogger(CloudantBaseService.class.getName());
private static final String GEN2_KNOWN_ISSUES_URL =
"https://github.com/IBM/cloudant-java-sdk/blob/main/KNOWN_ISSUES.md#cloudant-gen-2-compatibility";
static final String CLOUDANT_SDK_GEN1_ERRORS_PROP = "com.ibm.cloud.cloudant.sdk.gen1.errors";
static final String CLOUDANT_SDK_GEN1_ERRORS_ENV = "CLOUDANT_SDK_GEN1_ERRORS";

private static Interceptor errorInterceptor = new ErrorTransformInterceptor();

private int serviceUrlPathSegmentSize = 0;
Expand Down Expand Up @@ -146,6 +153,23 @@ private enum ValidationRule {
"headAttachment",
"putAttachment"
)
),
GEN1_ONLY(
0,
"",
Arrays.asList(
"getActivityTrackerEvents",
"getCapacityDatabasesInformation",
"getCapacityThroughputInformation",
"getCorsInformation",
"getCurrentDatabasesInformation",
"getCurrentThroughputInformation",
"postActivityTrackerEvents",
"postApiKeys",
"putCapacityThroughputConfiguration",
"putCloudantSecurityConfiguration",
"putCorsConfiguration"
)
);

private static final EnumSet<ValidationRule> NONE = EnumSet.noneOf(ValidationRule.class);
Expand All @@ -159,6 +183,12 @@ private enum ValidationRule {
this.operationIds = operationIds;
}
}

private static boolean isGen1ErrorEnabled() {
return Boolean.parseBoolean(System.getProperty(CLOUDANT_SDK_GEN1_ERRORS_PROP,
System.getenv(CLOUDANT_SDK_GEN1_ERRORS_ENV)));
}

static final Map<String, EnumSet<ValidationRule>> rulesByOperation = new TreeMap<>();
static {
for (ValidationRule rule : ValidationRule.values()) {
Expand Down Expand Up @@ -193,13 +223,27 @@ protected <T> ServiceCall<T> createServiceCall(Request request,
}
// Check each validation rule that applies to the operation.
for (ValidationRule rule : rulesByOperation.getOrDefault(operationId, ValidationRule.NONE)) {
// Allow for any path segments that might exist in e.g. the URL of a proxy by offseting from the service URL index.
int pathSegmentIndex = serviceUrlPathSegmentSize + rule.pathSegmentIndex;
if (requestUrlPathSegments.size() > pathSegmentIndex) {
String segmentToValidate = requestUrlPathSegments.get(pathSegmentIndex);
if (segmentToValidate.startsWith("_")) {
throw new IllegalArgumentException(String.format("%s %s starts with the invalid _ character.", rule.errorParameterName, segmentToValidate));
}
switch (rule) {
case GEN1_ONLY:
String message = String.format(
"The operation %s is unavailable for Gen 2 Cloudant plans. See %s for more information.",
operationId,
GEN2_KNOWN_ISSUES_URL);
LOGGER.warning(message);
if (isGen1ErrorEnabled()) {
throw new UnsupportedOperationException(message);
}
break;
default:
// Allow for any path segments that might exist in e.g. the URL of a proxy by offseting from the service URL index.
int pathSegmentIndex = serviceUrlPathSegmentSize + rule.pathSegmentIndex;
if (requestUrlPathSegments.size() > pathSegmentIndex) {
String segmentToValidate = requestUrlPathSegments.get(pathSegmentIndex);
if (segmentToValidate.startsWith("_")) {
throw new IllegalArgumentException(String.format("%s %s starts with the invalid _ character.", rule.errorParameterName, segmentToValidate));
}
}
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* © Copyright IBM Corporation 2021, 2022. All Rights Reserved.
* © Copyright IBM Corporation 2021, 2026. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -18,6 +18,7 @@
import java.util.Map.Entry;
import com.ibm.cloud.cloudant.common.SdkCommon;
import com.ibm.cloud.sdk.core.security.NoAuthAuthenticator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import okhttp3.HttpUrl;
import okhttp3.Request;
Expand Down Expand Up @@ -94,4 +95,70 @@ void validatesStripTrailingSlash() {
cloudantBaseService.setServiceUrl("https://cloudant.example/");
assertEquals(cloudantBaseService.getServiceUrl(), "https://cloudant.example");
}

@DataProvider(name = "gen1Operations")
public Object[][] gen1Operations() {
return new Object[][] {
{"getActivityTrackerEvents"},
{"getCapacityDatabasesInformation"},
{"getCapacityThroughputInformation"},
{"getCorsInformation"},
{"getCurrentDatabasesInformation"},
{"getCurrentThroughputInformation"},
{"postActivityTrackerEvents"},
{"postApiKeys"},
{"putCapacityThroughputConfiguration"},
{"putCloudantSecurityConfiguration"},
{"putCorsConfiguration"}
};
}

@Test(dataProvider = "gen1Operations", expectedExceptions = UnsupportedOperationException.class, expectedExceptionsMessageRegExp = "The operation .+ is unavailable for Gen 2 Cloudant plans\\..+")
void validatesGen1Operation(String operationId) {
System.setProperty(CloudantBaseService.CLOUDANT_SDK_GEN1_ERRORS_PROP, "true");
try {
CloudantBaseService cloudantBaseService = new CloudantBaseService(null, new NoAuthAuthenticator()) {
};
cloudantBaseService.setServiceUrl("https://cloudant.example");
Request.Builder rb = new Request.Builder().url(cloudantBaseService.getServiceUrl() + "/test").get();
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", operationId);
for (Entry<String, String> header : sdkHeaders.entrySet()) {
rb.header(header.getKey(), header.getValue());
}
cloudantBaseService.createServiceCall(rb.build(), null);
} finally {
System.clearProperty(CloudantBaseService.CLOUDANT_SDK_GEN1_ERRORS_PROP);
}
}

@Test(dataProvider = "gen1Operations")
void validatesGen1OperationNoErrorByDefault(String operationId) {
CloudantBaseService cloudantBaseService = new CloudantBaseService(null, new NoAuthAuthenticator()) {
};
cloudantBaseService.setServiceUrl("https://cloudant.example");
Request.Builder rb = new Request.Builder().url(cloudantBaseService.getServiceUrl() + "/test").get();
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", operationId);
for (Entry<String, String> header : sdkHeaders.entrySet()) {
rb.header(header.getKey(), header.getValue());
}
cloudantBaseService.createServiceCall(rb.build(), null);
}

@Test
void validatesGen2OperationNoErrorWhenGen1ErrorsEnabled() {
System.setProperty(CloudantBaseService.CLOUDANT_SDK_GEN1_ERRORS_PROP, "true");
try {
CloudantBaseService cloudantBaseService = new CloudantBaseService(null, new NoAuthAuthenticator()) {
};
cloudantBaseService.setServiceUrl("https://cloudant.example");
Request.Builder rb = new Request.Builder().url(cloudantBaseService.getServiceUrl() + "/test").get();
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getSessionInformation");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
rb.header(header.getKey(), header.getValue());
}
cloudantBaseService.createServiceCall(rb.build(), null);
} finally {
System.clearProperty(CloudantBaseService.CLOUDANT_SDK_GEN1_ERRORS_PROP);
}
}
}