Skip to content

Commit 0bcd9f7

Browse files
Merge pull request #2 from Husseinhj/main
Supported include and exclude queries
2 parents 7dd9cbd + 1efca59 commit 0bcd9f7

13 files changed

Lines changed: 287 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2.0.0
2+
- Support for include and exclude json path based on query params (Thanks to @[Husseinhj](https://github.com/Husseinhj))
3+
14
# 1.1.0
25
- Escape closing curly brace in regex to support in android
36
- Add support to retrofit HTTP annotation

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ interface Api {
6868
@Query("page") page: Int,
6969
@Query("limit") limit: Int
7070
): Call<List<Picsum>>
71+
72+
// if you want to filter through query params
73+
@Mock("picsum_recent.json", excludeQueries = ["type=favorites"])
74+
@GET("list")
75+
fun getRecentPicsums(
76+
@Query("type") type: String,
77+
@Query("page") page: Int,
78+
@Query("limit") limit: Int
79+
): Call<List<Picsum>>
80+
81+
// if you want to filter through query params
82+
@Mock("picsum_favorites.json", includeQueries = ["type=favorites"])
83+
@GET("list")
84+
fun getFavoritePicsums(
85+
@Query("type") type: String,
86+
@Query("page") page: Int,
87+
@Query("limit") limit: Int
88+
): Call<List<Picsum>>
7189
}
7290
```
7391

@@ -92,7 +110,7 @@ class RemoteDataSource(private val context: Context) {
92110
bodyFactory = { input -> context.resources.assets.open(input) }, // read asset file
93111
logger = { tag, message -> Log.d(tag, message) }, // pass logger to log events in logcat
94112
baseUrl = BASE_URL, // base url of your api
95-
requestPathToJsonMap = REQUEST_TO_JSON, // autogenerated constant, just press build button
113+
requestPathToMockPathRule = REQUEST_TO_JSON, // autogenerated constant, just press build button
96114
mockFilesPath = MOCK_FILES_PATH, // path to json files
97115
mockFitEnable = true, // master setting to enable or disable mocking
98116
apiEnableMock = true, // enable or disable mock when there are includes and excludes configs

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
implementation "com.squareup.retrofit2:retrofit:$versions.retrofit"
4040
implementation "com.squareup.retrofit2:converter-gson:$versions.retrofit"
4141
implementation "com.github.bumptech.glide:glide:$versions.glide"
42-
42+
4343
testImplementation "junit:junit:$versions.junit"
4444
androidTestImplementation "androidx.test.espresso:espresso-core:$versions.espresso"
4545

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
[
2+
{
3+
"id": "1016",
4+
"author": "Andrew Ridley",
5+
"width": 3844,
6+
"height": 2563,
7+
"url": "https://unsplash.com/photos/_h7aBovKia4",
8+
"download_url": "https://picsum.photos/id/1018/3914/2935"
9+
},
10+
{
11+
"id": "1018",
12+
"author": "Andrew Ridley",
13+
"width": 3914,
14+
"height": 2935,
15+
"url": "https://unsplash.com/photos/Kt5hRENuotI",
16+
"download_url": "https://picsum.photos/id/1018/3914/2935"
17+
},
18+
{
19+
"id": "1019",
20+
"author": "Patrick Fore",
21+
"width": 5472,
22+
"height": 3648,
23+
"url": "https://unsplash.com/photos/V6s1cmE39XM",
24+
"download_url": "https://picsum.photos/id/1019/5472/3648"
25+
},
26+
{
27+
"id": "102",
28+
"author": "Ben Moore",
29+
"width": 4320,
30+
"height": 3240,
31+
"url": "https://unsplash.com/photos/pJILiyPdrXI",
32+
"download_url": "https://picsum.photos/id/102/4320/3240"
33+
},
34+
{
35+
"id": "1020",
36+
"author": "Adam Willoughby-Knox",
37+
"width": 4288,
38+
"height": 2848,
39+
"url": "https://unsplash.com/photos/_snqARKTgoc",
40+
"download_url": "https://picsum.photos/id/1020/4288/2848"
41+
},
42+
{
43+
"id": "1022",
44+
"author": "Vashishtha Jogi",
45+
"width": 6000,
46+
"height": 3376,
47+
"url": "https://unsplash.com/photos/bClr95glx6k",
48+
"download_url": "https://picsum.photos/id/1022/6000/3376"
49+
},
50+
{
51+
"id": "1023",
52+
"author": "William Hook",
53+
"width": 3955,
54+
"height": 2094,
55+
"url": "https://unsplash.com/photos/93Ep1dhTd2s",
56+
"download_url": "https://picsum.photos/id/1023/3955/2094"
57+
},
58+
{
59+
"id": "1024",
60+
"author": "Мартин Тасев",
61+
"width": 1920,
62+
"height": 1280,
63+
"url": "https://unsplash.com/photos/7ALI0RYyq6s",
64+
"download_url": "https://picsum.photos/id/1024/1920/1280"
65+
},
66+
{
67+
"id": "1025",
68+
"author": "Matthew Wiebe",
69+
"width": 4951,
70+
"height": 3301,
71+
"url": "https://unsplash.com/photos/U5rMrSI7Pn4",
72+
"download_url": "https://picsum.photos/id/1025/4951/3301"
73+
},
74+
{
75+
"id": "1018",
76+
"author": "Andrew Ridley",
77+
"width": 3914,
78+
"height": 2935,
79+
"url": "https://unsplash.com/photos/Kt5hRENuotI",
80+
"download_url": "https://picsum.photos/id/1018/3914/2935"
81+
},
82+
{
83+
"id": "1019",
84+
"author": "Patrick Fore",
85+
"width": 5472,
86+
"height": 3648,
87+
"url": "https://unsplash.com/photos/V6s1cmE39XM",
88+
"download_url": "https://picsum.photos/id/1019/5472/3648"
89+
},
90+
{
91+
"id": "102",
92+
"author": "Ben Moore",
93+
"width": 4320,
94+
"height": 3240,
95+
"url": "https://unsplash.com/photos/pJILiyPdrXI",
96+
"download_url": "https://picsum.photos/id/102/4320/3240"
97+
},
98+
{
99+
"id": "1020",
100+
"author": "Adam Willoughby-Knox",
101+
"width": 4288,
102+
"height": 2848,
103+
"url": "https://unsplash.com/photos/_snqARKTgoc",
104+
"download_url": "https://picsum.photos/id/1020/4288/2848"
105+
},
106+
{
107+
"id": "1021",
108+
"author": "Frances Gunn",
109+
"width": 2048,
110+
"height": 1206,
111+
"url": "https://unsplash.com/photos/8BmNurlVR6M",
112+
"download_url": "https://picsum.photos/id/1021/2048/1206"
113+
},
114+
{
115+
"id": "1022",
116+
"author": "Vashishtha Jogi",
117+
"width": 6000,
118+
"height": 3376,
119+
"url": "https://unsplash.com/photos/bClr95glx6k",
120+
"download_url": "https://picsum.photos/id/1022/6000/3376"
121+
},
122+
{
123+
"id": "1023",
124+
"author": "William Hook",
125+
"width": 3955,
126+
"height": 2094,
127+
"url": "https://unsplash.com/photos/93Ep1dhTd2s",
128+
"download_url": "https://picsum.photos/id/1023/3955/2094"
129+
},
130+
{
131+
"id": "1024",
132+
"author": "Мартин Тасев",
133+
"width": 1920,
134+
"height": 1280,
135+
"url": "https://unsplash.com/photos/7ALI0RYyq6s",
136+
"download_url": "https://picsum.photos/id/1024/1920/1280"
137+
},
138+
{
139+
"id": "1025",
140+
"author": "Matthew Wiebe",
141+
"width": 4951,
142+
"height": 3301,
143+
"url": "https://unsplash.com/photos/U5rMrSI7Pn4",
144+
"download_url": "https://picsum.photos/id/1025/4951/3301"
145+
}
146+
]

app/src/main/java/ir/logicbase/mockfit/app/Api.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import retrofit2.http.Query
99

1010
interface Api {
1111

12-
@Mock("picsum_list.json")
12+
@Mock("picsum_list.json", excludeQueries = ["type={#}"])
1313
@GET("list")
1414
fun getListOfPicsums(
1515
@Query("page") page: Int,
1616
@Query("limit") limit: Int
1717
): Call<List<Picsum>>
18+
19+
@Mock("picsum_favorites.json", includeQueries = ["type=favorites"])
20+
@GET("list")
21+
fun getFavoritePicsums(
22+
@Query("type") type: String,
23+
@Query("page") page: Int,
24+
@Query("limit") limit: Int
25+
): Call<List<Picsum>>
1826
}

app/src/main/java/ir/logicbase/mockfit/app/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MainActivity : AppCompatActivity() {
4545
currentImageIndex = 0
4646
}
4747
executors.networkIO().execute {
48-
dataSource.api().getListOfPicsums(2, 20).enqueue(object : Callback<List<Picsum>> {
48+
dataSource.api().getFavoritePicsums("favorites", 2, 20).enqueue(object : Callback<List<Picsum>> {
4949
override fun onResponse(call: Call<List<Picsum>>, response: Response<List<Picsum>>) {
5050
loadingApi = false
5151
executors.mainThread().execute {

app/src/main/java/ir/logicbase/mockfit/app/RemoteDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RemoteDataSource(private val context: Context, var mockFitEnable: Boolean
3030
bodyFactory = { input -> context.resources.assets.open(input) }, // read asset file
3131
logger = { tag, message -> Log.d(tag, message) }, // pass logger to log events in logcat
3232
baseUrl = BASE_URL, // base url of your api
33-
requestPathToJsonMap = REQUEST_TO_JSON, // autogenerated constant, just press build button
33+
requestPathToMockPathRule = REQUEST_TO_JSON, // autogenerated constant, just press build button
3434
mockFilesPath = MOCK_FILES_PATH, // path to json files
3535
mockFitEnable = mockFitEnable, // master setting to enable or disable mocking
3636
apiEnableMock = true, // enable or disable mock when there are includes and excludes configs

common/src/main/java/ir/logicbase/mockfit/Mock.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ package ir.logicbase.mockfit
44
* Annotation meta to mark retrofit endpoints
55
*
66
* @property response path to mock file
7+
* @property includeQueries are the queries to separating same paths with different responses based on query values.
8+
* @property excludeQueries are the queries to separating same paths with different responses based on query values.
9+
* For example: ```["limit={#}"]``` or ```["limit"]``` for just including keys not value. For separate them with value you have to pass value as well like ```["limit=20"]```
710
*/
811
@MustBeDocumented
912
@Target(AnnotationTarget.FUNCTION)
1013
@Retention(AnnotationRetention.SOURCE)
1114
public annotation class Mock(
12-
val response: String
15+
val response: String,
16+
val includeQueries: Array<String> = [],
17+
val excludeQueries: Array<String> = [],
1318
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ir.logicbase.mockfit
2+
3+
public data class MockPathRule(
4+
val method: String,
5+
val route: String,
6+
val responseFile: String,
7+
val includeQueries: Array<String> = arrayOf(),
8+
val excludeQueries: Array<String> = arrayOf(),
9+
) {
10+
override fun equals(other: Any?): Boolean {
11+
if (this === other) return true
12+
if (javaClass != other?.javaClass) return false
13+
14+
other as MockPathRule
15+
16+
if (method != other.method) return false
17+
if (route != other.route) return false
18+
if (responseFile != other.responseFile) return false
19+
if (!includeQueries.contentEquals(other.includeQueries)) return false
20+
if (!excludeQueries.contentEquals(other.excludeQueries)) return false
21+
22+
return true
23+
}
24+
25+
override fun hashCode(): Int {
26+
var result = method.hashCode()
27+
result = 31 * result + route.hashCode()
28+
result = 31 * result + responseFile.hashCode()
29+
result = 31 * result + includeQueries.contentHashCode()
30+
result = 31 * result + excludeQueries.contentHashCode()
31+
return result
32+
}
33+
}

common/src/main/java/ir/logicbase/mockfit/StringExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ package ir.logicbase.mockfit
1010
public fun String.removeQueryParams(): String = this.replace("\\?.*".toRegex(), "")
1111

1212
/**
13-
* Replace resource path from given url, for example in this url : `users/2/form`
13+
* Replace resource path from given url, for example in this url : `users/2/form` or `user/cb7d8a83-0b25-4cca-911b-49a1974f1193/form`
1414
*
1515
* result will be `users/{#}/form`
1616
*/
17-
public fun String.replaceUrlDynamicPath(): String = this.replace("\\b\\d+((,\\d)+)?".toRegex(), "{#}")
17+
public fun String.replaceUrlDynamicPath(): String = this.replace("[\\da-f]{8}-[\\da-f]{4}-4[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}|\\b\\d+((,\\d)+)?".toRegex(), "{#}")
1818

1919
/**
2020
* Replace resource path from given url, for example in this url : `users/{id}/form`

0 commit comments

Comments
 (0)