@Test
public void testDeleteDeletesFromMetadata() throws Exception {
MetadataCategory cat1 = _repo.save(newMetadataCategory(_inc));
MetadataCategory cat2 = _repo.save(newMetadataCategory(_inc));
Metadata metadata1 = MetadataRepositoryTest.newMetadata(_inc);
metadata1.getCategories().add(cat1);
metadata1.getCategories().add(cat2);
metadata1 = _metadataRepo.save(metadata1);
Metadata metadata2 = MetadataRepositoryTest.newMetadata(_inc);
metadata2.getCategories().add(cat1);
metadata2 = _metadataRepo.save(metadata2);
_repo.deleteCategoryAndMetadataReferences(cat1.getId());
_entityManager.flush();
_entityManager.clear();
// org.fao.geonet.services.category.Remove assumes that this test passes. If this test can't pass
// then there needs to be a way to fix Remove as well.
final Set<MetadataCategory> foundCategories = _metadataRepo.findOne(metadata1.getId()).getCategories();
assertEquals(1, foundCategories.size());
assertEquals(cat2.getId(), foundCategories.iterator().next().getId());
assertEquals(0, _metadataRepo.findOne(metadata2.getId()).getCategories().size());
}