From 6e57c3e27c8ff5cc829c8b7b9ea3e786de2fb1c0 Mon Sep 17 00:00:00 2001 From: Veronika Al Raheem Date: Thu, 19 Nov 2026 14:16:37 +0100 Subject: [PATCH] feat: log warning for Gen 1-only operations --- .../internal/CloudantBaseService.java | 60 +++++++++++++--- .../cloudant/internal/ValidationTest.java | 69 ++++++++++++++++++- 2 files changed, 120 insertions(+), 9 deletions(-) diff --git a/modules/common/src/main/java/com/ibm/cloud/cloudant/internal/CloudantBaseService.java b/modules/common/src/main/java/com/ibm/cloud/cloudant/internal/CloudantBaseService.java index bd98e9da3..e364b3f7e 100644 --- a/modules/common/src/main/java/com/ibm/cloud/cloudant/internal/CloudantBaseService.java +++ b/modules/common/src/main/java/com/ibm/cloud/cloudant/internal/CloudantBaseService.java @@ -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 @@ -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. @@ -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; @@ -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 NONE = EnumSet.noneOf(ValidationRule.class); @@ -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> rulesByOperation = new TreeMap<>(); static { for (ValidationRule rule : ValidationRule.values()) { @@ -193,13 +223,27 @@ protected ServiceCall 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; } } } diff --git a/modules/common/src/test/java/com/ibm/cloud/cloudant/internal/ValidationTest.java b/modules/common/src/test/java/com/ibm/cloud/cloudant/internal/ValidationTest.java index 7f64e1866..ab1b10604 100644 --- a/modules/common/src/test/java/com/ibm/cloud/cloudant/internal/ValidationTest.java +++ b/modules/common/src/test/java/com/ibm/cloud/cloudant/internal/ValidationTest.java @@ -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 @@ -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; @@ -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 sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", operationId); + for (Entry 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 sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", operationId); + for (Entry 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 sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getSessionInformation"); + for (Entry header : sdkHeaders.entrySet()) { + rb.header(header.getKey(), header.getValue()); + } + cloudantBaseService.createServiceCall(rb.build(), null); + } finally { + System.clearProperty(CloudantBaseService.CLOUDANT_SDK_GEN1_ERRORS_PROP); + } + } }