Skip to content

Commit

Permalink
[tests] add test for new OperationModelStore check
Browse files Browse the repository at this point in the history
* Add new test file `OperationModelStoreTests`
* Add test called "does not load invalid cached operations" that uncaches valid and invalid operations based on the new checks in the OperationModelStore uncaching logic.
  • Loading branch information
nan-li committed Jul 31, 2024
1 parent 9306f90 commit f9e998b
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.onesignal.core.internal.operations

import com.onesignal.core.internal.operations.impl.OperationModelStore
import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
import com.onesignal.core.internal.preferences.PreferenceStores
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.mocks.MockPreferencesService
import com.onesignal.user.internal.operations.LoginUserOperation
import com.onesignal.user.internal.operations.SetPropertyOperation
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import org.json.JSONArray
import java.util.UUID

class OperationModelStoreTests : FunSpec({

beforeAny {
Logging.logLevel = LogLevel.NONE
}

test("does not load invalid cached operations") {
// Given
val prefs = MockPreferencesService()
val operationModelStore = OperationModelStore(prefs)
val jsonArray = JSONArray()

// Create a valid Operation with onesignalId
val validOperation = SetPropertyOperation(UUID.randomUUID().toString(), UUID.randomUUID().toString(), "property", "value")
validOperation.id = UUID.randomUUID().toString()

// Create a valid operation missing onesignalId
val validOperationMissing = LoginUserOperation()
validOperationMissing.id = UUID.randomUUID().toString()

// Create an invalid Operation missing onesignalId
val invalidOperation = SetPropertyOperation()
invalidOperation.id = UUID.randomUUID().toString()

// Add the Operations to the cache
jsonArray.put(validOperation.toJSON())
jsonArray.put(validOperationMissing.toJSON())
jsonArray.put(invalidOperation.toJSON())
prefs.saveString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + "operations", jsonArray.toString())

// When
operationModelStore.loadOperations()

// Then
operationModelStore.list().count() shouldBe 2
operationModelStore.get(validOperation.id) shouldNotBe null
operationModelStore.get(validOperationMissing.id) shouldNotBe null
operationModelStore.get(invalidOperation.id) shouldBe null
}
})

0 comments on commit f9e998b

Please sign in to comment.