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 14 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,70 @@
/*
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.IdentityFunctionalTestUtil.*;
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
setEdgeIdentityPersistence(createXDMIdentityMap(
new IdentityTestUtil.TestItem("ECID", "primaryECID"),
new IdentityTestUtil.TestItem("ECID", "secondaryECID"),
new IdentityTestUtil.TestItem("Email", "[email protected]"),
new IdentityTestUtil.TestItem("UserId", "JohnDoe")
));
registerEdgeIdentityExtension();

// 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,203 @@
/*
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.MobileCore;
import com.adobe.marketing.mobile.MobilePrivacyStatus;
import com.adobe.marketing.mobile.TestHelper;

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

import static com.adobe.marketing.mobile.edge.identity.IdentityTestUtil.*;
import static com.adobe.marketing.mobile.edge.identity.IdentityFunctionalTestUtil.*;
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
registerEdgeIdentityExtension();

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

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


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

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

// 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");
registerEdgeIdentityExtension();

// verify
verifyPrimaryECID("legacyECID");
}

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

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

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

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

// test
// TODO : Change to MobileCore.resetIdentities when Core version 1.8.0 is released
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);
}

@Test
public void testECID_AreDifferentAfterPrivacyChange() throws Exception {
/// Test Edge Identity and IdentityDirect have same ECID on bootup, and after privacy change ECIDs are different
registerIdentityDirectExtension();

// register EdgeIdentity extension
Identity.registerExtension();
TestHelper.waitForThreads(2000);

// verify ECID for both extensions are same
String directECID = getIdentityDirectECIDSync();
String edgeECID = getExperienceCloudIdSync();
assertNotNull(edgeECID);
assertEquals(directECID, edgeECID);

// Toggle privacy
togglePrivacyStatus();
directECID = getIdentityDirectECIDSync();

// verify legacy ECID added to IdentityMap
verifyPrimaryECID(edgeECID);
verifySecondaryECID(directECID);
}

@Test
public void testECID_AreDifferentAfterResetIdentitiesAndPrivacyChange() throws Exception {
/// Test Edge Identity and IdentityDirect have same ECID on bootup, and after resetIdentities and privacy change ECIDs are different

// 1) Register Identity then Edge Identity and verify both have same ECID
registerIdentityDirectExtension();;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: double semicolon.


// register EdgeIdentity extension
Identity.registerExtension();
TestHelper.waitForThreads(2000);

// verify ECID for both extensions are same
String directECID = getIdentityDirectECIDSync();
String edgeECID = getExperienceCloudIdSync();
assertNotNull(edgeECID);
assertEquals(directECID, edgeECID);

// 2) Reset identities and toggle privacy and verify legacy ECID added to IdentityMap
dispatchRequestResetEvent();
togglePrivacyStatus();

// verify
directECID = getIdentityDirectECIDSync();
edgeECID = getExperienceCloudIdSync();
verifyPrimaryECID(edgeECID);
verifySecondaryECID(directECID);
}


@Test
public void testECID_DirectEcidIsRemovedOnPrivacyOptOut() throws Exception {
// setup
registerBothIdentityExtensions();

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

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


// Set privacy opted-out
MobileCore.setPrivacyStatus(MobilePrivacyStatus.OPT_OUT);
TestHelper.waitForThreads(2000);
edgeECID = getExperienceCloudIdSync();

// verify that the secondary ECID is removed
verifyPrimaryECID(edgeECID);
verifySecondaryECID(null);
}

// --------------------------------------------------------------------------------------------
// private helpers methods
// --------------------------------------------------------------------------------------------

private void togglePrivacyStatus() {
MobileCore.setPrivacyStatus(MobilePrivacyStatus.OPT_OUT);
MobileCore.setPrivacyStatus(MobilePrivacyStatus.OPT_IN);
TestHelper.waitForThreads(2000);
}

}
Loading