Skip to content

Commit

Permalink
Mocking images (#18)
Browse files Browse the repository at this point in the history
* Major refactor, exposing a new dsl for mocking and supporting raw images (WIP)

* Small change in the DSL

* Fix compilation issues

* Small fixes

* Small fixes

* Small fixes

* Revert to port 0
  • Loading branch information
gmerinojimenez authored May 22, 2023
1 parent 16f570c commit c5882e1
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 250 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class App : Application() {
if (BuildConfig.DEFAULT_ENVIRONMENT == Environment.DEMO) {
CoroutineScope(Dispatchers.IO).launch {
mockHelper.setUp()
mockHelper.enqueue(getUserMocksUseCase()) // This use case provide a List<Mock>
mockHelper.enqueue {
whenever("/image.png").thenReturnFromRawFile("demo_image")
whenever("/?results=5").thenReturnFromFile("user_list_success_1.json")
whenever("/?results=10").thenReturn(Moshi.Builder().build().adapter(UserWrapperDto::class.java).toJson(DEMO_LIST))
}
}
}
...
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
applicationId "com.telefonica.mocks"
minSdk 21
targetSdk 32
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -38,7 +38,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion "1.4.7"
}
packagingOptions {
resources {
Expand Down Expand Up @@ -71,7 +71,7 @@ dependencies {

implementation project(":mock")

implementation("com.google.dagger:hilt-android:2.42")
implementation("com.google.dagger:hilt-android:2.46.1")
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"

implementation 'androidx.navigation:navigation-runtime-ktx:2.5.2'
Expand All @@ -88,6 +88,6 @@ dependencies {
implementation "com.squareup.moshi:moshi-kotlin:1.13.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.10.0"

kapt("com.google.dagger:hilt-android-compiler:2.42")
kapt("com.google.dagger:hilt-android-compiler:2.46.1")

}
18 changes: 9 additions & 9 deletions app/src/demo/assets/user_list_success_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@
"last": "Garcia"
},
"email": "[email protected]",
"phone": "618 42 69 62"
"phone": "666 66 66 66"
},
{
"name": {
"title": "Sra",
"first": "Maria",
"last": "Perez"
},
"email": "Perez_maria@gmail.com",
"phone": "638 55 41 00"
"email": "xxxxxx@gmail.com",
"phone": "666 66 66 66"
},
{
"name": {
"title": "Sr",
"first": "Carlos",
"last": "Garcia"
},
"email": "garciacarlos@gmail.com",
"phone": "601 48 12 12"
"email": "xxxxxx@gmail.com",
"phone": "666 66 66 66"
},
{
"name": {
"title": "Sr",
"first": "Felix",
"last": "Garcia"
},
"email": "garciafelix@gmail.com",
"phone": "699 11 31 11"
"email": "xxxxxx@gmail.com",
"phone": "666 66 66 66"
},
{
"name": {
"title": "Sra",
"first": "Virginia",
"last": "Sanz"
},
"email": "SanzVirginia.sg@gmail.com",
"phone": "688 31 41 41"
"email": "xxxxxx@gmail.com",
"phone": "666 66 66 66"
}
]
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/telefonica/mocks/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class App : Application() {
super.onCreate()
CoroutineScope(Dispatchers.IO).launch {
mockHelper.setUp(enableSsl = true)
mockHelper.enqueue(getUserMocksUseCase())
getUserMocksUseCase()
initBackendUrl()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
package com.telefonica.mocks.domain.mock

import com.squareup.moshi.Moshi
import com.telefonica.mock.Method
import com.telefonica.mock.Mock
import com.telefonica.mock.MockedApiResponse
import com.telefonica.mock.MockHelper
import com.telefonica.mocks.model.user.NameDto
import com.telefonica.mocks.model.user.UserDto
import com.telefonica.mocks.model.user.UserWrapperDto
import javax.inject.Inject

open class GetUserMocksUseCase @Inject constructor(
mockHelper: MockHelper
private val mockHelper: MockHelper
) {

operator fun invoke(): List<Mock> = listOf(
fiveUsersMock,
userMockError,
tenUserMock
)
operator fun invoke() {
mockHelper.enqueue {
whenever("/image.png").thenReturnFromRawFile("demo_image")
whenever("/?results=5").thenReturnFromFile("user_list_success_1.json")
}
}

private val userMockError = mockHelper.getMockFromFile(
path = "/?results=5",
localJsonFile = "user_list_success_1.json",
method = Method.Get,
httpResponseCode = 500
)
private val fiveUsersMock = mockHelper.getMockFromFile(
path = "/?results=5",
localJsonFile = "user_list_success_1.json",
method = Method.Get,
delayInMillis = 3000
)

private val tenUserMock = mockHelper.getMockFromObject(
path = "/?results=10",
method = Method.Get,
dataObject = UserWrapperDto(
companion object {
val DEMO_LIST = UserWrapperDto(
results = listOf(
UserDto(
name = NameDto(
Expand Down Expand Up @@ -108,5 +95,5 @@ open class GetUserMocksUseCase @Inject constructor(
),
),
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.telefonica.mocks.ui.common

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Card
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.telefonica.mocks.ui.seconduserlist

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ buildscript {
compose_version = '1.2.0-beta01'
}
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:2.42"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.46.1"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'org.jetbrains.kotlin.android' version '1.8.21' apply false
id 'io.github.gradle-nexus.publish-plugin' version '1.1.0' apply false
}

Expand Down
8 changes: 4 additions & 4 deletions mock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 32
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -36,8 +36,8 @@ dependencies {
implementation "com.squareup.okhttp3:mockwebserver:4.9.3"
implementation "com.squareup.okhttp3:okhttp-tls:4.9.3"

implementation "com.google.dagger:dagger:2.44"
kapt "com.google.dagger:dagger-compiler:2.44"
implementation "com.google.dagger:dagger:2.46.1"
kapt "com.google.dagger:dagger-compiler:2.46.1"

implementation "com.squareup.retrofit2:converter-gson:2.9.0"

Expand Down
41 changes: 41 additions & 0 deletions mock/src/main/java/com/telefonica/mock/FileReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.telefonica.mock

import android.content.Context
import com.google.gson.Gson
import okio.Buffer
import java.io.BufferedReader
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import javax.inject.Inject

open class FileReader @Inject constructor(
private val context: Context,
) {

fun readJsonFile(jsonFilePath: String?): String? {
return jsonFilePath?.let {
var bufferedReader: BufferedReader? = null
try {
bufferedReader = BufferedReader(InputStreamReader(context.assets.open(jsonFilePath), StandardCharsets.UTF_8))
var line: String?
val text = StringBuilder()
do {
line = bufferedReader.readLine()
line?.let { text.append(line) }
} while (line != null)
bufferedReader.close()
text.toString()
} catch (e: Exception) {
e.printStackTrace()
null
} finally {
bufferedReader?.close()
}
}
}

fun readRawFile(fileName: String): Buffer {
val inputStream = context.resources.openRawResource(context.resources.getIdentifier(fileName, "raw", context.packageName))
return Buffer().readFrom(inputStream)
}
}
17 changes: 0 additions & 17 deletions mock/src/main/java/com/telefonica/mock/Mock.kt

This file was deleted.

Loading

0 comments on commit c5882e1

Please sign in to comment.