Commit ba364488 authored by Georg Fritzsche's avatar Georg Fritzsche
Browse files

Cleanup JSON usage in experiments library tests

parent 72e15b03
......@@ -13,46 +13,46 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
val experimentsJson = """
{
"experiments": [
{
"buckets":{
"min":0,
"max":100
},
"name":"first",
"match":{
"regions": [
"esp"
],
"appId":"^org.mozilla.firefox_beta${'$'}",
"lang":"eng|es|deu|fra"
},
"description":"Description",
"id":"experiment-id",
"last_modified":1523549895713
},
{
"buckets":{
"min":5,
"max":10
},
"name":"second",
"match":{
"regions": [
"deu"
],
"appId":"^org.mozilla.firefox${'$'}",
"lang":"es|deu"
},
"description":"SecondDescription",
"id":"experiment-2-id",
"last_modified":1523549895749
}
],
"last_modified": 1523549895749
}
"""
{
"experiments": [
{
"buckets":{
"min":0,
"max":100
},
"name":"first",
"match":{
"regions": [
"esp"
],
"appId":"^org.mozilla.firefox_beta${'$'}",
"lang":"eng|es|deu|fra"
},
"description":"Description",
"id":"experiment-id",
"last_modified":1523549895713
},
{
"buckets":{
"min":5,
"max":10
},
"name":"second",
"match":{
"regions": [
"deu"
],
"appId":"^org.mozilla.firefox${'$'}",
"lang":"es|deu"
},
"description":"SecondDescription",
"id":"experiment-2-id",
"last_modified":1523549895749
}
],
"last_modified": 1523549895749
}
"""
@RunWith(RobolectricTestRunner::class)
class ExperimentsSerializerTest {
......
......@@ -97,8 +97,33 @@ class FlatFileExperimentStorageTest {
@Test
fun retrieve() {
val file = File(RuntimeEnvironment.application.filesDir, "experiments.json")
val json = """
{
"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
}
],
"last_modified": 1526991669
}
""".trimIndent()
file.writer().use {
it.write("""{"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}],"last_modified":1526991669}""")
it.write(json)
}
val experimentsResult = FlatFileExperimentStorage(file).retrieve()
val experiments = experimentsResult.experiments
......
......@@ -62,7 +62,26 @@ class JSONExperimentParserTest {
@Test
fun fromJson() {
val json = """{"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}"""
val json = """
{
"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
}
""".trimIndent()
val expectedExperiment = Experiment("sample-id",
"sample-name",
"sample-description",
......@@ -74,22 +93,72 @@ class JSONExperimentParserTest {
@Test
fun fromJsonNonPresentValues() {
val json = """{"id":"id","name":"name"}"""
val json = """
{
"id": "id",
"name": "name"
}
""".trimIndent()
assertEquals(Experiment("id", "name"), JSONExperimentParser().fromJson(JSONObject(json)))
}
@Test
fun fromJsonNullValues() {
val json = """{"buckets":null,"name":"sample-name","match":null,"description":null,"id":"sample-id","last_modified":null}"""
val json = """
{
"buckets": null,
"name": "sample-name",
"match": null,
"description": null,
"id": "sample-id",
"last_modified": null
}
""".trimIndent()
assertEquals(Experiment("sample-id", "sample-name"), JSONExperimentParser().fromJson(JSONObject(json)))
val emptyObjects = """{"id":"sample-id","name":"sample-name","buckets":{"min":null,"max":null},"match":{"lang":null,"appId":null,"region":null}}"""
val emptyObjects = """
{
"id": "sample-id",
"name": "sample-name",
"buckets": {
"min": null,
"max": null
},
"match": {
"lang": null,
"appId": null,
"region": null
}
}
""".trimIndent()
assertEquals(Experiment("sample-id", name = "sample-name", bucket = Experiment.Bucket(), match = Experiment.Matcher()),
JSONExperimentParser().fromJson(JSONObject(emptyObjects)))
}
@Test
fun payloadFromJson() {
val json = """{"buckets":null,"name":null,"match":null,"description":null,"id":"sample-id","last_modified":null,"values":{"a":"a","b":3,"c":3.5,"d":true,"e":[1,2,3,4]}}"""
val json = """
{
"buckets": null,
"name": null,
"match": null,
"description": null,
"id": "sample-id",
"last_modified": null,
"values": {
"a": "a",
"b": 3,
"c": 3.5,
"d": true,
"e": [
1,
2,
3,
4
]
}
}
""".trimIndent()
val experiment = JSONExperimentParser().fromJson(JSONObject(json))
assertEquals("a", experiment.payload?.get("a"))
assertEquals(3, experiment.payload?.get("b"))
......
......@@ -25,10 +25,15 @@ class KintoExperimentSourceTest {
@Test
fun noExperiments() {
val httpClient = mock(Client::class.java)
val url = "$baseUrl/buckets/$bucketName/collections/$collectionName/records"
val json = """
{
"data": []
}
""".trimIndent()
`when`(httpClient.fetch(any()))
.thenReturn(Response(url, 200, MutableHeaders(), Response.Body("""{"data":[]}""".byteInputStream())))
.thenReturn(Response(url, 200, MutableHeaders(), Response.Body(json.byteInputStream())))
val experimentSource = KintoExperimentSource(baseUrl, bucketName, collectionName, httpClient)
val result = experimentSource.getExperiments(ExperimentsSnapshot(listOf(), null))
assertEquals(0, result.experiments.size)
......@@ -40,11 +45,34 @@ class KintoExperimentSourceTest {
val httpClient = mock(Client::class.java)
val url = "$baseUrl/buckets/$bucketName/collections/$collectionName/records"
val json = """
{
"data":[
{
"name": "first-name",
"match": {
"lang": "eng",
"appId": "first-appId",
"regions": []
},
"schema": 1523549592861,
"buckets": {
"max": "100",
"min": "0"
},
"description": "first-description",
"id": "first-id",
"last_modified": 1523549895713
}
]
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response(url,
200,
MutableHeaders(),
Response.Body("""{"data":[{"name":"first-name","match":{"lang":"eng","appId":"first-appId",regions:[]},"schema":1523549592861,"buckets":{"max":"100","min":"0"},"description":"first-description", "id":"first-id","last_modified":1523549895713}]}""".byteInputStream())))
Response.Body(json.byteInputStream())))
val expectedExperiment = Experiment("first-id",
"first-name",
......@@ -65,11 +93,34 @@ class KintoExperimentSourceTest {
fun getExperimentsDiffAdd() {
val httpClient = mock(Client::class.java)
val url = "$baseUrl/buckets/$bucketName/collections/$collectionName/records?_since=1523549890000"
val json = """
{
"data": [
{
"name": "first-name",
"match": {
"lang": "eng",
"appId": "first-appId",
"regions": []
},
"schema": 1523549592861,
"buckets": {
"max": "100",
"min": "0"
},
"description": "first-description",
"id": "first-id",
"last_modified": 1523549895713
}
]
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response(url,
200,
MutableHeaders(),
Response.Body("""{"data":[{"name":"first-name","match":{"lang":"eng","appId":"first-appId",regions:[]},"schema":1523549592861,"buckets":{"max":"100","min":"0"},"description":"first-description", "id":"first-id","last_modified":1523549895713}]}""".byteInputStream())))
Response.Body(json.byteInputStream())))
val kintoExperiment = Experiment("first-id",
"first-name",
......@@ -114,22 +165,40 @@ class KintoExperimentSourceTest {
Experiment.Bucket(10, 5),
1523549890000)
val json = """
{
"data": [
{
"deleted": true,
"id": "id",
"last_modified": 1523549899999
}
]
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response("$baseUrl/buckets/$bucketName/collections/$collectionName/records?_since=1523549890000",
200,
MutableHeaders(),
Response.Body("""{"data":[{"deleted":true,"id":"id","last_modified":1523549899999}]}""".byteInputStream())))
Response.Body(json.byteInputStream())))
val experimentSource = KintoExperimentSource(baseUrl, bucketName, collectionName, httpClient)
val kintoExperiments = experimentSource.getExperiments(ExperimentsSnapshot(listOf(storageExperiment, secondExperiment), 1523549890000))
assertEquals(1, kintoExperiments.experiments.size)
assertEquals(1523549899999, kintoExperiments.lastModified)
val json2 = """
{
"data": []
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response("$baseUrl/buckets/$bucketName/collections/$collectionName/records?_since=1523549899999",
200,
MutableHeaders(),
Response.Body("""{"data":[]}""".byteInputStream())))
Response.Body(json2.byteInputStream())))
val experimentsResult = experimentSource.getExperiments(ExperimentsSnapshot(kintoExperiments.experiments, 1523549899999))
assertEquals(1, experimentsResult.experiments.size)
......@@ -140,11 +209,35 @@ class KintoExperimentSourceTest {
fun getExperimentsDiffUpdate() {
val httpClient = mock(Client::class.java)
val url = "$baseUrl/buckets/$bucketName/collections/$collectionName/records?_since=1523549800000"
val json = """
{
"data": [
{
"name": "first-name",
"match": {
"lang": "eng",
"appId": "first-appId",
"regions": []
},
"schema": 1523549592861,
"buckets": {
"max": "100",
"min": "0"
},
"description": "first-description",
"id": "first-id",
"last_modified": 1523549895713
}
]
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response(url,
200,
MutableHeaders(),
Response.Body("""{"data":[{"name":"first-name","match":{"lang":"eng","appId":"first-appId",regions:[]},"schema":1523549592861,"buckets":{"max":"100","min":"0"},"description":"first-description", "id":"first-id","last_modified":1523549895713}]}""".byteInputStream())))
Response.Body(json.byteInputStream())))
val kintoExperiment = Experiment("first-id",
"first-name",
......@@ -173,11 +266,18 @@ class KintoExperimentSourceTest {
fun getExperimentsEmptyDiff() {
val httpClient = mock(Client::class.java)
val url = "$baseUrl/buckets/$bucketName/collections/$collectionName/records?_since=1523549895713"
val json = """
{
"data": []
}
""".trimIndent()
`when`(httpClient.fetch(any())).thenReturn(
Response(url,
200,
MutableHeaders(),
Response.Body("""{"data":[]}""".byteInputStream())))
Response.Body(json.byteInputStream())))
val storageExperiment = Experiment("first-id",
"first-name",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment