Skip to content

Commit

Permalink
Merge pull request #241 from spoorthipujariadobe/unit_test
Browse files Browse the repository at this point in the history
unit tests + InternalMessage and PropositionItem parity with iOS
  • Loading branch information
spoorthipujariadobe authored Mar 14, 2024
2 parents ba08020 + 59d1072 commit abbe27a
Show file tree
Hide file tree
Showing 55 changed files with 4,807 additions and 1,028 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ jobs:
- android/restore-build-cache

# - run:
# name: Check Format
# command: make checkformat
# name: Check Format
# command: make checkformat

# - run:
# name: Check Style
# command: make checkstyle
- run:
name: Check Style
command: make checkstyle

- android/save-gradle-cache

Expand Down Expand Up @@ -121,4 +121,4 @@ jobs:
path: code/messaging/build/reports/androidTests

- store_test_results:
path: code/messaging/build/outputs/androidTest-results
path: code/messaging/build/outputs/androidTest-results

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class EdgePersonalizationResponseHandler {
this(parent, extensionApi, rulesEngine, feedRulesEngine, null);
}

@SuppressWarnings("NestedIfDepth")
@VisibleForTesting
EdgePersonalizationResponseHandler(final MessagingExtension parent, final ExtensionApi extensionApi, final LaunchRulesEngine rulesEngine, final FeedRulesEngine feedRulesEngine, final MessagingCacheUtilities messagingCacheUtilities) {
this.parent = parent;
Expand Down Expand Up @@ -508,23 +509,29 @@ private void updatePropositionInfo(final Map<String, PropositionInfo> newProposi
propositionInfo = tempPropositionInfoMap;
}

@SuppressWarnings("NestedForDepth")
private Map<Surface, List<Proposition>> getPropositionsFromFeedRulesEngine(final Event event) {
Map<Surface, List<Proposition>> surfacePropositions = new HashMap<>();
final Map<Surface, List<PropositionItem>> propositionItemsBySurface = feedRulesEngine.evaluate(event);
if (!MapUtils.isNullOrEmpty(propositionItemsBySurface)) {
for (final Map.Entry<Surface, List<PropositionItem>> entry: propositionItemsBySurface.entrySet()){
final List<Proposition> tempPropositions = new ArrayList<>();
for (final PropositionItem propositionItem: entry.getValue()) {
final PropositionInfo propositionInfo = this.propositionInfo.get(propositionItem.getPropositionItemId());
final PropositionInfo propositionInfo = this.propositionInfo.get(propositionItem.getItemId());
if (propositionInfo == null) {
continue;
}

final Proposition proposition = new Proposition(
propositionInfo.id,
propositionInfo.scope,
propositionInfo.scopeDetails,
new ArrayList<PropositionItem>() {{ add(propositionItem); }});
final Proposition proposition;
try {
proposition = new Proposition(
propositionInfo.id,
propositionInfo.scope,
propositionInfo.scopeDetails,
new ArrayList<PropositionItem>() {{ add(propositionItem); }});
} catch (MessageRequiredFieldMissingException e) {
continue;
}

// check to see if that proposition is already in the array (based on ID)
// if yes, append the propositionItem. if not, create a new entry for the
Expand Down Expand Up @@ -587,42 +594,18 @@ private Map<Surface, List<Proposition>> retrieveCachedPropositions(final List<Su
/**
* Creates an in-app message object then attempts to display it.
*
* @param triggeredConsequence A {@link RuleConsequence} containing an in-app message definition.
* @param propositionItem A {@link PropositionItem} containing an in-app message item data.
*/
void createInAppMessage(final RuleConsequence triggeredConsequence) {
if (triggeredConsequence == null) {
Log.debug(MessagingConstants.LOG_TAG, SELF_TAG, "Unable to create an in-app message, consequences are null.");
void createInAppMessage(final PropositionItem propositionItem) {
if (propositionItem == null) {
return;
}

final Map<String, Object> consequenceDetails = triggeredConsequence.getDetail();
if (MapUtils.isNullOrEmpty(consequenceDetails)) {
Log.warning(MessagingConstants.LOG_TAG, SELF_TAG, "Unable to create an in-app message, the consequence details are null or empty");
return;
}

final String consequenceType = DataReader.optString(consequenceDetails, MessagingConstants.EventDataKeys.RulesEngine.MESSAGE_CONSEQUENCE_DETAIL_KEY_SCHEMA, "");

// ensure we have a AJO IAM payload before creating a message
if (StringUtils.isNullOrEmpty(consequenceType)) {
Log.debug(MessagingConstants.LOG_TAG, SELF_TAG, "Unable to create an in-app message, missing consequence type.");
return;
}

if (!consequenceType.equals(MessagingConstants.SchemaValues.SCHEMA_IAM)) {
Log.debug(MessagingConstants.LOG_TAG, SELF_TAG, "Unable to create an in-app message, unknown message consequence type: %s.", consequenceType);
return;
}

try {
final Map<String, Object> detailsDataMap = DataReader.optTypedMap(Object.class, consequenceDetails, MessagingConstants.EventDataKeys.RulesEngine.MESSAGE_CONSEQUENCE_DETAIL_KEY_DATA, Collections.emptyMap());
final Map<String, Object> mobileParameters = DataReader.optTypedMap(Object.class, detailsDataMap, MessagingConstants.EventDataKeys.RulesEngine.MESSAGE_CONSEQUENCE_DETAIL_KEY_MOBILE_PARAMETERS, Collections.emptyMap());
final PresentableMessageMapper.InternalMessage message = (PresentableMessageMapper.InternalMessage) PresentableMessageMapper.getInstance().createMessage(
parent,
triggeredConsequence,
mobileParameters,
propositionItem,
messagingCacheUtilities.getAssetsMap(),
propositionInfo.get(triggeredConsequence.getId()));
propositionInfo.get(propositionItem.getItemId()));
message.trigger();
message.show();
} catch (final MessageRequiredFieldMissingException|IllegalStateException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public int getExpiryDate() {
return expiryDate;
}

@Nullable
public Map<String, Object> getMeta() {
return meta;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class InternalMessagingUtils {

static List<Proposition> getPropositionsFromPayloads(final List<Map<String, Object>> payloads) {
final List<Proposition> propositions = new ArrayList<>();
if (MessagingUtils.isNullOrEmpty(payloads)) {
return propositions;
}
for (final Map<String, Object> payload : payloads) {
if (payload != null) {
final Proposition proposition = Proposition.fromEventData(payload);
Expand All @@ -63,8 +66,15 @@ static List<Proposition> getPropositionsFromPayloads(final List<Map<String, Obje
* @return {@code JSONObject> containing the consequence details extracted from the rule json
*/
static JSONObject getConsequenceDetails(final JSONObject ruleJson) {
if (ruleJson == null) {
return null;
}
JSONObject consequenceDetails = null;
try {
JSONObject consequence = getConsequence(ruleJson);
if (consequence == null) {
return null;
}
consequenceDetails = getConsequence(ruleJson).getJSONObject(MessagingConstants.EventDataKeys.RulesEngine.MESSAGE_CONSEQUENCE_DETAIL);
} catch (final JSONException jsonException) {
Log.debug(MessagingConstants.LOG_TAG, "getConsequenceDetails", "Exception occurred retrieving consequence details: %s", jsonException.getLocalizedMessage());
Expand All @@ -79,6 +89,9 @@ static JSONObject getConsequenceDetails(final JSONObject ruleJson) {
* @return {@code JSONObject> containing the consequence extracted from the rule json
*/
static JSONObject getConsequence(final JSONObject ruleJson) {
if (ruleJson == null) {
return null;
}
JSONObject consequence = null;
try {
final JSONArray rulesArray = ruleJson.getJSONArray(MessagingConstants.EventDataKeys.RulesEngine.JSON_RULES_KEY);
Expand Down Expand Up @@ -147,12 +160,7 @@ static boolean isMessagingRequestContentEvent(final Event event) {
* @return {@code boolean} indicating if the passed in event is a refresh messages event.
*/
static boolean isRefreshMessagesEvent(final Event event) {
if (event == null || event.getEventData() == null) {
return false;
}

return EventType.MESSAGING.equalsIgnoreCase(event.getType())
&& EventSource.REQUEST_CONTENT.equalsIgnoreCase(event.getSource())
return isMessagingRequestContentEvent(event)
&& event.getEventData().containsKey(MessagingConstants.EventDataKeys.Messaging.REFRESH_MESSAGES);
}

Expand Down Expand Up @@ -289,6 +297,9 @@ static String getRequestEventId(final Event event) {
* @return {@code String} containing the ending event id
*/
static String getEndingEventId(final Event event) {
if (event == null || event.getEventData() == null) {
return null;
}
return DataReader.optString(event.getEventData(), MessagingConstants.EventDataKeys.Messaging.ENDING_EVENT_ID, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ final class MessagingCacheUtilities {
private final String assetCacheLocation;
private final String METADATA_KEY_PATH_TO_FILE = "pathToFile";
private final Map<String, String> assetMap = new HashMap<>();
private ObjectInputStream objectInputStream;
private ObjectOutputStream objectOutputStream;

public MessagingCacheUtilities() {
this.cacheService = ServiceProvider.getInstance().getCacheService();
Expand Down Expand Up @@ -96,9 +94,7 @@ Map<Surface, List<Proposition>> getCachedPropositions() {
ObjectInputStream objectInputStream = null;
Map<Surface, List<Proposition>> cachedPropositions = new HashMap<>();
try {
if (objectInputStream == null) {
objectInputStream = new ObjectInputStream(cacheResult.getData());
}
objectInputStream = new ObjectInputStream(cacheResult.getData());

final Object cachedData = objectInputStream.readObject();
if (cachedData == null) {
Expand All @@ -120,10 +116,8 @@ Map<Surface, List<Proposition>> getCachedPropositions() {
} else if (firstElement instanceof PropositionPayload) {
// handle cached PropositionPayload objects
final Map<Surface, List<PropositionPayload>> cachedPropositionPayloads = (Map<Surface, List<PropositionPayload>>) cachedData;
if (!MapUtils.isNullOrEmpty(cachedPropositionPayloads)) {
for (final Map.Entry<Surface, List<PropositionPayload>> entry : cachedPropositionPayloads.entrySet()) {
cachedPropositions.put(entry.getKey(), convertToPropositions(entry.getValue()));
}
for (final Map.Entry<Surface, List<PropositionPayload>> entry : cachedPropositionPayloads.entrySet()) {
cachedPropositions.put(entry.getKey(), convertToPropositions(entry.getValue()));
}
}
} catch (final NullPointerException nullPointerException) {
Expand Down Expand Up @@ -157,11 +151,15 @@ void cachePropositions(final Map<Surface, List<Proposition>> newPropositions, fi
final Map<Surface, List<Proposition>> cachedPropositions = getCachedPropositions();
final Map<Surface, List<Proposition>> updatedPropositions = cachedPropositions != null ? cachedPropositions : new HashMap<>();
updatedPropositions.putAll(newPropositions);
final List<Surface> propositionsToRemove = new ArrayList<>();
for (final Map.Entry<Surface, List<Proposition>> entry : updatedPropositions.entrySet()) {
if (surfacesToRemove.contains(entry.getKey())) {
updatedPropositions.remove(entry);
propositionsToRemove.add(entry.getKey());
}
}
for (final Surface surface : propositionsToRemove) {
updatedPropositions.remove(surface);
}

// clean any existing cached propositions first if the provided propositions are null or empty
final Map<Surface, List<Proposition>> propositions = new HashMap<>(updatedPropositions);
Expand All @@ -174,12 +172,10 @@ void cachePropositions(final Map<Surface, List<Proposition>> newPropositions, fi
Log.debug(MessagingConstants.LOG_TAG, SELF_TAG, "Creating new cached propositions");
ByteArrayOutputStream byteArrayOutputStream = null;
InputStream inputStream = null;

ObjectOutputStream objectOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
if (objectOutputStream == null) {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
}
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(propositions);
objectOutputStream.flush();
inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
Expand Down Expand Up @@ -213,12 +209,17 @@ void cachePropositions(final Map<Surface, List<Proposition>> newPropositions, fi
private List<Proposition> convertToPropositions(final List<PropositionPayload> propositionPayloads) {
final List<Proposition> propositions = new ArrayList<>();
final List<PropositionItem> propositionItems = new ArrayList<>();
for (final PropositionPayload propositionPayload : propositionPayloads) {
for (final PayloadItem payloadItem : propositionPayload.items) {
final PropositionItem propositionItem = new PropositionItem(payloadItem.id, SchemaType.fromString(payloadItem.schema), payloadItem.data);
propositionItems.add(propositionItem);
try {
for (final PropositionPayload propositionPayload : propositionPayloads) {
for (final PayloadItem payloadItem : propositionPayload.items) {
final PropositionItem propositionItem = new PropositionItem(payloadItem.id, SchemaType.fromString(payloadItem.schema), payloadItem.data);
propositionItems.add(propositionItem);
}
propositions.add(new Proposition(propositionPayload.propositionInfo.id, propositionPayload.propositionInfo.scope, propositionPayload.propositionInfo.scopeDetails, propositionItems));

}
propositions.add(new Proposition(propositionPayload.propositionInfo.id, propositionPayload.propositionInfo.scope, propositionPayload.propositionInfo.scopeDetails, propositionItems));
} catch (final MessageRequiredFieldMissingException exception) {
Log.warning(MessagingConstants.LOG_TAG, SELF_TAG, "Exception occurred creating Proposition: %s", exception.getLocalizedMessage());
}
return propositions;
}
Expand Down Expand Up @@ -281,14 +282,4 @@ private boolean assetIsDownloadable(final String asset) {
Map<String, String> getAssetsMap() {
return assetMap;
}

@VisibleForTesting
void setObjectInputStream(final ObjectInputStream objectInputStream) {
this.objectInputStream = objectInputStream;
}

@VisibleForTesting
void setObjectOutputStream(final ObjectOutputStream objectOutputStream) {
this.objectOutputStream = objectOutputStream;
}
}
Loading

0 comments on commit abbe27a

Please sign in to comment.