From 6399f722f880bcf6d1d5a7f605f7d131e26fff87 Mon Sep 17 00:00:00 2001 From: ParthPali <54657955+ParthPali@users.noreply.github.com> Date: Wed, 8 Jan 2020 16:37:29 -0800 Subject: [PATCH] Added offix library unit tests --- .../org/aerogear/offix/MutationDataCheck.kt | 55 +++++++++++++++++++ .../src/main/java/org/aerogear/offix/Offix.kt | 9 +-- .../aerogear/offix/OffixLibraryUnitTest.kt | 46 ++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 offix/src/main/java/org/aerogear/offix/MutationDataCheck.kt create mode 100644 offix/src/test/java/org/aerogear/offix/OffixLibraryUnitTest.kt diff --git a/offix/src/main/java/org/aerogear/offix/MutationDataCheck.kt b/offix/src/main/java/org/aerogear/offix/MutationDataCheck.kt new file mode 100644 index 0000000..4f9e670 --- /dev/null +++ b/offix/src/main/java/org/aerogear/offix/MutationDataCheck.kt @@ -0,0 +1,55 @@ +package org.aerogear.offix + +import org.json.JSONObject +import com.apollographql.apollo.api.OperationName + +object MutationDataCheck{ + fun checkOperationID(x1: String): String{ + if(validateOperationID(x1)){ + return x1 + } + throw Exception("Exception: NullOperationID") + } + fun checkOperationName(x2: OperationName): OperationName{ + if(validateOperationName(x2)){ + return x2 + } + throw Exception("Exception: EmptyOperationName") + } + fun checkOperationDoc(x3: String): String{ + if(validateOperationDoc(x3)){ + return x3 + } + throw Exception("Exception: NullOperationDoc") + } + fun checkJSONObject(x4: JSONObject): JSONObject{ + if(validateJSONObject(x4)){ + return x4 + } + throw Exception("Exception: NullJSONObject") + } + fun validateOperationID(x1: String): Boolean{ + if (x1.isNullOrBlank()){ + return false//Exception("Exception: NullOperationID") + } + return true + } + fun validateOperationName(x2: OperationName): Boolean{ + if (x2.name().isBlank()){ + return false//throw Exception("Exception: EmptyOperationName") + } + return true + } + fun validateOperationDoc(x3: String): Boolean{ + if (x3.isNullOrBlank()){ + return false//throw Exception("Exception: NullOperationDoc") + } + return true + } + fun validateJSONObject(x4: JSONObject): Boolean{ + if (x4 == null){ + return false//throw Exception("Exception: NullJSONObject") + } + return true + } +} \ No newline at end of file diff --git a/offix/src/main/java/org/aerogear/offix/Offix.kt b/offix/src/main/java/org/aerogear/offix/Offix.kt index 9a82d37..5b9797d 100644 --- a/offix/src/main/java/org/aerogear/offix/Offix.kt +++ b/offix/src/main/java/org/aerogear/offix/Offix.kt @@ -8,6 +8,7 @@ import com.apollographql.apollo.api.Input import com.apollographql.apollo.api.Mutation import com.apollographql.apollo.api.Operation import org.json.JSONObject +import org.aerogear.offix.MutationDataCheck /* conflictedMutationClass variable stores the name of the mutation class in which conflict has occurred. @@ -19,11 +20,11 @@ var conflictedMutationClass="" */ fun getPersistenceMutation(mutation: Mutation): org.aerogear.offix.persistence.Mutation { - val operationId = mutation.operationId() - val operationDoc = mutation.queryDocument() - val operationName = mutation.name() + val operationId = MutationDataCheck.checkOperationID(mutation.operationId()) + val operationDoc = MutationDataCheck.checkOperationDoc(mutation.queryDocument()) + val operationName = MutationDataCheck.checkOperationName(mutation.name()) val valMap = mutation.variables().valueMap() - val jsonObject = JSONObject(valMap) + val jsonObject = MutationDataCheck.checkJSONObject(JSONObject(valMap)) val responseClassName = mutation.javaClass.name /* Make an object of com.aerogear.offix.persistence.Mutation diff --git a/offix/src/test/java/org/aerogear/offix/OffixLibraryUnitTest.kt b/offix/src/test/java/org/aerogear/offix/OffixLibraryUnitTest.kt new file mode 100644 index 0000000..0785840 --- /dev/null +++ b/offix/src/test/java/org/aerogear/offix/OffixLibraryUnitTest.kt @@ -0,0 +1,46 @@ +package org.aerogear.offix + +import com.apollographql.apollo.api.OperationName +import com.google.gson.JsonParser +import org.json.JSONObject +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + + +/** + * This Offix Library unit test, will execute validate offix mutation data values for null and blank. + * + */ +class OffixLibraryUnitTest { + @Test + fun validateOperationIDisTrue(){ + assertTrue(MutationDataCheck.validateOperationID("2")) + } + @Test + fun validateOperationIDisFalse(){ + //String xx = null + assertFalse(MutationDataCheck.validateOperationID("")) + } + @Test + fun validateOperationNameisTrue(){ + assertTrue(MutationDataCheck.validateOperationName(OperationName { "testoperation" })) + } + @Test + fun validateOperationNameisFalse(){ + assertFalse(MutationDataCheck.validateOperationName(OperationName { "" })) + } + @Test + fun validateOperationDocisTrue(){ + assertTrue(MutationDataCheck.validateOperationDoc("doc value")) + } + @Test + fun validateOperationDocisFalse(){ + assertFalse(MutationDataCheck.validateOperationDoc("")) + } + @Test + fun validateJSONObjectisTrue(){ + val jo = JSONObject("""{"name":"test name", "age":25}""") + assertTrue(MutationDataCheck.validateJSONObject(jo)) + } +} \ No newline at end of file