diff --git a/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClient.java b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClient.java new file mode 100644 index 000000000..2deabcfb4 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClient.java @@ -0,0 +1,31 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab.client; + +import java.util.Map; + +public interface CmabClient { + /** + * Fetches a decision from the CMAB prediction service. + * + * @param ruleId The rule/experiment ID + * @param userId The user ID + * @param attributes User attributes + * @param cmabUUID The CMAB UUID + * @return CompletableFuture containing the variation ID as a String + */ + String fetchDecision(String ruleId, String userId, Map attributes, String cmabUUID); +} diff --git a/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClientConfig.java b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClientConfig.java new file mode 100644 index 000000000..90198d376 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabClientConfig.java @@ -0,0 +1,49 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab.client; + +import javax.annotation.Nullable; + +/** + * Configuration for CMAB client operations. + * Contains only retry configuration since HTTP client is handled separately. + */ +public class CmabClientConfig { + private final RetryConfig retryConfig; + + public CmabClientConfig(@Nullable RetryConfig retryConfig) { + this.retryConfig = retryConfig; + } + + @Nullable + public RetryConfig getRetryConfig() { + return retryConfig; + } + + /** + * Creates a config with default retry settings. + */ + public static CmabClientConfig withDefaultRetry() { + return new CmabClientConfig(RetryConfig.defaultConfig()); + } + + /** + * Creates a config with no retry. + */ + public static CmabClientConfig withNoRetry() { + return new CmabClientConfig(null); + } +} \ No newline at end of file diff --git a/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabFetchException.java b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabFetchException.java new file mode 100644 index 000000000..d76576ea2 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabFetchException.java @@ -0,0 +1,28 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab.client; + +import com.optimizely.ab.OptimizelyRuntimeException; + +public class CmabFetchException extends OptimizelyRuntimeException { + public CmabFetchException(String message) { + super(message); + } + + public CmabFetchException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabInvalidResponseException.java b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabInvalidResponseException.java new file mode 100644 index 000000000..de5550995 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/cmab/client/CmabInvalidResponseException.java @@ -0,0 +1,27 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab.client; + +import com.optimizely.ab.OptimizelyRuntimeException; + +public class CmabInvalidResponseException extends OptimizelyRuntimeException{ + public CmabInvalidResponseException(String message) { + super(message); + } + public CmabInvalidResponseException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/core-api/src/main/java/com/optimizely/ab/cmab/client/RetryConfig.java b/core-api/src/main/java/com/optimizely/ab/cmab/client/RetryConfig.java new file mode 100644 index 000000000..b5b04cfa3 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/cmab/client/RetryConfig.java @@ -0,0 +1,132 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab.client; +/** + * Configuration for retry behavior in CMAB client operations. + */ +public class RetryConfig { + private final int maxRetries; + private final long backoffBaseMs; + private final double backoffMultiplier; + private final int maxTimeoutMs; + + /** + * Creates a RetryConfig with custom retry and backoff settings. + * + * @param maxRetries Maximum number of retry attempts + * @param backoffBaseMs Base delay in milliseconds for the first retry + * @param backoffMultiplier Multiplier for exponential backoff (e.g., 2.0 for doubling) + * @param maxTimeoutMs Maximum total timeout in milliseconds for all retry attempts + */ + public RetryConfig(int maxRetries, long backoffBaseMs, double backoffMultiplier, int maxTimeoutMs) { + if (maxRetries < 0) { + throw new IllegalArgumentException("maxRetries cannot be negative"); + } + if (backoffBaseMs < 0) { + throw new IllegalArgumentException("backoffBaseMs cannot be negative"); + } + if (backoffMultiplier < 1.0) { + throw new IllegalArgumentException("backoffMultiplier must be >= 1.0"); + } + if (maxTimeoutMs < 0) { + throw new IllegalArgumentException("maxTimeoutMs cannot be negative"); + } + + this.maxRetries = maxRetries; + this.backoffBaseMs = backoffBaseMs; + this.backoffMultiplier = backoffMultiplier; + this.maxTimeoutMs = maxTimeoutMs; + } + + /** + * Creates a RetryConfig with default backoff settings and timeout (1 second base, 2x multiplier, 10 second timeout). + * + * @param maxRetries Maximum number of retry attempts + */ + public RetryConfig(int maxRetries) { + this(maxRetries, 1000, 2.0, 10000); // Default: 1 second base, exponential backoff, 10 second timeout + } + + /** + * Creates a default RetryConfig with 3 retries and exponential backoff. + */ + public static RetryConfig defaultConfig() { + return new RetryConfig(3); + } + + /** + * Creates a RetryConfig with no retries (single attempt only). + */ + public static RetryConfig noRetry() { + return new RetryConfig(0, 0, 1.0, 0); + } + + public int getMaxRetries() { + return maxRetries; + } + + public long getBackoffBaseMs() { + return backoffBaseMs; + } + + public double getBackoffMultiplier() { + return backoffMultiplier; + } + + public int getMaxTimeoutMs() { + return maxTimeoutMs; + } + + /** + * Calculates the delay for a specific retry attempt. + * + * @param attemptNumber The attempt number (0-based, so 0 = first retry) + * @return Delay in milliseconds + */ + public long calculateDelay(int attemptNumber) { + if (attemptNumber < 0) { + return 0; + } + return (long) (backoffBaseMs * Math.pow(backoffMultiplier, attemptNumber)); + } + + @Override + public String toString() { + return String.format("RetryConfig{maxRetries=%d, backoffBaseMs=%d, backoffMultiplier=%.1f, maxTimeoutMs=%d}", + maxRetries, backoffBaseMs, backoffMultiplier, maxTimeoutMs); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + RetryConfig that = (RetryConfig) obj; + return maxRetries == that.maxRetries && + backoffBaseMs == that.backoffBaseMs && + maxTimeoutMs == that.maxTimeoutMs && + Double.compare(that.backoffMultiplier, backoffMultiplier) == 0; + } + + @Override + public int hashCode() { + int result = maxRetries; + result = 31 * result + Long.hashCode(backoffBaseMs); + result = 31 * result + Double.hashCode(backoffMultiplier); + result = 31 * result + Integer.hashCode(maxTimeoutMs); + return result; + } +} diff --git a/core-httpclient-impl/src/main/java/com/optimizely/ab/cmab/DefaultCmabClient.java b/core-httpclient-impl/src/main/java/com/optimizely/ab/cmab/DefaultCmabClient.java new file mode 100644 index 000000000..6af4ac32a --- /dev/null +++ b/core-httpclient-impl/src/main/java/com/optimizely/ab/cmab/DefaultCmabClient.java @@ -0,0 +1,273 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.http.ParseException; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.optimizely.ab.OptimizelyHttpClient; +import com.optimizely.ab.cmab.client.CmabClient; +import com.optimizely.ab.cmab.client.CmabClientConfig; +import com.optimizely.ab.cmab.client.CmabFetchException; +import com.optimizely.ab.cmab.client.CmabInvalidResponseException; +import com.optimizely.ab.cmab.client.RetryConfig; + +public class DefaultCmabClient implements CmabClient { + + private static final Logger logger = LoggerFactory.getLogger(DefaultCmabClient.class); + private static final int DEFAULT_TIMEOUT_MS = 10000; + // Update constants to match JS error messages format + private static final String CMAB_FETCH_FAILED = "CMAB decision fetch failed with status: %s"; + private static final String INVALID_CMAB_FETCH_RESPONSE = "Invalid CMAB fetch response"; + private static final Pattern VARIATION_ID_PATTERN = Pattern.compile("\"variation_id\"\\s*:\\s*\"?([^\"\\s,}]+)\"?"); + private static final String CMAB_PREDICTION_ENDPOINT = "https://prediction.cmab.optimizely.com/predict/%s"; + + private final OptimizelyHttpClient httpClient; + private final RetryConfig retryConfig; + + // Primary constructor - all others delegate to this + public DefaultCmabClient(OptimizelyHttpClient httpClient, CmabClientConfig config) { + this.retryConfig = config != null ? config.getRetryConfig() : null; + this.httpClient = httpClient != null ? httpClient : createDefaultHttpClient(); + } + + // Constructor with HTTP client only (no retry) + public DefaultCmabClient(OptimizelyHttpClient httpClient) { + this(httpClient, CmabClientConfig.withNoRetry()); + } + + // Constructor with just retry config (uses default HTTP client) + public DefaultCmabClient(CmabClientConfig config) { + this(null, config); + } + + // Default constructor (no retry, default HTTP client) + public DefaultCmabClient() { + this(null, CmabClientConfig.withNoRetry()); + } + + // Extract HTTP client creation logic + private OptimizelyHttpClient createDefaultHttpClient() { + int timeoutMs = (retryConfig != null) ? retryConfig.getMaxTimeoutMs() : DEFAULT_TIMEOUT_MS; + return OptimizelyHttpClient.builder().setTimeoutMillis(timeoutMs).build(); + } + + @Override + public String fetchDecision(String ruleId, String userId, Map attributes, String cmabUuid) { + // Implementation will use this.httpClient and this.retryConfig + String url = String.format(CMAB_PREDICTION_ENDPOINT, ruleId); + String requestBody = buildRequestJson(userId, ruleId, attributes, cmabUuid); + + // Use retry logic if configured, otherwise single request + if (retryConfig != null && retryConfig.getMaxRetries() > 0) { + return doFetchWithRetry(url, requestBody, retryConfig.getMaxRetries()); + } else { + return doFetch(url, requestBody); + } + } + + private String doFetch(String url, String requestBody) { + HttpPost request = new HttpPost(url); + try { + request.setEntity(new StringEntity(requestBody)); + } catch (UnsupportedEncodingException e) { + String errorMessage = String.format(CMAB_FETCH_FAILED, e.getMessage()); + logger.error(errorMessage); + throw new CmabFetchException(errorMessage); + } + request.setHeader("content-type", "application/json"); + CloseableHttpResponse response = null; + try { + response = httpClient.execute(request); + + if (!isSuccessStatusCode(response.getStatusLine().getStatusCode())) { + StatusLine statusLine = response.getStatusLine(); + String errorMessage = String.format(CMAB_FETCH_FAILED, statusLine.getReasonPhrase()); + logger.error(errorMessage); + throw new CmabFetchException(errorMessage); + } + + String responseBody; + try { + responseBody = EntityUtils.toString(response.getEntity()); + + if (!validateResponse(responseBody)) { + logger.error(INVALID_CMAB_FETCH_RESPONSE); + throw new CmabInvalidResponseException(INVALID_CMAB_FETCH_RESPONSE); + } + return parseVariationId(responseBody); + } catch (IOException | ParseException e) { + logger.error(CMAB_FETCH_FAILED); + throw new CmabInvalidResponseException(INVALID_CMAB_FETCH_RESPONSE); + } + + } catch (IOException e) { + String errorMessage = String.format(CMAB_FETCH_FAILED, e.getMessage()); + logger.error(errorMessage); + throw new CmabFetchException(errorMessage); + } finally { + closeHttpResponse(response); + } + } + + private String doFetchWithRetry(String url, String requestBody, int maxRetries) { + double backoff = retryConfig.getBackoffBaseMs(); + Exception lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) { + try { + return doFetch(url, requestBody); + } catch (CmabFetchException | CmabInvalidResponseException e) { + lastException = e; + + // If this is the last attempt, don't wait - just break and throw + if (attempt >= maxRetries) { + break; + } + + // Log retry attempt + logger.info("Retrying CMAB request (attempt: {}) after {} ms...", + attempt + 1, (int) backoff); + + try { + Thread.sleep((long) backoff); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + String errorMessage = String.format(CMAB_FETCH_FAILED, "Request interrupted during retry"); + logger.error(errorMessage); + throw new CmabFetchException(errorMessage, ie); + } + + // Calculate next backoff using exponential backoff with multiplier + backoff = Math.min( + backoff * Math.pow(retryConfig.getBackoffMultiplier(), attempt + 1), + retryConfig.getMaxTimeoutMs() + ); + } + } + + // If we get here, all retries were exhausted + String errorMessage = String.format(CMAB_FETCH_FAILED, "Exhausted all retries for CMAB request"); + logger.error(errorMessage); + throw new CmabFetchException(errorMessage, lastException); + } + + private String buildRequestJson(String userId, String ruleId, Map attributes, String cmabUuid) { + StringBuilder json = new StringBuilder(); + json.append("{\"instances\":[{"); + json.append("\"visitorId\":\"").append(escapeJson(userId)).append("\","); + json.append("\"experimentId\":\"").append(escapeJson(ruleId)).append("\","); + json.append("\"cmabUUID\":\"").append(escapeJson(cmabUuid)).append("\","); + json.append("\"attributes\":["); + + boolean first = true; + for (Map.Entry entry : attributes.entrySet()) { + if (!first) { + json.append(","); + } + json.append("{\"id\":\"").append(escapeJson(entry.getKey())).append("\","); + json.append("\"value\":").append(formatJsonValue(entry.getValue())).append(","); + json.append("\"type\":\"custom_attribute\"}"); + first = false; + } + + json.append("]}]}"); + return json.toString(); + } + + private String escapeJson(String value) { + if (value == null) { + return ""; + } + return value.replace("\\", "\\\\") + .replace("\"", "\\\"") + .replace("\n", "\\n") + .replace("\r", "\\r") + .replace("\t", "\\t"); + } + + private String formatJsonValue(Object value) { + if (value == null) { + return "null"; + } else if (value instanceof String) { + return "\"" + escapeJson((String) value) + "\""; + } else if (value instanceof Number || value instanceof Boolean) { + return value.toString(); + } else { + return "\"" + escapeJson(value.toString()) + "\""; + } + } + + // Helper methods + private boolean isSuccessStatusCode(int statusCode) { + return statusCode >= 200 && statusCode < 300; + } + + private boolean validateResponse(String responseBody) { + try { + return responseBody.contains("predictions") && + responseBody.contains("variation_id") && + parseVariationIdForValidation(responseBody) != null; + } catch (Exception e) { + return false; + } + } + + private boolean shouldRetry(Exception exception) { + return (exception instanceof CmabFetchException) || + (exception instanceof CmabInvalidResponseException); + } + + private String parseVariationIdForValidation(String jsonResponse) { + Matcher matcher = VARIATION_ID_PATTERN.matcher(jsonResponse); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } + + private String parseVariationId(String jsonResponse) { + // Simple regex to extract variation_id from predictions[0].variation_id + Pattern pattern = Pattern.compile("\"predictions\"\\s*:\\s*\\[\\s*\\{[^}]*\"variation_id\"\\s*:\\s*\"?([^\"\\s,}]+)\"?"); + Matcher matcher = pattern.matcher(jsonResponse); + if (matcher.find()) { + return matcher.group(1); + } + throw new CmabInvalidResponseException(INVALID_CMAB_FETCH_RESPONSE); + } + + private static void closeHttpResponse(CloseableHttpResponse response) { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + logger.warn(e.getLocalizedMessage()); + } + } + } +} diff --git a/core-httpclient-impl/src/test/java/com/optimizely/ab/cmab/DefaultCmabClientTest.java b/core-httpclient-impl/src/test/java/com/optimizely/ab/cmab/DefaultCmabClientTest.java new file mode 100644 index 000000000..63fca3832 --- /dev/null +++ b/core-httpclient-impl/src/test/java/com/optimizely/ab/cmab/DefaultCmabClientTest.java @@ -0,0 +1,280 @@ +/** + * Copyright 2025, Optimizely + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.cmab; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.util.EntityUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.optimizely.ab.OptimizelyHttpClient; +import com.optimizely.ab.cmab.client.CmabClientConfig; +import com.optimizely.ab.cmab.client.CmabFetchException; +import com.optimizely.ab.cmab.client.CmabInvalidResponseException; +import com.optimizely.ab.cmab.client.RetryConfig; +import com.optimizely.ab.internal.LogbackVerifier; + +import ch.qos.logback.classic.Level; + +public class DefaultCmabClientTest { + + private static final String validCmabResponse = "{\"predictions\":[{\"variation_id\":\"treatment_1\"}]}"; + + @Rule + public LogbackVerifier logbackVerifier = new LogbackVerifier(); + + OptimizelyHttpClient mockHttpClient; + DefaultCmabClient cmabClient; + + @Before + public void setUp() throws Exception { + setupHttpClient(200); + cmabClient = new DefaultCmabClient(mockHttpClient); + } + + private void setupHttpClient(int statusCode) throws Exception { + mockHttpClient = mock(OptimizelyHttpClient.class); + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + + when(statusLine.getStatusCode()).thenReturn(statusCode); + when(statusLine.getReasonPhrase()).thenReturn(statusCode == 500 ? "Internal Server Error" : "OK"); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(new StringEntity(validCmabResponse)); + + when(mockHttpClient.execute(any(HttpPost.class))) + .thenReturn(httpResponse); + } + + @Test + public void testBuildRequestJson() throws Exception { + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + attributes.put("browser", "chrome"); + attributes.put("isMobile", true); + String cmabUuid = "uuid_789"; + + // Fixed: Direct method call instead of CompletableFuture + String result = cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + + assertEquals("treatment_1", result); + verify(mockHttpClient, times(1)).execute(any(HttpPost.class)); + + ArgumentCaptor request = ArgumentCaptor.forClass(HttpPost.class); + verify(mockHttpClient).execute(request.capture()); + String actualRequestBody = EntityUtils.toString(request.getValue().getEntity()); + + assertTrue(actualRequestBody.contains("\"visitorId\":\"user_456\"")); + assertTrue(actualRequestBody.contains("\"experimentId\":\"rule_123\"")); + assertTrue(actualRequestBody.contains("\"cmabUUID\":\"uuid_789\"")); + assertTrue(actualRequestBody.contains("\"browser\"")); + assertTrue(actualRequestBody.contains("\"chrome\"")); + assertTrue(actualRequestBody.contains("\"isMobile\"")); + assertTrue(actualRequestBody.contains("true")); + } + + @Test + public void returnVariationWhenStatusIs200() throws Exception { + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + attributes.put("segment", "premium"); + String cmabUuid = "uuid_789"; + + // Fixed: Direct method call instead of CompletableFuture + String result = cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + + assertEquals("treatment_1", result); + verify(mockHttpClient, times(1)).execute(any(HttpPost.class)); + + // Note: Remove this line if your implementation doesn't log this specific message + // logbackVerifier.expectMessage(Level.INFO, "CMAB returned variation 'treatment_1' for rule 'rule_123' and user 'user_456'"); + } + + @Test + public void returnErrorWhenStatusIsNot200AndLogError() throws Exception { + // Create new mock for 500 error + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(500); + when(statusLine.getReasonPhrase()).thenReturn("Internal Server Error"); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(new StringEntity("Server Error")); + when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + try { + cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + fail("Expected CmabFetchException"); + } catch (CmabFetchException e) { + assertTrue(e.getMessage().contains("Internal Server Error")); + } + + verify(mockHttpClient, times(1)).execute(any(HttpPost.class)); + // Fixed: Match actual log message format + logbackVerifier.expectMessage(Level.ERROR, "CMAB decision fetch failed with status: Internal Server Error"); + } + + @Test + public void returnErrorWhenInvalidResponseAndLogError() throws Exception { + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(new StringEntity("{\"predictions\":[]}")); + when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + try { + cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + fail("Expected CmabInvalidResponseException"); + } catch (CmabInvalidResponseException e) { + assertEquals("Invalid CMAB fetch response", e.getMessage()); + } + + verify(mockHttpClient, times(1)).execute(any(HttpPost.class)); + logbackVerifier.expectMessage(Level.ERROR, "Invalid CMAB fetch response"); + } + + @Test + public void testNoRetryWhenNoRetryConfig() throws Exception { + when(mockHttpClient.execute(any(HttpPost.class))) + .thenThrow(new IOException("Network error")); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + try { + cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + fail("Expected CmabFetchException"); + } catch (CmabFetchException e) { + assertTrue(e.getMessage().contains("Network error")); + } + + verify(mockHttpClient, times(1)).execute(any(HttpPost.class)); + logbackVerifier.expectMessage(Level.ERROR, "CMAB decision fetch failed with status: Network error"); + } + + @Test + public void testRetryOnNetworkError() throws Exception { + // Create retry config + RetryConfig retryConfig = new RetryConfig(2, 50L, 1.5, 10000); + CmabClientConfig config = new CmabClientConfig(retryConfig); + DefaultCmabClient cmabClientWithRetry = new DefaultCmabClient(mockHttpClient, config); + + // Setup response for successful retry + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(new StringEntity(validCmabResponse)); + + // First call fails with IOException, second succeeds + when(mockHttpClient.execute(any(HttpPost.class))) + .thenThrow(new IOException("Network error")) + .thenReturn(httpResponse); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + String result = cmabClientWithRetry.fetchDecision(ruleId, userId, attributes, cmabUuid); + + assertEquals("treatment_1", result); + verify(mockHttpClient, times(2)).execute(any(HttpPost.class)); + + // Fixed: Match actual retry log message format + logbackVerifier.expectMessage(Level.INFO, "Retrying CMAB request (attempt: 1) after 50 ms..."); + } + + @Test + public void testRetryExhausted() throws Exception { + RetryConfig retryConfig = new RetryConfig(2, 50L, 1.5, 10000); + CmabClientConfig config = new CmabClientConfig(retryConfig); + DefaultCmabClient cmabClientWithRetry = new DefaultCmabClient(mockHttpClient, config); + + // All calls fail + when(mockHttpClient.execute(any(HttpPost.class))) + .thenThrow(new IOException("Network error")); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + try { + cmabClientWithRetry.fetchDecision(ruleId, userId, attributes, cmabUuid); + fail("Expected CmabFetchException"); + } catch (CmabFetchException e) { + assertTrue(e.getMessage().contains("Exhausted all retries for CMAB request")); + } + + // Should attempt initial call + 2 retries = 3 total + verify(mockHttpClient, times(3)).execute(any(HttpPost.class)); + logbackVerifier.expectMessage(Level.ERROR, "CMAB decision fetch failed with status: Exhausted all retries for CMAB request"); + } + + @Test + public void testEmptyResponseThrowsException() throws Exception { + CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(new StringEntity("")); + when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + + String ruleId = "rule_123"; + String userId = "user_456"; + Map attributes = new HashMap<>(); + String cmabUuid = "uuid_789"; + + try { + cmabClient.fetchDecision(ruleId, userId, attributes, cmabUuid); + fail("Expected CmabInvalidResponseException"); + } catch (CmabInvalidResponseException e) { + assertEquals("Invalid CMAB fetch response", e.getMessage()); + } + } +} \ No newline at end of file