Skip to content

Commit 42f8899

Browse files
authored
Refact: ODPAPIManager now returns fetchQualifiedSegments list instead of string (#503)
1 parent cbaeb85 commit 42f8899

File tree

7 files changed

+40
-32
lines changed

7 files changed

+40
-32
lines changed

core-api/src/main/java/com/optimizely/ab/odp/ODPApiManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely Inc. and contributors
2+
* Copyright 2022-2023, Optimizely Inc. and contributors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,11 @@
1515
*/
1616
package com.optimizely.ab.odp;
1717

18+
import java.util.List;
1819
import java.util.Set;
1920

2021
public interface ODPApiManager {
21-
String fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck);
22+
List<String> fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck);
2223

2324
Integer sendEvents(String apiKey, String apiEndpoint, String eventPayload);
2425
}

core-api/src/main/java/com/optimizely/ab/odp/ODPEventManager.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public void identifyUser(@Nullable String vuid, @Nullable String userId) {
108108
identifiers.put(ODPUserKey.VUID.getKeyString(), vuid);
109109
}
110110
if (userId != null) {
111-
identifiers.put(ODPUserKey.FS_USER_ID.getKeyString(), userId);
111+
if (isVuid(userId)) {
112+
identifiers.put(ODPUserKey.VUID.getKeyString(), userId);
113+
} else {
114+
identifiers.put(ODPUserKey.FS_USER_ID.getKeyString(), userId);
115+
}
112116
}
113117
ODPEvent event = new ODPEvent("fullstack", "identified", identifiers, null);
114118
sendEvent(event);
@@ -125,7 +129,7 @@ public void sendEvent(ODPEvent event) {
125129
}
126130

127131
@VisibleForTesting
128-
Map<String, Object> augmentCommonData(Map<String, Object> sourceData) {
132+
protected Map<String, Object> augmentCommonData(Map<String, Object> sourceData) {
129133
// priority: sourceData > userCommonData > sdkCommonData
130134

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

142146
@VisibleForTesting
143-
Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifiers) {
147+
protected Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifiers) {
144148
// priority: sourceIdentifiers > userCommonIdentifiers
145149

146150
Map<String, String> identifiers = new HashMap<>();
@@ -149,6 +153,10 @@ Map<String, String> augmentCommonIdentifiers(Map<String, String> sourceIdentifie
149153
return identifiers;
150154
}
151155

156+
private boolean isVuid(String userId) {
157+
return userId.startsWith("vuid_");
158+
}
159+
152160
private void processEvent(ODPEvent event) {
153161
if (!isRunning) {
154162
logger.warn("Failed to Process ODP Event. ODPEventManager is not running");

core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentManager.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2022, Optimizely
3+
* Copyright 2022-2023, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -89,16 +89,7 @@ public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue, L
8989

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

92-
ResponseJsonParser parser = ResponseJsonParserFactory.getParser();
93-
String qualifiedSegmentsResponse = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments());
94-
try {
95-
qualifiedSegments = parser.parseQualifiedSegments(qualifiedSegmentsResponse);
96-
} catch (Exception e) {
97-
logger.error("Audience segments fetch failed (Error Parsing Response)");
98-
logger.debug(e.getMessage());
99-
qualifiedSegments = null;
100-
}
101-
92+
qualifiedSegments = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments());
10293
if (qualifiedSegments != null && !options.contains(ODPSegmentOption.IGNORE_CACHE)) {
10394
segmentsCache.save(cacheKey, qualifiedSegments);
10495
}

core-api/src/test/java/com/optimizely/ab/odp/ODPManagerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2022, Optimizely
3+
* Copyright 2022-2023, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -24,13 +24,14 @@
2424
import java.util.Arrays;
2525
import java.util.Collections;
2626
import java.util.HashSet;
27+
import java.util.List;
2728

2829
import static org.mockito.Matchers.*;
2930
import static org.mockito.Mockito.*;
3031
import static org.junit.Assert.*;
3132

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

3536
@Mock
3637
ODPApiManager mockApiManager;

core-api/src/test/java/com/optimizely/ab/odp/ODPSegmentManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2022, Optimizely
3+
* Copyright 2022-2023, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public class ODPSegmentManagerTest {
4343
@Mock
4444
ODPApiManager mockApiManager;
4545

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

4848
@Before
4949
public void setup() {

core-httpclient-impl/src/main/java/com/optimizely/ab/odp/DefaultODPApiManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely Inc. and contributors
2+
* Copyright 2022-2023, Optimizely Inc. and contributors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717

1818
import com.optimizely.ab.OptimizelyHttpClient;
1919
import com.optimizely.ab.annotations.VisibleForTesting;
20+
import com.optimizely.ab.odp.parser.ResponseJsonParser;
21+
import com.optimizely.ab.odp.parser.ResponseJsonParserFactory;
2022
import org.apache.http.StatusLine;
2123
import org.apache.http.client.methods.CloseableHttpResponse;
2224
import org.apache.http.client.methods.HttpPost;
@@ -28,6 +30,7 @@
2830
import java.io.IOException;
2931
import java.io.UnsupportedEncodingException;
3032
import java.util.Iterator;
33+
import java.util.List;
3134
import java.util.Set;
3235

3336
public class DefaultODPApiManager implements ODPApiManager {
@@ -144,7 +147,7 @@ String getSegmentsStringForRequest(Set<String> segmentsList) {
144147
}
145148
*/
146149
@Override
147-
public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck) {
150+
public List<String> fetchQualifiedSegments(String apiKey, String apiEndpoint, String userKey, String userValue, Set<String> segmentsToCheck) {
148151
HttpPost request = new HttpPost(apiEndpoint);
149152
String segmentsString = getSegmentsStringForRequest(segmentsToCheck);
150153

@@ -174,11 +177,14 @@ public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String u
174177
closeHttpResponse(response);
175178
return null;
176179
}
177-
180+
ResponseJsonParser parser = ResponseJsonParserFactory.getParser();
178181
try {
179-
return EntityUtils.toString(response.getEntity());
182+
return parser.parseQualifiedSegments(EntityUtils.toString(response.getEntity()));
180183
} catch (IOException e) {
181184
logger.error("Error converting ODP segments response to string", e);
185+
} catch (Exception e) {
186+
logger.error("Audience segments fetch failed (Error Parsing Response)");
187+
logger.debug(e.getMessage());
182188
} finally {
183189
closeHttpResponse(response);
184190
}
@@ -201,7 +207,6 @@ public String fetchQualifiedSegments(String apiKey, String apiEndpoint, String u
201207
"type": "fullstack"
202208
}
203209
]
204-
205210
Returns:
206211
1. null, When there was a non-recoverable error and no retry is needed.
207212
2. 0 If an unexpected error occurred and retrying can be useful.

core-httpclient-impl/src/test/java/com/optimizely/ab/odp/DefaultODPApiManagerTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely Inc. and contributors
2+
* Copyright 2022-2023, Optimizely Inc. and contributors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,13 +30,15 @@
3030

3131
import java.util.Arrays;
3232
import java.util.HashSet;
33+
import java.util.List;
3334

3435
import static org.junit.Assert.*;
3536
import static org.mockito.Matchers.any;
3637
import static org.mockito.Mockito.*;
3738

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

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

5658
when(statusLine.getStatusCode()).thenReturn(statusCode);
5759
when(httpResponse.getStatusLine()).thenReturn(statusLine);
58-
when(httpResponse.getEntity()).thenReturn(new StringEntity(validResponse));
60+
when(httpResponse.getEntity()).thenReturn(new StringEntity(validRequestResponse));
5961

6062
when(mockHttpClient.execute(any(HttpPost.class)))
6163
.thenReturn(httpResponse);
@@ -99,19 +101,19 @@ public void generateCorrectRequestBody() throws Exception {
99101
@Test
100102
public void returnResponseStringWhenStatusIs200() throws Exception {
101103
ODPApiManager apiManager = new DefaultODPApiManager(mockHttpClient);
102-
String responseString = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
104+
List<String> response = apiManager.fetchQualifiedSegments("key", "endPoint", "fs_user_id", "test_user", new HashSet<>(Arrays.asList("segment_1", "segment_2")));
103105
verify(mockHttpClient, times(1)).execute(any(HttpPost.class));
104-
assertEquals(validResponse, responseString);
106+
assertEquals(validResponse, response);
105107
}
106108

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

117119
@Test

0 commit comments

Comments
 (0)