/** * {@inheritDoc}<br> * * Delete a person resource. */ @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) { // Recover if failed add(resource.name(),dataSet); throw new ApplicationRuntimeException("Deletion failed",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)); } }
@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)); } }
/** * {@inheritDoc} <br> * * Update a person resource. */ @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) { // Recover if failed add(resource.name(),dataSet); logError(resource,e,"Something went wrong",e); throw new ApplicationRuntimeException("Update failed",e); } }
@Test public void testCreate$exceptionPath() throws Exception { PersonContainerHandler containerHandler = new PersonContainerHandler(); Deencapsulation.setField(containerHandler,"id",new AtomicInteger()); PersonHandler resourceHandler=new PersonHandler(); final Name<String> containerName = name("container"); containerHandler.setHandler(resourceHandler); final DataSet data = empty(containerName); final Name<Integer> memberName = NamingScheme.getDefault().name(1); new Expectations() {{ memberSnapshot.name();result=memberName; session.saveChanges();result=new WriteSessionException("FAILURE"); }}; try { containerHandler.create(snapshot, data, session); fail("Should not create if the session fails"); } catch (ApplicationRuntimeException e) { assertThat(e.getCause(),instanceOf(WriteSessionException.class)); assertThat(e.getCause().getMessage(),equalTo("FAILURE")); try { resourceHandler.get(memberSnapshot); fail("Resource should not be available in the resource handler"); } catch (UnknownResourceException e1) { } } }
@Test public void testCreate$happyPath() throws Exception { PersonContainerHandler containerHandler = new PersonContainerHandler(); Deencapsulation.setField(containerHandler,"id",new AtomicInteger()); PersonHandler resourceHandler=new PersonHandler(); final Name<String> containerName = name("container"); containerHandler.setHandler(resourceHandler); final DataSet data = empty(containerName); final Name<Integer> memberName = NamingScheme.getDefault().name(1); new Expectations() {{ memberSnapshot.name();result=memberName; }}; containerHandler.create(snapshot, data, session); assertThat(resourceHandler.get(memberSnapshot),sameInstance(data)); }
@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$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)); } } }
@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) { } }