-
Notifications
You must be signed in to change notification settings - Fork 14
fix: add nullabulity check #191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
189 changes: 189 additions & 0 deletions
189
android/src/test/java/com/usercentrics/reactnative/extensions/NullabilityTest.kt
This file contains hidden or 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,189 @@ | ||
| package com.usercentrics.reactnative.extensions | ||
|
|
||
| import com.facebook.react.bridge.JavaOnlyArray | ||
| import com.facebook.react.bridge.JavaOnlyMap | ||
| import org.junit.Test | ||
| import org.junit.Assert.* | ||
|
|
||
| /** | ||
| * Tests for nullability handling in React Native 0.77+ | ||
| * These tests ensure that null values from ReadableArray/ReadableMap are handled safely | ||
| */ | ||
| class NullabilityTest { | ||
|
|
||
| @Test | ||
| fun `deserializeUserDecision handles null map entries`() { | ||
| // Given: array with null entries | ||
| val array = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("serviceId", "service1", "consent", true)) | ||
| pushNull() // Simulate null entry | ||
| pushMap(JavaOnlyMap.of("serviceId", "service2", "consent", false)) | ||
| } | ||
|
|
||
| // When: deserializing | ||
| val result = array.deserializeUserDecision() | ||
|
|
||
| // Then: null entries are skipped, valid entries are processed | ||
| assertEquals(2, result.size) | ||
| assertEquals("service1", result[0].serviceId) | ||
| assertEquals(true, result[0].consent) | ||
| assertEquals("service2", result[1].serviceId) | ||
| assertEquals(false, result[1].consent) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializePurposeLIDecisionsMap handles null map entries`() { | ||
| // Given: array with null entries | ||
| val array = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("id", 1, "legitimateInterestConsent", true)) | ||
| pushNull() // Simulate null entry | ||
| pushMap(JavaOnlyMap.of("id", 2, "legitimateInterestConsent", false)) | ||
| } | ||
|
|
||
| // When: deserializing | ||
| val result = array.deserializePurposeLIDecisionsMap() | ||
|
|
||
| // Then: null entries are skipped | ||
| assertNotNull(result) | ||
| assertEquals(2, result?.size) | ||
| assertEquals(true, result?.get(1)) | ||
| assertEquals(false, result?.get(2)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializePurposeLIDecisionsMap returns null for empty array`() { | ||
| // Given: empty array | ||
| val array = JavaOnlyArray() | ||
|
|
||
| // When: deserializing | ||
| val result = array.deserializePurposeLIDecisionsMap() | ||
|
|
||
| // Then: returns null | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeTCFUserDecisions handles null purpose entries`() { | ||
| // Given: TCF decisions with null entries in purposes array | ||
| val purposesArray = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("id", 1, "consent", true)) | ||
| pushNull() // Simulate null entry | ||
| pushMap(JavaOnlyMap.of("id", 2, "consent", false)) | ||
| } | ||
|
|
||
| val tcfMap = JavaOnlyMap.of("purposes", purposesArray) | ||
|
|
||
| // When: deserializing | ||
| val result = tcfMap.deserializeTCFUserDecisions() | ||
|
|
||
| // Then: null entries are skipped | ||
| assertNotNull(result.purposes) | ||
| assertEquals(2, result.purposes?.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeTCFUserDecisions handles null special features entries`() { | ||
| // Given: TCF decisions with null entries in specialFeatures array | ||
| val specialFeaturesArray = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("id", 1, "consent", true)) | ||
| pushNull() | ||
| pushMap(JavaOnlyMap.of("id", 2, "consent", false)) | ||
| } | ||
|
|
||
| val tcfMap = JavaOnlyMap.of("specialFeatures", specialFeaturesArray) | ||
|
|
||
| // When: deserializing | ||
| val result = tcfMap.deserializeTCFUserDecisions() | ||
|
|
||
| // Then: null entries are skipped | ||
| assertNotNull(result.specialFeatures) | ||
| assertEquals(2, result.specialFeatures?.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeTCFUserDecisions handles null vendors entries`() { | ||
| // Given: TCF decisions with null entries in vendors array | ||
| val vendorsArray = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("id", 1, "consent", true)) | ||
| pushNull() | ||
| pushMap(JavaOnlyMap.of("id", 2, "consent", false)) | ||
| } | ||
|
|
||
| val tcfMap = JavaOnlyMap.of("vendors", vendorsArray) | ||
|
|
||
| // When: deserializing | ||
| val result = tcfMap.deserializeTCFUserDecisions() | ||
|
|
||
| // Then: null entries are skipped | ||
| assertNotNull(result.vendors) | ||
| assertEquals(2, result.vendors?.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeTCFUserDecisions handles null adTechProviders entries`() { | ||
| // Given: TCF decisions with null entries in adTechProviders array | ||
| val adTechArray = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("id", 1, "consent", true)) | ||
| pushNull() | ||
| pushMap(JavaOnlyMap.of("id", 2, "consent", false)) | ||
| } | ||
|
|
||
| val tcfMap = JavaOnlyMap.of("adTechProviders", adTechArray) | ||
|
|
||
| // When: deserializing | ||
| val result = tcfMap.deserializeTCFUserDecisions() | ||
|
|
||
| // Then: null entries are skipped | ||
| assertNotNull(result.adTechProviders) | ||
| assertEquals(2, result.adTechProviders.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeTCFUserDecisions handles missing arrays gracefully`() { | ||
| // Given: empty TCF map | ||
| val tcfMap = JavaOnlyMap() | ||
|
|
||
| // When: deserializing | ||
| val result = tcfMap.deserializeTCFUserDecisions() | ||
|
|
||
| // Then: all fields are null or empty | ||
| assertNull(result.purposes) | ||
| assertNull(result.specialFeatures) | ||
| assertNull(result.vendors) | ||
| assertEquals(0, result.adTechProviders.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeUserDecision handles array with all null entries`() { | ||
| // Given: array with only null entries | ||
| val array = JavaOnlyArray().apply { | ||
| pushNull() | ||
| pushNull() | ||
| pushNull() | ||
| } | ||
|
|
||
| // When: deserializing | ||
| val result = array.deserializeUserDecision() | ||
|
|
||
| // Then: returns empty list | ||
| assertEquals(0, result.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserializeUserDecision skips entries with null serviceId`() { | ||
| // Given: array with entry missing serviceId | ||
| val array = JavaOnlyArray().apply { | ||
| pushMap(JavaOnlyMap.of("serviceId", "service1", "consent", true)) | ||
| pushMap(JavaOnlyMap.of("consent", false)) // Missing serviceId | ||
| pushMap(JavaOnlyMap.of("serviceId", "service2", "consent", true)) | ||
| } | ||
|
|
||
| // When: deserializing | ||
| val result = array.deserializeUserDecision() | ||
|
|
||
| // Then: entry without serviceId is skipped | ||
| assertEquals(2, result.size) | ||
| assertEquals("service1", result[0].serviceId) | ||
| assertEquals("service2", result[1].serviceId) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.