Skip to content
Permalink
Browse files

Issue #32: JSONExperimentParserTest is not deterministic

Closes #32: JSONExperimentParserTest is not deterministic
  • Loading branch information...
fercarcedo committed Jun 21, 2018
1 parent 7d575c5 commit 57d91cfcce0f028a1a3523bc390966f99ad2caa5
@@ -8,6 +8,7 @@ import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import android.util.AtomicFile
import mozilla.components.service.fretboard.Experiment
import org.json.JSONObject
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals
@@ -31,7 +32,24 @@ class AtomicFileExperimentStorageTest {
assertFalse(atomicFile.baseFile.exists())
AtomicFileExperimentStorage(atomicFile).save(experiments)
assertTrue(atomicFile.baseFile.exists())
assertEquals("""{"experiments":[{"name":"sample-name","match":{"lang":"es|en","appId":"sample-appId","regions":["US"]},"buckets":{"max":20,"min":0},"description":"sample-description","id":"sample-id","last_modified":1526991669}]}""", String(atomicFile.readFully()))
val experimentsJson = JSONObject(String(atomicFile.readFully()))
assertEquals(1, experimentsJson.length())
val experimentsJsonArray = experimentsJson.getJSONArray("experiments")
assertEquals(1, experimentsJsonArray.length())
val experimentJson = experimentsJsonArray[0] as JSONObject
assertEquals("sample-name", experimentJson.getString("name"))
val experimentMatch = experimentJson.getJSONObject("match")
assertEquals("es|en", experimentMatch.getString("lang"))
assertEquals("sample-appId", experimentMatch.getString("appId"))
val experimentRegions = experimentMatch.getJSONArray("regions")
assertEquals(1, experimentRegions.length())
assertEquals("US", experimentRegions[0])
val experimentBuckets = experimentJson.getJSONObject("buckets")
assertEquals(20, experimentBuckets.getInt("max"))
assertEquals(0, experimentBuckets.getInt("min"))
assertEquals("sample-description", experimentJson.getString("description"))
assertEquals("sample-id", experimentJson.getString("id"))
assertEquals(1526991669, experimentJson.getLong("last_modified"))
atomicFile.delete()
}

@@ -6,6 +6,7 @@ package mozilla.components.service.fretboard.storage.atomicfile

import mozilla.components.service.fretboard.Experiment
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -107,12 +108,47 @@ class ExperimentsSerializerTest {
Experiment.Bucket(10, 5),
1523549895749)
)
assertEquals(experimentsJson.replace("\\s+".toRegex(), ""), ExperimentsSerializer().toJson(experiments))
val json = JSONObject(ExperimentsSerializer().toJson(experiments))
assertEquals(1, json.length())
val experimentsArray = json.getJSONArray("experiments")
assertEquals(2, experimentsArray.length())
val firstExperiment = experimentsArray[0] as JSONObject
val firstExperimentBuckets = firstExperiment.getJSONObject("buckets")
assertEquals(0, firstExperimentBuckets.getInt("min"))
assertEquals(100, firstExperimentBuckets.getInt("max"))
assertEquals("first", firstExperiment.getString("name"))
val firstExperimentMatch = firstExperiment.getJSONObject("match")
val firstExperimentRegions = firstExperimentMatch.getJSONArray("regions")
assertEquals(1, firstExperimentRegions.length())
assertEquals("esp", firstExperimentRegions[0])
assertEquals("^org.mozilla.firefox_beta${'$'}", firstExperimentMatch.getString("appId"))
assertEquals("eng|es|deu|fra", firstExperimentMatch.getString("lang"))
assertEquals("Description", firstExperiment.getString("description"))
assertEquals("experiment-id", firstExperiment.getString("id"))
assertEquals(1523549895713, firstExperiment.getLong("last_modified"))

val secondExperiment = experimentsArray[1] as JSONObject
val secondExperimentBuckets = secondExperiment.getJSONObject("buckets")
assertEquals(5, secondExperimentBuckets.getInt("min"))
assertEquals(10, secondExperimentBuckets.getInt("max"))
assertEquals("second", secondExperiment.getString("name"))
val secondExperimentMatch = secondExperiment.getJSONObject("match")
val secondExperimentRegions = secondExperimentMatch.getJSONArray("regions")
assertEquals(1, secondExperimentRegions.length())
assertEquals("deu", secondExperimentRegions[0])
assertEquals("^org.mozilla.firefox${'$'}", secondExperimentMatch.getString("appId"))
assertEquals("es|deu", secondExperimentMatch.getString("lang"))
assertEquals("SecondDescription", secondExperiment.getString("description"))
assertEquals("experiment-2-id", secondExperiment.getString("id"))
assertEquals(1523549895749, secondExperiment.getLong("last_modified"))
}

@Test
fun testToJsonEmptyList() {
val experiments = listOf<Experiment>()
assertEquals("""{"experiments":[]}""", ExperimentsSerializer().toJson(experiments))
val experimentsJson = JSONObject(ExperimentsSerializer().toJson(experiments))
assertEquals(1, experimentsJson.length())
val experimentsJsonArray = experimentsJson.getJSONArray("experiments")
assertEquals(0, experimentsJsonArray.length())
}
}
@@ -20,14 +20,31 @@ class JSONExperimentParserTest {
Experiment.Matcher("es|en", "sample-appId", listOf("US")),
Experiment.Bucket(20, 0),
1526991669)
assertEquals("""{"buckets":{"min":0,"max":20},"name":"sample-name","match":{"regions":["US"],"appId":"sample-appId","lang":"es|en"},"description":"sample-description","id":"sample-id","last_modified":1526991669}""",
JSONExperimentParser().toJson(experiment).toString())
val jsonObject = JSONExperimentParser().toJson(experiment)
val buckets = jsonObject.getJSONObject("buckets")
assertEquals(0, buckets.getInt("min"))
assertEquals(20, buckets.getInt("max"))
assertEquals("sample-name", jsonObject.getString("name"))
val match = jsonObject.getJSONObject("match")
val regions = match.getJSONArray("regions")
assertEquals(1, regions.length())
assertEquals("US", regions.get(0))
assertEquals("sample-appId", match.getString("appId"))
assertEquals("es|en", match.getString("lang"))
assertEquals("sample-description", jsonObject.getString("description"))
assertEquals("sample-id", jsonObject.getString("id"))
assertEquals(1526991669, jsonObject.getLong("last_modified"))
}

@Test
fun testToJsonNullValues() {
val experiment = Experiment("id")
assertEquals("""{"buckets":{},"match":{},"id":"id"}""", JSONExperimentParser().toJson(experiment).toString())
val jsonObject = JSONExperimentParser().toJson(experiment)
val buckets = jsonObject.getJSONObject("buckets")
assertEquals(0, buckets.length())
val match = jsonObject.getJSONObject("match")
assertEquals(0, match.length())
assertEquals("id", jsonObject.getString("id"))
}

@Test
@@ -78,6 +95,21 @@ class JSONExperimentParserTest {
payload.put("e", listOf(1, 2, 3, 4))
val experiment = Experiment("id", payload = payload)
val json = JSONExperimentParser().toJson(experiment)
assertEquals("""{"payload":{"a":"a","b":3,"c":3.5,"d":true,"e":[1,2,3,4]},"buckets":{},"match":{},"id":"id"}""", json.toString())
val payloadJson = json.getJSONObject("payload")
assertEquals("a", payloadJson.getString("a"))
assertEquals(3, payloadJson.getInt("b"))
assertEquals(3.5, payloadJson.getDouble("c"), 0.01)
assertEquals(true, payloadJson.getBoolean("d"))
val list = payloadJson.getJSONArray("e")
assertEquals(4, list.length())
assertEquals(1, list[0])
assertEquals(2, list[1])
assertEquals(3, list[2])
assertEquals(4, list[3])
val buckets = json.getJSONObject("buckets")
assertEquals(0, buckets.length())
val match = json.getJSONObject("match")
assertEquals(0, match.length())
assertEquals("id", json.getString("id"))
}
}

0 comments on commit 57d91cf

Please sign in to comment.
You can’t perform that action at this time.