Skip to content

Commit

Permalink
Merge pull request #683 from prudrabhat/debug_event_support
Browse files Browse the repository at this point in the history
Add support for debug events
  • Loading branch information
prudrabhat authored Jun 19, 2024
2 parents 90638b4 + d135c3d commit ee20486
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
6 changes: 6 additions & 0 deletions code/core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public final class com/adobe/marketing/mobile/EventSource {
public static final field CONSENT_PREFERENCE Ljava/lang/String;
public static final field CONTENT_COMPLETE Ljava/lang/String;
public static final field CREATE_TRACKER Ljava/lang/String;
public static final field DEBUG Ljava/lang/String;
public static final field ERROR_RESPONSE_CONTENT Ljava/lang/String;
public static final field NONE Ljava/lang/String;
public static final field OS Ljava/lang/String;
Expand Down Expand Up @@ -1088,6 +1089,11 @@ public class com/adobe/marketing/mobile/util/EventDataUtils {
public static fun immutableClone (Ljava/util/Map;)Ljava/util/Map;
}

public final class com/adobe/marketing/mobile/util/EventUtils {
public static final fun getDebugEventSource (Lcom/adobe/marketing/mobile/Event;)Ljava/lang/String;
public static final fun getDebugEventType (Lcom/adobe/marketing/mobile/Event;)Ljava/lang/String;
}

public final class com/adobe/marketing/mobile/util/JSONUtils {
public static fun isNullOrEmpty (Lorg/json/JSONArray;)Z
public static fun isNullOrEmpty (Lorg/json/JSONObject;)Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ private EventSource() {}
public static final String CREATE_TRACKER = "com.adobe.eventSource.createTracker";
public static final String TRACK_MEDIA = "com.adobe.eventSource.trackMedia";
public static final String CONTENT_COMPLETE = "com.adobe.eventSource.contentComplete";
public static final String DEBUG = "com.adobe.eventSource.debug";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

@file:JvmName("EventUtils") // Allows Java callers to use EventUtils.<> instead of EventUtilsKt.<>

package com.adobe.marketing.mobile.util

import com.adobe.marketing.mobile.Event
import com.adobe.marketing.mobile.EventSource
import com.adobe.marketing.mobile.EventType

private const val KEY_EVENT_DATA_DEBUG = "debug"
private const val KEY_DEBUG_EVENT_TYPE = "eventType"
private const val KEY_DEBUG_EVENT_SOURCE = "eventSource"

/**
* Returns the debug event type (identified by debug.eventType) from the event data if present, otherwise null.
* @return the debug event type if present, otherwise null
*/
fun Event.getDebugEventType(): String? {
val debugData = getDebugEventData() ?: return null
val debugEventType = debugData[KEY_DEBUG_EVENT_TYPE]
if (debugEventType !is String) return null

return debugEventType
}

/**
* Returns the debug event source (identified by debug.eventSource) from the event data if present, otherwise null.
* @return the debug event source if present, otherwise null
*/
fun Event.getDebugEventSource(): String? {
val debugData = getDebugEventData() ?: return null
val debugEventSource = debugData[KEY_DEBUG_EVENT_SOURCE]
if (debugEventSource !is String) return null

return debugEventSource
}

/**
* Returns the debug event data (identified by data.debug) from the event if present, otherwise null.
* @return the content of "debug" key within "Event.data" if present,
* null if the event is not a debug event or if the debug data does not exist
*/
private fun Event.getDebugEventData(): Map<String, Any?>? {
if (type != EventType.SYSTEM || source != EventSource.DEBUG) return null

if (eventData == null) return null

return DataReader.optTypedMap(Any::class.java, eventData, KEY_EVENT_DATA_DEBUG, null) ?: null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

package com.adobe.marketing.mobile.util

import com.adobe.marketing.mobile.Event
import com.adobe.marketing.mobile.EventSource
import com.adobe.marketing.mobile.EventType
import org.junit.Assert.assertNull
import org.junit.Test
import kotlin.test.assertEquals

class EventUtilsTest {
companion object {
private const val TEST_EVENT_NAME = "testName"
private const val TEST_EVENT_TYPE = EventType.SYSTEM
private const val TEST_EVENT_SOURCE = EventSource.DEBUG
}

@Test
fun `Test getDebugEventType returns null on non debug event`() {
val event = Event.Builder(TEST_EVENT_NAME, EventType.HUB, EventSource.REQUEST_CONTENT).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null on no eventData`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null on no debug data`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(mapOf("key" to "value")).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null when debug is not a map`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(mapOf("debug" to "value")).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null on invalid debug key`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"_debug" to mapOf(
"eventType" to EventType.RULES_ENGINE,
"eventSource" to EventSource.RESET_COMPLETE
)
)
).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null when key is absent`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventSource" to EventSource.RESET_COMPLETE
)
)
).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns debug event type`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventType" to EventType.RULES_ENGINE,
"eventSource" to EventSource.RESET_COMPLETE
)
)
).build()

assertEquals(EventType.RULES_ENGINE, event.getDebugEventType())
}

@Test
fun `Test getDebugEventType returns null when debugEvent type is not a string`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventType" to mapOf("1" to "Hi"),
"eventSource" to "testEventSource"
)
)
).build()

assertNull(event.getDebugEventType())
}

@Test
fun `Test getDebugEventSource returns null on non debug event`() {
val event = Event.Builder(TEST_EVENT_NAME, EventType.HUB, EventSource.REQUEST_CONTENT).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null on no eventData`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null when debug is not a map`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(mapOf("debug" to "value")).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null on no debug data`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(mapOf("key" to "value")).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null on invalid debug key`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"_debug" to mapOf(
"eventType" to EventType.RULES_ENGINE,
"eventSource" to EventSource.RESET_COMPLETE
)
)
).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns debug event source`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventType" to EventType.RULES_ENGINE,
"eventSource" to EventSource.RESET_COMPLETE
)
)
).build()

assertEquals(EventSource.RESET_COMPLETE, event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null when debugEvent source is not a string`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventType" to "testEventType",
"eventSource" to mapOf("1" to "Hi")
)
)
).build()

assertNull(event.getDebugEventSource())
}

@Test
fun `Test getDebugEventSource returns null when key is absent`() {
val event = Event.Builder(TEST_EVENT_NAME, TEST_EVENT_TYPE, TEST_EVENT_SOURCE)
.setEventData(
mapOf(
"debug" to mapOf(
"eventType" to EventType.RULES_ENGINE
)
)
).build()

assertNull(event.getDebugEventSource())
}
}

0 comments on commit ee20486

Please sign in to comment.