InstanceDeleter { public void delete(Long id) { Instance instance = instancesRepository.get(id); instancesRepository.delete(id); Form form = formsRepository.get(instance.getJrFormId(), instance.getJrVersion()); if (form != null && form.isDeleted()) { List<Instance> otherInstances = instancesRepository.getAllByJrFormIdAndJrVersionNotDeleted(form.getJrFormId(), form.getJrVersion()); if (otherInstances.isEmpty()) { formsRepository.delete(form.getId()); } } } InstanceDeleter(InstancesRepository instancesRepository, FormsRepository formsRepository); void delete(Long id);  }
@Test public void whenFormForInstanceIsSoftDeleted_andThereIsAnotherInstance_doesNotDeleteForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .deleted(true) .build() ); instancesRepository.save(buildInstance(1L, "1", "version").build()); instancesRepository.save(buildInstance(2L, "1", "version").build()); instanceDeleter.delete(2L); assertThat(formsRepository.getAll().size(), is(1)); }
@Test public void whenFormForInstanceIsSoftDeleted_andThereIsAnotherSoftDeletedInstance_deletesForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .deleted(true) .build() ); instancesRepository.save(new Instance.Builder() .id(1L) .jrFormId("1") .deletedDate(0L) .jrVersion("version") .build() ); instancesRepository.save(new Instance.Builder() .id(2L) .jrFormId("1") .jrVersion("version") .build() ); instanceDeleter.delete(2L); assertThat(formsRepository.getAll().size(), is(0)); }
@Test public void whenFormForInstanceIsSoftDeleted_andThereAreNoOtherInstances_deletesForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .deleted(true) .build() ); instancesRepository.save(new Instance.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .build() ); instanceDeleter.delete(1L); assertThat(formsRepository.getAll().isEmpty(), is(true)); }
@Test public void whenFormForInstanceIsSoftDeleted_andThereAreNoOtherInstancesForThisVersion_deletesForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("1") .deleted(true) .build() ); formsRepository.save(new Form.Builder() .id(2L) .jrFormId("1") .jrVersion("2") .deleted(true) .build() ); instancesRepository.save(new Instance.Builder() .id(1L) .jrFormId("1") .jrVersion("1") .build() ); instancesRepository.save(new Instance.Builder() .id(2L) .jrFormId("1") .jrVersion("2") .build() ); instanceDeleter.delete(1L); assertThat(formsRepository.getAll().size(), is(1)); assertThat(formsRepository.getAll().get(0).getJrVersion(), is("2")); }
@Test public void whenFormForInstanceIsNotSoftDeleted_andThereAreNoOtherInstances_doesNotDeleteForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .deleted(false) .build() ); instancesRepository.save(new Instance.Builder() .id(1L) .jrFormId("1") .jrVersion("version") .build() ); instanceDeleter.delete(1L); assertThat(formsRepository.getAll().size(), is(1)); }
@Test public void whenFormVersionForInstanceIsNotSoftDeleted_andThereAreNoOtherInstances_doesNotDeleteForm() { formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("1") .deleted(true) .build() ); formsRepository.save(new Form.Builder() .id(1L) .jrFormId("1") .jrVersion("2") .deleted(false) .build() ); instancesRepository.save(new Instance.Builder() .id(1L) .jrFormId("1") .jrVersion("2") .build() ); instanceDeleter.delete(1L); assertThat(formsRepository.getAll().size(), is(2)); }