forked from fossasia/open-event-attendee-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unit testing for utils and error utils (fossasia#2305)
- Loading branch information
1 parent
90f232d
commit 90c4b5d
Showing
8 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
app/src/test/java/org/fossasia/openevent/general/ErrorUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package org.fossasia.openevent.general | ||
|
||
import io.reactivex.android.plugins.RxAndroidPlugins | ||
import io.reactivex.plugins.RxJavaPlugins | ||
import okhttp3.ResponseBody | ||
import io.reactivex.schedulers.Schedulers | ||
import org.junit.Before | ||
import android.accounts.Account | ||
import com.google.common.io.Resources | ||
import com.google.common.base.Charsets | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import org.fossasia.openevent.general.utils.ErrorUtils | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
import java.io.IOException | ||
import java.net.URISyntaxException | ||
import java.net.URL | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class ErrorUtilsTest { | ||
var contentType = "application/vnd.api+json" | ||
|
||
private lateinit var url1: URL | ||
private lateinit var url2: URL | ||
private lateinit var url3: URL | ||
private lateinit var url4: URL | ||
private lateinit var url5: URL | ||
|
||
private lateinit var content1: String | ||
private lateinit var content2: String | ||
private lateinit var content3: String | ||
private lateinit var content4: String | ||
private lateinit var content5: String | ||
|
||
private lateinit var httpException1: HttpException | ||
private lateinit var httpException2: HttpException | ||
private lateinit var httpException3: HttpException | ||
private lateinit var httpException4: HttpException | ||
private lateinit var httpException5: HttpException | ||
|
||
private lateinit var responseBody1: ResponseBody | ||
private lateinit var responseBody2: ResponseBody | ||
private lateinit var responseBody3: ResponseBody | ||
private lateinit var responseBody4: ResponseBody | ||
private lateinit var responseBody5: ResponseBody | ||
|
||
private lateinit var errorResponse1: Response<Account> | ||
private lateinit var errorResponse2: Response<Account> | ||
private lateinit var errorResponse3: Response<Account> | ||
private lateinit var errorResponse4: Response<Account> | ||
private lateinit var errorResponse5: Response<Account> | ||
|
||
@Before | ||
fun setUp() { | ||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } | ||
RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() } | ||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } | ||
|
||
url1 = Resources.getResource("raw/content1.json") | ||
url2 = Resources.getResource("raw/content2.json") | ||
url3 = Resources.getResource("raw/content3.json") | ||
url4 = Resources.getResource("raw/content4.json") | ||
url5 = Resources.getResource("raw/content5.json") | ||
|
||
try { | ||
content1 = Resources.toString(url1, Charsets.UTF_8) | ||
content2 = Resources.toString(url2, Charsets.UTF_8) | ||
content3 = Resources.toString(url3, Charsets.UTF_8) | ||
content4 = Resources.toString(url4, Charsets.UTF_8) | ||
content5 = Resources.toString(url5, Charsets.UTF_8) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
|
||
responseBody1 = ResponseBody.create(contentType.toMediaTypeOrNull(), content1) | ||
errorResponse1 = Response.error(422, responseBody1) | ||
|
||
responseBody2 = ResponseBody.create(contentType.toMediaTypeOrNull(), content2) | ||
errorResponse2 = Response.error(422, responseBody2) | ||
|
||
responseBody3 = ResponseBody.create(contentType.toMediaTypeOrNull(), content3) | ||
errorResponse3 = Response.error(422, responseBody3) | ||
|
||
responseBody4 = ResponseBody.create(contentType.toMediaTypeOrNull(), content4) | ||
errorResponse4 = Response.error(400, responseBody4) | ||
|
||
responseBody5 = ResponseBody.create(contentType.toMediaTypeOrNull(), content5) | ||
errorResponse5 = Response.error(400, responseBody5) | ||
|
||
httpException1 = HttpException(errorResponse1) | ||
httpException2 = HttpException(errorResponse2) | ||
httpException3 = HttpException(errorResponse3) | ||
httpException4 = HttpException(errorResponse4) | ||
httpException5 = HttpException(errorResponse5) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
RxJavaPlugins.reset() | ||
RxAndroidPlugins.reset() | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class, URISyntaxException::class) | ||
fun shouldReturnNullOnNullAnsEmptyPointedField() { | ||
assertNull(ErrorUtils.getPointedField(null)) | ||
assertNull(ErrorUtils.getPointedField("")) | ||
} | ||
|
||
@Test | ||
fun shouldReturnPointedField() { | ||
val pointer = "/data/attributes/form/end_field" | ||
val pointer1 = "/data/attributes/form_field" | ||
val pointer2 = "/data/attributes/" | ||
val pointer3 = "/data" | ||
|
||
assertEquals("end_field", ErrorUtils.getPointedField(pointer)) | ||
assertEquals("form_field", ErrorUtils.getPointedField(pointer1)) | ||
assertNull(ErrorUtils.getPointedField(pointer2)) | ||
assertNull(ErrorUtils.getPointedField(pointer3)) | ||
} | ||
|
||
@Test | ||
fun shouldReturnErrorDetailsWithPointedFieldSuccessfully() { | ||
val str = ErrorUtils.getErrorDetails(httpException1).toString() | ||
assertEquals("Missing data for required field - licence", str) | ||
} | ||
|
||
@Test | ||
fun shouldReturnErrorDetailsWithNullOrEmptyPointedFieldSuccessfully() { | ||
val str = ErrorUtils.getErrorDetails(httpException3).toString() | ||
val str1 = ErrorUtils.getErrorDetails(httpException2).toString() | ||
assertEquals("Missing data for required field.", str1) | ||
assertEquals("Missing data for required field.", str) | ||
} | ||
|
||
@Test | ||
fun shouldReturnErrorMessageSuccessfully() { | ||
val str = ErrorUtils.getMessage(httpException1).toString() | ||
val str1 = ErrorUtils.getMessage(httpException5).toString() | ||
assertEquals("Missing data for required field - licence", str) | ||
assertEquals("Access Forbidden: Co-Organizer access required - order_id", str1) | ||
} | ||
|
||
@Test | ||
fun shouldReturnErrorMessageTitleAndDetailSuccessfully() { | ||
val str = ErrorUtils.getErrorTitleAndDetails(httpException4).toString() | ||
assertEquals("Bad Request: The URL does not exist", str) | ||
} | ||
|
||
@Test | ||
fun shouldReturnErrorMessageTitleDetailAndPointerSuccessfully() { | ||
val str = ErrorUtils.getErrorTitleAndDetails(httpException5).toString() | ||
assertEquals("Access Forbidden: Co-Organizer access required - order_id", str) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/test/java/org/fossasia/openevent/general/UtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.fossasia.openevent.general | ||
|
||
import org.fossasia.openevent.general.utils.Utils | ||
import org.fossasia.openevent.general.utils.Utils.getCardType | ||
import org.fossasia.openevent.general.utils.Utils.cardType | ||
import org.junit.Test | ||
import org.junit.Assert.assertEquals | ||
|
||
class UtilsTest { | ||
|
||
private fun getCardNumber(type: cardType): String { | ||
return when (type) { | ||
cardType.AMERICAN_EXPRESS -> "371449635398431" | ||
cardType.DINERS_CLUB -> "30569309025904" | ||
cardType.DISCOVER -> "6011111111111117" | ||
cardType.MASTER_CARD -> "5555555555554444" | ||
cardType.UNIONPAY -> "6200000000000005" | ||
cardType.VISA -> "4242424242424242" | ||
cardType.NONE -> "" | ||
} | ||
} | ||
|
||
@Test | ||
fun `should get american express`() { | ||
val cardNumber = getCardNumber(Utils.cardType.AMERICAN_EXPRESS) | ||
assertEquals(getCardType(cardNumber), cardType.AMERICAN_EXPRESS) | ||
} | ||
|
||
@Test | ||
fun `should get diners club`() { | ||
val cardNumber = getCardNumber(Utils.cardType.DINERS_CLUB) | ||
assertEquals(getCardType(cardNumber), cardType.DINERS_CLUB) | ||
} | ||
|
||
@Test | ||
fun `should get discover`() { | ||
val cardNumber = getCardNumber(Utils.cardType.DISCOVER) | ||
assertEquals(getCardType(cardNumber), cardType.DISCOVER) | ||
} | ||
|
||
@Test | ||
fun `should get master card`() { | ||
val cardNumber = getCardNumber(Utils.cardType.MASTER_CARD) | ||
assertEquals(getCardType(cardNumber), cardType.MASTER_CARD) | ||
} | ||
|
||
@Test | ||
fun `should get union pay`() { | ||
val cardNumber = getCardNumber(Utils.cardType.UNIONPAY) | ||
assertEquals(getCardType(cardNumber), cardType.UNIONPAY) | ||
} | ||
|
||
@Test | ||
fun `should get visa`() { | ||
val cardNumber = getCardNumber(Utils.cardType.VISA) | ||
assertEquals(getCardType(cardNumber), cardType.VISA) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"status": "422", | ||
"source": { | ||
"pointer": "/data/attributes/licence" | ||
}, | ||
"detail": "Missing data for required field.", | ||
"title": "Validation error" | ||
} | ||
], | ||
"jsonapi": { | ||
"version": "1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"status": "422", | ||
"source": { | ||
"pointer": "" | ||
}, | ||
"detail": "Missing data for required field.", | ||
"title": "Validation error" | ||
} | ||
], | ||
"jsonapi": { | ||
"version": "1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"status": "422", | ||
"source": { | ||
|
||
}, | ||
"detail": "Missing data for required field.", | ||
"title": "Validation error" | ||
} | ||
], | ||
"jsonapi": { | ||
"version": "1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"status": "400", | ||
"source": { | ||
|
||
}, | ||
"detail": "The URL does not exist", | ||
"title": "Bad Request" | ||
} | ||
], | ||
"jsonapi": { | ||
"version": "1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"status": "403", | ||
"source": { | ||
"pointer": "/data/attributes/order_id" | ||
}, | ||
"detail": "Co-Organizer access required", | ||
"title": "Access Forbidden" | ||
} | ||
], | ||
"jsonapi": { | ||
"version": "1.0" | ||
} | ||
} |