@Test
public void testHasCategory() throws Exception {

    final MetadataCategory cat1 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc));
    final MetadataCategory cat2 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc));
    final MetadataCategory cat3 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc));
    final MetadataCategory cat4 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc));
    final Metadata metadata = newMetadata(_inc);
    metadata.getCategories().add(cat1);
    metadata.getCategories().add(cat2);
    Metadata md1 = _repository.save(metadata);
    final Metadata metadata2 = newMetadata(_inc);
    metadata2.getCategories().add(cat1);
    metadata2.getCategories().add(cat3);
    Metadata md2 = _repository.save(metadata2);
    final Metadata metadata3 = newMetadata(_inc);
    metadata3.getCategories().add(cat2);
    Metadata md3 = _repository.save(metadata3);
    List<Metadata> found = _repository.findAll((Specification<Metadata>)hasCategory(cat1), SortUtils.createSort(Metadata_.id));
    assertEquals(2, found.size());
    assertEquals(md1.getId(), found.get(0).getId());
    assertEquals(md2.getId(), found.get(1).getId());
    found = _repository.findAll((Specification<Metadata>)hasCategory(cat2), SortUtils.createSort(Metadata_.id));
    assertEquals(2, found.size());
    assertEquals(md1.getId(), found.get(0).getId());
    assertEquals(md3.getId(), found.get(1).getId());
    found = _repository.findAll((Specification<Metadata>)hasCategory(cat3), SortUtils.createSort(Metadata_.id));
    assertEquals(1, found.size());
    assertEquals(md2.getId(), found.get(0).getId());
    found = _repository.findAll((Specification<Metadata>)hasCategory(cat4), SortUtils.createSort(Metadata_.id));
    assertEquals(0, found.size());
}