use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity in project opennms by OpenNMS.
the class BusinessServiceDaoIT method verifyDeleteOnCascade.
/**
* If we do not explicitly delete the map or reduce function it should be deleted if not referenced anymore.
*/
@Test
@Transactional
public void verifyDeleteOnCascade() {
BusinessServiceEntity child2 = new BusinessServiceEntityBuilder().name("Child 2").reduceFunction(new HighestSeverityEntity()).addReductionKey("some-key", new IdentityEntity()).toEntity();
BusinessServiceEntity child1 = new BusinessServiceEntityBuilder().name("Child 1").reduceFunction(new HighestSeverityEntity()).addChildren(child2, new IdentityEntity()).toEntity();
BusinessServiceEntity parent = new BusinessServiceEntityBuilder().name("Parent Web Servers").addAttribute("dc", "RDU").addReductionKey("TestReductionKeyA", new IdentityEntity()).addReductionKey("TestReductionKeyB", new IdentityEntity()).addIpService(getMonitoredServiceFromNode1(), new IdentityEntity()).reduceFunction(m_highestSeverity).addChildren(child1, new IdentityEntity()).toEntity();
m_businessServiceDao.save(child2);
m_businessServiceDao.save(child1);
m_businessServiceDao.save(parent);
m_businessServiceDao.flush();
assertEquals(3, m_businessServiceDao.countAll());
assertEquals(3, m_reductionFunctionDao.countAll());
assertEquals(6, m_edgeDao.countAll());
// Deletion of child does not delete the edges referencing to that child
// remove all parent -> child associations manually
BusinessServiceChildEdgeEntity parentToChild1Edge = parent.getChildEdges().iterator().next();
parent.removeEdge(parentToChild1Edge);
m_edgeDao.delete(parentToChild1Edge);
// edges do not need to be deleted manually, deletes will be cascaded
m_businessServiceDao.delete(child1);
m_businessServiceDao.flush();
assertEquals(2, m_businessServiceDao.countAll());
assertEquals(2, m_reductionFunctionDao.countAll());
assertEquals(4, m_edgeDao.countAll());
// Deletion of parent should delete all references
m_businessServiceDao.delete(parent);
assertEquals(1, m_businessServiceDao.countAll());
assertEquals(1, m_reductionFunctionDao.countAll());
assertEquals(1, m_edgeDao.countAll());
// Deletion of Child 2 should also work
m_businessServiceDao.delete(child2);
assertEquals(0, m_businessServiceDao.countAll());
assertEquals(0, m_reductionFunctionDao.countAll());
assertEquals(0, m_edgeDao.countAll());
}
use of org.opennms.netmgt.bsm.persistence.api.BusinessServiceChildEdgeEntity in project opennms by OpenNMS.
the class BusinessServiceManagerImpl method deleteBusinessService.
@Override
public void deleteBusinessService(BusinessService businessService) {
BusinessServiceEntity entity = getBusinessServiceEntity(businessService);
// remove all parent -> child associations
for (BusinessServiceEntity parent : getDao().findParents(entity)) {
List<BusinessServiceChildEdgeEntity> collect = parent.getChildEdges().stream().filter(e -> entity.equals(e.getChild())).collect(Collectors.toList());
collect.forEach(x -> {
parent.removeEdge(x);
// we need to delete this edge manually as they cannot be deleted automatically
edgeDao.delete(x);
});
}
// edges of the entity are deleted automatically by hibernate
getDao().delete(entity);
}
Aggregations