Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUM-1517 create Stub Core module #1740

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reliability/single-fit/logs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
}
}
testImplementation(testFixtures(project(":dd-sdk-android-core")))
testImplementation(project(":reliability:stub-core"))
testImplementation(libs.bundles.jUnit5)
testImplementation(libs.bundles.testTools)
testImplementation(libs.okHttp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
package com.datadog.android.logs.integration

import android.util.Log
import com.datadog.android.api.StubEvent
import com.datadog.android.api.StubSDKCore
import com.datadog.android.api.context.NetworkInfo
import com.datadog.android.api.feature.Feature
import com.datadog.android.core.stub.StubSDKCore
import com.datadog.android.log.Logger
import com.datadog.android.log.Logs
import com.datadog.android.log.LogsConfiguration
Expand All @@ -36,7 +35,6 @@ import org.junit.jupiter.api.extension.Extensions
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
import java.lang.Exception

@Extensions(
ExtendWith(MockitoExtension::class),
Expand Down Expand Up @@ -69,7 +67,7 @@ class LoggerTest {
logger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -88,7 +86,7 @@ class LoggerTest {
logger.v(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -107,7 +105,7 @@ class LoggerTest {
logger.d(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -126,7 +124,7 @@ class LoggerTest {
logger.i(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -145,7 +143,7 @@ class LoggerTest {
logger.w(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -164,7 +162,7 @@ class LoggerTest {
logger.e(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -183,7 +181,7 @@ class LoggerTest {
logger.wtf(fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -204,7 +202,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(fakeServiceName)
Expand All @@ -225,7 +223,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -250,7 +248,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -276,7 +274,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -299,7 +297,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -325,7 +323,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -350,7 +348,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -372,7 +370,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).isEmpty()
}

Expand All @@ -394,7 +392,7 @@ class LoggerTest {

// Then
val expectedCount = (repeatCount * fakeSampleRate / 100f).toInt()
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten.size).isCloseTo(expectedCount, offset(offsetMargin))
}

Expand All @@ -420,7 +418,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand Down Expand Up @@ -452,7 +450,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -476,7 +474,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage, null, mapOf(fakeKey to fakeValue))

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -498,7 +496,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage, fakeException)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand All @@ -525,7 +523,7 @@ class LoggerTest {
testedLogger.log(fakeLevel, fakeMessage, fakeErrorKind, fakeErrorMessage, fakeErrorStack)

// Then
val eventsWritten: List<StubEvent> = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
val eventsWritten = stubSdkCore.eventsWritten(Feature.LOGS_FEATURE_NAME)
assertThat(eventsWritten).hasSize(1)
val event0 = JsonParser.parseString(eventsWritten[0].eventData) as JsonObject
assertThat(event0.getString("service")).isEqualTo(stubSdkCore.getDatadogContext().service)
Expand Down
51 changes: 51 additions & 0 deletions reliability/stub-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

import com.datadog.gradle.config.androidLibraryConfig
import com.datadog.gradle.config.dependencyUpdateConfig
import com.datadog.gradle.config.kotlinConfig
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
// Build
id("com.android.library")
kotlin("android")
id("com.google.devtools.ksp")

// Analysis tools
id("com.github.ben-manes.versions")

// Tests
id("de.mobilej.unmock")
id("org.jetbrains.kotlinx.kover")
}

android {
namespace = "com.datadog.android.core.stub"
}

dependencies {
implementation(project(":dd-sdk-android-core"))
implementation(libs.kotlin)

// Testing
implementation(project(":tools:unit")) {
attributes {
attribute(
com.android.build.api.attributes.ProductFlavorAttr.of("platform"),
objects.named("jvm")
)
}
}
implementation(libs.bundles.jUnit5)
implementation(libs.bundles.testTools)
implementation(libs.okHttp)
implementation(libs.gson)
}

androidLibraryConfig()
kotlinConfig(jvmBytecodeTarget = JvmTarget.JVM_11)
dependencyUpdateConfig()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.api
package com.datadog.android.core.stub

/**
* Holds the data and metadata of an event written via the [StubSDKCore].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.api
package com.datadog.android.core.stub

import com.datadog.android.api.context.DatadogContext
import com.datadog.android.api.feature.Feature
Expand All @@ -14,6 +14,7 @@ import com.datadog.android.api.storage.RawBatchEvent
import org.mockito.Mockito.mock
import org.mockito.Mockito.mockingDetails

@Suppress("CheckInternal", "UnsafeThirdPartyFunctionCall")
internal class StubFeatureScope(
private val feature: Feature,
private val datadogContextProvider: () -> DatadogContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.api
package com.datadog.android.core.stub

import com.datadog.android.core.internal.net.FirstPartyHostHeaderTypeResolver
import com.datadog.android.trace.TracingHeaderType
import okhttp3.HttpUrl

/**
* Stubbed implementation of [FirstPartyHostHeaderTypeResolver], which is an
* allow all, trace all implementation (using the W3C TraceContext headers).
*/
class StubFirstPartyHostHeaderTypeResolver :
FirstPartyHostHeaderTypeResolver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.api
package com.datadog.android.core.stub

import com.datadog.android.api.InternalLogger

@Suppress("UnsafeThirdPartyFunctionCall")
internal class StubInternalLogger : InternalLogger {
override fun log(
level: InternalLogger.Level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.android.api
package com.datadog.android.core.stub

import android.content.Context
import com.datadog.android.api.InternalLogger
import com.datadog.android.api.context.DatadogContext
import com.datadog.android.api.context.NetworkInfo
import com.datadog.android.api.context.TimeInfo
Expand All @@ -26,9 +27,10 @@ import org.mockito.kotlin.whenever
* It adds several functions to get info about internal state and usage:
* [eventsWritten], …
*/
@Suppress("UnsafeThirdPartyFunctionCall")
class StubSDKCore(
private val forge: Forge,
val mockContext: Context = mock(),
private val mockContext: Context = mock(),
private val mockSdkCore: InternalSdkCore = mock()
) : InternalSdkCore by mockSdkCore {

Expand All @@ -51,10 +53,18 @@ class StubSDKCore(
return featureScopes[featureName]?.eventsWritten() ?: emptyList()
}

/**
* Stubs the network info visible via the SDK Core.
* @param networkInfo the network info
*/
fun stubNetworkInfo(networkInfo: NetworkInfo) {
datadogContext = datadogContext.copy(networkInfo = networkInfo)
}

/**
* Stubs the user info visible via the SDK Core.
* @param userInfo the user info
*/
fun stubUserInfo(userInfo: UserInfo) {
networkInfo
datadogContext = datadogContext.copy(userInfo = userInfo)
Expand Down Expand Up @@ -125,7 +135,7 @@ class StubSDKCore(
email: String?,
extraInfo: Map<String, Any?>
) {
datadogContext = datadogContext.copy(userInfo = UserInfo(id, name, email, extraInfo))
stubUserInfo(UserInfo(id, name, email, extraInfo))
}

// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.gson.JsonObject
* }
* }
*/
@Suppress("UnsafeThirdPartyFunctionCall")
fun JsonObject.getString(path: String): String? {
return if (has(path)) {
getAsJsonPrimitive(path)?.asString
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ include(":integrations:dd-sdk-android-okhttp")
include(":integrations:dd-sdk-android-rum-coroutines")
include(":integrations:dd-sdk-android-trace-coroutines")

// TESTING UTILS
include(":reliability:stub-core")

// SINGLE FEATURE INTEGRATION TESTS
include(":reliability:single-fit:logs")
include(":reliability:single-fit:rum")
Expand Down
Loading