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

[AMSDK-11329] - Functional tests on Edge Identity #21

Merged
merged 16 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion code/edgeidentity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ dependencies {
// where Objenesis 2 requires minimum Android API 26
androidTestImplementation 'org.powermock:powermock-module-junit4:1+'
androidTestImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.9'

androidTestImplementation "com.adobe.marketing.mobile:identity:1.+"
}

tasks.withType(Test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class TestPersistenceHelper {
private static ArrayList<String> knownDatastoreName = new ArrayList<String>() {{
add(IdentityTestConstants.DataStoreKey.IDENTITY_DATASTORE);
add(IdentityTestConstants.DataStoreKey.CONFIG_DATASTORE);
add(IdentityTestConstants.DataStoreKey.IDENTITY_DIRECT_DATASTORE);
}};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2021 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.edge.identity;

import com.adobe.marketing.mobile.TestHelper;
import com.adobe.marketing.mobile.TestPersistenceHelper;

import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;

import java.util.Map;

import static com.adobe.marketing.mobile.TestHelper.getXDMSharedStateFor;
import static com.adobe.marketing.mobile.edge.identity.IdentityECIDHandlingTest.registerExtensionWithPersistenceData;
import static com.adobe.marketing.mobile.edge.identity.IdentityTestUtil.createXDMIdentityMap;
import static com.adobe.marketing.mobile.edge.identity.IdentityTestUtil.flattenMap;
import static org.junit.Assert.assertEquals;

public class IdentityBootUpTest {

@Rule
public RuleChain rule = RuleChain.outerRule(new TestHelper.SetupCoreRule())
.around(new TestHelper.RegisterMonitorExtensionRule());

// --------------------------------------------------------------------------------------------
// OnBootUp
// --------------------------------------------------------------------------------------------

@Test
public void testOnBootUp_LoadsAllIdentitiesFromPreference() throws Exception {
// test
registerExtensionWithPersistenceData(createXDMIdentityMap(
new IdentityTestUtil.TestItem("ECID", "primaryECID"),
new IdentityTestUtil.TestItem("ECID", "secondaryECID"),
new IdentityTestUtil.TestItem("Email", "[email protected]"),
new IdentityTestUtil.TestItem("UserId", "JohnDoe")
));

// verify xdm shared state
Map<String, String> xdmSharedState = flattenMap(getXDMSharedStateFor(IdentityConstants.EXTENSION_NAME, 1000));
assertEquals(12, xdmSharedState.size()); // 3 for ECID and 3 for secondaryECID + 6
assertEquals("primaryECID", xdmSharedState.get("identityMap.ECID[0].id"));
assertEquals("secondaryECID", xdmSharedState.get("identityMap.ECID[1].id"));
assertEquals("[email protected]", xdmSharedState.get("identityMap.Email[0].id"));
assertEquals("JohnDoe", xdmSharedState.get("identityMap.UserId[0].id"));


//verify persisted data
final String persistedJson = TestPersistenceHelper.readPersistedData(IdentityConstants.DataStoreKey.DATASTORE_NAME, IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES);
Map<String, String> persistedMap = flattenMap(IdentityTestUtil.toMap(new JSONObject(persistedJson)));
assertEquals(12, persistedMap.size()); // 3 for ECID and 3 for secondaryECID + 6
}

// --------------------------------------------------------------------------------------------
// All the other bootUp tests with to ECID is coded in IdentityECIDHandling
// --------------------------------------------------------------------------------------------

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
Copyright 2021 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.edge.identity;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.adobe.marketing.mobile.AdobeCallback;
import com.adobe.marketing.mobile.MobileCore;
import com.adobe.marketing.mobile.TestHelper;
import com.adobe.marketing.mobile.TestPersistenceHelper;

import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
PravinPK marked this conversation as resolved.
Show resolved Hide resolved

import static com.adobe.marketing.mobile.TestHelper.getXDMSharedStateFor;
import static com.adobe.marketing.mobile.TestHelper.resetTestExpectations;
import static com.adobe.marketing.mobile.edge.identity.IdentityTestUtil.*;
import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class IdentityECIDHandlingTest {
PravinPK marked this conversation as resolved.
Show resolved Hide resolved

@Rule
public RuleChain rule = RuleChain.outerRule(new TestHelper.SetupCoreRule())
.around(new TestHelper.RegisterMonitorExtensionRule());

@Test
public void testECID_autoGeneratedWhenBooted() throws InterruptedException {
// setup
registerIdentityExtension();

// verify ECID is not null
verifyPrimaryECIDNotNull();
}

@Test
public void testECID_loadedFromPersistence() throws Exception {
// setup
registerExtensionWithPersistenceData(createXDMIdentityMap(
PravinPK marked this conversation as resolved.
Show resolved Hide resolved
new TestItem("ECID", "primaryECID"),
new TestItem("ECID", "secondaryECID")
));

// verify
verifyPrimaryECID("primaryECID");
verifySecondaryECID("secondaryECID");
}

@Test
public void testECID_edgePersistenceTakesPreferenceOverDirectExtension() throws Exception {
// setup
setIdentityDirectPersistedECID("legacyECID");
registerExtensionWithPersistenceData(CreateIdentityMap("ECID", "edgeECID").asXDMMap());

// verify
verifyPrimaryECID("edgeECID");
verifySecondaryECID(null);
}

@Test
public void testECID_loadsIdentityDirectECID() throws Exception {
// This will happen when EdgeIdentity extension is installed after Identity direct extension
// setup
setIdentityDirectPersistedECID("legacyECID");
registerIdentityExtension();

// verify
verifyPrimaryECID("legacyECID");
}

@Test
public void testECID_whenBothExtensionRegistered() throws Exception {
// setup
RegisterBothIdentityExtension();

String edgeECID = getExperienceCloudIdSync();
String directECID = getIdentityDirectECIDSync();

// verify ECID
verifyPrimaryECID(edgeECID);
verifySecondaryECID(directECID);
}

@Test
public void testECID_onResetClearsOldECID() throws Exception {
// setup
registerExtensionWithPersistenceData(createXDMIdentityMap(
new TestItem("ECID", "primaryECID"),
new TestItem("ECID", "secondaryECID")
));

// test
dispatchRequestResetEvent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use MobileCore.resetIdentities

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting for Core version 1.8.0 to be released. Added a todo for now

String newECID = getExperienceCloudIdSync();

// verify the new ecid is not the same as old one and the secondary ECID is cleared
assertNotNull(newECID);
assertNotEquals("primaryECID", newECID);
verifyPrimaryECID(newECID);
verifySecondaryECID(null);
}

// --------------------------------------------------------------------------------------------
// private helper methods
// --------------------------------------------------------------------------------------------

private void verifyPrimaryECIDNotNull() throws InterruptedException {
PravinPK marked this conversation as resolved.
Show resolved Hide resolved
String ecid = getExperienceCloudIdSync();
assertNotNull(ecid);

// verify xdm shared state is has ECID
Map<String, String> xdmSharedState = flattenMap(getXDMSharedStateFor(IdentityConstants.EXTENSION_NAME, 1000));
assertNotNull(xdmSharedState.get("identityMap.ECID[0].id"));
}

private void verifyPrimaryECID(final String primaryECID) throws Exception {
String ecid = getExperienceCloudIdSync();
assertEquals(primaryECID, ecid);

// verify xdm shared state is has correct primary ECID
Map<String, String> xdmSharedState = flattenMap(getXDMSharedStateFor(IdentityConstants.EXTENSION_NAME, 1000));
assertEquals(primaryECID, xdmSharedState.get("identityMap.ECID[0].id"));

// verify primary ECID in persistence
final String persistedJson = TestPersistenceHelper.readPersistedData(IdentityConstants.DataStoreKey.DATASTORE_NAME, IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES);
Map<String, String> persistedMap = flattenMap(IdentityTestUtil.toMap(new JSONObject(persistedJson)));
assertEquals(primaryECID, persistedMap.get("identityMap.ECID[0].id"));
}

private void verifySecondaryECID(final String secondaryECID) throws Exception {
// verify xdm shared state is has correct secondary ECID
Map<String, String> xdmSharedState = flattenMap(getXDMSharedStateFor(IdentityConstants.EXTENSION_NAME, 1000));
assertEquals(secondaryECID, xdmSharedState.get("identityMap.ECID[1].id"));

// verify secondary ECID in persistence
final String persistedJson = TestPersistenceHelper.readPersistedData(IdentityConstants.DataStoreKey.DATASTORE_NAME, IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES);
Map<String, String> persistedMap = flattenMap(IdentityTestUtil.toMap(new JSONObject(persistedJson)));
assertEquals(secondaryECID, persistedMap.get("identityMap.ECID[1].id"));
}

static void registerExtensionWithPersistenceData(final Map<String, Object> persistedData) throws InterruptedException {
PravinPK marked this conversation as resolved.
Show resolved Hide resolved
if (persistedData != null) {
final JSONObject persistedJSON = new JSONObject(persistedData);
TestPersistenceHelper.updatePersistence(IdentityConstants.DataStoreKey.DATASTORE_NAME, IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, persistedJSON.toString());
}
registerIdentityExtension();
}

static void registerIdentityExtension() throws InterruptedException {
Identity.registerExtension();

final CountDownLatch latch = new CountDownLatch(1);
MobileCore.start(new AdobeCallback() {
@Override
public void call(Object o) {
latch.countDown();
}
});

latch.await();
TestHelper.waitForThreads(2000);
resetTestExpectations();
}

private void RegisterBothIdentityExtension() throws Exception {
PravinPK marked this conversation as resolved.
Show resolved Hide resolved
HashMap<String, Object> config = new HashMap<String, Object>() {
{
put("global.privacy", "optunknown");
put("experienceCloud.org", "testOrg@AdobeOrg");
}
};
MobileCore.updateConfiguration(config);


Identity.registerExtension();
com.adobe.marketing.mobile.Identity.registerExtension();
PravinPK marked this conversation as resolved.
Show resolved Hide resolved

final CountDownLatch latch = new CountDownLatch(1);
MobileCore.start(new AdobeCallback() {
@Override
public void call(Object o) {
latch.countDown();
}
});

latch.await();
TestHelper.waitForThreads(2000);
resetTestExpectations();
}

private void setIdentityDirectPersistedECID(final String legacyECID) {
TestPersistenceHelper.updatePersistence(IdentityConstants.DataStoreKey.IDENTITY_DIRECT_DATASTORE_NAME, IdentityConstants.DataStoreKey.IDENTITY_DIRECT_ECID_KEY, legacyECID);
}

static String getIdentityDirectECIDSync() {
PravinPK marked this conversation as resolved.
Show resolved Hide resolved
try {
final HashMap<String, String> getExperienceCloudIdResponse = new HashMap<>();
final CountDownLatch latch = new CountDownLatch(1);
com.adobe.marketing.mobile.Identity.getExperienceCloudId(new AdobeCallback<String>() {
@Override
public void call(final String ecid) {
getExperienceCloudIdResponse.put(IdentityTestConstants.GetIdentitiesHelper.VALUE, ecid);
latch.countDown();
}
});
latch.await();

return getExperienceCloudIdResponse.get(IdentityTestConstants.GetIdentitiesHelper.VALUE);
} catch (Exception exp) {
return null;
}
}

}
Loading