1
1
package com.statsig.sdk
2
2
3
3
import com.google.gson.*
4
+ import com.google.gson.annotations.SerializedName
4
5
import org.junit.Assert.*
5
6
import org.junit.Before
6
7
import org.junit.Test
7
8
9
+ internal data class APIDownloadedConfigsFromLocalEval (
10
+ @SerializedName(" dynamic_configs" ) val dynamicConfigs : Array <APIConfig >,
11
+ @SerializedName(" feature_gates" ) val featureGates : Array <APIConfig >,
12
+ @SerializedName(" layer_configs" ) val layerConfigs : Array <APIConfig >,
13
+ @SerializedName(" layers" ) val layers : Map <String , Array <String >>? ,
14
+ @SerializedName(" time" ) val time : Long = 0 ,
15
+ @SerializedName(" has_updates" ) val hasUpdates : Boolean ,
16
+ @SerializedName(" diagnostics" ) val diagnostics : Map <String , Int >? = null ,
17
+ )
18
+
8
19
class OnDeviceEvalClientInitializeFormatterTest {
9
20
private lateinit var driver: StatsigServer
10
21
private lateinit var gson: Gson
@@ -25,62 +36,24 @@ class OnDeviceEvalClientInitializeFormatterTest {
25
36
gson.fromJson(dcs, APIDownloadedConfigs ::class .java)
26
37
specStore.setDownloadedConfigs(configs)
27
38
28
- val formatter = OnDeviceEvalClientInitializeFormatter (specStore, clientSDKKey = " client-sdk " )
29
- val response = formatter.getFormattedResponse()
39
+ val formatter = OnDeviceEvalClientInitializeFormatter (specStore, null )
40
+ val response = formatter.getFormattedResponse().toMap()
30
41
val serializedResponse = gson.toJson(response)
31
42
assertNotNull(serializedResponse)
32
43
assertTrue(serializedResponse is String )
33
44
assertTrue(serializedResponse.isNotEmpty())
34
45
35
- val actualJson = gson.fromJson(serializedResponse, Map ::class .java) as Map <String , Any >
36
- val expectedJson = gson.fromJson(dcs, Map ::class .java) as Map <String , Any >
37
-
38
- val actualFeatureGatesMap = actualJson[" feature_gates" ] as Map <String , Any >
39
- val expectedFeatureGatesList = expectedJson[" feature_gates" ] as List <Map <String , Any >>
40
- val expectedFeatureGatesMap = expectedFeatureGatesList.associateBy { it[" name" ] as String }
41
-
42
- val gateName = " always_on_gate"
43
- val actualGate = actualFeatureGatesMap[gateName] as Map <String , Any >
44
- val expectedGate = expectedFeatureGatesMap[gateName] as Map <String , Any >
45
-
46
- val actualRules = actualGate[" rules" ] as List <Map <String , Any >>
47
- val expectedRules = expectedGate[" rules" ] as List <Map <String , Any >>
48
-
49
- assertEquals(expectedGate[" name" ], actualGate[" name" ])
50
- assertEquals(expectedGate[" type" ], actualGate[" type" ])
51
- assertEquals(expectedGate[" salt" ], actualGate[" salt" ])
52
- assertEquals(expectedGate[" enabled" ], actualGate[" enabled" ])
53
- assertEquals(expectedGate[" defaultValue" ], actualGate[" defaultValue" ])
54
-
55
- assertEquals(expectedRules.size, actualRules.size)
56
-
57
- for (i in expectedRules.indices) {
58
- val expectedRule = expectedRules[i]
59
- val actualRule = actualRules[i]
60
-
61
- assertEquals(expectedRule[" name" ], actualRule[" name" ])
62
- assertEquals(expectedRule[" groupName" ], actualRule[" groupName" ])
63
- assertEquals(expectedRule[" passPercentage" ], actualRule[" passPercentage" ])
64
- assertEquals(expectedRule[" returnValue" ], actualRule[" returnValue" ])
65
- assertEquals(expectedRule[" id" ], actualRule[" id" ])
66
- assertEquals(expectedRule[" salt" ], actualRule[" salt" ])
67
-
68
- val actualConditions = actualRule[" conditions" ] as List <Map <String , Any >>
69
- val expectedConditions = expectedRule[" conditions" ] as List <Map <String , Any >>
70
-
71
- assertEquals(expectedConditions.size, actualConditions.size)
72
-
73
- for (j in expectedConditions.indices) {
74
- val expectedCondition = expectedConditions[j]
75
- val actualCondition = actualConditions[j]
76
-
77
- assertEquals(expectedCondition[" type" ], actualCondition[" type" ])
78
- assertEquals(expectedCondition[" targetValue" ], actualCondition[" targetValue" ])
79
- assertEquals(expectedCondition[" operator" ], actualCondition[" operator" ])
80
- assertEquals(expectedCondition[" field" ], actualCondition[" field" ])
81
- assertEquals(expectedCondition[" additionalValues" ], actualCondition[" additionalValues" ])
82
- }
83
- }
46
+ val actualJson = gson.fromJson(serializedResponse, APIDownloadedConfigsFromLocalEval ::class .java)
47
+ val expectedJson = configs
48
+
49
+ // Compare the seven fields ( since local eval only have below fields)
50
+ assertEquals(gson.toJson(expectedJson.dynamicConfigs), gson.toJson(actualJson.dynamicConfigs))
51
+ assertEquals(gson.toJson(expectedJson.featureGates), gson.toJson(actualJson.featureGates))
52
+ assertEquals(gson.toJson(expectedJson.layerConfigs), gson.toJson(actualJson.layerConfigs))
53
+ assertEquals(gson.toJson(expectedJson.layers), gson.toJson(actualJson.layers))
54
+ assertEquals(gson.toJson(expectedJson.time), gson.toJson(actualJson.time))
55
+ assertEquals(gson.toJson(expectedJson.hasUpdates), gson.toJson(actualJson.hasUpdates))
56
+ assertEquals(gson.toJson(expectedJson.diagnostics), gson.toJson(actualJson.diagnostics))
84
57
}
85
58
86
59
@Test
0 commit comments