Skip to content

Refact: ODPAPIManager now returns fetchQualifiedSegments list instead of string #503

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

Merged
merged 7 commits into from
Feb 8, 2023
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 2022, Optimizely Inc. and contributors
* Copyright 2022-2023, Optimizely Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,11 @@
*/
package com.optimizely.ab.odp;

import java.util.List;
import java.util.Set;

public interface ODPApiManager {
String fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck);
List<String> fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck);

Integer sendEvents(String apiKey, String apiEndpoint, String eventPayload);
}
14 changes: 11 additions & 3 deletions core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public void identifyUser(@Nullable String vuid, @Nullable String userId) {
identifiers.put(ODPUserKey.VUID.getKeyString(), vuid);
}
if (userId != null) {
identifiers.put(ODPUserKey.FS_USER_ID.getKeyString(), userId);
if (isVuid(userId)) {
identifiers.put(ODPUserKey.VUID.getKeyString(), userId);
} else {
identifiers.put(ODPUserKey.FS_USER_ID.getKeyString(), userId);
}
}
ODPEvent event = new ODPEvent("fullstack", "identified", identifiers, null);
sendEvent(event);
Expand All @@ -125,7 +129,7 @@ public void sendEvent(ODPEvent event) {
}

@VisibleForTesting
Map<String, Object> augmentCommonData(Map<String, Object> sourceData) {
protected Map<String, Object> augmentCommonData(Map<String, Object> sourceData) {
// priority: sourceData > userCommonData > sdkCommonData

Map<String, Object> data = new HashMap<>();
Expand All @@ -140,7 +144,7 @@ Map<String, Object> augmentCommonData(Map<String, Object> sourceData) {
}

@VisibleForTesting
Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifiers) {
protected Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifiers) {
// priority: sourceIdentifiers > userCommonIdentifiers

Map<String, String> identifiers = new HashMap<>();
Expand All @@ -149,6 +153,10 @@ Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifie
return identifiers;
}

private boolean isVuid(String userId) {
return userId.startsWith("vuid_");
}

private void processEvent(ODPEvent event) {
if (!isRunning) {
logger.warn("Failed to Process ODP Event. ODPEventManager is not running");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2022, Optimizely
* Copyright 2022-2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,16 +89,7 @@ public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue, L

logger.debug("ODP Cache Miss. Making a call to ODP Server.");

ResponseJsonParser parser = ResponseJsonParserFactory.getParser();
String qualifiedSegmentsResponse = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments());
try {
qualifiedSegments = parser.parseQualifiedSegments(qualifiedSegmentsResponse);
} catch (Exception e) {
logger.error("Audience segments fetch failed (Error Parsing Response)");
logger.debug(e.getMessage());
qualifiedSegments = null;
}

qualifiedSegments = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments());
if (qualifiedSegments != null && !options.contains(ODPSegmentOption.IGNORE_CACHE)) {
segmentsCache.save(cacheKey, qualifiedSegments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2022, Optimizely
* Copyright 2022-2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,13 +24,14 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

public class ODPManagerTest {
private static final String API_RESPONSE = "{\"data\":{\"customer\":{\"audiences\":{\"edges\":[{\"node\":{\"name\":\"segment1\",\"state\":\"qualified\"}},{\"node\":{\"name\":\"segment2\",\"state\":\"qualified\"}}]}}}}";
private static final List<String> API_RESPONSE = Arrays.asList(new String[]{"segment1", "segment2"});

@Mock
ODPApiManager mockApiManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2022, Optimizely
* Copyright 2022-2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,7 +43,7 @@ public class ODPSegmentManagerTest {
@Mock
ODPApiManager mockApiManager;

private static final String API_RESPONSE = "{\"data\":{\"customer\":{\"audiences\":{\"edges\":[{\"node\":{\"name\":\"segment1\",\"state\":\"qualified\"}},{\"node\":{\"name\":\"segment2\",\"state\":\"qualified\"}}]}}}}";
private static final List<String> API_RESPONSE = Arrays.asList(new String[]{"segment1", "segment2"});

@Before
public void setup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, Optimizely Inc. and contributors
* Copyright 2022-2023, Optimizely Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import com.optimizely.ab.OptimizelyHttpClient;
import com.optimizely.ab.annotations.VisibleForTesting;
import com.optimizely.ab.odp.parser.ResponseJsonParser;
import com.optimizely.ab.odp.parser.ResponseJsonParserFactory;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -28,6 +30,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class DefaultODPApiManager implements ODPApiManager {
Expand Down Expand Up @@ -144,7 +147,7 @@ String getSegmentsStringForRequest(Set<String> segmentsList) {
}
*/
@Override
public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck) {
public List<String> fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck) {
HttpPost request = new HttpPost(apiEndpoint);
String segmentsString = getSegmentsStringForRequest(segmentsToCheck);

Expand Down Expand Up @@ -174,11 +177,14 @@ public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String u
closeHttpResponse(response);
return null;
}

ResponseJsonParser parser = ResponseJsonParserFactory.getParser();
try {
return EntityUtils.toString(response.getEntity());
return parser.parseQualifiedSegments(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
logger.error("Error converting ODP segments response to string", e);
} catch (Exception e) {
logger.error("Audience segments fetch failed (Error Parsing Response)");
logger.debug(e.getMessage());
} finally {
closeHttpResponse(response);
}
Expand All @@ -201,7 +207,6 @@ public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String u
"type": "fullstack"
}
]

Returns:
1. null, When there was a non-recoverable error and no retry is needed.
2. 0 If an unexpected error occurred and retrying can be useful.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, Optimizely Inc. and contributors
* Copyright 2022-2023, Optimizely Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,13 +30,15 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

public class DefaultODPApiManagerTest {
private static final String validResponse = "{\"data\":{\"customer\":{\"audiences\":{\"edges\":[{\"node\":{\"name\":\"has_email\",\"state\":\"qualified\"}},{\"node\":{\"name\":\"has_email_opted_in\",\"state\":\"qualified\"}}]}}}}";
private static final List<String> validResponse = Arrays.asList(new String[] {"has_email", "has_email_opted_in"});
private static final String validRequestResponse = "{\"data\":{\"customer\":{\"audiences\":{\"edges\":[{\"node\":{\"name\":\"has_email\",\"state\":\"qualified\"}},{\"node\":{\"name\":\"has_email_opted_in\",\"state\":\"qualified\"}}]}}}}";

@Rule
public LogbackVerifier logbackVerifier = new LogbackVerifier();
Expand All @@ -55,7 +57,7 @@ private void setupHttpClient(int statusCode) throws Exception {

when(statusLine.getStatusCode()).thenReturn(statusCode);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(httpResponse.getEntity()).thenReturn(new StringEntity(validResponse));
when(httpResponse.getEntity()).thenReturn(new StringEntity(validRequestResponse));

when(mockHttpClient.execute(any(HttpPost.class)))
.thenReturn(httpResponse);
Expand Down Expand Up @@ -99,19 +101,19 @@ public void generateCorrectRequestBody() throws Exception {
@Test
public void returnResponseStringWhenStatusIs200() throws Exception {
ODPApiManager apiManager = new DefaultODPApiManager(mockHttpClient);
String responseString = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
List<String> response = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
verify(mockHttpClient, times(1)).execute(any(HttpPost.class));
assertEquals(validResponse, responseString);
assertEquals(validResponse, response);
}

@Test
public void returnNullWhenStatusIsNot200AndLogError() throws Exception {
setupHttpClient(500);
ODPApiManager apiManager = new DefaultODPApiManager(mockHttpClient);
String responseString = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
List<String> response = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
verify(mockHttpClient, times(1)).execute(any(HttpPost.class));
logbackVerifier.expectMessage(Level.ERROR, "Unexpected response from ODP server, Response code: 500, null");
assertNull(responseString);
assertNull(response);
}

@Test
Expand Down