|
59 | 59 | import java.util.Arrays; |
60 | 60 | import java.util.HashMap; |
61 | 61 | import java.util.HashSet; |
| 62 | +import java.util.Iterator; |
62 | 63 | import java.util.List; |
63 | 64 | import java.util.Locale; |
64 | 65 | import java.util.Map; |
@@ -1333,4 +1334,125 @@ public void testParseFeatureFlags() throws JSONException { |
1333 | 1334 | Set<String> parsedFeatureFlags = Leanplum.parseFeatureFlags(response); |
1334 | 1335 | assertEquals(new HashSet<>(Arrays.asList("test")), parsedFeatureFlags); |
1335 | 1336 | } |
| 1337 | + |
| 1338 | + /** |
| 1339 | + * Responses without uuid should be matched on index. |
| 1340 | + */ |
| 1341 | + @Test |
| 1342 | + public void testParseLastStartResponseWithoutUUID() { |
| 1343 | + final String index = "index"; |
| 1344 | + |
| 1345 | + List<Map<String, Object>> requests = new ArrayList<>(startRequestsWithCount(2)); |
| 1346 | + requests.addAll(trackRequestsWithCount(2)); |
| 1347 | + requests.addAll(startRequestsWithCount(3)); |
| 1348 | + requests.addAll(trackRequestsWithCount(3)); |
| 1349 | + requests.addAll(startRequestsWithCount(4)); |
| 1350 | + |
| 1351 | + List<JSONObject> responsesList = new ArrayList<>(); |
| 1352 | + for (int i=0;i < 15; i ++) { |
| 1353 | + Map<String, Object> responseMap = new HashMap<>(); |
| 1354 | + responseMap.put(index, Integer.toString(i)); |
| 1355 | + responsesList.add(new JSONObject(responseMap)); |
| 1356 | + } |
| 1357 | + |
| 1358 | + Map<String, Object> responsesMap = new HashMap<>(); |
| 1359 | + responsesMap.put("response", new JSONArray(responsesList)); |
| 1360 | + JSONObject response = new JSONObject(responsesMap); |
| 1361 | + |
| 1362 | + JSONObject lastStartResponse = Leanplum.parseLastStartResponse(response, requests); |
| 1363 | + assertNotNull(lastStartResponse); |
| 1364 | + lastStartResponse.optString(index).equals("13"); |
| 1365 | + } |
| 1366 | + |
| 1367 | + /** |
| 1368 | + * Empty response should return null for parseLastStartResponse |
| 1369 | + */ |
| 1370 | + @Test |
| 1371 | + public void testParseLastStartResponseEmpty() { |
| 1372 | + JSONObject response = new JSONObject(); |
| 1373 | + List<Map<String, Object>> requests = new ArrayList<>(); |
| 1374 | + |
| 1375 | + JSONObject lastStartResponse = Leanplum.parseLastStartResponse(response, requests); |
| 1376 | + assertNull(lastStartResponse); |
| 1377 | + } |
| 1378 | + |
| 1379 | + /** |
| 1380 | + * Single response should return response for parseLastStartResponse |
| 1381 | + */ |
| 1382 | + @Test |
| 1383 | + public void testParseLastStartResponseGivenSingleStartShouldReturnResponse() throws JSONException { |
| 1384 | + List<Map<String, Object>> requests = startRequestsWithCount(1); |
| 1385 | + |
| 1386 | + List<JSONObject> responsesList = startResponsesWithCount(1); |
| 1387 | + |
| 1388 | + Map<String, Object> responsesMap = new HashMap<>(); |
| 1389 | + responsesMap.put("response", new JSONArray(responsesList)); |
| 1390 | + JSONObject response = new JSONObject(responsesMap); |
| 1391 | + |
| 1392 | + |
| 1393 | + JSONObject lastStartResponse = Leanplum.parseLastStartResponse(response, requests); |
| 1394 | + assertNotNull(lastStartResponse); |
| 1395 | + lastStartResponse.optString("uuid").equals("start-uuid-0"); |
| 1396 | + } |
| 1397 | + |
| 1398 | + /** |
| 1399 | + * Multiple responses should return correct response for parseLastStartResponse |
| 1400 | + */ |
| 1401 | + @Test |
| 1402 | + public void testParseLastStartResponseGivenMultipleStartShouldReturnResponse() throws JSONException { |
| 1403 | + List<Map<String, Object>> requests = new ArrayList<>(startRequestsWithCount(2)); |
| 1404 | + requests.addAll(trackRequestsWithCount(2)); |
| 1405 | + requests.addAll(startRequestsWithCount(3)); |
| 1406 | + requests.addAll(trackRequestsWithCount(3)); |
| 1407 | + requests.addAll(startRequestsWithCount(4)); |
| 1408 | + |
| 1409 | + List<JSONObject> responsesList = startResponsesWithCount(2); |
| 1410 | + responsesList.addAll(trackResponsesWithCount(3)); |
| 1411 | + responsesList.addAll(startResponsesWithCount(4)); |
| 1412 | + responsesList.addAll(trackResponsesWithCount(3)); |
| 1413 | + |
| 1414 | + Map<String, Object> responsesMap = new HashMap<>(); |
| 1415 | + responsesMap.put("response", new JSONArray(responsesList)); |
| 1416 | + JSONObject response = new JSONObject(responsesMap); |
| 1417 | + |
| 1418 | + JSONObject lastStartResponse = Leanplum.parseLastStartResponse(response, requests); |
| 1419 | + assertNotNull(lastStartResponse); |
| 1420 | + lastStartResponse.optString("uuid").equals("start-uuid-3"); |
| 1421 | + } |
| 1422 | + |
| 1423 | + private List<Map<String, Object>> startRequestsWithCount(int n) { |
| 1424 | + return requestsWithCountAndPrefix(n, "start"); } |
| 1425 | + |
| 1426 | + private List<Map<String, Object>> trackRequestsWithCount(int n) { |
| 1427 | + return requestsWithCountAndPrefix(n, "track"); |
| 1428 | + } |
| 1429 | + |
| 1430 | + private List<Map<String, Object>> requestsWithCountAndPrefix(int n, String prefix) { |
| 1431 | + List<Map<String, Object>> requests = new ArrayList<>(); |
| 1432 | + for (int i=0;i < n; i ++) { |
| 1433 | + Map<String, Object> request = new HashMap<>(); |
| 1434 | + request.put(Request.REQUEST_ID_KEY, prefix + "-uuid-" + Integer.toString(i)); |
| 1435 | + request.put(Constants.Params.ACTION, Constants.Methods.START); |
| 1436 | + requests.add(request); |
| 1437 | + } |
| 1438 | + return requests; |
| 1439 | + } |
| 1440 | + |
| 1441 | + private List<JSONObject> startResponsesWithCount(int n) { |
| 1442 | + return responsesWithCountAndPrefix(n, "start"); |
| 1443 | + } |
| 1444 | + |
| 1445 | + private List<JSONObject> trackResponsesWithCount(int n) { |
| 1446 | + return responsesWithCountAndPrefix(n, "track"); |
| 1447 | + } |
| 1448 | + |
| 1449 | + private List<JSONObject> responsesWithCountAndPrefix(int n, String prefix) { |
| 1450 | + List<JSONObject> responsesList = new ArrayList<>(); |
| 1451 | + for (int i=0;i < n; i ++) { |
| 1452 | + Map<String, Object> responseMap = new HashMap<>(); |
| 1453 | + responseMap.put(Request.REQUEST_ID_KEY, prefix + "-uuid-" + Integer.toString(i)); |
| 1454 | + responsesList.add(new JSONObject(responseMap)); |
| 1455 | + } |
| 1456 | + return responsesList; |
| 1457 | + } |
1336 | 1458 | } |
0 commit comments