aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/androidTest/java/org/wordpress/android/mocks/RestClientCustomizableMock.java
blob: 6f066d6d1df56af37446300c3ee8b37e01b661bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.wordpress.android.mocks;

import android.content.Context;

import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.wordpress.rest.RestClient;
import com.wordpress.rest.RestRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.TestUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;

import java.io.IOException;
import java.io.InputStream;

public class RestClientCustomizableMock extends RestClient {
    private Context mContext;
    private String mPrefix;

    public void setContextAndPrefix(Context context, String prefix) {
        mContext = context;
        mPrefix = prefix;
    }

    public void setPrefix(String prefix) {
        mPrefix = prefix;
    }

    public void setContext(Context context) {
        mContext = context;
    }

    public RestClientCustomizableMock(com.android.volley.RequestQueue queue) {
        super(queue);
    }

    public RestClientCustomizableMock(com.android.volley.RequestQueue queue, String token) {
        super(queue, token, REST_API_ENDPOINT_URL_V1);
    }

    public String getAbsoluteURL(String url) {
        return null;
    }

    public String getAbsoluteURL(String path, java.util.Map<String, String> params) {
        return null;
    }

    public RestRequest get(String path, RestRequest.Listener listener, RestRequest.ErrorListener errorListener) {
        AppLog.v(T.TESTS, this.getClass() + ": get(" + path + ")");
        return new RestRequest(Method.GET, path, null, listener, errorListener);
    }

    public RestRequest post(String path, java.util.Map<String, String> body, RestRequest.Listener listener,
                            RestRequest.ErrorListener errorListener) {
        AppLog.v(T.TESTS, this.getClass() + ": post(" + path + ")");
        return new RestRequest(Method.POST, path, body, listener, errorListener);
    }

    private VolleyError forgeVolleyErrorFromFilename(String filename) {
        String strData = fileToString(filename);
        byte[] data = new byte[0];
        if (strData != null) {
            data = strData.getBytes();
        }
        NetworkResponse networkResponse = new NetworkResponse(400, data, null, false);
        VolleyError ve = new VolleyError(networkResponse);
        return ve;
    }

    private TimeoutError forgeVolleyTimeoutError() {
        TimeoutError te = new TimeoutError();
        return te;
    }

    private String fileToString(String filename) {
        try {
            InputStream is = mContext.getAssets().open(filename);
            String data = TestUtils.convertStreamToString(is);
            AppLog.v(T.TESTS, "file read:" + filename);
            return data;
        } catch (IOException e) {
            AppLog.e(T.TESTS, "can't read file: " + filename + " - " + e.toString());
        }
        return null;
    }

    public RestRequest makeRequest(int method, String url, java.util.Map<String, String> params,
                                   RestRequest.Listener listener, RestRequest.ErrorListener errorListener) {
        AppLog.v(T.TESTS, this.getClass() + ": makeRequest(" + url + ")");
        RestRequest dummyReturnValue = new RestRequest(method, url, params, listener, errorListener);
        // URL example: https://public-api.wordpress.com/rest/v1/me
        // Filename: default-public-api-wordpress-com-rest-v1-me.json
        String filename = mPrefix + "-" + url.replace("https://", "").replace("/", "-").replace(".", "-").replace("?",
                "-") + ".json";

        if ("password-invalid".equals(mPrefix) && errorListener != null) {
            errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
            return dummyReturnValue;
        }

        if ("username-exists".equals(mPrefix) && errorListener != null) {
            errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
            return dummyReturnValue;
        }

        if ("timeout".equals(mPrefix) && errorListener != null) {
            errorListener.onErrorResponse(forgeVolleyTimeoutError());
            return dummyReturnValue;
        }

        if ("site-reserved".equals(mPrefix) && errorListener != null) {
            errorListener.onErrorResponse(forgeVolleyErrorFromFilename(filename));
            return dummyReturnValue;
        }

        String data = fileToString(filename);
        if (data == null) {
            AppLog.e(T.TESTS, "Can't read file: " + filename);
            throw new RuntimeException("Can't read file: " + filename);
        }

        try {
            JSONObject jsonObj = new JSONObject(data);
            listener.onResponse(jsonObj);
        } catch (JSONException je) {
            AppLog.e(T.TESTS, je.toString());
        }
        return dummyReturnValue;
    }

    public RestRequest send(RestRequest request) {
        return request;
    }

    public void setUserAgent(String userAgent) {
    }

    public void setAccessToken(String token) {
    }

    public boolean isAuthenticated() {
        return true;
    }
}