DeleteDataFunction implements DataFunction<List<?>> { @Override public <K, E> List<E> apply(DataStore<K, E> dataStore, QueryDescriptor query, RepositoryConfiguration repositoryConfiguration, List<E> selection) { if (dataStore == null) { log.error("Cannot delete entities when the data store is null"); throw new InvalidArgumentException("Data store cannot be null"); } if (query == null) { log.error("Cannot delete entities when the query is null"); throw new InvalidArgumentException("Query cannot be null"); } if (selection == null) { log.error("Cannot delete entities when the selection is null"); throw new InvalidArgumentException("Selection cannot be null"); } final String identifier = query.getRepositoryMetadata().getIdentifierProperty(); log.info("Using property " + identifier + " to delete the entities"); final List<E> deleted = new LinkedList<>(); for (E item : selection) { final Object key; try { key = PropertyUtils.getPropertyValue(item, identifier); } catch (Exception e) { log.error("The value of property " + identifier + " could not be read "); throw new DataFunctionException("Failed to read property value for property " + identifier, e); } if (key == null) { log.error("Cannot delete an entity when the identifier property has been set to null"); throw new DataFunctionException( "Cannot delete an entity without the key property being set: " + identifier); } if (dataStore.delete((K) key)) { deleted.add(item); } } log.error("Deleted " + deleted.size() + " entities as the result of the delete call"); return deleted; }  @Override List<E> apply(DataStore<K, E> dataStore, QueryDescriptor query,
                                RepositoryConfiguration repositoryConfiguration, List<E> selection);  }
@Test(expectedExceptions = InvalidArgumentException.class, expectedExceptionsMessageRegExp = "Data store .*") public void testPassingNullDataStore() { final DataFunction<List<?>> function = new DeleteDataFunction(); function.apply(null, query, null, new LinkedList<>()); }
@Test(expectedExceptions = InvalidArgumentException.class, expectedExceptionsMessageRegExp = "Query .*") public void testPassingNullQuery() { final DataFunction<List<?>> function = new DeleteDataFunction(); function.apply(dataStore, null, null, new LinkedList<>()); }
@Test(expectedExceptions = InvalidArgumentException.class, expectedExceptionsMessageRegExp = "Selection .*") public void testPassingNullSelection() { function.apply(dataStore, query, null, null); }
@Test(expectedExceptions = DataFunctionException.class, expectedExceptionsMessageRegExp = "Failed to read .*") public void testWrongIdentifierProperty() { final DataFunction<List<?>> function = new DeleteDataFunction(); final RepositoryMetadata repositoryMetadata = new ImmutableRepositoryMetadata(String.class, Person.class, null, "xyz"); final QueryDescriptor descriptor = new DefaultQueryDescriptor(false, null, 0, null, null, null, null, repositoryMetadata); function.apply(dataStore, descriptor, null, Collections.singletonList(new Person())); }
@Test(expectedExceptions = DataFunctionException.class, expectedExceptionsMessageRegExp = "Cannot delete an entity without the key property being set: id") public void testNullIdentifierValue() { final DataFunction<List<?>> function = new DeleteDataFunction(); function.apply(dataStore, query, null, Collections.singletonList(new Person())); }
@Test public void testThatItSendsRequestsToTheUnderlyingDataStore() { final List<Person> selection = new ArrayList<>(); selection.add(new Person().setId("1")); selection.add(new Person().setId("2")); function.apply(dataStore, query, null, selection); assertThat(dataStore.getRequests(), hasSize(2)); assertThat(dataStore.getRequests().get(0).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(0).getKey(), Matchers.is(selection.get(0).getId())); assertThat(dataStore.getRequests().get(1).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(1).getKey(), Matchers.is(selection.get(1).getId())); }
@Test public void testThatItDeletesTheExactSetOfItemsSpecified() { final List<Person> entities = new ArrayList<>(); entities.add(new Person().setId("1")); entities.add(new Person().setId("2")); entities.add(new Person().setId("3")); entities.add(new Person().setId("4")); for (Person person : entities) { dataStore.save(person.getId(), person); } dataStore.reset(); final List<Person> selection = entities.subList(1, 3); function.apply(dataStore, query, null, selection); assertThat(dataStore.getRequests(), hasSize(2)); assertThat(dataStore.getRequests().get(0).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(0).getKey(), Matchers.is(entities.get(1).getId())); assertThat(dataStore.getRequests().get(1).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(1).getKey(), Matchers.is(entities.get(2).getId())); assertThat(dataStore.hasKey(entities.get(0).getId()), is(true)); assertThat(dataStore.hasKey(entities.get(1).getId()), is(false)); assertThat(dataStore.hasKey(entities.get(2).getId()), is(false)); assertThat(dataStore.hasKey(entities.get(3).getId()), is(true)); }
@Test public void testThatItReturnsTheDeletedItems() { final List<Person> entities = new ArrayList<>(); entities.add(new Person().setId("1")); entities.add(new Person().setId("2")); entities.add(new Person().setId("3")); entities.add(new Person().setId("4")); for (Person person : entities) { dataStore.save(person.getId(), person); } dataStore.reset(); final List<Person> selection = new ArrayList<>(entities.subList(1, 3)); selection.add(new Person().setId("5")); final List<Person> deleted = function.apply(dataStore, query, null, selection); assertThat(dataStore.getRequests(), hasSize(3)); assertThat(dataStore.getRequests().get(0).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(0).getKey(), Matchers.is(entities.get(1).getId())); assertThat(dataStore.getRequests().get(1).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(1).getKey(), Matchers.is(entities.get(2).getId())); assertThat(dataStore.getRequests().get(2).getOperation(), is(Operation.DELETE)); assertThat(dataStore.getRequests().get(2).getKey(), Matchers.is(selection.get(2).getId())); assertThat(deleted, hasSize(2)); final List<String> deletedIds = new ArrayList<>( Arrays.asList(entities.get(1).getId(), entities.get(2).getId())); for (Person person : deleted) { assertThat(person.getId(), isIn(deletedIds)); deletedIds.remove(person.getId()); } assertThat(deletedIds, is(Collections.<String>emptyList())); }