Android Open Source - android-test-demo Mock Api Service






From Project

Back to project page android-test-demo.

License

The source code is released under:

MIT License

If you think the Android project android-test-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.andydyer.androidtestdemo.api;
// ww w .ja  v  a  2s .com
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;

import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.Path;
import retrofit.mime.TypedByteArray;

/**
 * Implement ApiService interface to return test data from JSON files and avoid making network requests.
 */
public class MockApiService implements ApiService {

    private static final int HTTP_OK_STATUS = 200;

    private static final String EVENTS_RESPONSE_FILE = "events_response.json";

    @Override
    public void getEvents(@Path("organization") String organization, Callback<Events> callback) {
        try {
            String json = readFileFromAssets(EVENTS_RESPONSE_FILE);
            Response response = getMockResponse(HTTP_OK_STATUS, json);
            Events events = getMockResponseData(json, Events.class);
            callback.success(events, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String readFileFromAssets(String fileName) throws IOException {
        InputStream stream = getClass().getClassLoader().getResourceAsStream("assets/" + fileName);
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        return IOUtils.toString(reader);
    }

    private <T> T getMockResponseData(String json, Class<T> klass) {
        Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
        return gson.fromJson(json, klass);
    }

    private Response getMockResponse(int httpCode, String json) throws IOException {
        return new Response("url", httpCode, "", Collections.EMPTY_LIST,
                new TypedByteArray("application/json", json.getBytes()));
    }
}




Java Source Code List

org.andydyer.androidtestdemo.ApplicationTest.java
org.andydyer.androidtestdemo.DemoApplication.java
org.andydyer.androidtestdemo.LoginActivityTest.java
org.andydyer.androidtestdemo.MainActivityTest.java
org.andydyer.androidtestdemo.Modules.java
org.andydyer.androidtestdemo.Modules.java
org.andydyer.androidtestdemo.api.Actor.java
org.andydyer.androidtestdemo.api.ApiServiceModule.java
org.andydyer.androidtestdemo.api.ApiServiceTest.java
org.andydyer.androidtestdemo.api.ApiService.java
org.andydyer.androidtestdemo.api.AuthenticationService.java
org.andydyer.androidtestdemo.api.Event.java
org.andydyer.androidtestdemo.api.Events.java
org.andydyer.androidtestdemo.api.MockApiServiceModule.java
org.andydyer.androidtestdemo.api.MockApiService.java
org.andydyer.androidtestdemo.api.Repo.java
org.andydyer.androidtestdemo.ui.LoginActivity.java
org.andydyer.androidtestdemo.ui.MainActivity.java
org.andydyer.androidtestdemo.ui.WebViewActivity.java
org.andydyer.androidtestdemo.ui.fragments.EventListFragment.java
org.andydyer.androidtestdemo.ui.widgets.SimpleDividerItemDecoration.java