summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap/bitmaskclient/testutils/matchers/BundleMatcher.java
blob: d2d2a102c02258e473bc0d9ed91457194292aba0 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * Copyright (c) 2018 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.testutils.matchers;

import android.os.Bundle;
import android.os.Parcelable;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by cyberta on 09.01.18.
 */

public class BundleMatcher extends BaseMatcher<Bundle> {

    private final HashMap<String, Integer> expectedIntegers;
    private final HashMap<String, String> expectedStrings;
    private final HashMap<String, Boolean> expectedBooleans;
    private final HashMap<String, Parcelable> expectedParcelables;
    private HashMap<String, Integer> unfoundExpectedInteger  = new HashMap<>();
    private HashMap<String, Boolean> unfoundExpectedBoolean = new HashMap<>();
    private HashMap<String, String> unfoundExpectedString = new HashMap<>();
    private HashMap<String, Parcelable> unfoundExpectedParcelable = new HashMap<>();
    private HashMap<String, Object> unexpectedAdditionalObjects = new HashMap<>();

    public BundleMatcher(HashMap<String, Integer> expectedIntegers, HashMap<String, String> expectedStrings, HashMap<String, Boolean> expectedBooleans, HashMap<String, Parcelable> expectedParcelables) {
        this.expectedBooleans = expectedBooleans;
        this.expectedIntegers = expectedIntegers;
        this.expectedStrings = expectedStrings;
        this.expectedParcelables = expectedParcelables;
    }

    @Override
    public boolean matches(Object item) {
        if (item instanceof Bundle) {
            Bundle actualBundle = (Bundle) item;
            return checkActualBundleHasAllExpectedBooleanValues(actualBundle) &&
                    checkActualBundleHasAllExpectedStringValues(actualBundle) &&
                    checkActualBundleHasAllExpectedIntValues(actualBundle) &&
                    checkActualBundleHasAllExpectedParcelableValues(actualBundle) &&
                    checkUnexpectedAdditionalValuesIn(actualBundle);
        }
        return false;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("Bundle didn't match expectation!");

        if (!unfoundExpectedInteger.isEmpty()) {
            Iterator<String> iterator = unfoundExpectedInteger.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (unfoundExpectedInteger.get(key) == null) {
                    description.appendText("\n unfound Integer in actual Bundle: ").appendValue(iterator.next());
                } else {
                    description.appendText("\n expected Integer for key " + key).appendValue(expectedIntegers.get(key)).
                            appendText("\n found Integer was: ").appendValue(unfoundExpectedInteger.get(key));
                }
            }
        }
        if (!unfoundExpectedBoolean.isEmpty()) {
            Iterator<String> iterator = unfoundExpectedBoolean.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (unfoundExpectedBoolean.get(key) == null) {
                    description.appendText("\n unfound Boolean in actual Bundle: ").appendValue(iterator.next());
                } else {
                    description.appendText("\n expected Boolean for key " + key).appendValue(expectedBooleans.get(key)).
                            appendText("\n found Boolean was: ").appendValue(unfoundExpectedBoolean.get(key));
                }
            }
        }
        if (!unfoundExpectedString.isEmpty()) {
            Iterator<String> iterator = unfoundExpectedString.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (unfoundExpectedString.get(key) == null) {
                    description.appendText("\n unfound String in actual Bundle: ").appendValue(iterator.next());
                } else {
                    description.appendText("\n expected String for key " + key).appendValue(expectedStrings.get(key)).
                            appendText("\n found String was: ").appendValue(unfoundExpectedString.get(key));
                }
            }
        }
        if (!unfoundExpectedParcelable.isEmpty()) {
            Iterator<String> iterator = unfoundExpectedInteger.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (unfoundExpectedParcelable.get(key) == null) {
                    description.appendText("\n unfound Parcelable in actual Bundle: ").appendValue(iterator.next());
                } else {
                    description.appendText("\n expected Parcelable or key " + key).appendValue(expectedParcelables.get(key)).
                            appendText("\n found Parcelable was: ").appendValue(unfoundExpectedParcelable.get(key));
                }
            }
        }

        if (!unexpectedAdditionalObjects.isEmpty()) {
            Iterator<String> iterator = unexpectedAdditionalObjects.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = unexpectedAdditionalObjects.get(key);
                if (value instanceof String) {
                    description.appendText("\n unexpected String found in actual Bundle: ").appendValue(key).appendText(", ").appendValue(value);
                } else if (value instanceof Boolean) {
                    description.appendText("\n unexpected Boolean found in actual Bundle: ").appendValue(key).appendText(", ").appendValue(value);
                } else if (value instanceof Integer) {
                    description.appendText("\n unexpected Integer found in actual Bundle: ").appendValue(key).appendText(", ").appendValue(value);
                } else if (value instanceof Parcelable) {
                    description.appendText("\n unexpected Parcelable found in actual Bundle: ").appendValue(key).appendText(", ").appendValue(value);
                } else {
                    description.appendText("\n unexpected Object found in actual Bundle: ").appendValue(key).appendText(", ").appendValue(value);
                }
            }
        }
    }

    private boolean checkActualBundleHasAllExpectedBooleanValues(Bundle actualBundle) {
        Set<String> booleanKeys = expectedBooleans.keySet();
        for (String key : booleanKeys) {
            Object valueObject = actualBundle.get(key);
            if (valueObject == null ||
                    !(valueObject instanceof Boolean) ||
                    valueObject != expectedBooleans.get(key)) {
                unfoundExpectedBoolean.put(key, (Boolean) valueObject);
                return false;
            }
        }
        return true;
    }

    private boolean checkActualBundleHasAllExpectedStringValues(Bundle actualBundle) {
        Set<String> stringKeys = expectedStrings.keySet();
        for (String key : stringKeys) {
            Object valueObject = actualBundle.get(key);
            if (valueObject == null ||
                    !(valueObject instanceof String) ||
                    !valueObject.equals(expectedStrings.get(key))) {
                unfoundExpectedString.put(key, (String) valueObject);
                return false;
            }
        }
        return true;
    }

    private boolean checkActualBundleHasAllExpectedIntValues(Bundle actualBundle) {
        Set<String> stringKeys = expectedIntegers.keySet();
        for (String key : stringKeys) {
            Object valueObject = actualBundle.get(key);
            if (valueObject == null ||
                    !(valueObject instanceof Integer) ||
                    ((Integer) valueObject).compareTo(expectedIntegers.get(key)) != 0) {
                unfoundExpectedInteger.put(key, (Integer) valueObject);
                return false;
            }
        }
        return true;
    }

    private boolean checkActualBundleHasAllExpectedParcelableValues(Bundle actualBundle) {
        Set<String> stringKeys = expectedParcelables.keySet();
        for (String key : stringKeys) {
            Object valueObject = actualBundle.get(key);
            if (valueObject == null ||
                    !(valueObject instanceof Parcelable) ||
                    !valueObject.equals(expectedParcelables.get(key))) {
                unfoundExpectedParcelable.put(key, (Parcelable) valueObject);
                return false;
            }
        }
        return true;
    }

    private boolean checkUnexpectedAdditionalValuesIn(Bundle actualBundle) {
        Set<String> keys = actualBundle.keySet();

        for (String key : keys) {
            if (!expectedStrings.containsKey(key) &&
                    !expectedIntegers.containsKey(key) &&
                    !expectedBooleans.containsKey(key) &&
                    !expectedParcelables.containsKey(key)
                    ) {
                unexpectedAdditionalObjects.put(key, actualBundle.getString(key));
            }
        }
        return unexpectedAdditionalObjects.isEmpty();
    }
}