|
15 | 15 | package com.google.firebase.heartbeatinfo;
|
16 | 16 |
|
17 | 17 | import static com.google.common.truth.Truth.assertThat;
|
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
18 | 19 | import static org.mockito.ArgumentMatchers.anyLong;
|
19 | 20 | import static org.mockito.ArgumentMatchers.anyString;
|
20 | 21 | import static org.mockito.Mockito.mock;
|
|
40 | 41 | import java.util.concurrent.ThreadPoolExecutor;
|
41 | 42 | import java.util.concurrent.TimeUnit;
|
42 | 43 | import java.util.zip.GZIPOutputStream;
|
43 |
| -import org.json.JSONArray; |
44 | 44 | import org.json.JSONException;
|
45 |
| -import org.json.JSONObject; |
46 | 45 | import org.junit.Before;
|
47 | 46 | import org.junit.Test;
|
48 | 47 | import org.junit.runner.RunWith;
|
@@ -115,13 +114,9 @@ public void generateHeartBeat_oneHeartBeat()
|
115 | 114 | heartBeatController
|
116 | 115 | .getHeartBeatsHeader()
|
117 | 116 | .addOnCompleteListener(executor, getOnCompleteListener);
|
118 |
| - String expected = |
119 |
| - Base64.getUrlEncoder() |
120 |
| - .withoutPadding() |
121 |
| - .encodeToString( |
122 |
| - compress( |
123 |
| - "{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-02-03]\"}],\"version\":\"2\"}") |
124 |
| - .getBytes()); |
| 117 | + String str = |
| 118 | + "{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-02-03\"]}],\"version\":\"2\"}"; |
| 119 | + String expected = compress(str); |
125 | 120 | assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
|
126 | 121 | }
|
127 | 122 |
|
@@ -162,10 +157,7 @@ public void firstOldThenNew_synchronizedCorrectly()
|
162 | 157 | DefaultHeartBeatController controller =
|
163 | 158 | new DefaultHeartBeatController(
|
164 | 159 | () -> heartBeatInfoStorage, logSources, executor, () -> publisher, context);
|
165 |
| - String emptyString = |
166 |
| - Base64.getUrlEncoder() |
167 |
| - .withoutPadding() |
168 |
| - .encodeToString(compress("{\"heartbeats\":[],\"version\":\"2\"}").getBytes()); |
| 160 | + String emptyString = compress("{\"heartbeats\":[],\"version\":\"2\"}"); |
169 | 161 | controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener);
|
170 | 162 | storeOnCompleteListener.await();
|
171 | 163 | int heartBeatCode = controller.getHeartBeatCode("test").getCode();
|
@@ -202,27 +194,32 @@ public void generateHeartBeat_twoHeartBeatsSameUserAgent()
|
202 | 194 | heartBeatController
|
203 | 195 | .getHeartBeatsHeader()
|
204 | 196 | .addOnCompleteListener(executor, getOnCompleteListener);
|
205 |
| - JSONObject output = new JSONObject(); |
206 |
| - JSONArray array = new JSONArray(); |
207 |
| - JSONObject obj = new JSONObject(); |
208 |
| - obj.put("agent", "test-agent"); |
209 |
| - obj.put("date", dateList); |
210 |
| - array.put(obj); |
211 |
| - output.put("heartbeats", array); |
212 |
| - output.put("version", "2"); |
213 |
| - String expected = |
214 |
| - Base64.getUrlEncoder() |
215 |
| - .withoutPadding() |
216 |
| - .encodeToString(compress(output.toString()).getBytes()); |
| 197 | + String str = |
| 198 | + "{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\",\"2015-03-01\"]}],\"version\":\"2\"}"; |
| 199 | + String expected = compress(str); |
217 | 200 | assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
|
218 | 201 | }
|
219 | 202 |
|
| 203 | + private static String base64Encode(byte[] input) { |
| 204 | + return Base64.getUrlEncoder().withoutPadding().encodeToString(input); |
| 205 | + } |
| 206 | + |
| 207 | + private static byte[] gzip(String input) { |
| 208 | + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
| 209 | + try { |
| 210 | + try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOutputStream)) { |
| 211 | + gzipOutputStream.write(input.getBytes(UTF_8)); |
| 212 | + } |
| 213 | + byte[] gzipped = byteOutputStream.toByteArray(); |
| 214 | + byteOutputStream.close(); |
| 215 | + return gzipped; |
| 216 | + } catch (IOException e) { |
| 217 | + return null; |
| 218 | + } |
| 219 | + } |
| 220 | + |
220 | 221 | private String compress(String str) throws IOException {
|
221 |
| - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
222 |
| - GZIPOutputStream gzip = new GZIPOutputStream(out); |
223 |
| - gzip.write(str.toString().getBytes()); |
224 |
| - gzip.close(); |
225 |
| - return out.toString("UTF-8"); |
| 222 | + return base64Encode(gzip(str)); |
226 | 223 | }
|
227 | 224 |
|
228 | 225 | @Config(sdk = 29)
|
@@ -250,13 +247,9 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
|
250 | 247 | heartBeatController
|
251 | 248 | .getHeartBeatsHeader()
|
252 | 249 | .addOnCompleteListener(executor, getOnCompleteListener);
|
253 |
| - String expected = |
254 |
| - Base64.getUrlEncoder() |
255 |
| - .withoutPadding() |
256 |
| - .encodeToString( |
257 |
| - compress( |
258 |
| - "{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-03-02]\"},{\"agent\":\"test-agent-1\",\"date\":\"[2015-03-03]\"}],\"version\":\"2\"}") |
259 |
| - .getBytes()); |
| 250 | + String str = |
| 251 | + "{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\"]},{\"agent\":\"test-agent-1\",\"dates\":[\"2015-03-03\"]}],\"version\":\"2\"}"; |
| 252 | + String expected = compress(str); |
260 | 253 | assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
|
261 | 254 | }
|
262 | 255 | }
|
0 commit comments