Skip to content

Commit

Permalink
Merge pull request #90 from adobe/staging
Browse files Browse the repository at this point in the history
Staging -> Main
  • Loading branch information
spoorthipujariadobe authored Aug 20, 2024
2 parents a41b4a4 + 597e5e7 commit 1ad151a
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 36 deletions.
2 changes: 1 addition & 1 deletion code/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ org.gradle.caching=true
android.useAndroidX=true

moduleName=optimize
moduleVersion=3.0.1
moduleVersion=3.0.2

#Maven artifact
mavenRepoName=AdobeMobileOptimizeSdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,170 @@ public void call(
Assert.assertEquals(1, offer.getCharacteristics().size());
Assert.assertEquals("true", offer.getCharacteristics().get("testing"));
}
// 7a
@Test
public void testGetPropositions_defaultContentItem() throws InterruptedException, IOException {
// setup
final Map<String, Object> configData = new HashMap<>();
configData.put("edge.configId", "ffffffff-ffff-ffff-ffff-ffffffffffff");
updateConfiguration(configData);

final String decisionScopeString = "someDecisionScope";
Optimize.updatePropositions(
Collections.singletonList(new DecisionScope(decisionScopeString)), null, null);
List<Event> eventsListEdge =
TestHelper.getDispatchedEventsWith(
OptimizeTestConstants.EventType.EDGE,
OptimizeTestConstants.EventSource.REQUEST_CONTENT,
1000);
Assert.assertEquals(1, eventsListEdge.size());
Event edgeEvent = eventsListEdge.get(0);
final String requestEventId = edgeEvent.getUniqueIdentifier();
Assert.assertFalse(requestEventId.isEmpty());

// Send Edge Response event
final String edgeResponseData =
"{\r\n"
+ " \"payload\": [\r\n"
+ " {\r\n"
+ " \"scopeDetails\": {\r\n"
+ " \"characteristics\": {\r\n"
+ " \"eventToken\": \"someEventToken\"\r\n"
+ " },\r\n"
+ " \"activity\": {\r\n"
+ " \"id\": \"716226\"\r\n"
+ " },\r\n"
+ " \"strategies\": [\r\n"
+ " {\r\n"
+ " \"trafficType\": \"0\",\r\n"
+ " \"step\": \"entry\"\r\n"
+ " },\r\n"
+ " {\r\n"
+ " \"trafficType\": \"0\",\r\n"
+ " \"step\": \"display\"\r\n"
+ " }\r\n"
+ " ],\r\n"
+ " \"correlationID\": \"716226:0:0\",\r\n"
+ " \"decisionProvider\": \"TGT\",\r\n"
+ " \"experience\": {\r\n"
+ " \"id\": \"0\"\r\n"
+ " }\r\n"
+ " },\r\n"
+ " \"scope\": \"someDecisionScope\",\r\n"
+ " \"id\": \"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\r\n"
+ " \"items\": [\r\n"
+ " {\r\n"
+ " \"schema\":"
+ " \"https://ns.adobe.com/personalization/default-content-item\",\r\n"
+ " \"meta\": {\r\n"
+ " \"activity.name\": \"Some Test Activity\",\r\n"
+ " \"profile.timeNow\": \"1722212083855\",\r\n"
+ " \"profile.audienceUserNeed\": \"\",\r\n"
+ " \"profile.language\": \"\",\r\n"
+ " \"experience.name\": \"Default Content\",\r\n"
+ " \"profile.site\": \"\",\r\n"
+ " \"profile.url\": \"\",\r\n"
+ " \"profile.subjects\": \"\",\r\n"
+ " \"profile.path\": \"\",\r\n"
+ " \"profile.subjectPrimary\": \"\",\r\n"
+ " \"profile.translatedTabbedShelfTitle\": \"Discover SBS in 63"
+ " Languages\",\r\n"
+ " \"profile.environment\": \"production\",\r\n"
+ " \"profile.brandName\": \"\",\r\n"
+ " \"profile.audioChannelLastPlayed\": \"\",\r\n"
+ " \"profile.type\": \"\"\r\n"
+ " },\r\n"
+ " \"id\": \"0\"\r\n"
+ " }\r\n"
+ " ]\r\n"
+ " }\r\n"
+ " ],\r\n"
+ " \"requestId\": \"someRequestId\",\r\n"
+ " \"requestEventId\": \""
+ requestEventId
+ "\",\r\n \"type\": \"personalization:decisions\"\r\n }";

ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> eventData =
objectMapper.readValue(
edgeResponseData, new TypeReference<Map<String, Object>>() {});

Event event =
new Event.Builder(
"AEP Response Event Handle",
OptimizeTestConstants.EventType.EDGE,
OptimizeTestConstants.EventSource.PERSONALIZATION)
.setEventData(eventData)
.build();

// Action
MobileCore.dispatchEvent(event);

Thread.sleep(1000);

// Send completion event
Map<String, Object> completionEventData =
new HashMap<String, Object>() {
{
put("completedUpdateRequestForEventId", requestEventId);
}
};
Event completionEvent =
new Event.Builder(
"Optimize Update Propositions Complete",
OptimizeTestConstants.EventType.OPTIMIZE,
OptimizeTestConstants.EventSource.CONTENT_COMPLETE)
.setEventData(completionEventData)
.build();
MobileCore.dispatchEvent(completionEvent);

Thread.sleep(1000);
TestHelper.resetTestExpectations();
DecisionScope decisionScope = new DecisionScope(decisionScopeString);
final Map<DecisionScope, OptimizeProposition> propositionMap = new HashMap<>();
final ADBCountDownLatch countDownLatch = new ADBCountDownLatch(1);
Optimize.getPropositions(
Collections.singletonList(decisionScope),
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
public void fail(AdobeError adobeError) {
Assert.fail("Error in getting cached propositions");
}

@Override
public void call(
Map<DecisionScope, OptimizeProposition> decisionScopePropositionMap) {
propositionMap.putAll(decisionScopePropositionMap);
countDownLatch.countDown();
}
});

countDownLatch.await(1, TimeUnit.SECONDS);
// Assertions
List<Event> optimizeResponseEventsList =
TestHelper.getDispatchedEventsWith(
OptimizeTestConstants.EventType.OPTIMIZE,
OptimizeTestConstants.EventSource.RESPONSE_CONTENT);

Assert.assertNotNull(optimizeResponseEventsList);
Assert.assertEquals(1, optimizeResponseEventsList.size());
Assert.assertNull(optimizeResponseEventsList.get(0).getEventData().get("responseerror"));
Assert.assertEquals(1, propositionMap.size());
OptimizeProposition optimizeProposition = propositionMap.get(decisionScope);
Assert.assertNotNull(optimizeProposition);
Assert.assertEquals("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", optimizeProposition.getId());
Assert.assertEquals("someDecisionScope", optimizeProposition.getScope());
Assert.assertEquals(1, optimizeProposition.getOffers().size());

Offer offer = optimizeProposition.getOffers().get(0);
Assert.assertEquals("0", offer.getId());
Assert.assertEquals(null, offer.getEtag());
Assert.assertEquals(0, offer.getScore());
Assert.assertEquals(
"https://ns.adobe.com/personalization/default-content-item", offer.getSchema());
Assert.assertEquals(OfferType.UNKNOWN, offer.getType());
Assert.assertEquals("", offer.getContent());
}

// 7b
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class OptimizeTestConstants {

static final String EXTENSION_VERSION = "3.0.1";
static final String EXTENSION_VERSION = "3.0.2";
public static final String LOG_TAG = "OptimizeTest";
static final String CONFIG_DATA_STORE = "AdobeMobile_ConfigState";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ static Offer fromEventData(final Map<String, Object> data) {

if (!OptimizeUtils.isNullOrEmpty(offerData)) {
final String nestedId =
(String) offerData.get(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_ID);
DataReader.getString(
offerData, OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_ID);
if (OptimizeUtils.isNullOrEmpty(id) || !id.equals(nestedId)) {
Log.debug(
OptimizeConstants.LOG_TAG,
Expand All @@ -472,16 +473,15 @@ static Offer fromEventData(final Map<String, Object> data) {
}

final String format =
(String) offerData.get(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_FORMAT);
if (OptimizeUtils.isNullOrEmpty(format)) {
Log.debug(
OptimizeConstants.LOG_TAG,
SELF_TAG,
"Cannot create Offer object, provided data Map doesn't contain valid"
+ " item data format.");
return null;
}

DataReader.getString(
offerData, OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_FORMAT);
final OfferType offerType =
(format != null)
? OfferType.from(format)
: OfferType.from(
DataReader.getString(
offerData,
OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_TYPE));
final List<String> language =
DataReader.getStringList(
offerData, OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_LANGUAGE);
Expand All @@ -496,10 +496,10 @@ static Offer fromEventData(final Map<String, Object> data) {
} else if (offerData.containsKey(
OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_DELIVERYURL)) {
content =
(String)
offerData.get(
OptimizeConstants.JsonKeys
.PAYLOAD_ITEM_DATA_DELIVERYURL);
DataReader.optString(
offerData,
OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_DELIVERYURL,
null);
}
if (content == null) {
Log.debug(
Expand All @@ -510,7 +510,7 @@ static Offer fromEventData(final Map<String, Object> data) {
return null;
}

return new Builder(id, OfferType.from(format), content)
return new Builder(id, offerType, content)
.setEtag(etag)
.setScore(score)
.setSchema(schema)
Expand Down Expand Up @@ -564,8 +564,8 @@ Map<String, Object> toEventData() {
offerMap.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_META, this.meta);

final Map<String, Object> data = new HashMap<>();
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_ID, this.id);
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_FORMAT, this.type.toString());
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_ID, this.id);
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_TYPE, this.type.toString());
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_CONTENT, this.content);
data.put(OptimizeConstants.JsonKeys.PAYLOAD_ITEM_DATA_LANGUAGE, this.language);
data.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class OptimizeConstants {
static final String LOG_TAG = "Optimize";
static final String EXTENSION_VERSION = "3.0.1";
static final String EXTENSION_VERSION = "3.0.2";
static final String EXTENSION_NAME = "com.adobe.optimize";
static final String FRIENDLY_NAME = "Optimize";
static final long DEFAULT_RESPONSE_CALLBACK_TIMEOUT = 500L;
Expand Down Expand Up @@ -129,6 +129,7 @@ static final class JsonKeys {
static final String PAYLOAD_ITEM_DATA_CONTENT = "content";
static final String PAYLOAD_ITEM_DATA_DELIVERYURL = "deliveryURL";
static final String PAYLOAD_ITEM_DATA_FORMAT = "format";
static final String PAYLOAD_ITEM_DATA_TYPE = "type";
static final String PAYLOAD_ITEM_DATA_LANGUAGE = "language";
static final String PAYLOAD_ITEM_DATA_CHARACTERISTICS = "characteristics";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class OptimizeExtension extends Extension {

private static final String SELF_TAG = "OptimizeExtension";
private Map<DecisionScope, OptimizeProposition> cachedPropositions;

// Concurrent Map containing the cached propositions returned in various
// personalization:decisions events
// for the same Edge personalization request.
// This is accessed from multiple threads.
private Map<DecisionScope, OptimizeProposition> cachedPropositions = new ConcurrentHashMap<>();

// Events dispatcher used to maintain the processing order of update and get propositions
// events.
Expand All @@ -59,13 +65,18 @@ public boolean doWork(final Event event) {
}
});

// Map containing the update event IDs (and corresponding requested scopes) for Edge events that
// haven't yet received an Edge completion response.
private Map<String, List<DecisionScope>> updateRequestEventIdsInProgress = new HashMap<>();
// Concurrent Map containing the update event IDs (and corresponding requested scopes) for Edge
// events that haven't yet received an Edge completion response.
// This is accessed from multiple threads.
private final Map<String, List<DecisionScope>> updateRequestEventIdsInProgress =
new ConcurrentHashMap<>();

// a dictionary to accumulate propositions returned in various personalization:decisions events
// Concurrent Map to accumulate propositions returned in various personalization:decisions
// events
// for the same Edge personalization request.
private Map<DecisionScope, OptimizeProposition> propositionsInProgress = new HashMap<>();
// This is accessed from multiple threads.
private final Map<DecisionScope, OptimizeProposition> propositionsInProgress =
new ConcurrentHashMap<>();

// List containing the schema strings for the proposition items supported by the SDK, sent in
// the personalization query request.
Expand Down Expand Up @@ -107,8 +118,6 @@ public boolean doWork(final Event event) {
*/
protected OptimizeExtension(final ExtensionApi extensionApi) {
super(extensionApi);

cachedPropositions = new HashMap<>();
}

@Override
Expand Down Expand Up @@ -868,7 +877,8 @@ Map<DecisionScope, OptimizeProposition> getPropositionsInProgress() {
@VisibleForTesting
void setPropositionsInProgress(
final Map<DecisionScope, OptimizeProposition> propositionsInProgress) {
this.propositionsInProgress = propositionsInProgress;
this.propositionsInProgress.clear();
this.propositionsInProgress.putAll(propositionsInProgress);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,39 +318,39 @@ public void testFromEventData_invalidOfferNoContent() throws Exception {
}

@Test
public void testFromEventData_invalidOfferNoFormat() throws Exception {
public void testFromEventData_invalidOfferNoItemData() throws Exception {
Map<String, Object> offerData =
new ObjectMapper()
.readValue(
getClass()
.getClassLoader()
.getResource("json/OFFER_INVALID_MISSING_FORMAT.json"),
.getResource("json/OFFER_INVALID_MISSING_ITEM_DATA.json"),
HashMap.class);
final Offer offer = Offer.fromEventData(offerData);
Assert.assertNull(offer);
}

@Test
public void testFromEventData_invalidOfferNoItemData() throws Exception {
public void testFromEventData_invalidOfferIdMismatch() throws Exception {
Map<String, Object> offerData =
new ObjectMapper()
.readValue(
getClass()
.getClassLoader()
.getResource("json/OFFER_INVALID_MISSING_ITEM_DATA.json"),
.getResource("json/OFFER_INVALID_ID_MISMATCH.json"),
HashMap.class);
final Offer offer = Offer.fromEventData(offerData);
Assert.assertNull(offer);
}

@Test
public void testFromEventData_invalidOfferIdMismatch() throws Exception {
public void testFromEventData_invalidOfferFormatTypeIsNotString() throws Exception {
Map<String, Object> offerData =
new ObjectMapper()
.readValue(
getClass()
.getClassLoader()
.getResource("json/OFFER_INVALID_ID_MISMATCH.json"),
.getResource("json/OFFER_INVALID_FORMAT_TYPE.json"),
HashMap.class);
final Offer offer = Offer.fromEventData(offerData);
Assert.assertNull(offer);
Expand Down
Loading

0 comments on commit 1ad151a

Please sign in to comment.