em.getTransaction().begin();
final String childId = "3";
Tag t3 = new Tag();
t3.setTagId("t3");
em.persist(t3);
ComponentSpec child = createComponentSpec(childId, "xyz", "deleted", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap());
ComponentSpec parent1 = createComponentSpec("1", "xyz", "parent1", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap());
ComponentSpec parent2 = createComponentSpec("2", "xyz", "parent2", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap());
ComponentSpec parent3 = createComponentSpec("p2", "xyz", "parent2", "123", "xyz", Collections.<Tag>singletonList(t3), Collections.<String,String>emptyMap());
List<ComponentSpec> parents = Arrays.asList(parent1,parent2);
for (ComponentSpec cs : parents) {
cs.getReferencedComponents().add(child);
}
parent3.getReferencedComponents().add(child);
em.persist(child);
em.persist(parent1);
em.persist(parent2);
em.persist(parent3);
em.getTransaction().commit();
em.clear();
int objVersion = em.find(ComponentSpec.class, "1").getObjVersion();
em.clear();
Assert.assertNotNull(em.find(Tag.class, "t3"));
AbstractComponent parent2Component = serviceImpl.getComponent("p2");
serviceImpl.delete(Collections.singleton(parent2Component));
Assert.assertNotNull(em.find(Tag.class, "t3"));
AbstractComponent deleted = serviceImpl.getComponent(child.getComponentId());
serviceImpl.delete(Collections.singleton(deleted));
Assert.assertNull(em.find(ComponentSpec.class, childId));
for (ComponentSpec cs: parents) {
ComponentSpec p = em.find(ComponentSpec.class, cs.getComponentId());
Assert.assertEquals(p.getObjVersion(), objVersion+1);
}
AbstractComponent unsavedObj = Mockito.mock(AbstractComponent.class);
Mockito.when(unsavedObj.getComponentId()).thenReturn("thisObjUnsaved");
serviceImpl.delete(Collections.singleton(unsavedObj));