### Question:
PersonHandler extends InMemoryResourceHandler implements Modifiable, Deletable { @Override public void delete(ResourceSnapshot resource, WriteSession session) throws UnknownResourceException { DataSet dataSet = get(resource); try { logDebug(resource,"Deleting state:%n%s",dataSet); remove(resource.name()); session.delete(resource); session.saveChanges(); } catch (WriteSessionException e) { add(resource.name(),dataSet); throw new ApplicationRuntimeException("Deletion failed",e); } } PersonHandler(); @Override void delete(ResourceSnapshot resource, WriteSession session); @Override void update(ResourceSnapshot resource, DataSet content, WriteSession session); static final String ID; static final String RELATIVES_ID; static final String RELATIVES_PATH; static final String ADDRESS_ID; static final String ADDRESS_PATH; static final URI READ_ONLY_PROPERTY; }### Answer:
@Test public void testDeletion$happyPath() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data=empty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; session.delete(snapshot); session.saveChanges(); }}; sut.delete(snapshot, session); try { assertThat(sut.get(snapshot),sameInstance(data)); fail("Should be deleted if the operation succeeds"); } catch (UnknownResourceException e) { } }
@Test public void testDeletion$fail() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data=empty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; session.delete(snapshot); session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { sut.delete(snapshot, session); fail("Should not delete if the session fails"); } catch (ApplicationRuntimeException e) { assertThat(e.getCause(),instanceOf(WriteSessionException.class)); assertThat(e.getCause().getMessage(),equalTo("FAILURE")); assertThat(sut.get(snapshot),sameInstance(data)); } }