-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from prudrabhat/debug_event_support
Add support for debug events
- Loading branch information
Showing
4 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
code/core/src/main/java/com/adobe/marketing/mobile/util/EventUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
205 changes: 205 additions & 0 deletions
205
code/core/src/test/java/com/adobe/marketing/mobile/util/EventUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |