### Question:
PersonHandler extends InMemoryResourceHandler implements Modifiable, Deletable { @Override public void update(ResourceSnapshot resource, DataSet content, WriteSession session) throws InconsistentContentException, UnknownResourceException { DataSet dataSet = get(resource); logDebug(resource, "Enforcing consistency..."); enforceConsistency(resource,content,dataSet); try { logDebug(resource,"Persisting new state:%n%s",content); add(resource.name(),content); session.modify(resource); session.saveChanges(); } catch (Exception e) { add(resource.name(),dataSet); logError(resource,e,"Something went wrong",e); throw new ApplicationRuntimeException("Update 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 testUpdate$happyPath() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data = withReadOnlyProperty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; session.modify(snapshot); }}; DataSet modified = withReadOnlyProperty(resourceName); sut.update(snapshot,modified,session); assertThat(sut.get(snapshot),sameInstance(modified)); }
@Test public void testUpdate$sessionFailure() 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.modify(snapshot); session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { sut.update(snapshot,empty(resourceName),session); fail("Should not modify 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)); } }
@Test public void testUpdate$validationFailure() throws Exception { PersonHandler sut = new PersonHandler(); final Name<String> resourceName = name("resource"); final DataSet data = withReadOnlyProperty(resourceName); sut.add(resourceName, data); new Expectations() {{ snapshot.name();result=resourceName; }}; try { sut.update(snapshot,empty(resourceName),session); fail("Should not modify if the session fails"); } catch (InconsistentContentException e) { assertThat(sut.get(snapshot),sameInstance(data)); } }