-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-5525 Add the integration tests for the SdkCore APIs
- Loading branch information
Showing
13 changed files
with
534 additions
and
18 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
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,7 @@ | ||
# Needed to make sure we don't remove any test code | ||
-dontshrink | ||
#-dontoptimize | ||
#-keepattributes *Annotation* | ||
|
||
-dontwarn kotlin.Experimental$Level | ||
-dontwarn kotlin.Experimental |
157 changes: 157 additions & 0 deletions
157
...bility/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/SdkCoreTest.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,157 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.datadog.android.core.integration | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.datadog.android.Datadog | ||
import com.datadog.android.api.context.UserInfo | ||
import com.datadog.android.api.feature.FeatureSdkCore | ||
import com.datadog.android.api.feature.StorageBackedFeature | ||
import com.datadog.android.api.feature.stub.StubStorageBackedFeature | ||
import com.datadog.android.core.configuration.Configuration | ||
import com.datadog.android.core.integration.tests.MockServerTest | ||
import com.datadog.android.core.integration.tests.forge.factories.ConfigurationCoreForgeryFactory | ||
import com.datadog.android.privacy.TrackingConsent | ||
import com.datadog.tools.unit.ConditionWatcher | ||
import com.datadog.tools.unit.forge.exhaustiveAttributes | ||
import com.datadog.tools.unit.forge.useToolsFactories | ||
import fr.xgouchet.elmyr.junit4.ForgeRule | ||
import fr.xgouchet.elmyr.jvm.useJvmFactories | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SdkCoreTest : MockServerTest() { | ||
|
||
@get:Rule | ||
var forge = ForgeRule() | ||
|
||
private var fakeUserId: String? = null | ||
private var fakeUserName: String? = null | ||
private var fakeUserEmail: String? = null | ||
private var fakeUserAdditionalProperties: Map<String, Any?> = emptyMap() | ||
private lateinit var stubFeature: StorageBackedFeature | ||
private lateinit var fakeFeatureName: String | ||
|
||
@Before | ||
fun setUp() { | ||
forge = forge.useJvmFactories().useToolsFactories().withFactory(ConfigurationCoreForgeryFactory()) | ||
fakeFeatureName = forge.anAlphabeticalString() | ||
stubFeature = StubStorageBackedFeature( | ||
forge, | ||
fakeFeatureName, | ||
getMockServerWrapper().getServerUrl() | ||
) | ||
fakeUserId = forge.aNullable { anHexadecimalString() } | ||
fakeUserName = forge.aNullable { forge.aStringMatching("[A-Z][a-z]+ [A-Z]\\. [A-Z][a-z]+") } | ||
fakeUserEmail = forge.aNullable { forge.aStringMatching("[a-z]+\\.[a-z]+@[a-z]+\\.[a-z]{3}") } | ||
fakeUserAdditionalProperties = forge.exhaustiveAttributes(excludedKeys = setOf("id", "name", "email")) | ||
} | ||
|
||
// region set UserInfo | ||
|
||
@Test | ||
fun must_addUserInformationIntoEvents_when_setUserInformation() { | ||
// Given | ||
val configuration: Configuration = forge.getForgery() | ||
val sdkCore = Datadog.initialize( | ||
ApplicationProvider.getApplicationContext(), | ||
configuration, | ||
forge.aValueFrom(TrackingConsent::class.java) | ||
) | ||
val featureSdkCore = sdkCore as? FeatureSdkCore | ||
featureSdkCore?.registerFeature(stubFeature) | ||
|
||
// When | ||
sdkCore?.setUserInfo(fakeUserId, fakeUserName, fakeUserEmail, fakeUserAdditionalProperties) | ||
|
||
// Then | ||
ConditionWatcher { | ||
var readUserInfo: UserInfo? = null | ||
featureSdkCore?.getFeature(stubFeature.name)?.withWriteContext { datadogContext, _ -> | ||
readUserInfo = datadogContext.userInfo | ||
} | ||
assertThat(readUserInfo?.id).isEqualTo(fakeUserId) | ||
assertThat(readUserInfo?.name).isEqualTo(fakeUserName) | ||
assertThat(readUserInfo?.email).isEqualTo(fakeUserEmail) | ||
assertThat(readUserInfo?.additionalProperties) | ||
.containsExactlyInAnyOrderEntriesOf(fakeUserAdditionalProperties) | ||
true | ||
}.doWait(timeoutMs = FINAL_WAIT_MS) | ||
} | ||
|
||
// endregion | ||
|
||
// region add User Properties | ||
|
||
@Test | ||
fun must_addUserExtraProperties_when_addUserProperties() { | ||
// Given | ||
val configuration: Configuration = forge.getForgery() | ||
val sdkCore = Datadog.initialize( | ||
ApplicationProvider.getApplicationContext(), | ||
configuration, | ||
forge.aValueFrom(TrackingConsent::class.java) | ||
) | ||
val featureSdkCore = sdkCore as? FeatureSdkCore | ||
featureSdkCore?.registerFeature(stubFeature) | ||
sdkCore?.setUserInfo(fakeUserId, fakeUserName, fakeUserEmail, fakeUserAdditionalProperties) | ||
val expectedUserExtraProperties = forge.exhaustiveAttributes() | ||
|
||
// When | ||
sdkCore?.addUserProperties(expectedUserExtraProperties) | ||
|
||
// Then | ||
ConditionWatcher { | ||
var readUserInfo: UserInfo? = null | ||
featureSdkCore?.getFeature(stubFeature.name)?.withWriteContext { datadogContext, _ -> | ||
readUserInfo = datadogContext.userInfo | ||
} | ||
assertThat(readUserInfo?.id).isEqualTo(fakeUserId) | ||
assertThat(readUserInfo?.name).isEqualTo(fakeUserName) | ||
assertThat(readUserInfo?.email).isEqualTo(fakeUserEmail) | ||
assertThat(readUserInfo?.additionalProperties) | ||
.containsExactlyInAnyOrderEntriesOf(fakeUserAdditionalProperties + expectedUserExtraProperties) | ||
true | ||
}.doWait(timeoutMs = FINAL_WAIT_MS) | ||
} | ||
|
||
// endregion | ||
|
||
// region set Tracking Consent | ||
|
||
@Test | ||
fun must_updateTrackingConsent_when_setTrackingConsent() { | ||
// Given | ||
val configuration: Configuration = forge.getForgery() | ||
val expectedTrackingConsent = forge.aValueFrom(TrackingConsent::class.java) | ||
val sdkCore = Datadog.initialize( | ||
ApplicationProvider.getApplicationContext(), | ||
configuration, | ||
forge.aValueFrom(TrackingConsent::class.java) | ||
) | ||
val featureSdkCore = sdkCore as? FeatureSdkCore | ||
featureSdkCore?.registerFeature(stubFeature) | ||
|
||
// When | ||
sdkCore?.setTrackingConsent(expectedTrackingConsent) | ||
|
||
// Then | ||
ConditionWatcher { | ||
featureSdkCore?.getFeature(stubFeature.name)?.withWriteContext { datadogContext, _ -> | ||
assertThat(datadogContext.trackingConsent).isEqualTo(expectedTrackingConsent) | ||
} | ||
true | ||
}.doWait(timeoutMs = FINAL_WAIT_MS) | ||
} | ||
|
||
// endregion | ||
} |
17 changes: 17 additions & 0 deletions
17
...ity/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/BaseTest.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,17 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.datadog.android.core.integration.tests | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
@Suppress("UtilityClassWithPublicConstructor") | ||
abstract class BaseTest { | ||
|
||
companion object { | ||
internal val FINAL_WAIT_MS = TimeUnit.SECONDS.toMillis(30) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...re-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/MockServerTest.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,40 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.datadog.android.core.integration.tests | ||
|
||
import com.datadog.android.core.integration.tests.utils.MockWebServerWrapper | ||
import org.junit.AfterClass | ||
import org.junit.BeforeClass | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
abstract class MockServerTest : BaseTest() { | ||
|
||
fun getMockServerWrapper(): MockWebServerWrapper { | ||
return mockServerWrapper | ||
} | ||
|
||
companion object { | ||
private val mockServerStarted = AtomicBoolean(false) | ||
protected val mockServerWrapper: MockWebServerWrapper = MockWebServerWrapper() | ||
|
||
@BeforeClass | ||
@JvmStatic | ||
fun setupTestSuite() { | ||
if (mockServerStarted.compareAndSet(false, true)) { | ||
mockServerWrapper.start() | ||
} | ||
} | ||
|
||
@AfterClass | ||
@JvmStatic | ||
fun tearDown() { | ||
if (mockServerStarted.compareAndSet(true, false)) { | ||
mockServerWrapper.shutdown() | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...datadog/android/core/integration/tests/forge/factories/ConfigurationCoreForgeryFactory.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,47 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.datadog.android.core.integration.tests.forge.factories | ||
|
||
import com.datadog.android.DatadogSite | ||
import com.datadog.android._InternalProxy | ||
import com.datadog.android.core.configuration.Configuration | ||
import com.datadog.android.trace.TracingHeaderType | ||
import fr.xgouchet.elmyr.Forge | ||
import fr.xgouchet.elmyr.ForgeryFactory | ||
import java.util.UUID | ||
|
||
internal class ConfigurationCoreForgeryFactory : | ||
ForgeryFactory<Configuration> { | ||
override fun getForgery(forge: Forge): Configuration { | ||
return Configuration.Builder( | ||
UUID.randomUUID().toString(), | ||
forge.anHexadecimalString(), | ||
forge.anHexadecimalString(), | ||
forge.aNullable { | ||
anAlphaNumericalString() | ||
} | ||
) | ||
.setUseDeveloperModeWhenDebuggable(forge.aBool()) | ||
.setFirstPartyHostsWithHeaderType( | ||
forge.aMap { | ||
val fakeUrl = forge.aStringMatching("https://[a-z0-9]+\\.com") | ||
fakeUrl to aList { | ||
aValueFrom( | ||
TracingHeaderType::class.java | ||
) | ||
}.toSet() | ||
} | ||
) | ||
.apply { | ||
_InternalProxy.allowClearTextHttp(this) | ||
} | ||
.setBatchSize(forge.getForgery()) | ||
.setUploadFrequency(forge.getForgery()) | ||
.useSite(forge.aValueFrom(DatadogSite::class.java)) | ||
.build() | ||
} | ||
} |
Oops, something went wrong.