Skip to content
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

Staging to Main #100

Merged
merged 6 commits into from
Oct 8, 2024
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ functional-test-coverage:

javadoc:
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) javadocJar)
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) dokkaJavadoc)

assemble-phone:
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) assemblePhone)
Expand All @@ -55,4 +56,4 @@ ci-publish-staging: assemble-phone-release
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) publishReleasePublicationToSonatypeRepository)

ci-publish: assemble-phone-release
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) publishReleasePublicationToSonatypeRepository -Prelease)
(./code/gradlew -p code/$(EXTENSION-LIBRARY-FOLDER-NAME) publishReleasePublicationToSonatypeRepository -Prelease)
4 changes: 2 additions & 2 deletions code/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ org.gradle.caching=true
android.useAndroidX=true

moduleName=optimize
moduleVersion=3.0.2
moduleVersion=3.1.0

#Maven artifact
mavenRepoName=AdobeMobileOptimizeSdk
mavenRepoDescription=Adobe Experience Platform Optimize extension for the Adobe Experience Platform Mobile SDK
mavenUploadDryRunFlag=false

# production versions for production build
mavenCoreVersion=3.0.0
mavenCoreVersion=3.2.0
mavenEdgeVersion=3.0.0
functionalTestEdgeVersion=3.0.0
1 change: 1 addition & 0 deletions code/optimize/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ val mavenEdgeVersion: String by project

aepLibrary {
namespace = "com.adobe.marketing.mobile.optimize"
enableDokkaDoc = true
enableSpotless = true
enableCheckStyle = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ public void testExtensionVersion() {
Assert.assertEquals(OptimizeTestConstants.EXTENSION_VERSION, Optimize.extensionVersion());
}

@Test
public void testUpdatePropositions_timeoutError() throws Exception {
// Setup
final String decisionScopeName = "decisionScope";
Map<String, Object> configData = new HashMap<>();
configData.put("edge.configId", "ffffffff-ffff-ffff-ffff-ffffffffffff");
updateConfiguration(configData);

// Action
Optimize.updatePropositions(
Collections.singletonList(new DecisionScope(decisionScopeName)),
null,
null,
new AdobeCallbackWithOptimizeError<Map<DecisionScope, OptimizeProposition>>() {
@Override
public void fail(AEPOptimizeError error) {
Assert.fail(OptimizeConstants.ErrorData.Timeout.DETAIL);
Assert.assertEquals(
OptimizeConstants.ErrorData.Timeout.STATUS, error.getStatus());
Assert.assertEquals(
OptimizeConstants.ErrorData.Timeout.TITLE, error.getTitle());
Assert.assertEquals(
OptimizeConstants.ErrorData.Timeout.DETAIL, error.getDetail());
}

@Override
public void call(
Map<DecisionScope, OptimizeProposition> decisionScopePropositionMap) {
Assert.assertNull(decisionScopePropositionMap);
}
});
}

// 2a
@Test
public void testUpdatePropositions_validDecisionScope() throws InterruptedException {
Expand Down Expand Up @@ -819,8 +852,11 @@ public void call(
OptimizeTestConstants.EventSource.RESPONSE_CONTENT);

Assert.assertNotNull(optimizeResponseEventsList);
Assert.assertEquals(1, optimizeResponseEventsList.size());
Assert.assertNull(optimizeResponseEventsList.get(0).getEventData().get("responseerror"));

// 1 additional event is being sent from handleUpdatePropositions() to provide callback for
// updatePropositons()
Assert.assertEquals(2, optimizeResponseEventsList.size());

Assert.assertEquals(1, propositionMap.size());
OptimizeProposition optimizeProposition = propositionMap.get(decisionScope);
Assert.assertNotNull(optimizeProposition);
Expand Down Expand Up @@ -1208,6 +1244,7 @@ public void call(
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());
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.2";
static final String EXTENSION_VERSION = "3.1.0";
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
@@ -0,0 +1,122 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you 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 http://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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

package com.adobe.marketing.mobile.optimize

import com.adobe.marketing.mobile.AdobeError

/**
* AEPOptimizeError class is used to create AEPOptimizeError from error details received from Experience Edge.
* @param type The type of error that occurred.
* @param status The HTTP status code of the error.
* @param title The title of the error.
* @param detail The details of the error.
* @param report The report of the error.
* @param adobeError The corresponding AdobeError.
*/

data class AEPOptimizeError(
val type: String? = "",
val status: Int? = 0,
val title: String? = "",
val detail: String? = "",
var report: Map<String, Any>?,
var adobeError: AdobeError?
) {

fun toEventData(): Map<String, Any?> = mapOf(
TYPE to type,
STATUS to status,
TITLE to title,
DETAIL to detail,
REPORT to report,
ADOBE_ERROR to adobeError?.toEventData()
)

companion object {
const val TYPE = "type"
const val STATUS = "status"
const val TITLE = "title"
const val DETAIL = "detail"
const val REPORT = "report"
const val ADOBE_ERROR = "adobeError"
const val ERROR_NAME = "errorName"
const val ERROR_CODE = "errorCode"

private val serverErrors = listOf(
OptimizeConstants.HTTPResponseCodes.tooManyRequests,
OptimizeConstants.HTTPResponseCodes.internalServerError,
OptimizeConstants.HTTPResponseCodes.serviceUnavailable
)

private val networkErrors = listOf(
OptimizeConstants.HTTPResponseCodes.badGateway,
OptimizeConstants.HTTPResponseCodes.gatewayTimeout
)

fun AdobeError.toEventData(): Map<String, Any?> = mapOf(
ERROR_NAME to errorName,
ERROR_CODE to errorCode,
)

@JvmStatic
fun toAEPOptimizeError(data: Map<String, Any?>): AEPOptimizeError {
return AEPOptimizeError(
type = data[TYPE] as? String ?: "",
status = data[STATUS] as? Int ?: 0,
title = data[TITLE] as? String ?: "",
detail = data[DETAIL] as? String ?: "",
report = data[REPORT] as? Map<String, Any>,
adobeError = toAdobeError(data[ADOBE_ERROR] as Map<String, Any?>)
)
}

@JvmStatic
fun toAdobeError(data: Map<String, Any?>): AdobeError {
return getAdobeErrorFromStatus(data[STATUS] as Int?)
}

fun getTimeoutError(): AEPOptimizeError {
return AEPOptimizeError(
null,
OptimizeConstants.ErrorData.Timeout.STATUS,
OptimizeConstants.ErrorData.Timeout.TITLE,
OptimizeConstants.ErrorData.Timeout.DETAIL,
null,
AdobeError.CALLBACK_TIMEOUT
)
}

fun getUnexpectedError(): AEPOptimizeError {
return AEPOptimizeError(
null,
null,
OptimizeConstants.ErrorData.Unexpected.TITLE,
OptimizeConstants.ErrorData.Unexpected.DETAIL,
null,
AdobeError.UNEXPECTED_ERROR
)
}

private fun getAdobeErrorFromStatus(status: Int?): AdobeError = when {
status == OptimizeConstants.HTTPResponseCodes.clientTimeout -> AdobeError.CALLBACK_TIMEOUT
serverErrors.contains(status) -> AdobeError.SERVER_ERROR
networkErrors.contains(status) -> AdobeError.NETWORK_ERROR
else -> AdobeError.UNEXPECTED_ERROR
}
}

init {
if (adobeError == null) {
adobeError = getAdobeErrorFromStatus(status)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you 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 http://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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

package com.adobe.marketing.mobile.optimize;

import com.adobe.marketing.mobile.AdobeCallback;

public interface AdobeCallbackWithOptimizeError<T> extends AdobeCallback<T> {
void fail(final AEPOptimizeError error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,43 @@ public static void updatePropositions(
@NonNull final List<DecisionScope> decisionScopes,
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data) {

updatePropositions(decisionScopes, xdm, data, null);
}

/**
* This API dispatches an Event for the Edge network extension to fetch decision propositions,
* for the provided decision scopes list, from the decisioning services enabled in the
* Experience Edge network.
*
* <p>The returned decision propositions are cached in-memory in the Optimize SDK extension and
* can be retrieved using {@link #getPropositions(List, AdobeCallback)} API.
*
* @param decisionScopes {@code List<DecisionScope>} containing scopes for which offers need to
* be updated.
* @param xdm {@code Map<String, Object>} containing additional XDM-formatted data to be sent in
* the personalization query request.
* @param data {@code Map<String, Object>} containing additional free-form data to be sent in
* the personalization query request.
* @param callback {@code AdobeCallback<Map<DecisionScope, OptimizeProposition>>} which will be
* invoked when decision propositions are received from the Edge network.
*/
public static void updatePropositions(
@NonNull final List<DecisionScope> decisionScopes,
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data,
@Nullable final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {

if (OptimizeUtils.isNullOrEmpty(decisionScopes)) {
Log.warning(
OptimizeConstants.LOG_TAG,
SELF_TAG,
"Cannot update propositions, provided list of decision scopes is null or"
+ " empty.");

AEPOptimizeError aepOptimizeError = AEPOptimizeError.Companion.getUnexpectedError();
failWithOptimizeError(callback, aepOptimizeError);

return;
}

Expand Down Expand Up @@ -115,7 +146,82 @@ public static void updatePropositions(
.setEventData(eventData)
.build();

MobileCore.dispatchEvent(event);
MobileCore.dispatchEventWithResponseCallback(
event,
OptimizeConstants.EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT,
new AdobeCallbackWithError<Event>() {
@Override
public void fail(final AdobeError adobeError) {
AEPOptimizeError aepOptimizeError;
if (adobeError == AdobeError.CALLBACK_TIMEOUT) {
aepOptimizeError = AEPOptimizeError.Companion.getTimeoutError();
} else {
aepOptimizeError = AEPOptimizeError.Companion.getUnexpectedError();
}
failWithOptimizeError(callback, aepOptimizeError);
}

@Override
public void call(final Event event) {
try {
final Map<String, Object> eventData = event.getEventData();
if (OptimizeUtils.isNullOrEmpty(eventData)) {

AEPOptimizeError aepOptimizeError =
AEPOptimizeError.Companion.getUnexpectedError();
failWithOptimizeError(callback, aepOptimizeError);
return;
}

if (eventData.containsKey(
OptimizeConstants.EventDataKeys.RESPONSE_ERROR)) {
Object error =
eventData.get(
OptimizeConstants.EventDataKeys.RESPONSE_ERROR);
if (error instanceof Map) {
failWithOptimizeError(
callback,
AEPOptimizeError.toAEPOptimizeError(
(Map<String, ? extends Object>) error));
}
}

if (!eventData.containsKey(
OptimizeConstants.EventDataKeys.PROPOSITIONS)) {
return;
}

final List<Map<String, Object>> propositionsList;
propositionsList =
DataReader.getTypedListOfMap(
Object.class,
eventData,
OptimizeConstants.EventDataKeys.PROPOSITIONS);
final Map<DecisionScope, OptimizeProposition> propositionsMap =
new HashMap<>();
if (propositionsList != null) {
for (final Map<String, Object> propositionData : propositionsList) {
final OptimizeProposition optimizeProposition =
OptimizeProposition.fromEventData(propositionData);
if (optimizeProposition != null
&& !OptimizeUtils.isNullOrEmpty(
optimizeProposition.getScope())) {
final DecisionScope scope =
new DecisionScope(optimizeProposition.getScope());
propositionsMap.put(scope, optimizeProposition);
}
}
}

if (callback != null) {
callback.call(propositionsMap);
}
} catch (DataReaderException e) {
failWithOptimizeError(
callback, AEPOptimizeError.Companion.getUnexpectedError());
}
}
});
}

/**
Expand Down Expand Up @@ -327,4 +433,17 @@ private static void failWithError(final AdobeCallback<?> callback, final AdobeEr
callbackWithError.fail(error);
}
}

protected static void failWithOptimizeError(
final AdobeCallback<?> callback, final AEPOptimizeError error) {

final AdobeCallbackWithOptimizeError<?> callbackWithError =
callback instanceof AdobeCallbackWithOptimizeError
? (AdobeCallbackWithOptimizeError<?>) callback
: null;

if (callbackWithError != null) {
callbackWithError.fail(error);
}
}
}
Loading
Loading