PendingAttributeMutationStore extends JsonDataStoreQueue<List<AttributeMutation>> { void collapseAndSaveMutations() { apply(new Function<List<List<AttributeMutation>>, List<List<AttributeMutation>>>() { @Override public List<List<AttributeMutation>> apply(List<List<AttributeMutation>> input) { List<AttributeMutation> combined = new ArrayList<>(); for (List<AttributeMutation> mutations : input) { combined.addAll(mutations); } if (combined.isEmpty()) { return Collections.emptyList(); } return Collections.singletonList(AttributeMutation.collapseMutations(combined)); } }); } PendingAttributeMutationStore(PreferenceDataStore dataStore, String storeKey);   }
@Test public void testRemoveTwoCollapseAndSave() { List<AttributeMutation> mutations = new ArrayList<>(); mutations.add(AttributeMutation.newRemoveAttributeMutation("expected_key", 0)); mutations.add(AttributeMutation.newRemoveAttributeMutation("expected_key2", 0)); store.add(mutations); store.collapseAndSaveMutations(); String expectedResult = "[{\"action\":\"remove\",\"key\":\"expected_key\",\"timestamp\":\"1970-01-01T00:00:00\"},"+ "{\"action\":\"remove\",\"key\":\"expected_key2\",\"timestamp\":\"1970-01-01T00:00:00\"}]"; assertEquals(expectedResult, JsonValue.wrapOpt(store.peek()).toString()); }
@Test public void testAddTwoCollapseAndSave() { List<AttributeMutation> mutations = new ArrayList<>(); mutations.add(AttributeMutation.newSetAttributeMutation("expected_key", JsonValue.wrapOpt("expected_value"), 0)); mutations.add(AttributeMutation.newSetAttributeMutation("expected_key2", JsonValue.wrapOpt("expected_value2"), 0)); mutations.add(AttributeMutation.newRemoveAttributeMutation("expected_key2", 0)); String expectedResult = "[{\"action\":\"set\",\"value\":\"expected_value\",\"key\":\"expected_key\",\"timestamp\":\"1970-01-01T00:00:00\"},"+ "{\"action\":\"remove\",\"key\":\"expected_key2\",\"timestamp\":\"1970-01-01T00:00:00\"}]"; store.add(mutations); store.collapseAndSaveMutations(); assertEquals(expectedResult, JsonValue.wrapOpt(store.peek()).toString()); }
@Test public void testAddTwoRemoveOneCollapseAndSave() { List<AttributeMutation> mutations = new ArrayList<>(); mutations.add(AttributeMutation.newSetAttributeMutation("expected_key", JsonValue.wrapOpt("expected_value"), 999999999)); mutations.add(AttributeMutation.newSetAttributeMutation("expected_key2", JsonValue.wrapOpt("expected_value2"), 999999999)); mutations.add(AttributeMutation.newRemoveAttributeMutation("expected_key2", 999999999)); String expectedResult = "[{\"action\":\"set\",\"value\":\"expected_value\",\"key\":\"expected_key\",\"timestamp\":\"1970-01-12T13:46:39\"},"+ "{\"action\":\"remove\",\"key\":\"expected_key2\",\"timestamp\":\"1970-01-12T13:46:39\"}]"; store.add(mutations); store.collapseAndSaveMutations(); assertEquals(expectedResult, JsonValue.wrapOpt(store.peek()).toString()); }