From 2d0013afc2bc9a440921ab38df4001d4aa215174 Mon Sep 17 00:00:00 2001 From: timkimadobe <95260439+timkimadobe@users.noreply.github.com> Date: Wed, 5 Jun 2024 17:19:51 -0700 Subject: [PATCH] Remove unconverted functional tests --- .../marketing/mobile/EdgeFunctionalTests.java | 1640 ----------------- .../mobile/EdgePathOverwriteTests.java | 281 --- .../mobile/IdentityStateFunctionalTests.java | 215 --- ...NetworkResponseHandlerFunctionalTests.java | 1224 ------------ .../mobile/NoConfigFunctionalTests.java | 238 --- .../mobile/RestartFunctionalTests.java | 274 --- .../mobile/SampleFunctionalTests.java | 217 --- 7 files changed, 4089 deletions(-) delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgeFunctionalTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgePathOverwriteTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/IdentityStateFunctionalTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/NetworkResponseHandlerFunctionalTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/NoConfigFunctionalTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/RestartFunctionalTests.java delete mode 100644 code/edge/src/androidTest/java/com/adobe/marketing/mobile/SampleFunctionalTests.java diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgeFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgeFunctionalTests.java deleted file mode 100644 index f2735553..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgeFunctionalTests.java +++ /dev/null @@ -1,1640 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.services.HttpMethod.POST; -import static com.adobe.marketing.mobile.util.TestHelper.LogOnErrorRule; -import static com.adobe.marketing.mobile.util.TestHelper.SetupCoreRule; -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.assertUnexpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.getDispatchedEventsWith; -import static com.adobe.marketing.mobile.util.TestHelper.getSharedStateFor; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static com.adobe.marketing.mobile.util.TestHelper.sleep; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.edge.identity.Identity; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.NamedCollection; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import com.adobe.marketing.mobile.util.TestUtils; -import com.adobe.marketing.mobile.util.TestXDMSchema; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class EdgeFunctionalTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final String EXEDGE_INTERACT_URL_STRING = TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING; - private static final String EXEDGE_INTERACT_OR2_LOC_URL_STRING = - TestConstants.Defaults.EXEDGE_INTERACT_OR2_LOC_URL_STRING; - private static final String CONFIG_ID = "1234abcd-abcd-1234-5678-123456abcdef"; - private static final String DEFAULT_RESPONSE_STRING = "\u0000{\"test\": \"json\"}"; - private static final int TIMEOUT_MILLIS = 5000; - private static final ExperienceEvent XDM_EXPERIENCE_EVENT = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("key", "value"); - } - } - ) - .setData(null) - .build(); - - @Rule - public RuleChain rule = RuleChain.outerRule(new LogOnErrorRule()).around(new SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - - setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 4); - - HashMap config = new HashMap() { - { - put("edge.configId", CONFIG_ID); - } - }; - MobileCore.updateConfiguration(config); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, Identity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - latch.await(); - - assertExpectedEvents(false); - resetTestExpectations(); - } - - @After - public void tearDown() { - mockNetworkService.reset(); - } - - // -------------------------------------------------------------------------------------------- - // test request event format - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_withXDMData_sendsCorrectRequestEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - assertExpectedEvents(false); - List resultEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, resultEvents.size()); - Map eventData = resultEvents.get(0).getEventData(); - assertNotNull(eventData); - - Map flattenedData = TestUtils.flattenMap(eventData); - assertEquals(7, flattenedData.size()); - assertEquals("xdm", flattenedData.get("xdm.testString")); - assertEquals("10", flattenedData.get("xdm.testInt")); - assertEquals("false", flattenedData.get("xdm.testBool")); - assertEquals("12.89", flattenedData.get("xdm.testDouble")); - assertEquals("elem1", flattenedData.get("xdm.testArray[0]")); - assertEquals("elem2", flattenedData.get("xdm.testArray[1]")); - assertEquals("value", flattenedData.get("xdm.testMap.key")); - } - - @Test - public void testSendEvent_withXDMDataAndCustomData_sendsCorrectRequestEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .setData( - new HashMap() { - { - put("testString", "stringValue"); - put("testInt", 101); - put("testBool", true); - put("testDouble", 13.66); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - assertExpectedEvents(false); - List resultEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, resultEvents.size()); - Map eventData = resultEvents.get(0).getEventData(); - assertNotNull(eventData); - - Map flattenedData = TestUtils.flattenMap(eventData); - assertEquals(8, flattenedData.size()); - assertEquals("xdm", flattenedData.get("xdm.testString")); - assertEquals("stringValue", flattenedData.get("data.testString")); - assertEquals("101", flattenedData.get("data.testInt")); - assertEquals("true", flattenedData.get("data.testBool")); - assertEquals("13.66", flattenedData.get("data.testDouble")); - assertEquals("elem1", flattenedData.get("data.testArray[0]")); - assertEquals("elem2", flattenedData.get("data.testArray[1]")); - assertEquals("value", flattenedData.get("data.testMap.key")); - } - - @Test - public void testSendEvent_withXDMDataAndNullData_sendsCorrectRequestEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .setData(null) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - assertExpectedEvents(false); - List resultEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, resultEvents.size()); - Map eventData = resultEvents.get(0).getEventData(); - assertNotNull(eventData); - - Map flattenedData = TestUtils.flattenMap(eventData); - assertEquals(1, flattenedData.size()); - assertEquals("xdm", flattenedData.get("xdm.testString")); - } - - @Test - public void testSendEvent_withEmptyXDMDataAndNullData_DoesNotSendRequestEvent() throws InterruptedException { - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema(new HashMap<>()) - .setData(null) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - assertUnexpectedEvents(); - } - - @Test - public void testSendEvent_withEmptyXDMSchema_DoesNotSendRequestEvent() throws InterruptedException { - ExperienceEvent experienceEvent = new ExperienceEvent.Builder().setXdmSchema(new TestXDMSchema()).build(); - Edge.sendEvent(experienceEvent, null); - - // verify - assertUnexpectedEvents(); - } - - // -------------------------------------------------------------------------------------------- - // test complete event format - // -------------------------------------------------------------------------------------------- - - @Test - public void testDispatchEvent_sendCompleteEvent_sendsPairedCompleteEvent() throws InterruptedException { - Map eventData = new HashMap() { - { - put( - "xdm", - new HashMap() { - { - put("test", "data"); - } - } - ); - put( - "request", - new HashMap() { - { - put("sendCompletion", true); - } - } - ); - } - }; - - Event edgeEvent = new Event.Builder( - "Edge Event Completion Request", - EventType.EDGE, - EventSource.REQUEST_CONTENT - ) - .setEventData(eventData) - .build(); - - final CountDownLatch latch = new CountDownLatch(1); - - MobileCore.dispatchEventWithResponseCallback( - edgeEvent, - 2000, - new AdobeCallbackWithError() { - @Override - public void fail(AdobeError adobeError) { - Assert.fail("DispatchEventWithResponseCallback returned an error: " + adobeError.toString()); - } - - @Override - public void call(Event event) { - assertEquals("AEP Response Complete", event.getName()); - assertEquals(EventType.EDGE, event.getType()); - assertEquals(EventSource.CONTENT_COMPLETE, event.getSource()); - assertEquals(edgeEvent.getUniqueIdentifier(), event.getParentID()); - assertEquals(edgeEvent.getUniqueIdentifier(), event.getResponseID()); - assertNotNull(event.getEventData()); - - Map flattenedData = TestUtils.flattenMap(event.getEventData()); - assertEquals(1, flattenedData.size()); - assertNotNull(flattenedData.get("requestId")); - latch.countDown(); - } - } - ); - - assertTrue(latch.await(3000, TimeUnit.MILLISECONDS)); - } - - // -------------------------------------------------------------------------------------------- - // test network request format - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_withXDMData_sendsExEdgeNetworkRequest() throws Exception { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdmValue"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - Map resultPayload = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals(18, resultPayload.size()); - assertEquals("true", resultPayload.get("meta.konductorConfig.streaming.enabled")); - assertEquals("\u0000", resultPayload.get("meta.konductorConfig.streaming.recordSeparator")); - assertEquals("\n", resultPayload.get("meta.konductorConfig.streaming.lineFeed")); - assertNotNull(resultPayload.get("xdm.identityMap.ECID[0].id")); - assertEquals("false", resultPayload.get("xdm.identityMap.ECID[0].primary")); - assertEquals("ambiguous", resultPayload.get("xdm.identityMap.ECID[0].authenticatedState")); - assertNotNull(resultPayload.get("events[0].xdm._id")); - assertNotNull(resultPayload.get("events[0].xdm.timestamp")); - assertEquals("xdmValue", resultPayload.get("events[0].xdm.testString")); - assertEquals("10", resultPayload.get("events[0].xdm.testInt")); - assertEquals("false", resultPayload.get("events[0].xdm.testBool")); - assertEquals("12.89", resultPayload.get("events[0].xdm.testDouble")); - assertEquals("elem1", resultPayload.get("events[0].xdm.testArray[0]")); - assertEquals("elem2", resultPayload.get("events[0].xdm.testArray[1]")); - assertEquals("value", resultPayload.get("events[0].xdm.testMap.key")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertEquals("app", resultPayload.get("xdm.implementationDetails.environment")); - assertEquals( - EdgeJson.Event.ImplementationDetails.BASE_NAMESPACE, - resultPayload.get("xdm.implementationDetails.name") - ); - assertEquals( - MobileCore.extensionVersion() + "+" + Edge.extensionVersion(), - resultPayload.get("xdm.implementationDetails.version") - ); - } - - @Test - public void testSendEvent_withXDMDataAndCustomData_sendsExEdgeNetworkRequest() throws Exception { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdmValue"); - } - } - ) - .setData( - new HashMap() { - { - put("testString", "stringValue"); - put("testInt", 101); - put("testBool", true); - put("testDouble", 13.66); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - Map resultPayload = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals(19, resultPayload.size()); - assertEquals("true", resultPayload.get("meta.konductorConfig.streaming.enabled")); - assertEquals("\u0000", resultPayload.get("meta.konductorConfig.streaming.recordSeparator")); - assertEquals("\n", resultPayload.get("meta.konductorConfig.streaming.lineFeed")); - assertNotNull(resultPayload.get("xdm.identityMap.ECID[0].id")); - assertEquals("false", resultPayload.get("xdm.identityMap.ECID[0].primary")); - assertEquals("ambiguous", resultPayload.get("xdm.identityMap.ECID[0].authenticatedState")); - assertNotNull(resultPayload.get("events[0].xdm._id")); - assertNotNull(resultPayload.get("events[0].xdm.timestamp")); - assertEquals("xdmValue", resultPayload.get("events[0].xdm.testString")); - assertEquals("stringValue", resultPayload.get("events[0].data.testString")); - assertEquals("101", resultPayload.get("events[0].data.testInt")); - assertEquals("true", resultPayload.get("events[0].data.testBool")); - assertEquals("13.66", resultPayload.get("events[0].data.testDouble")); - assertEquals("elem1", resultPayload.get("events[0].data.testArray[0]")); - assertEquals("elem2", resultPayload.get("events[0].data.testArray[1]")); - assertEquals("value", resultPayload.get("events[0].data.testMap.key")); - assertEquals("app", resultPayload.get("xdm.implementationDetails.environment")); - assertEquals( - EdgeJson.Event.ImplementationDetails.BASE_NAMESPACE, - resultPayload.get("xdm.implementationDetails.name") - ); - assertEquals( - MobileCore.extensionVersion() + "+" + Edge.extensionVersion(), - resultPayload.get("xdm.implementationDetails.version") - ); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - @Test - public void testSendEvent_withXDMSchema_sendsExEdgeNetworkRequest() throws Exception { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - TestXDMSchema.TestXDMObject xdmObject = new TestXDMSchema.TestXDMObject(); - xdmObject.innerKey = "testInnerObject"; - TestXDMSchema xdmSchema = new TestXDMSchema(); - xdmSchema.boolObject = true; - xdmSchema.intObject = 100; - xdmSchema.stringObject = "testWithXdmSchema"; - xdmSchema.doubleObject = 3.42; - xdmSchema.xdmObject = xdmObject; - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder().setXdmSchema(xdmSchema).build(); - Edge.sendEvent(experienceEvent, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - Map resultPayload = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals(17, resultPayload.size()); - - assertEquals("true", resultPayload.get("meta.konductorConfig.streaming.enabled")); - assertEquals("\u0000", resultPayload.get("meta.konductorConfig.streaming.recordSeparator")); - assertEquals("\n", resultPayload.get("meta.konductorConfig.streaming.lineFeed")); - assertNotNull(resultPayload.get("xdm.identityMap.ECID[0].id")); - assertEquals("false", resultPayload.get("xdm.identityMap.ECID[0].primary")); - assertEquals("ambiguous", resultPayload.get("xdm.identityMap.ECID[0].authenticatedState")); - assertNotNull(resultPayload.get("events[0].xdm._id")); - assertNotNull(resultPayload.get("events[0].xdm.timestamp")); - - assertEquals("testWithXdmSchema", resultPayload.get("events[0].xdm.stringObject")); - assertEquals("100", resultPayload.get("events[0].xdm.intObject")); - assertEquals("true", resultPayload.get("events[0].xdm.boolObject")); - assertEquals("3.42", resultPayload.get("events[0].xdm.doubleObject")); - assertEquals("testInnerObject", resultPayload.get("events[0].xdm.xdmObject.innerKey")); - assertEquals("abc123def", resultPayload.get("events[0].meta.collect.datasetId")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertEquals("app", resultPayload.get("xdm.implementationDetails.environment")); - assertEquals( - EdgeJson.Event.ImplementationDetails.BASE_NAMESPACE, - resultPayload.get("xdm.implementationDetails.name") - ); - assertEquals( - MobileCore.extensionVersion() + "+" + Edge.extensionVersion(), - resultPayload.get("xdm.implementationDetails.version") - ); - } - - @Test - public void testSendEvent_withEmptyXDMSchema_doesNotSendExEdgeNetworkRequest() throws InterruptedException { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder().setXdmSchema(new TestXDMSchema()).build(); - Edge.sendEvent(experienceEvent, null); - - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST - ); - assertEquals(0, resultRequests.size()); - } - - @Test - public void testSendEvent_withEmptyXDMSchemaAndEmptyData_doesNotSendExEdgeNetworkRequest() - throws InterruptedException { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema(new TestXDMSchema()) - .setData(new HashMap<>()) - .build(); - Edge.sendEvent(experienceEvent, null); - - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST - ); - assertEquals(0, resultRequests.size()); - } - - @Test - public void testSendEvent_withEmptyXDMSchemaAndNullData_doesNotSendExEdgeNetworkRequest() - throws InterruptedException { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema(new TestXDMSchema()) - .setData(null) - .build(); - Edge.sendEvent(experienceEvent, null); - - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST - ); - assertEquals(0, resultRequests.size()); - } - - // -------------------------------------------------------------------------------------------- - // Configurable Endpoint - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_withConfigurableEndpoint_withEmptyConfig_usesProductionEndpoint() throws Exception { - mockNetworkService.setExpectationForNetworkRequest(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, POST, 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - assertTrue(resultRequests.get(0).getUrl().startsWith(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - @Test - public void testSendEvent_withConfigurableEndpoint_withInvalidConfig_usesProductionEndpoint() throws Exception { - updateConfiguration( - new HashMap() { - { - put("edge.environment", "invalid"); - } - } - ); - - mockNetworkService.setExpectationForNetworkRequest(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, POST, 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - assertTrue(resultRequests.get(0).getUrl().startsWith(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - @Test - public void testSendEvent_withConfigurableEndpoint_withProdConfigEndpoint_usesProductionEndpoint() - throws Exception { - updateConfiguration( - new HashMap() { - { - put("edge.environment", "prod"); - } - } - ); - - mockNetworkService.setExpectationForNetworkRequest(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, POST, 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - assertTrue(resultRequests.get(0).getUrl().startsWith(TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - @Test - public void testSendEvent_withConfigurableEndpoint_withPreProdConfigEndpoint_usesPreProductionEndpoint() - throws Exception { - updateConfiguration( - new HashMap() { - { - put("edge.environment", "pre-prod"); - } - } - ); - - mockNetworkService.setExpectationForNetworkRequest( - TestConstants.Defaults.EXEDGE_INTERACT_PRE_PROD_URL_STRING, - POST, - 1 - ); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - TestConstants.Defaults.EXEDGE_INTERACT_PRE_PROD_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - assertTrue( - resultRequests.get(0).getUrl().startsWith(TestConstants.Defaults.EXEDGE_INTERACT_PRE_PROD_URL_STRING) - ); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - @Test - public void testSendEvent_withConfigurableEndpoint_withIntConfigEndpoint_usesIntegrationEndpoint() - throws Exception { - updateConfiguration( - new HashMap() { - { - put("edge.environment", "int"); - } - } - ); - - mockNetworkService.setExpectationForNetworkRequest( - TestConstants.Defaults.EXEDGE_INTERACT_INT_URL_STRING, - POST, - 1 - ); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - TestConstants.Defaults.EXEDGE_INTERACT_INT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - assertTrue(resultRequests.get(0).getUrl().startsWith(TestConstants.Defaults.EXEDGE_INTERACT_INT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - } - - // -------------------------------------------------------------------------------------------- - // Client-side store - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_twoConsecutiveCalls_appendsReceivedClientSideStore() throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - final String storeResponseBody = - "\u0000{\"requestId\": \"0000-4a4e-1111-bf5c-abcd\",\"handle\": [{\"payload\": [{\"key\": \"kndctr_testOrg_AdobeOrg_identity\",\"value\": \"hashed_value\",\"maxAge\": 34128000},{\"key\": \"kndctr_testOrg_AdobeOrg_consent_check\",\"value\": \"1\",\"maxAge\": 7200},{\"key\": \"expired_key\",\"value\": \"1\",\"maxAge\": 0}],\"type\": \"state:store\"}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(storeResponseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // first network call, no stored data - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - Map requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 12 elements, but found: " + requestBody, 12, requestBody.size()); - - assertExpectedEvents(true); - resetTestExpectations(); - - // send a new event, should contain previously stored store data - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - resultRequests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST, TIMEOUT_MILLIS); - assertEquals(1, resultRequests.size()); - requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 18 elements, but found: " + requestBody, 18, requestBody.size()); - - String firstStore = requestBody.get("meta.state.entries[0].key"); - assertNotNull("client-side store not found", firstStore); - int identityIndex = firstStore.equals("kndctr_testOrg_AdobeOrg_identity") ? 0 : 1; - int consentIndex = firstStore.equals("kndctr_testOrg_AdobeOrg_identity") ? 1 : 0; - - assertEquals( - "kndctr_testOrg_AdobeOrg_identity", - requestBody.get("meta.state.entries[" + identityIndex + "].key") - ); - assertEquals("hashed_value", requestBody.get("meta.state.entries[" + identityIndex + "].value")); - assertEquals("34128000", requestBody.get("meta.state.entries[" + identityIndex + "].maxAge")); - - assertEquals( - "kndctr_testOrg_AdobeOrg_consent_check", - requestBody.get("meta.state.entries[" + consentIndex + "].key") - ); - assertEquals("1", requestBody.get("meta.state.entries[" + consentIndex + "].value")); - assertEquals("7200", requestBody.get("meta.state.entries[" + consentIndex + "].maxAge")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertExpectedEvents(true); - } - - @Test - public void testSendEvent_twoConsecutiveCalls_resetBefore_appendsReceivedClientSideStore() - throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - // send the reset event before - final Event resetEvent = new Event.Builder("resetEvent", EventType.EDGE_IDENTITY, EventSource.RESET_COMPLETE) - .build(); - MobileCore.dispatchEvent(resetEvent); - - final String storeResponseBody = - "\u0000{\"requestId\": \"0000-4a4e-1111-bf5c-abcd\",\"handle\": [{\"payload\": [{\"key\": \"kndctr_testOrg_AdobeOrg_identity\",\"value\": \"hashed_value\",\"maxAge\": 34128000},{\"key\": \"kndctr_testOrg_AdobeOrg_consent_check\",\"value\": \"1\",\"maxAge\": 7200},{\"key\": \"expired_key\",\"value\": \"1\",\"maxAge\": 0}],\"type\": \"state:store\"}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(storeResponseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // first network call, no stored data - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - Map requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 12 elements, but found: " + requestBody, 12, requestBody.size()); - - assertExpectedEvents(true); - resetTestExpectations(); - - // send a new event, should contain previously stored store data - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - resultRequests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST, TIMEOUT_MILLIS); - assertEquals(1, resultRequests.size()); - requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 18 elements, but found: " + requestBody, 18, requestBody.size()); - - String firstStore = requestBody.get("meta.state.entries[0].key"); - assertNotNull("client-side store not found", firstStore); - int identityIndex = firstStore.equals("kndctr_testOrg_AdobeOrg_identity") ? 0 : 1; - int consentIndex = firstStore.equals("kndctr_testOrg_AdobeOrg_identity") ? 1 : 0; - - assertEquals( - "kndctr_testOrg_AdobeOrg_identity", - requestBody.get("meta.state.entries[" + identityIndex + "].key") - ); - assertEquals("hashed_value", requestBody.get("meta.state.entries[" + identityIndex + "].value")); - assertEquals("34128000", requestBody.get("meta.state.entries[" + identityIndex + "].maxAge")); - - assertEquals( - "kndctr_testOrg_AdobeOrg_consent_check", - requestBody.get("meta.state.entries[" + consentIndex + "].key") - ); - assertEquals("1", requestBody.get("meta.state.entries[" + consentIndex + "].value")); - assertEquals("7200", requestBody.get("meta.state.entries[" + consentIndex + "].maxAge")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_INTERACT_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertExpectedEvents(true); - } - - @Test - public void testSendEvent_twoConsecutiveCalls_resetBetween_clearsClientSideStore() throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - final String storeResponseBody = - "\u0000{\"requestId\": \"0000-4a4e-1111-bf5c-abcd\",\"handle\": [{\"payload\": [{\"key\": \"kndctr_testOrg_AdobeOrg_identity\",\"value\": \"hashed_value\",\"maxAge\": 34128000},{\"key\": \"kndctr_testOrg_AdobeOrg_consent_check\",\"value\": \"1\",\"maxAge\": 7200},{\"key\": \"expired_key\",\"value\": \"1\",\"maxAge\": 0}],\"type\": \"state:store\"}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(storeResponseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // first network call, no stored data - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - Map requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 12 elements, but found: " + requestBody, 12, requestBody.size()); - - assertExpectedEvents(true); - resetTestExpectations(); - - // send the reset event in-between - final Event resetEvent = new Event.Builder("resetEvent", EventType.EDGE_IDENTITY, EventSource.RESET_COMPLETE) - .build(); - MobileCore.dispatchEvent(resetEvent); - - // send a new event, should contain previously stored store data - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, "state:store", 1); - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - resultRequests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST, TIMEOUT_MILLIS); - assertEquals(1, resultRequests.size()); - requestBody = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals("Expected request body with 12 elements, but found: " + requestBody, 12, requestBody.size()); - - String firstStore = requestBody.get("meta.state.entries[0].key"); - assertNull("client-side store was found", firstStore); - } - - // -------------------------------------------------------------------------------------------- - // Paired request-response events - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_receivesResponseEventHandle_sendsResponseEvent_pairedWithTheRequestEventId() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.EDGE, "personalization:decisions", 1); - - final String responseBody = - "\u0000{\"requestId\": \"0ee43289-4a4e-469a-bf5c-1d8186919a26\",\"handle\": [{\"payload\": [{\"id\": \"AT:eyJhY3Rpdml0eUlkIjoiMTE3NTg4IiwiZXhwZXJpZW5jZUlkIjoiMSJ9\",\"scope\": \"buttonColor\",\"items\": [{ \"schema\": \"https://ns.adobe.com/personalization/json-content-item\",\"data\": {\"content\": {\"value\": \"#D41DBA\"}}}]}],\"type\": \"personalization:decisions\",\"eventIndex\": 0}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(responseBody, 200); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(true); - - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - String requestId = resultRequests.get(0).queryParam("requestId"); - - List requestEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, requestEvents.size()); - String requestEventUuid = requestEvents.get(0).getUniqueIdentifier(); - - List responseEvents = getDispatchedEventsWith(EventType.EDGE, "personalization:decisions"); - assertEquals(1, responseEvents.size()); - Map responseEventData = responseEvents.get(0).getEventData(); - assertNotNull(responseEventData); - - Map flattenedEventData = TestUtils.flattenMap(responseEventData); - assertEquals(7, flattenedEventData.size()); - assertEquals("personalization:decisions", flattenedEventData.get("type")); - assertEquals( - "AT:eyJhY3Rpdml0eUlkIjoiMTE3NTg4IiwiZXhwZXJpZW5jZUlkIjoiMSJ9", - flattenedEventData.get("payload[0].id") - ); - assertEquals("#D41DBA", flattenedEventData.get("payload[0].items[0].data.content.value")); - assertEquals( - "https://ns.adobe.com/personalization/json-content-item", - flattenedEventData.get("payload[0].items[0].schema") - ); - assertEquals("buttonColor", flattenedEventData.get("payload[0].scope")); - assertEquals(requestId, flattenedEventData.get("requestId")); - assertEquals(requestEventUuid, flattenedEventData.get("requestEventId")); - assertEquals(requestEventUuid, responseEvents.get(0).getParentID()); - } - - @Test - public void testSendEvent_receivesResponseEventWarning_sendsErrorResponseEvent_pairedWithTheRequestEventId() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - - final String responseBody = - "\u0000{\"requestId\": \"0ee43289-4a4e-469a-bf5c-1d8186919a26\",\"handle\": [],\"warnings\": [{\"code\": \"personalization:0\",\"message\": \"Failed due to unrecoverable system error\",\"report\":{\"eventIndex\":0}}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(responseBody, 200); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - ExperienceEvent event = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("eventType", "personalizationEvent"); - put("test", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(event, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(false); - - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - String requestId = resultRequests.get(0).queryParam("requestId"); - - List requestEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, requestEvents.size()); - String requestEventUuid = requestEvents.get(0).getUniqueIdentifier(); - - List errorResponseEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, errorResponseEvents.size()); - Map responseEventData = errorResponseEvents.get(0).getEventData(); - assertNotNull(responseEventData); - - Map flattenedEventData = TestUtils.flattenMap(responseEventData); - assertEquals(4, flattenedEventData.size()); - assertEquals("personalization:0", flattenedEventData.get("code")); - assertEquals("Failed due to unrecoverable system error", flattenedEventData.get("message")); - assertEquals(requestId, flattenedEventData.get("requestId")); - assertEquals(requestEventUuid, flattenedEventData.get("requestEventId")); - assertEquals(requestEventUuid, errorResponseEvents.get(0).getParentID()); - } - - // -------------------------------------------------------------------------------------------- - // Location Hint result - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_edgeNetworkResponseContainsLocationHint_nextSendEventIncludesLocationHint() - throws InterruptedException { - final String hintResponseBody = - "\u0000{\"requestId\": \"0000-4a4e-1111-bf5c-abcd\",\"handle\": [{\"payload\": [{\"scope\": \"EdgeNetwork\",\"hint\": \"or2\",\"ttlSeconds\": 1800}],\"type\": \"locationHint:result\"}]}\n"; - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(hintResponseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_OR2_LOC_URL_STRING, POST, 1); - - setExpectationEvent(EventType.EDGE, "locationHint:result", 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // first network call, no location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - // second network call, has location hint - resultRequests = - mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_OR2_LOC_URL_STRING, POST, TIMEOUT_MILLIS); - assertEquals(1, resultRequests.size()); - - // location hint handle dispatched - assertExpectedEvents(true); - } - - @Test - public void testSendEvent_edgeNetworkResponseContainsLocationHint_sendEventDoesNotIncludeExpiredLocationHint() - throws InterruptedException { - final String hintResponseBody = - "\u0000{\"requestId\": \"0000-4a4e-1111-bf5c-abcd\",\"handle\": [{\"payload\": [{\"scope\": \"EdgeNetwork\",\"hint\": \"or2\",\"ttlSeconds\": 1}],\"type\": \"locationHint:result\"}]}\n"; - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(hintResponseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 2); - - setExpectationEvent(EventType.EDGE, "locationHint:result", 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - Thread.sleep(1500); // wait for hint to expire - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - // all network calls, no location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(2, resultRequests.size()); - - // location hint handle dispatched - assertExpectedEvents(true); - } - - // -------------------------------------------------------------------------------------------- - // test get and set location hint - // -------------------------------------------------------------------------------------------- - - @Test - public void testGetLocationHint_whenValueHint_returnsValue() throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - - Edge.setLocationHint("or2"); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertEquals("or2", result[0]); - } - - @Test - public void testGetLocationHint_whenNullHint_returnsNull() throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - - Edge.setLocationHint("or2"); - Edge.setLocationHint(null); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertNull(result[0]); - } - - @Test - public void testGetLocationHint_whenEmptyHint_returnsNull() throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - - Edge.setLocationHint("or2"); - Edge.setLocationHint(""); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertNull(result[0]); - } - - @Test - public void testSetLocationHint_withValueHint_createsSharedState() throws InterruptedException { - Edge.setLocationHint("or2"); - sleep(500); // wait for state creation - Map sharedState = getSharedStateFor(TestConstants.SharedState.EDGE, 1000); - assertNotNull(sharedState); - assertEquals("or2", sharedState.get("locationHint")); - } - - @Test - public void testSetLocationHint_withValueHint_savesToDataStore() throws InterruptedException { - final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); - calendar.add(Calendar.SECOND, 1800); // default hint TTL - long expectedExpiryTime = calendar.getTimeInMillis(); - - Edge.setLocationHint("or2"); - sleep(500); // wait for event to get processed - - NamedCollection dataStore = ServiceProvider - .getInstance() - .getDataStoreService() - .getNamedCollection(EdgeConstants.EDGE_DATA_STORAGE); - final String hint = dataStore.getString(EdgeConstants.DataStoreKeys.PROPERTY_LOCATION_HINT, null); - final long hintExpiry = dataStore.getLong( - EdgeConstants.DataStoreKeys.PROPERTY_LOCATION_HINT_EXPIRY_TIMESTAMP, - 0 - ); - - assertEquals("or2", hint); - assertEquals(expectedExpiryTime, hintExpiry, 100); - } - - @Test - public void testSetLocationHint_withValueHint_edgeNetworkResponseContainsLocationHint() - throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_OR2_LOC_URL_STRING, POST, 1); - - Edge.setLocationHint("or2"); // set hint - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); // send event - - // verify send event request includes set location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_OR2_LOC_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - } - - @Test - public void testSetLocationHint_withHintWithSpaces_edgeNetworkResponseContainsLocationHint() - throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest( - "https://edge.adobedc.net/ee/incorrect location hint/v1/interact", - POST, - 1 - ); - - Edge.setLocationHint("incorrect location hint"); // set hint - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); // send event - - // verify send event request includes set location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - "https://edge.adobedc.net/ee/incorrect location hint/v1/interact", - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - } - - @Test - public void testSetLocationHint_withHintWithSpecialCharacters_edgeNetworkResponseContainsLocationHint() - throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest( - "https://edge.adobedc.net/ee/{\"example\":\"incorrect\"}/v1/interact", - POST, - 1 - ); - - Edge.setLocationHint("{\"example\":\"incorrect\"}"); // set hint - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); // send event - - // verify send event request includes set location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - "https://edge.adobedc.net/ee/{\"example\":\"incorrect\"}/v1/interact", - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - } - - @Test - public void testSetLocationHint_withHintWithUnicodeCharacters_edgeNetworkResponseContainsLocationHint() - throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest( - "https://edge.adobedc.net/ee/\u0048\u0065\u006C\u006C\u006F/v1/interact", - POST, - 1 - ); - - Edge.setLocationHint("\u0048\u0065\u006C\u006C\u006F"); // set hint - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); // send event - - // verify send event request includes set location hint - List resultRequests = mockNetworkService.getNetworkRequestsWith( - "https://edge.adobedc.net/ee/\u0048\u0065\u006C\u006C\u006F/v1/interact", - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - } - - @Test - public void testSetLocationHint_andGetLocationHint_hintWithSpaces_doesNotCrash() throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - - Edge.setLocationHint("incorrect location hint"); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertEquals("incorrect location hint", result[0]); - } - - @Test - public void testSetLocationHint_andGetLocationHint_hintWithSpecialCharacters_doesNotCrash() - throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - final String expectedHint = "{\"example\":\"incorrect\"}"; - - Edge.setLocationHint(expectedHint); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertEquals(expectedHint, result[0]); - } - - @Test - public void testSetLocationHint_andGetLocationHint_hintWithUnicodeCharacters_doesNotCrash() - throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - final String[] result = new String[1]; - final String expectedHint = "\u0048\u0065\u006C\u006C\u006F World"; - - Edge.setLocationHint(expectedHint); - Edge.getLocationHint(s -> { - result[0] = s; - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - assertEquals(expectedHint, result[0]); - } - - @Test - public void testGetLocationHint_responseEventChainedToParentId() throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - - Edge.setLocationHint("or2"); - Edge.getLocationHint(s -> { - latch.countDown(); - }); - - latch.await(2000, TimeUnit.MILLISECONDS); - - List dispatchedRequests = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_IDENTITY); - assertEquals(1, dispatchedRequests.size()); - List dispatchedResponses = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_IDENTITY); - assertEquals(1, dispatchedResponses.size()); - - assertEquals(dispatchedRequests.get(0).getUniqueIdentifier(), dispatchedResponses.get(0).getParentID()); - } - - // -------------------------------------------------------------------------------------------- - // test persisted hits (recoverable errors) - // -------------------------------------------------------------------------------------------- - - @Test - public void testSendEvent_withXDMData_sendsExEdgeNetworkRequest_afterRetry() throws InterruptedException { - String edgeResponse = - "\u0000{\"requestId\": \"test-req-id\",\"handle\": [],\"errors\": [],\"warnings\": [{\"type\": \"https://ns.adobe.com/aep/errors/EXEG-0204-502\",\"status\": 503,\"title\": \"A warning occurred.\",\"report\": {\"cause\": {\"message\": \"Unavailable\",\"code\": 503}}}]}"; - - // bad connection, hits will be retried - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - null, - edgeResponse, - 503, - null, - null - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - ExperienceEvent event = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("test", "xdm"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(event, null); - mockNetworkService.assertAllNetworkRequestExpectations(); - resetTestExpectations(); - - // good connection, hits sent - responseConnection = mockNetworkService.createMockNetworkResponse(edgeResponse, 200); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(true); - } - - @Test - public void testSendEvent_withXDMData_sendsExEdgeNetworkRequest_afterRetryMultipleHits() - throws InterruptedException { - String edgeResponse = - "\u0000{" + - " \"requestId\": \"test-req-id\"," + - " \"handle\": [" + - " ]," + - " \"errors\": [" + - " ]," + - " \"warnings\": [" + - " {" + - " \"type\": \"https://ns.adobe.com/aep/errors/EXEG-0204-502\"," + - " \"status\": 503," + - " \"title\": \"A warning occurred.\"," + - " \"report\": {" + - " \"cause\": {" + - " \"message\": \"Unavailable\"," + - " \"code\": 503" + - " }" + - " }" + - " }" + - " ]" + - "}"; - - // bad connection, hits will be retried - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - null, - edgeResponse, - 503, - null, - null - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 2); - - ExperienceEvent event = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("test", "xdm"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ) - .build(); - Edge.sendEvent(event, null); - Edge.sendEvent(event, null); - - assertExpectedEvents(false); - mockNetworkService.assertAllNetworkRequestExpectations(); - resetTestExpectations(); - - // good connection, hits sent - responseConnection = mockNetworkService.createMockNetworkResponse(edgeResponse, 200); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 2); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 2); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(false); - } - - @Test - public void testSendEvent_multiStatusResponse_dispatchesEvents() throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - final String response = - "\u0000{\"requestId\":\"72eaa048-207e-4dde-bf16-0cb2b21336d5\",\"handle\":[],\"errors\":[{\"type\":\"https://ns.adobe.com/aep/errors/EXEG-0201-504\",\"status\":504,\"title\":\"The 'com.adobe.experience.platform.ode' service is temporarily unable to serve this request. Please try again later.\",\"report\":{\"eventIndex\":0}}],\"warnings\":[{\"type\":\"https://ns.adobe.com/aep/errors/EXEG-0204-200\",\"status\":200,\"title\":\"A warning occurred while calling the 'com.adobe.audiencemanager' service for this request.\",\"report\":{\"eventIndex\":0,\"cause\":{\"message\":\"Cannot read related customer for device id: ...\",\"code\":202}}}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(response, 207); - - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 2); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(false); - - List requestEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, requestEvents.size()); - - List resultEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(2, resultEvents.size()); - - Map eventData1 = TestUtils.flattenMap(resultEvents.get(0).getEventData()); - assertEquals(5, eventData1.size()); - assertEquals("504", eventData1.get("status")); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0201-504", eventData1.get("type")); - assertEquals( - "The 'com.adobe.experience.platform.ode' service is temporarily unable to serve this request. Please try again later.", - eventData1.get("title") - ); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), eventData1.get("requestEventId")); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), resultEvents.get(0).getParentID()); - - Map eventData2 = TestUtils.flattenMap(resultEvents.get(1).getEventData()); - assertEquals(7, eventData2.size()); - assertEquals("200", eventData2.get("status")); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0204-200", eventData2.get("type")); - assertEquals( - "A warning occurred while calling the 'com.adobe.audiencemanager' service for this request.", - eventData2.get("title") - ); - assertEquals("Cannot read related customer for device id: ...", eventData2.get("report.cause.message")); - assertEquals("202", eventData2.get("report.cause.code")); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), eventData2.get("requestEventId")); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), resultEvents.get(1).getParentID()); - } - - @Test - public void testSendEvent_fatalError() throws InterruptedException { - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - final String response = - "{" + - "\"type\" : \"https://ns.adobe.com/aep/errors/EXEG-0104-422\"," + - "\"status\": 422," + - "\"title\" : \"Unprocessable Entity\"," + - "\"detail\": \"Invalid request (report attached). Please check your input and try again.\"," + - "\"report\": {" + - " \"errors\": [" + - " \"Allowed Adobe version is 1.0 for standard 'Adobe' at index 0\"," + - " \"Allowed IAB version is 2.0 for standard 'IAB TCF' at index 1\"," + - " \"IAB consent string value must not be empty for standard 'IAB TCF' at index 1\"" + - " ]," + - " \"requestId\": \"0f8821e5-ed1a-4301-b445-5f336fb50ee8\"," + - " \"orgId\": \"test@AdobeOrg\"" + - " }" + - "}"; - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - null, - response, - 422, - null, - null - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - - Edge.sendEvent(XDM_EXPERIENCE_EVENT, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertExpectedEvents(false); - - List requestEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.REQUEST_CONTENT); - assertEquals(1, requestEvents.size()); - - List resultEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, resultEvents.size()); - - Map eventData = TestUtils.flattenMap(resultEvents.get(0).getEventData()); - - assertEquals(11, eventData.size()); - assertEquals("422", eventData.get("status")); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0104-422", eventData.get("type")); - assertEquals("Unprocessable Entity", eventData.get("title")); - assertEquals( - "Invalid request (report attached). Please check your input and try again.", - eventData.get("detail") - ); - assertEquals("Allowed Adobe version is 1.0 for standard 'Adobe' at index 0", eventData.get("report.errors[0]")); - assertEquals("Allowed IAB version is 2.0 for standard 'IAB TCF' at index 1", eventData.get("report.errors[1]")); - assertEquals( - "IAB consent string value must not be empty for standard 'IAB TCF' at index 1", - eventData.get("report.errors[2]") - ); - assertEquals("0f8821e5-ed1a-4301-b445-5f336fb50ee8", eventData.get("report.requestId")); - assertEquals("test@AdobeOrg", eventData.get("report.orgId")); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), eventData.get("requestEventId")); - assertEquals(requestEvents.get(0).getUniqueIdentifier(), resultEvents.get(0).getParentID()); - } - - /** - * Resets all test helper expectations and recorded data - */ - private void resetTestExpectations() { - mockNetworkService.reset(); - TestHelper.resetTestExpectations(); - } - - private void updateConfiguration(final Map config) throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.updateConfiguration(config); - MobileCore.getPrivacyStatus(mobilePrivacyStatus -> latch.countDown()); - - assertTrue(latch.await(2, TimeUnit.SECONDS)); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgePathOverwriteTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgePathOverwriteTests.java deleted file mode 100644 index a68e81ca..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/EdgePathOverwriteTests.java +++ /dev/null @@ -1,281 +0,0 @@ -/* - Copyright 2023 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; - -import static com.adobe.marketing.mobile.services.HttpMethod.POST; -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.resetTestExpectations; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.edge.identity.Identity; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class EdgePathOverwriteTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final String EXEDGE_MEDIA_URL_STRING = TestConstants.Defaults.EXEDGE_MEDIA_PROD_URL_STRING; - private static final String EXEDGE_MEDIA_OR2_LOC_URL_STRING = - TestConstants.Defaults.EXEDGE_MEDIA_OR2_LOC_URL_STRING; - private static final String CONFIG_ID = "1234abcd-abcd-1234-5678-123456abcdef"; - private static final String DEFAULT_RESPONSE_STRING = "\u0000{\"test\": \"json\"}"; - private static final int TIMEOUT_MILLIS = 5000; - - @Rule - public RuleChain rule = RuleChain.outerRule(new TestHelper.LogOnErrorRule()).around(new TestHelper.SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - - setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 4); - - HashMap config = new HashMap() { - { - put("edge.configId", CONFIG_ID); - } - }; - MobileCore.updateConfiguration(config); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, Identity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - latch.await(); - - assertExpectedEvents(false); - resetTestExpectations(); - } - - @After - public void tearDown() { - mockNetworkService.reset(); - } - - @Test - public void testSendEvent_withXDMData_withOverwritePath_overwritesRequestPathAndSendsExEdgeNetworkRequest() - throws Exception { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_MEDIA_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_MEDIA_URL_STRING, POST, 1); - - Event experienceEvent = new Event.Builder("test-experience-event", EventType.EDGE, EventSource.REQUEST_CONTENT) - .setEventData( - new HashMap() { - { - put( - "xdm", - new HashMap() { - { - put("testString", "xdmValue"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ); - - put( - "request", - new HashMap() { - { - put("path", "/va/v1/sessionstart"); - } - } - ); - } - } - ) - .build(); - MobileCore.dispatchEvent(experienceEvent); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_MEDIA_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - Map resultPayload = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals(18, resultPayload.size()); - assertEquals("true", resultPayload.get("meta.konductorConfig.streaming.enabled")); - assertEquals("\u0000", resultPayload.get("meta.konductorConfig.streaming.recordSeparator")); - assertEquals("\n", resultPayload.get("meta.konductorConfig.streaming.lineFeed")); - assertNotNull(resultPayload.get("xdm.identityMap.ECID[0].id")); - assertEquals("false", resultPayload.get("xdm.identityMap.ECID[0].primary")); - assertEquals("ambiguous", resultPayload.get("xdm.identityMap.ECID[0].authenticatedState")); - assertNotNull(resultPayload.get("events[0].xdm._id")); - assertNotNull(resultPayload.get("events[0].xdm.timestamp")); - assertEquals("xdmValue", resultPayload.get("events[0].xdm.testString")); - assertEquals("10", resultPayload.get("events[0].xdm.testInt")); - assertEquals("false", resultPayload.get("events[0].xdm.testBool")); - assertEquals("12.89", resultPayload.get("events[0].xdm.testDouble")); - assertEquals("elem1", resultPayload.get("events[0].xdm.testArray[0]")); - assertEquals("elem2", resultPayload.get("events[0].xdm.testArray[1]")); - assertEquals("value", resultPayload.get("events[0].xdm.testMap.key")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_MEDIA_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertEquals("app", resultPayload.get("xdm.implementationDetails.environment")); - assertEquals( - EdgeJson.Event.ImplementationDetails.BASE_NAMESPACE, - resultPayload.get("xdm.implementationDetails.name") - ); - assertEquals( - MobileCore.extensionVersion() + "+" + Edge.extensionVersion(), - resultPayload.get("xdm.implementationDetails.version") - ); - } - - @Test - public void testSendEvent_withXDMData_withOverwritePath_withLocationHintSet_overwritesRequestPathAndSendsExEdgeNetworkRequestWithSetLocationHint() - throws Exception { - Edge.setLocationHint("or2"); - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(DEFAULT_RESPONSE_STRING, 200); - mockNetworkService.setMockResponseFor(EXEDGE_MEDIA_OR2_LOC_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_MEDIA_OR2_LOC_URL_STRING, POST, 1); - - Event experienceEvent = new Event.Builder("test-experience-event", EventType.EDGE, EventSource.REQUEST_CONTENT) - .setEventData( - new HashMap() { - { - put( - "xdm", - new HashMap() { - { - put("testString", "xdmValue"); - put("testInt", 10); - put("testBool", false); - put("testDouble", 12.89); - put( - "testArray", - new ArrayList() { - { - add("elem1"); - add("elem2"); - } - } - ); - put( - "testMap", - new HashMap() { - { - put("key", "value"); - } - } - ); - } - } - ); - - put( - "request", - new HashMap() { - { - put("path", "/va/v1/sessionstart"); - } - } - ); - } - } - ) - .build(); - MobileCore.dispatchEvent(experienceEvent); - - // verify - mockNetworkService.assertAllNetworkRequestExpectations(); - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_MEDIA_OR2_LOC_URL_STRING, - POST, - TIMEOUT_MILLIS - ); - assertEquals(1, resultRequests.size()); - - Map resultPayload = mockNetworkService.getFlattenedNetworkRequestBody(resultRequests.get(0)); - assertEquals(18, resultPayload.size()); - assertEquals("true", resultPayload.get("meta.konductorConfig.streaming.enabled")); - assertEquals("\u0000", resultPayload.get("meta.konductorConfig.streaming.recordSeparator")); - assertEquals("\n", resultPayload.get("meta.konductorConfig.streaming.lineFeed")); - assertNotNull(resultPayload.get("xdm.identityMap.ECID[0].id")); - assertEquals("false", resultPayload.get("xdm.identityMap.ECID[0].primary")); - assertEquals("ambiguous", resultPayload.get("xdm.identityMap.ECID[0].authenticatedState")); - assertNotNull(resultPayload.get("events[0].xdm._id")); - assertNotNull(resultPayload.get("events[0].xdm.timestamp")); - assertEquals("xdmValue", resultPayload.get("events[0].xdm.testString")); - assertEquals("10", resultPayload.get("events[0].xdm.testInt")); - assertEquals("false", resultPayload.get("events[0].xdm.testBool")); - assertEquals("12.89", resultPayload.get("events[0].xdm.testDouble")); - assertEquals("elem1", resultPayload.get("events[0].xdm.testArray[0]")); - assertEquals("elem2", resultPayload.get("events[0].xdm.testArray[1]")); - assertEquals("value", resultPayload.get("events[0].xdm.testMap.key")); - - assertTrue(resultRequests.get(0).getUrl().startsWith(EXEDGE_MEDIA_OR2_LOC_URL_STRING)); - assertEquals(CONFIG_ID, resultRequests.get(0).queryParam("configId")); - assertNotNull(resultRequests.get(0).queryParam("requestId")); - - assertEquals("app", resultPayload.get("xdm.implementationDetails.environment")); - assertEquals( - EdgeJson.Event.ImplementationDetails.BASE_NAMESPACE, - resultPayload.get("xdm.implementationDetails.name") - ); - assertEquals( - MobileCore.extensionVersion() + "+" + Edge.extensionVersion(), - resultPayload.get("xdm.implementationDetails.version") - ); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/IdentityStateFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/IdentityStateFunctionalTests.java deleted file mode 100644 index dc5fca78..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/IdentityStateFunctionalTests.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.services.HttpMethod.POST; -import static com.adobe.marketing.mobile.util.TestHelper.LogOnErrorRule; -import static com.adobe.marketing.mobile.util.TestHelper.SetupCoreRule; -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.resetTestExpectations; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.FakeIdentity; -import com.adobe.marketing.mobile.util.JSONUtils; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import org.json.JSONObject; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class IdentityStateFunctionalTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final String EXEDGE_INTERACT_URL_STRING = TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING; - private static final String CONFIG_ID = "1234abcd-abcd-1234-5678-123456abcdef"; - - @Rule - public RuleChain rule = RuleChain.outerRule(new LogOnErrorRule()).around(new SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - - setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 3); - - HashMap config = new HashMap() { - { - put("edge.configId", CONFIG_ID); - } - }; - MobileCore.updateConfiguration(config); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, FakeIdentity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - - latch.await(); - - assertExpectedEvents(false); - resetTestExpectations(); - } - - @After - public void tearDown() { - mockNetworkService.reset(); - } - - @Test - public void testSendEvent_withPendingIdentityState_noRequestSent() throws InterruptedException { - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - List requests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST - ); - assertTrue(requests.isEmpty()); - } - - @Test - public void testSendEvent_withPendingIdentityState_thenValidIdentityState_requestSentAfterChange() - throws Exception { - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - List requests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - 2000 - ); - assertTrue(requests.isEmpty()); // no network requests sent yet - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - "\u0000{\"test\": \"json\"}", - 200 - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - final String jsonStr = - "{\n" + - " \"identityMap\": {\n" + - " \"ECID\": [\n" + - " {\n" + - " \"id\":" + - "1234" + - ",\n" + - " \"authenticatedState\": \"ambiguous\",\n" + - " \"primary\": false\n" + - " }\n" + - " ]\n" + - " }\n" + - "}"; - - final JSONObject jsonObject = new JSONObject(jsonStr); - final Map identityMap = JSONUtils.toMap(jsonObject); - // Once the shared state is set, the Edge Extension is expected to reprocess the original - // Send Event request once the Hub Shared State event is received. - FakeIdentity.setXDMSharedState(identityMap, FakeIdentity.EVENT_TYPE); - - mockNetworkService.assertAllNetworkRequestExpectations(); - - requests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST); - assertEquals(1, requests.size()); - Map flattenedRequestBody = mockNetworkService.getFlattenedNetworkRequestBody(requests.get(0)); - assertEquals("1234", flattenedRequestBody.get("xdm.identityMap.ECID[0].id")); - } - - @Test - public void testSendEvent_withNoECIDInIdentityState_requestSentWithoutECID() throws Exception { - final String jsonStr = - "{\n" + - " \"identityMap\": {\n" + - " \"USERID\": [\n" + - " {\n" + - " \"id\":" + - "someUserID" + - ",\n" + - " \"authenticatedState\": \"authenticated\",\n" + - " \"primary\": false\n" + - " }\n" + - " ]\n" + - " }\n" + - "}"; - - final JSONObject jsonObject = new JSONObject(jsonStr); - final Map identityState = JSONUtils.toMap(jsonObject); - FakeIdentity.setXDMSharedState(identityState, FakeIdentity.EVENT_TYPE); // set state without ECID - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - "\u0000{\"test\": \"json\"}", - 200 - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - - // Assert network request does not contain an ECID - List requests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST - ); - assertEquals(1, requests.size()); - Map flattenedRequestBody = mockNetworkService.getFlattenedNetworkRequestBody(requests.get(0)); - assertNull(flattenedRequestBody.get("xdm.identityMap.ECID[0].id")); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NetworkResponseHandlerFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NetworkResponseHandlerFunctionalTests.java deleted file mode 100644 index 88805a4a..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NetworkResponseHandlerFunctionalTests.java +++ /dev/null @@ -1,1224 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.getDispatchedEventsWith; -import static com.adobe.marketing.mobile.util.TestHelper.resetTestExpectations; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.edge.identity.Identity; -import com.adobe.marketing.mobile.services.MockDataStoreService; -import com.adobe.marketing.mobile.services.NamedCollection; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import com.adobe.marketing.mobile.util.TestUtils; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class NetworkResponseHandlerFunctionalTests { - - private static final String CONFIG_ID = "1234abcd-abcd-1234-5678-123456abcdef"; - - private static final Event event1 = new Event.Builder("e1", "eventType", "eventSource").build(); - private static final Event event2 = new Event.Builder("e2", "eventType", "eventSource").build(); - private static final Event eventSendComplete = new Event.Builder("ec", "eventType", "eventSource") - .setEventData( - new HashMap() { - { - put( - "xdm", - new HashMap() { - { - put("test", "data"); - } - } - ); - put( - "request", - new HashMap() { - { - put("sendCompletion", true); - } - } - ); - } - } - ) - .build(); - private NetworkResponseHandler networkResponseHandler; - - private String receivedSetLocationHint; - private Integer receivedSetTtlSeconds; - private final MockDataStoreService mockDataStoreService = new MockDataStoreService(); - private NamedCollection testNamedCollection; - - private final EdgeStateCallback edgeStateCallback = new EdgeStateCallback() { - @Override - public Map getImplementationDetails() { - return null; // not called by NetworkResponseHandler - } - - @Override - public String getLocationHint() { - return null; // not called by NetworkResponseHandler - } - - @Override - public void setLocationHint(String hint, int ttlSeconds) { - receivedSetLocationHint = hint; - receivedSetTtlSeconds = ttlSeconds; - } - }; - - @Rule - public RuleChain rule = RuleChain.outerRule(new TestHelper.LogOnErrorRule()).around(new TestHelper.SetupCoreRule()); - - @Before - public void setup() throws Exception { - setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 4); - - HashMap config = new HashMap() { - { - put("edge.configId", CONFIG_ID); - } - }; - MobileCore.updateConfiguration(config); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, Identity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - latch.await(); - - assertExpectedEvents(false); - resetTestExpectations(); - - this.testNamedCollection = mockDataStoreService.getNamedCollection(TestConstants.EDGE_DATA_STORAGE); - this.networkResponseHandler = new NetworkResponseHandler(testNamedCollection, edgeStateCallback); - } - - // --------------------------------------------------------------------------------------------- - // processResponseOnError - // --------------------------------------------------------------------------------------------- - - @Test - public void testProcessResponseOnError_WhenEmptyJsonError_doesNotHandleError() throws InterruptedException { - final String jsonError = ""; - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnError_WhenInvalidJsonError_doesNotHandleError() throws InterruptedException { - final String jsonError = "{ invalid json }"; - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnError_WhenGenericJsonError_noMatchingEvents_dispatchesEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String jsonError = - "{\n" + - "\"type\": \"https://ns.adobe.com/aep/errors/EXEG-0201-503\",\n" + - "\"title\": \"Request to Data platform failed with an unknown exception\"" + - "\n}"; - networkResponseHandler.addWaitingEvent("abc", event1); // Request ID does not match - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 5000); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(3, flattenReceivedData.size()); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0201-503", flattenReceivedData.get("type")); - assertEquals("Request to Data platform failed with an unknown exception", flattenReceivedData.get("title")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertNull(flattenReceivedData.get("requestEventId")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenGenericJsonError_dispatchesEventChainedToParentEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String jsonError = - "{\n" + - "\"type\": \"https://ns.adobe.com/aep/errors/EXEG-0201-503\",\n" + - "\"title\": \"Request to Data platform failed with an unknown exception\"" + - "\n}"; - networkResponseHandler.addWaitingEvent("123", event1); - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 5000); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0201-503", flattenReceivedData.get("type")); - assertEquals("Request to Data platform failed with an unknown exception", flattenReceivedData.get("title")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenOneEventJsonError_dispatchesEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [\n" + - " {\n" + - " \"status\": 500,\n" + - " \"type\": \"https://ns.adobe.com/aep/errors/EXEG-0201-503\",\n" + - " \"title\": \"Failed due to unrecoverable system error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.commerce.purchases\"\n" + - " }\n" + - " ]\n" + - " }"; - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0201-503", flattenReceivedData.get("type")); - assertEquals("500", flattenReceivedData.get("status")); - assertEquals( - "Failed due to unrecoverable system error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.commerce.purchases", - flattenReceivedData.get("title") - ); - assertEquals("123", flattenReceivedData.get("requestId")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenJsonErrorIncorrectType_doesNotHandleError() throws InterruptedException { - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\":\n" + // Json Array expected - " {\n" + - " \"status\": 500,\n" + - " \"type\": \"https://ns.adobe.com/aep/errors/EXEG-0201-503\",\n" + - " \"title\": \"Failed due to unrecoverable system error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.commerce.purchases\"\n" + - " }\n" + - " }"; - networkResponseHandler.processResponseOnError(jsonError, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnError_WhenValidEventIndex_dispatchesPairedEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [\n" + - " {\n" + - " \"status\": 100,\n" + - " \"type\": \"personalization\",\n" + - " \"title\": \"Button color not found\",\n" + - " \"report\": {\n" + - " \"eventIndex\": 1\n" + - " }\n" + - " }\n" + - " ]\n" + - " }"; - networkResponseHandler.addWaitingEvents( - requestId, - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnError(jsonError, requestId); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(5, flattenReceivedData.size()); - assertEquals("personalization", flattenReceivedData.get("type")); - assertEquals("100", flattenReceivedData.get("status")); - assertEquals("Button color not found", flattenReceivedData.get("title")); - assertEquals(requestId, flattenReceivedData.get("requestId")); - assertEquals(event2.getUniqueIdentifier(), flattenReceivedData.get("requestEventId")); - assertEquals(event2.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenUnknownEventIndex_doesNotCrash() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [\n" + - " {\n" + - " \"status\": 100,\n" + - " \"type\": \"personalization\",\n" + - " \"title\": \"Button color not found\",\n" + - " \"report\": {\n" + - " \"eventIndex\": 10\n" + - " }\n" + - " }\n" + - " ]\n" + - " }"; - networkResponseHandler.addWaitingEvents( - requestId, - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnError(jsonError, requestId); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("personalization", flattenReceivedData.get("type")); - assertEquals("100", flattenReceivedData.get("status")); - assertEquals("Button color not found", flattenReceivedData.get("title")); - assertEquals(requestId, flattenReceivedData.get("requestId")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenUnknownRequestId_doesNotCrash() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [\n" + - " {\n" + - " \"status\": 100,\n" + - " \"type\": \"personalization\",\n" + - " \"title\": \"Button color not found\",\n" + - " \"report\": {\n" + - " \"eventIndex\": 0\n" + - " }\n" + - " }\n" + - " ]\n" + - " }"; - networkResponseHandler.addWaitingEvents( - requestId, - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnError(jsonError, "567"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("personalization", flattenReceivedData.get("type")); - assertEquals("100", flattenReceivedData.get("status")); - assertEquals("Button color not found", flattenReceivedData.get("title")); - assertEquals("567", flattenReceivedData.get("requestId")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnError_WhenTwoEventJsonError_dispatchesTwoEvents() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 2); - final String requestId = "123"; - final String jsonError = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [\n" + - " {\n" + - " \"status\": 0,\n" + - " \"type\": \"https://ns.adobe.com/aep/errors/EXEG-0201-503\",\n" + - " \"title\": \"Failed due to unrecoverable system error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.commerce.purchases\"\n" + - " },\n" + - " {\n" + - " \"status\": 2003,\n" + - " \"type\": \"personalization\",\n" + - " \"title\": \"Failed to process personalization event\"\n" + - " }\n" + - " ]\n" + - " }"; - networkResponseHandler.addWaitingEvent(requestId, event1); - networkResponseHandler.processResponseOnError(jsonError, requestId); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(2, dispatchEvents.size()); - - Map flattenReceivedData1 = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(5, flattenReceivedData1.size()); - assertEquals("0", flattenReceivedData1.get("status")); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0201-503", flattenReceivedData1.get("type")); - assertEquals( - "Failed due to unrecoverable system error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.commerce.purchases", - flattenReceivedData1.get("title") - ); - assertEquals(requestId, flattenReceivedData1.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData1.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - - Map flattenReceivedData2 = TestUtils.flattenMap(dispatchEvents.get(1).getEventData()); - assertEquals(5, flattenReceivedData2.size()); - assertEquals("2003", flattenReceivedData2.get("status")); - assertEquals("personalization", flattenReceivedData2.get("type")); - assertEquals("Failed to process personalization event", flattenReceivedData2.get("title")); - assertEquals(requestId, flattenReceivedData2.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData2.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(1).getParentID()); - } - - // --------------------------------------------------------------------------------------------- - // processResponseOnSuccess - // --------------------------------------------------------------------------------------------- - - @Test - public void testProcessResponseOnSuccess_WhenEmptyStringResponse_doesNotDispatchEvent() - throws InterruptedException { - final String jsonResponse = ""; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnSuccess_WhenEmptyJsonResponse_doesNotDispatchEvent() throws InterruptedException { - final String jsonResponse = "{}"; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnSuccess_WhenInvalidJsonResponse_doesNotDispatchEvent() - throws InterruptedException { - final String jsonResponse = "{ invalid json }"; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_CONTENT); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnSuccess_WhenOneEventHandle_dispatchesEvent() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"state:store\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - networkResponseHandler.addWaitingEvent("123", event1); - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "state:store"); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(6, flattenReceivedData.size()); - assertEquals("state:store", flattenReceivedData.get("type")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("s_ecid", flattenReceivedData.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData.get("payload[0].maxAge")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_WhenOneEventHandle_emptyEventHandleType_dispatchesEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("s_ecid", flattenReceivedData.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData.get("payload[0].maxAge")); - } - - @Test - public void testProcessResponseOnSuccess_WhenOneEventHandle_nullEventHandleType_dispatchesEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.RESPONSE_CONTENT); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData.size()); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("s_ecid", flattenReceivedData.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData.get("payload[0].maxAge")); - } - - @Test - public void testProcessResponseOnSuccess_WhenTwoEventHandles_dispatchesTwoEvents() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 2); - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"state:store\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]},\n" + - " {\n" + - " \"type\": \"identity:persist\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"id\": \"29068398647607325310376254630528178721\",\n" + - " \"namespace\": {\n" + - " \"code\": \"ECID\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "state:store"); - dispatchEvents.addAll(getDispatchedEventsWith(EventType.EDGE, "identity:persist")); - assertEquals(2, dispatchEvents.size()); - - // verify event 1 - Map flattenReceivedData1 = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(5, flattenReceivedData1.size()); - assertEquals("state:store", flattenReceivedData1.get("type")); - assertEquals("123", flattenReceivedData1.get("requestId")); - assertEquals("s_ecid", flattenReceivedData1.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData1.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData1.get("payload[0].maxAge")); - assertNull(dispatchEvents.get(0).getParentID()); - - // verify event 2 - Map flattenReceivedData2 = TestUtils.flattenMap(dispatchEvents.get(1).getEventData()); - assertEquals(4, flattenReceivedData2.size()); - assertEquals("identity:persist", flattenReceivedData2.get("type")); - assertEquals("123", flattenReceivedData2.get("requestId")); - assertEquals("29068398647607325310376254630528178721", flattenReceivedData2.get("payload[0].id")); - assertEquals("ECID", flattenReceivedData2.get("payload[0].namespace.code")); - assertNull(dispatchEvents.get(1).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_WhenEventHandleWithEventIndex_dispatchesEventWithRequestEventId() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 2); - final String requestId = "123"; - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"state:store\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]},\n" + - " {\n" + - " \"type\": \"pairedeventexample\",\n" + - " \"eventIndex\": 1,\n" + - " \"payload\": [\n" + - " {\n" + - " \"id\": \"123612123812381\"\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - networkResponseHandler.addWaitingEvents( - requestId, - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnSuccess(jsonResponse, requestId); - - // verify event 1 - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "state:store"); - dispatchEvents.addAll(getDispatchedEventsWith(EventType.EDGE, "pairedeventexample")); - assertEquals(2, dispatchEvents.size()); - - Map flattenReceivedData1 = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(6, flattenReceivedData1.size()); - assertEquals("state:store", flattenReceivedData1.get("type")); - assertEquals("s_ecid", flattenReceivedData1.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData1.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData1.get("payload[0].maxAge")); - assertEquals("123", flattenReceivedData1.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData1.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - - // verify event 2 - Map flattenReceivedData2 = TestUtils.flattenMap(dispatchEvents.get(1).getEventData()); - assertEquals(4, flattenReceivedData2.size()); - assertEquals("pairedeventexample", flattenReceivedData2.get("type")); - assertEquals("123612123812381", flattenReceivedData2.get("payload[0].id")); - assertEquals("123", flattenReceivedData2.get("requestId")); - assertEquals(event2.getUniqueIdentifier(), flattenReceivedData2.get("requestEventId")); - assertEquals(event2.getUniqueIdentifier(), dispatchEvents.get(1).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_WhenEventHandleWithUnknownEventIndex_dispatchesUnpairedEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"pairedeventexample\",\n" + - " \"eventIndex\": 10,\n" + - " \"payload\": [\n" + - " {\n" + - " \"id\": \"123612123812381\"\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.addWaitingEvents( - requestId, - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnSuccess(jsonResponse, requestId); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "pairedeventexample"); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(3, flattenReceivedData.size()); - assertEquals("pairedeventexample", flattenReceivedData.get("type")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("123612123812381", flattenReceivedData.get("payload[0].id")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_WhenUnknownRequestId_doesNotCrash() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"pairedeventexample\",\n" + - " \"eventIndex\": 0,\n" + - " \"payload\": [\n" + - " {\n" + - " \"id\": \"123612123812381\"\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.addWaitingEvents( - "567", - new ArrayList() { - { - add(event1); - add(event2); - } - } - ); - networkResponseHandler.processResponseOnSuccess(jsonResponse, requestId); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "pairedeventexample"); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(3, flattenReceivedData.size()); - assertEquals("pairedeventexample", flattenReceivedData.get("type")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("123612123812381", flattenReceivedData.get("payload[0].id")); - assertNull(dispatchEvents.get(0).getParentID()); - } - - // --------------------------------------------------------------------------------------------- - // processResponseOnSuccess with mixed event handles, errors, warnings - // --------------------------------------------------------------------------------------------- - - @Test - public void testProcessResponseOnSuccess_WhenEventHandleAndError_dispatchesTwoEvents() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 1); - final String requestId = "123"; - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"state:store\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]}],\n" + - " \"errors\": [" + - " {\n" + - " \"status\": 2003,\n" + - " \"type\": \"personalization\",\n" + - " \"title\": \"Failed to process personalization event\"\n" + - " }\n" + - " ]\n" + - " }"; - - networkResponseHandler.addWaitingEvents(requestId, Arrays.asList(event1, event2)); - networkResponseHandler.processResponseOnSuccess(jsonResponse, requestId); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "state:store"); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData1 = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(6, flattenReceivedData1.size()); - assertEquals("state:store", flattenReceivedData1.get("type")); - assertEquals("123", flattenReceivedData1.get("requestId")); - assertEquals("s_ecid", flattenReceivedData1.get("payload[0].key")); - assertEquals("MCMID|29068398647607325310376254630528178721", flattenReceivedData1.get("payload[0].value")); - assertEquals("15552000", flattenReceivedData1.get("payload[0].maxAge")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData1.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - - List dispatchErrorEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(1, dispatchErrorEvents.size()); - - Map flattenReceivedData2 = TestUtils.flattenMap(dispatchErrorEvents.get(0).getEventData()); - assertEquals(5, flattenReceivedData2.size()); - assertEquals("personalization", flattenReceivedData2.get("type")); - assertEquals("2003", flattenReceivedData2.get("status")); - assertEquals("Failed to process personalization event", flattenReceivedData2.get("title")); - assertEquals("123", flattenReceivedData2.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData2.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchErrorEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_WhenErrorAndWarning_dispatchesTwoEvents() throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT, 2); - final String requestId = "123"; - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [],\n" + - " \"errors\": [" + - " {\n" + - " \"status\": 2003,\n" + - " \"title\": \"Failed to process personalization event\",\n" + - " \"report\": {\n" + - " \"eventIndex\": 1 \n" + - " }\n" + - " }\n" + - " ],\n" + - " \"warnings\": [" + - " {\n" + - " \"type\": \"https://ns.adobe.com/aep/errors/EXEG-0204-200\",\n" + - " \"status\": 98,\n" + - " \"title\": \"Some Informative stuff here\",\n" + - " \"report\": {" + - " \"eventIndex\": 0, \n" + - " \"cause\": {" + - " \"message\": \"Some Informative stuff here\",\n" + - " \"code\": 202\n" + - " }" + - " }" + - " }\n" + - " ]\n" + - " }"; - - networkResponseHandler.addWaitingEvents(requestId, Arrays.asList(event1, event2)); - networkResponseHandler.processResponseOnSuccess(jsonResponse, requestId); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.ERROR_RESPONSE_CONTENT); - assertEquals(2, dispatchEvents.size()); - - Map flattenReceivedData1 = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(4, flattenReceivedData1.size()); - assertEquals("2003", flattenReceivedData1.get("status")); - assertEquals("Failed to process personalization event", flattenReceivedData1.get("title")); - assertEquals("123", flattenReceivedData1.get("requestId")); - assertEquals(event2.getUniqueIdentifier(), flattenReceivedData1.get("requestEventId")); - assertEquals(event2.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - - Map flattenReceivedData2 = TestUtils.flattenMap(dispatchEvents.get(1).getEventData()); - assertEquals(7, flattenReceivedData2.size()); - assertEquals("https://ns.adobe.com/aep/errors/EXEG-0204-200", flattenReceivedData2.get("type")); - assertEquals("98", flattenReceivedData2.get("status")); - assertEquals("Some Informative stuff here", flattenReceivedData2.get("title")); - assertEquals("Some Informative stuff here", flattenReceivedData2.get("report.cause.message")); - assertEquals("202", flattenReceivedData2.get("report.cause.code")); - assertEquals("123", flattenReceivedData2.get("requestId")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData2.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(1).getParentID()); - } - - // --------------------------------------------------------------------------------------------- - // processResponseOnSuccess with locationHint:result - // --------------------------------------------------------------------------------------------- - - @Test - public void testProcessResponseOnSuccess_WhenLocationHintResultEventHandle_dispatchesEvent() - throws InterruptedException { - setExpectationEvent(EventType.EDGE, EventSource.RESPONSE_CONTENT, 1); - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\",\n" + - " \"ttlSeconds\": 1800\n" + - " },\n" + - " {\n" + - " \"scope\": \"Target\",\n" + - " \"hint\": \"edge34\",\n" + - " \"ttlSeconds\": 600\n" + - " }\n" + - " ]\n" + - " }]\n" + - " }"; - networkResponseHandler.addWaitingEvent("123", event1); - networkResponseHandler.processResponseOnSuccess(jsonResponse, "123"); - - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, "locationHint:result"); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(9, flattenReceivedData.size()); - assertEquals("locationHint:result", flattenReceivedData.get("type")); - assertEquals("123", flattenReceivedData.get("requestId")); - assertEquals("EdgeNetwork", flattenReceivedData.get("payload[0].scope")); - assertEquals("or2", flattenReceivedData.get("payload[0].hint")); - assertEquals("1800", flattenReceivedData.get("payload[0].ttlSeconds")); - assertEquals("Target", flattenReceivedData.get("payload[1].scope")); - assertEquals("edge34", flattenReceivedData.get("payload[1].hint")); - assertEquals("600", flattenReceivedData.get("payload[1].ttlSeconds")); - assertEquals(event1.getUniqueIdentifier(), flattenReceivedData.get("requestEventId")); - assertEquals(event1.getUniqueIdentifier(), dispatchEvents.get(0).getParentID()); - } - - @Test - public void testProcessResponseOnSuccess_afterResetEvent_updatesLocationHint() { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(event1.getTimestamp()); - calendar.add(Calendar.SECOND, -10); // date is before event timestamp - networkResponseHandler.setLastResetDate(calendar.getTimeInMillis()); - - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\",\n" + - " \"ttlSeconds\": 1800\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertEquals("or2", receivedSetLocationHint); - assertEquals(Integer.valueOf(1800), receivedSetTtlSeconds); - } - - @Test - public void testProcessResponseOnSuccess_beforeResetEvent_doesNotUpdateLocationHint() { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(event1.getTimestamp()); - calendar.add(Calendar.SECOND, 10); // date is after event timestamp - networkResponseHandler.setLastResetDate(calendar.getTimeInMillis()); - - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\",\n" + - " \"ttlSeconds\": 1800\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertNull(receivedSetLocationHint); - assertNull(receivedSetTtlSeconds); - } - - @Test - public void testProcessResponseOnSuccess_whenEventHandleHasBothStateStoreAndLocationHintResult_stateStoreSaved_locationHintUpdated() { - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\",\n" + - " \"ttlSeconds\": 1800\n" + - " }\n" + - " ]},\n" + - " {\n" + - " \"type\": \"state:store\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"key\": \"s_ecid\",\n" + - " \"value\": \"MCMID|29068398647607325310376254630528178721\",\n" + - " \"maxAge\": 15552000\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertEquals("or2", receivedSetLocationHint); - assertEquals(Integer.valueOf(1800), receivedSetTtlSeconds); - - // verify saved state - StoreResponsePayloadManager payloadManager = new StoreResponsePayloadManager(testNamedCollection); - assertFalse(payloadManager.getActiveStores().isEmpty()); - } - - @Test - public void testProcessResponseOnSuccess_whenLocationHintHandleDoesNotHaveHint_thenLocationHintNotUpdate() { - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"ttlSeconds\": 1800\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertNull(receivedSetLocationHint); - assertNull(receivedSetTtlSeconds); - } - - @Test - public void testProcessResponseOnSuccess_whenLocationHintHandleHasEmptyHint_thenLocationHintNotUpdated() { - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"\",\n" + - " \"ttlSeconds\": 1800\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertNull(receivedSetLocationHint); - assertNull(receivedSetTtlSeconds); - } - - @Test - public void testProcessResponseOnSuccess_whenLocationHintHandleDoesNotHaveTtl_thenLocationHintNotUpdated() { - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\"\n" + - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertNull(receivedSetLocationHint); - assertNull(receivedSetTtlSeconds); - } - - @Test - public void testProcessResponseOnSuccess_whenLocationHintHandleHasIncorrectTtlType_thenLocationHintNotUpdated() { - networkResponseHandler.addWaitingEvents( - "d81c93e5-7558-4996-a93c-489d550748b8", - new ArrayList() { - { - add(event1); - } - } - ); - - final String jsonResponse = - "{\n" + - " \"requestId\": \"d81c93e5-7558-4996-a93c-489d550748b8\",\n" + - " \"handle\": [" + - " {\n" + - " \"type\": \"locationHint:result\",\n" + - " \"payload\": [\n" + - " {\n" + - " \"scope\": \"EdgeNetwork\",\n" + - " \"hint\": \"or2\",\n" + - " \"ttlSeconds\": \"1800\"\n" + // String but expect Int - " }\n" + - " ]\n" + - " }],\n" + - " \"errors\": []\n" + - " }"; - - networkResponseHandler.processResponseOnSuccess(jsonResponse, "d81c93e5-7558-4996-a93c-489d550748b8"); - - // verify saved location hint - assertNull(receivedSetLocationHint); - assertNull(receivedSetTtlSeconds); - } - - // --------------------------------------------------------------------------------------------- - // processResponseOnComplete - // --------------------------------------------------------------------------------------------- - - @Test - public void testProcessResponseOnComplete_whenNoEventRequestsCompletion_thenNoEventDispatched() - throws InterruptedException { - networkResponseHandler.addWaitingEvents( - "123", - new ArrayList() { - { - add(event1); - } - } - ); - networkResponseHandler.processResponseOnComplete("123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.CONTENT_COMPLETE); - assertEquals(0, dispatchEvents.size()); - } - - @Test - public void testProcessResponseOnComplete_whenEventRequestsCompletion_thenDispatchCompleteEvent() - throws InterruptedException { - networkResponseHandler.addWaitingEvents( - "123", - new ArrayList() { - { - add(eventSendComplete); - } - } - ); - networkResponseHandler.processResponseOnComplete("123"); - List dispatchEvents = getDispatchedEventsWith(EventType.EDGE, EventSource.CONTENT_COMPLETE); - assertEquals(1, dispatchEvents.size()); - - Map flattenReceivedData = TestUtils.flattenMap(dispatchEvents.get(0).getEventData()); - assertEquals(1, flattenReceivedData.size()); - assertEquals("123", flattenReceivedData.get("requestId")); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NoConfigFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NoConfigFunctionalTests.java deleted file mode 100644 index ed6aec7a..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/NoConfigFunctionalTests.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.util.TestHelper.LogOnErrorRule; -import static com.adobe.marketing.mobile.util.TestHelper.SetupCoreRule; -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.getSharedStateFor; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.HttpMethod; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.FakeIdentity; -import com.adobe.marketing.mobile.util.JSONUtils; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import com.adobe.marketing.mobile.util.TestUtils; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.json.JSONObject; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class NoConfigFunctionalTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final String EXEDGE_INTERACT_URL_STRING = TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING; - - @Rule - public RuleChain rule = RuleChain.outerRule(new LogOnErrorRule()).around(new SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 2); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, FakeIdentity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - - latch.await(); - - assertExpectedEvents(false); - resetTestExpectations(); - } - - @After - public void tearDown() { - mockNetworkService.reset(); - } - - @Test - public void testHandleExperienceEventRequest_withPendingConfigurationState_expectEventsQueueIsBlocked() - throws Exception { - Map configState = getSharedStateFor( - TestConstants.SharedState.CONFIGURATION, - TestConstants.Defaults.WAIT_SHARED_STATE_TIMEOUT_MS - ); - assertNull(configState); // verify Configuration state is pending - - // Set "fake" ECID - final String jsonStr = - "{\n" + - " \"identityMap\": {\n" + - " \"ECID\": [\n" + - " {\n" + - " \"id\":" + - "1234" + - ",\n" + - " \"authenticatedState\": \"ambiguous\",\n" + - " \"primary\": false\n" + - " }\n" + - " ]\n" + - " }\n" + - "}"; - - final JSONObject jsonObject = new JSONObject(jsonStr); - final Map identityMap = JSONUtils.toMap(jsonObject); - - resetTestExpectations(); // reset received events - setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("testString", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - // verify the request event was only event sent - assertExpectedEvents(false); - - // verify the network request was not sent - List requests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - HttpMethod.POST - ); - assertTrue(requests.isEmpty()); - } - - @Test - public void testCompletionHandler_withPendingConfigurationState_thenValidConfig_returnsEventHandles() - throws Exception { - final String responseBody = - "\u0000{\"requestId\": \"0ee43289-4a4e-469a-bf5c-1d8186919a26\",\"handle\": [{\"payload\": [{\"id\": \"AT:eyJhY3Rpdml0eUlkIjoiMTE3NTg4IiwiZXhwZXJpZW5jZUlkIjoiMSJ9\",\"scope\": \"buttonColor\",\"items\": [{ \"schema\": \"https://ns.adobe.com/personalization/json-content-item\",\"data\": {\"content\": {\"value\": \"#D41DBA\"}}}]}],\"type\": \"personalization:decisions\"},{\"payload\": [{\"type\": \"url\",\"id\": 411,\"spec\": {\"url\": \"//example.url?d_uuid=9876\",\"hideReferrer\": false,\"ttlMinutes\": 10080}}],\"type\": \"identity:exchange\"}]}\n"; - - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(responseBody, 200); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, HttpMethod.POST, responseConnection); - - // Set "fake" ECID - final String jsonStr = - "{\n" + - " \"identityMap\": {\n" + - " \"ECID\": [\n" + - " {\n" + - " \"id\":" + - "1234" + - ",\n" + - " \"authenticatedState\": \"ambiguous\",\n" + - " \"primary\": false\n" + - " }\n" + - " ]\n" + - " }\n" + - "}"; - - final JSONObject jsonObject = new JSONObject(jsonStr); - final Map identityMap = JSONUtils.toMap(jsonObject); - FakeIdentity.setXDMSharedState(identityMap, FakeIdentity.EVENT_TYPE); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("eventType", "personalizationEvent"); - put("test", "xdm"); - } - } - ) - .build(); - - final CountDownLatch latch = new CountDownLatch(1); - final List receivedHandles = new ArrayList<>(); - Edge.sendEvent( - experienceEvent, - handles -> { - receivedHandles.addAll(handles); - latch.countDown(); - } - ); - - List resultNetworkRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - HttpMethod.POST - ); - assertTrue(resultNetworkRequests.isEmpty()); - - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, HttpMethod.POST, 1); - - HashMap config = new HashMap() { - { - put("edge.configId", "123567"); - } - }; - MobileCore.updateConfiguration(config); - - mockNetworkService.assertAllNetworkRequestExpectations(); - assertTrue(latch.await(1, TimeUnit.SECONDS)); - - assertEquals(2, receivedHandles.size()); - - // verify handle 1 - assertEquals("personalization:decisions", receivedHandles.get(0).getType()); - assertEquals(1, receivedHandles.get(0).getPayload().size()); - - Map handle1 = TestUtils.flattenMap(receivedHandles.get(0).getPayload().get(0)); - - assertEquals(4, handle1.size()); - assertEquals("AT:eyJhY3Rpdml0eUlkIjoiMTE3NTg4IiwiZXhwZXJpZW5jZUlkIjoiMSJ9", handle1.get("id")); - assertEquals("#D41DBA", handle1.get("items[0].data.content.value")); - assertEquals("https://ns.adobe.com/personalization/json-content-item", handle1.get("items[0].schema")); - assertEquals("buttonColor", handle1.get("scope")); - - // verify handle 2 - assertEquals("identity:exchange", receivedHandles.get(1).getType()); - assertEquals(1, receivedHandles.get(1).getPayload().size()); - - Map handle2 = TestUtils.flattenMap(receivedHandles.get(1).getPayload().get(0)); - - assertEquals(5, handle2.size()); - assertEquals("411", handle2.get("id")); - assertEquals("url", handle2.get("type")); - assertEquals("//example.url?d_uuid=9876", handle2.get("spec.url")); - assertEquals("false", handle2.get("spec.hideReferrer")); - assertEquals("10080", handle2.get("spec.ttlMinutes")); - } - - /** - * Resets all test helper expectations and recorded data - */ - private void resetTestExpectations() { - mockNetworkService.reset(); - TestHelper.resetTestExpectations(); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/RestartFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/RestartFunctionalTests.java deleted file mode 100644 index 8bed7e75..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/RestartFunctionalTests.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.services.HttpMethod.POST; -import static com.adobe.marketing.mobile.util.TestHelper.assertExpectedEvents; -import static com.adobe.marketing.mobile.util.TestHelper.setExpectationEvent; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import com.adobe.marketing.mobile.edge.consent.Consent; -import com.adobe.marketing.mobile.edge.identity.Identity; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.ADBCountDownLatch; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; - -public class RestartFunctionalTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final String EXEDGE_INTERACT_URL_STRING = TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING; - private static final String CONFIG_ID = "1234abcd-abcd-1234-5678-123456abcdef"; - private static final int EVENTS_COUNT = 5; - private static final int TIMEOUT_MILLIS = 5000; - - @Rule - public RuleChain rule = RuleChain.outerRule(new TestHelper.LogOnErrorRule()).around(new TestHelper.SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - setupCore(false); - resetTestExpectations(); - } - - @Test - public void testAddEventsToPendingQueue_restartSDK_verifyEventsDispatched_whenConsentYes() throws Exception { - // initial pending - updateCollectConsent(ConsentStatus.PENDING); - getConsentsSync(); - fireManyEvents(); - - //verify - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - 2000 - ); - assertEquals(0, resultRequests.size()); - - // reset - resetCore(); - resetTestExpectations(); - setupCore(true); - - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, EVENTS_COUNT); - - // test - change to yes - updateCollectConsent(ConsentStatus.YES); - getConsentsSync(); - - // verify - resultRequests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST, TIMEOUT_MILLIS); - assertEquals(EVENTS_COUNT, resultRequests.size()); - } - - @Test - public void testAddEventsToPendingQueue_restartSDK_queueMoreEvents_verifyEventsDispatched_whenConsentYes() - throws Exception { - // initial pending - updateCollectConsent(ConsentStatus.PENDING); - getConsentsSync(); - fireManyEvents(); - - //verify - List resultRequests = mockNetworkService.getNetworkRequestsWith( - EXEDGE_INTERACT_URL_STRING, - POST, - 2000 - ); - assertEquals(0, resultRequests.size()); - - // reset - resetCore(); - resetTestExpectations(); - setupCore(true); - - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, EVENTS_COUNT * 2); - - // more pending events - fireManyEvents(); - - // test - change to yes - updateCollectConsent(ConsentStatus.YES); - getConsentsSync(); - - // verify - resultRequests = mockNetworkService.getNetworkRequestsWith(EXEDGE_INTERACT_URL_STRING, POST, 10000); - assertEquals(EVENTS_COUNT * 2, resultRequests.size()); - } - - @Test - public void testRetryResponseQueuesEvents_restartSDK_verifyEventsDispatched_whenNetworkResponseSuccess() - throws Exception { - // initial pending - updateCollectConsent(ConsentStatus.YES); - getConsentsSync(); - - // Setup network response error to trigger retry of queued events - String edgeResponse = - "\u0000{\"requestId\": \"test-req-id\",\"handle\": [],\"errors\": [],\"warnings\": [{\"type\": \"https://ns.adobe.com/aep/errors/EXEG-0204-502\",\"status\": 503,\"title\": \"A warning occurred.\",\"report\": {\"cause\": {\"message\": \"Unavailable\",\"code\": 503}}}]}"; - - // bad connection, hits will be retried - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse( - null, - edgeResponse, - 503, - null, - null - ); - mockNetworkService.setMockResponseFor(EXEDGE_INTERACT_URL_STRING, POST, responseConnection); - - // Expect the one initial request - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, 1); - - fireManyEvents(); - - // verify the one initial request sent - mockNetworkService.assertAllNetworkRequestExpectations(); - - // reset - resetCore(); - resetTestExpectations(); // Clears "bad connection" network response - - // Expect all queued hits to be sent - mockNetworkService.setExpectationForNetworkRequest(EXEDGE_INTERACT_URL_STRING, POST, EVENTS_COUNT); - setupCore(true); - - // verify all events sent - mockNetworkService.assertAllNetworkRequestExpectations(); - } - - public void resetCore() throws Exception { - TestHelper.resetCoreHelper(); - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - } - - /** - * Setup Mobile Core for testing. Registers Edge, Edge Identity, Consent, and Monitor extensions. - * - * @param restart true if setup for restart case of Core, false if clean start of Core - * @throws InterruptedException in case the latch wait fails - */ - public void setupCore(final boolean restart) throws InterruptedException { - if (restart) { - // On restart, expect Consent to load preferences from persistence and dispatch them to Hub - setExpectationEvent(EventType.CONSENT, EventSource.RESPONSE_CONTENT, 1); - } else { - setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - } - - setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - - // hub shared state update for extensions Edge, Config, Consent, Identity, Hub) - // Expect 4 shared state events on clean start. On restart, Consent also sets a shared state - setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, restart ? 5 : 4); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, Identity.EXTENSION, Consent.EXTENSION, MonitorExtension.EXTENSION), - o -> { - if (!restart) { - // Set configuration on clean start. On restart Configuration will load from persistence. - HashMap config = new HashMap() { - { - put("edge.configId", CONFIG_ID); - } - }; - MobileCore.updateConfiguration(config); - } - latch.countDown(); - } - ); - assertTrue(latch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); - - assertExpectedEvents(false); - // Note: Should not call resetTestExpectations so this helper can be used for testing scenarios - // where events are queued prior to the restart and unblocked soon after restart - } - - /** - * Resets all test helper expectations and recorded data - */ - private void resetTestExpectations() { - mockNetworkService.reset(); - TestHelper.resetTestExpectations(); - } - - private void fireManyEvents() { - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("test", "xdm"); - } - } - ) - .build(); - - for (int i = 0; i < EVENTS_COUNT; i++) { - Edge.sendEvent(experienceEvent, null); - } - } - - private void updateCollectConsent(final ConsentStatus status) { - if (status == null) { - return; - } - - updateCollectConsent(status.getValue()); - } - - private void updateCollectConsent(final String status) { - final Map consentStatus = new HashMap() { - { - put("val", status); - } - }; - - final Map consentCollect = new HashMap() { - { - put("collect", consentStatus); - } - }; - - final Map consents = new HashMap() { - { - put("consents", consentCollect); - } - }; - - Consent.update(consents); - } - - private void getConsentsSync() throws Exception { - final ADBCountDownLatch latch = new ADBCountDownLatch(1); - Consent.getConsents(stringObjectMap -> latch.countDown()); - - latch.await(2000, TimeUnit.MILLISECONDS); - } -} diff --git a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/SampleFunctionalTests.java b/code/edge/src/androidTest/java/com/adobe/marketing/mobile/SampleFunctionalTests.java deleted file mode 100644 index c739ba88..00000000 --- a/code/edge/src/androidTest/java/com/adobe/marketing/mobile/SampleFunctionalTests.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - 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; - -import static com.adobe.marketing.mobile.services.HttpMethod.POST; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.adobe.marketing.mobile.edge.identity.Identity; -import com.adobe.marketing.mobile.services.HttpConnecting; -import com.adobe.marketing.mobile.services.ServiceProvider; -import com.adobe.marketing.mobile.services.TestableNetworkRequest; -import com.adobe.marketing.mobile.util.MockNetworkService; -import com.adobe.marketing.mobile.util.MonitorExtension; -import com.adobe.marketing.mobile.util.TestConstants; -import com.adobe.marketing.mobile.util.TestHelper; -import com.adobe.marketing.mobile.util.TestUtils; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class SampleFunctionalTests { - - private static final MockNetworkService mockNetworkService = new MockNetworkService(); - private static final Event event1 = new Event.Builder("e1", "eventType", "eventSource").build(); - private static final Event event2 = new Event.Builder("e2", "eventType", "eventSource") - .setEventData( - new HashMap() { - { - put("test", "data"); - } - } - ) - .build(); - private static final Event event3 = new Event.Builder("e3", "unexpectedType", "unexpectedSource") - .setEventData( - new HashMap() { - { - put("test", "data"); - } - } - ) - .build(); - private static final String exEdgeInteractUrlString = TestConstants.Defaults.EXEDGE_INTERACT_URL_STRING; - private static final String responseBody = "{\"test\": \"json\"}"; - - @Rule - public RuleChain rule = RuleChain.outerRule(new TestHelper.LogOnErrorRule()).around(new TestHelper.SetupCoreRule()); - - @Before - public void setup() throws Exception { - ServiceProvider.getInstance().setNetworkService(mockNetworkService); - // expectations for update config request&response events - TestHelper.setExpectationEvent(EventType.CONFIGURATION, EventSource.REQUEST_CONTENT, 1); - TestHelper.setExpectationEvent(EventType.CONFIGURATION, EventSource.RESPONSE_CONTENT, 1); - // hub shared state update Edge, EventHub, Configuration, and Identity - TestHelper.setExpectationEvent(EventType.HUB, EventSource.SHARED_STATE, 4); - - HashMap config = new HashMap() { - { - put("edge.configId", "12345-example"); - } - }; - MobileCore.updateConfiguration(config); - - final CountDownLatch latch = new CountDownLatch(1); - MobileCore.registerExtensions( - Arrays.asList(Edge.EXTENSION, Identity.EXTENSION, MonitorExtension.EXTENSION), - o -> latch.countDown() - ); - latch.await(); - - // Wait for and verify all expected events are received - TestHelper.assertExpectedEvents(false); - mockNetworkService.reset(); - TestHelper.resetTestExpectations(); - } - - @After - public void tearDown() { - mockNetworkService.reset(); - } - - @Test - public void testSample_AssertUnexpectedEvents() throws InterruptedException { - // set event expectations specifying the event type, source and the count (count should be > 0) - TestHelper.setExpectationEvent("eventType", "eventSource", 2); - MobileCore.dispatchEvent(event1); - MobileCore.dispatchEvent(event1); - - // assert that no unexpected event was received - TestHelper.assertUnexpectedEvents(); - } - - @Test - public void testSample_AssertExpectedEvents() throws InterruptedException { - TestHelper.setExpectationEvent("eventType", "eventSource", 2); - Event unexpectedEvent = new Event.Builder("e3", "unexpectedType", "unexpectedSource") - .setEventData( - new HashMap() { - { - put("test", "data"); - } - } - ) - .build(); - MobileCore.dispatchEvent(event1); - MobileCore.dispatchEvent(unexpectedEvent); - MobileCore.dispatchEvent(event1); - - // assert all expected events were received and ignore any unexpected events - // when ignoreUnexpectedEvents is set on false, an extra assertUnexpectedEvents step is performed - TestHelper.assertExpectedEvents(true); - } - - @Test - public void testSample_DispatchedEvents() throws InterruptedException { - MobileCore.dispatchEvent(event1); // eventType and eventSource - MobileCore.dispatchEvent(event2); // eventType and eventSource - MobileCore.dispatchEvent(event3); // unexpectedType and unexpectedSource - - // assert on count and data for events of a certain type, source - List dispatchedEvents = TestHelper.getDispatchedEventsWith("eventType", "eventSource"); - - assertEquals(2, dispatchedEvents.size()); - - Map eventData = dispatchedEvents.get(1).getEventData(); - assertNotNull(eventData); - - assertEquals(1, TestUtils.flattenMap(eventData).size()); - } - - @Test - public void testSample_AssertNetworkRequestsCount() throws InterruptedException { - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(responseBody, 200); - mockNetworkService.setMockResponseFor(exEdgeInteractUrlString, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(exEdgeInteractUrlString, POST, 2); - - ExperienceEvent experienceEvent1 = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("test1", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent1, null); - - ExperienceEvent experienceEvent2 = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("test2", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent2, null); - - mockNetworkService.assertAllNetworkRequestExpectations(); - } - - @Test - public void testSample_AssertNetworkRequestAndResponseEvent() throws InterruptedException { - TestHelper.setExpectationEvent(EventType.EDGE, EventSource.REQUEST_CONTENT, 1); - TestHelper.setExpectationEvent(EventType.EDGE, "identity:exchange", 1); - - final String responseBody = - "\u0000{\"requestId\":\"ded17427-c993-4182-8d94-2a169c1a23e2\",\"handle\":[{\"type\":\"identity:exchange\",\"payload\":[{\"type\":\"url\",\"id\":411,\"spec\":{\"url\":\"//cm.everesttech.net/cm/dd?d_uuid=42985602780892980519057012517360930936\",\"hideReferrer\":false,\"ttlMinutes\":10080}}]}]}\n"; - HttpConnecting responseConnection = mockNetworkService.createMockNetworkResponse(responseBody, 200); - mockNetworkService.setMockResponseFor(exEdgeInteractUrlString, POST, responseConnection); - mockNetworkService.setExpectationForNetworkRequest(exEdgeInteractUrlString, POST, 1); - - ExperienceEvent experienceEvent = new ExperienceEvent.Builder() - .setXdmSchema( - new HashMap() { - { - put("eventType", "testType"); - put("test", "xdm"); - } - } - ) - .build(); - Edge.sendEvent(experienceEvent, null); - - List requests = mockNetworkService.getNetworkRequestsWith( - exEdgeInteractUrlString, - POST - ); - assertEquals(1, requests.size()); - - Map flattendRequestBody = mockNetworkService.getFlattenedNetworkRequestBody(requests.get(0)); - assertEquals("testType", flattendRequestBody.get("events[0].xdm.eventType")); - - TestHelper.assertExpectedEvents(true); - } -}