├── .gitignore ├── .idea ├── encodings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── .travis.yml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── car2go │ │ └── endpoint2mock2 │ │ ├── GithubApi.kt │ │ └── MainActivity.kt │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── endpoint2mock2-compiler ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── car2go │ └── endpoint2mock2 │ └── MockAnnotationProccessor.java ├── endpoint2mock2 ├── .gitignore ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── car2go │ │ └── endpoint2mock2 │ │ ├── BooleanFunction.java │ │ ├── MockableClient.java │ │ ├── MockedEndpoint.java │ │ ├── Registry.java │ │ └── ReplaceEndpointClient.java │ └── test │ └── java │ └── com │ └── car2go │ ├── endpoint2mock2 │ ├── MockableClientTest.java │ ├── RegistryTest.java │ └── ReplaceEndpointClientTest.java │ └── mock │ └── FakeRegistry.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── json-server ├── README.md ├── db.json ├── routes.json └── startServer.sh └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | *.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | 57 | # Mac 58 | .DS_Store 59 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | Android 36 | 37 | 38 | Android Lint 39 | 40 | 41 | CorrectnessLintAndroid 42 | 43 | 44 | General 45 | 46 | 47 | LintAndroid 48 | 49 | 50 | Maven 51 | 52 | 53 | 54 | 55 | Android 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 67 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - tools 6 | - platform-tools 7 | - build-tools-26.0.1 8 | - android-26 9 | - extra-android-m2repository 10 | - extra-android-support 11 | - extra-google-m2repository 12 | 13 | jdk: 14 | - oraclejdk8 15 | 16 | notifications: 17 | email: false 18 | 19 | script: ./gradlew build test 20 | 21 | cache: 22 | directories: 23 | - $HOME/.m2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 car2go 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Endpoint2mock2 2 | 3 | [![Build Status](https://travis-ci.org/car2go/Endpoint2mock2.svg?branch=master)](https://travis-ci.org/car2go/Endpoint2mock2) 4 | 5 | Sometimes when working with REST APIs there is a need to return some mock data. Maybe for demo purposes, or real endpoint is just not ready yet. Endpoint2mock2 aims to help with that by integrating with Retrofit2 and allowing you to easily redirect some (but not all!) of your requests to your mock server. 6 | 7 | Here is how it goes. 8 | 9 | ## Retrofit 1 10 | 11 | This library is working only with Retrofit 2. For Retrofit 1 use the other version of the library: https://github.com/car2go/Endpoint2mock 12 | 13 | ## Add it to your project 14 | 15 | ### Step one 16 | 17 | Add `jitpack.io` to the list of your repositories. 18 | 19 | ```groovy 20 | repositories { 21 | maven { url 'https://jitpack.io' } 22 | } 23 | ``` 24 | 25 | ### Step two 26 | 27 | Add dependencies. 28 | 29 | ```groovy 30 | compile 'com.github.car2go.Endpoint2mock2:endpoint2mock2:1.1.0' 31 | 32 | // If you use Kotlin 33 | kapt 'com.github.car2go.Endpoint2mock2:endpoint2mock2-compiler:1.1.0' 34 | 35 | // If you do not use Kotlin 36 | annotationProcessor 'com.github.car2go.Endpoint2mock2:endpoint2mock2-compiler:1.1.0' 37 | ``` 38 | 39 | ### Step three 40 | 41 | Add `@MockedEndpoint` annotation to your Retrofit interface. For the sake of example let's use Github API. 42 | 43 | ```kotlin 44 | interface GithubApi { 45 | 46 | /** 47 | * Will call mocked server if MockableClient is enabled, and call the real server when it is not enabled. 48 | */ 49 | @MockedEndpoint 50 | @GET("/users/car2go/repos") 51 | fun getCompanyRepositories(): Single> 52 | 53 | } 54 | ``` 55 | 56 | ### Step four 57 | 58 | Use `MockableClient` as client for your Retrofit builder. 59 | 60 | ```kotlin 61 | val client = MockableClient.baseUrl(mockServerUrl) // Assuming that you have something running at this URL 62 | .mockWhen { mockToggle.isChecked } // Default is true 63 | .build() 64 | 65 | val githubApi = Retrofit.Builder() 66 | .client(client) 67 | .baseUrl("https://api.github.com") 68 | .addConverterFactory(GsonConverterFactory.create()) 69 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 70 | .build() 71 | .create(GithubApi::class.java) 72 | ``` 73 | 74 | You can check [json-server](https://github.com/car2go/Endpoint2mock2/tree/master/json-server) folder for an example of mock server. 75 | 76 | And you are ready to go! 77 | 78 | ## Proguard 79 | 80 | Just one line. 81 | 82 | ``` 83 | -keep class com.car2go.mock.FakeRegistry { *; } 84 | ``` 85 | 86 | ## License 87 | 88 | ``` 89 | Copyright 2017 car2go group GmbH 90 | 91 | Released under the MIT license. 92 | 93 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 94 | 95 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 96 | 97 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 98 | ``` 99 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 26 7 | buildToolsVersion "26.0.1" 8 | defaultConfig { 9 | applicationId "car2go.com.endpoint2mock2" 10 | minSdkVersion 20 11 | targetSdkVersion 26 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | // This is a convenient trick to bind your application to the mock server running on your desktop machine. 17 | buildConfigField 'String', 'BUILD_HOST_ADDRESS', "\"${InetAddress.getLocalHost().hostAddress}:3000\"" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | // We use Kotlin in this example, but you can use it with Java as well 29 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 30 | 31 | // You can use it without RxJava as well, but we'll use it to simplify the example 32 | compile 'io.reactivex:rxandroid:1.2.1' 33 | compile 'io.reactivex:rxjava:1.2.1' 34 | 35 | compile project(':endpoint2mock2') 36 | kapt project(':endpoint2mock2-compiler') 37 | 38 | compile "com.squareup.retrofit2:retrofit:$retrofit_version" 39 | compile "com.squareup.retrofit2:adapter-rxjava:$retrofit_version" 40 | compile "com.squareup.retrofit2:converter-gson:$retrofit_version" 41 | 42 | 43 | compile 'com.android.support:appcompat-v7:26.1.0' 44 | } 45 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/sherifmakhlouf/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/car2go/endpoint2mock2/GithubApi.kt: -------------------------------------------------------------------------------- 1 | package com.car2go.endpoint2mock2 2 | 3 | import retrofit2.http.GET 4 | import retrofit2.http.Path 5 | import rx.Observable 6 | import rx.Single 7 | 8 | data class Repository(val name: String) 9 | 10 | /** 11 | * Loads repositories from GitHub. 12 | */ 13 | interface GithubApi { 14 | 15 | /** 16 | * @return [Observable] which emits list of repositories of car2go company. 17 | * 18 | * There is an annotation, so it can be mocked. 19 | */ 20 | @MockedEndpoint 21 | @GET("/users/{user}/repos") 22 | fun getRepositories(@Path("user") user: String): Single> 23 | } -------------------------------------------------------------------------------- /app/src/main/java/com/car2go/endpoint2mock2/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.car2go.endpoint2mock2 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import kotlinx.android.synthetic.main.activity_main.* 6 | import retrofit2.Retrofit 7 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory 8 | import retrofit2.converter.gson.GsonConverterFactory 9 | import rx.Subscription 10 | import rx.android.schedulers.AndroidSchedulers 11 | import rx.schedulers.Schedulers 12 | 13 | class MainActivity : AppCompatActivity() { 14 | 15 | private var subscription: Subscription? = null 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_main) 20 | 21 | val client = MockableClient.baseUrl("http://${BuildConfig.BUILD_HOST_ADDRESS}") // Assuming that you have something running at this URL 22 | .mockWhen { mockToggle.isChecked } 23 | .build() 24 | 25 | val githubApi = Retrofit.Builder() 26 | .client(client) 27 | .addConverterFactory(GsonConverterFactory.create()) 28 | .baseUrl("https://api.github.com") 29 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 30 | .build() 31 | .create(GithubApi::class.java) 32 | 33 | requestButton.setOnClickListener { 34 | subscription?.unsubscribe() 35 | 36 | subscription = githubApi.getRepositories("car2go") 37 | .subscribeOn(Schedulers.io()) 38 | .observeOn(AndroidSchedulers.mainThread()) 39 | .subscribe( 40 | { repositories -> 41 | result.text = repositories.toString() 42 | }, 43 | { result.text = it.toString() } 44 | ) 45 | } 46 | } 47 | 48 | override fun onPause() { 49 | super.onPause() 50 | 51 | subscription?.unsubscribe() 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 |