@Test
  public void testReparent() throws Exception {
    _logger.info("Starting reparent CatalogService test");

    ModelClient modelClient = getModelClient();

    CatalogCategory parent1 = createCategoryWithLabel("Parent 1");
    modelClient.save(parent1);

    CatalogCategory parent2 = createCategoryWithLabel("Parent 2");
    modelClient.save(parent2);

    CatalogService service = createWithLabel("Service");
    setParent(parent1, service);
    modelClient.save(service);

    List<CatalogService> parent1Children =
        modelClient.catalogServices().findByCatalogCategory(parent1.getId());
    Assert.assertEquals(1, parent1Children.size());
    Assert.assertEquals(service.getId(), parent1Children.get(0).getId());

    List<CatalogService> parent2Children =
        modelClient.catalogServices().findByCatalogCategory(parent2.getId());
    Assert.assertEquals(0, parent2Children.size());

    setParent(parent2, service);
    modelClient.save(service);

    parent1Children = modelClient.catalogServices().findByCatalogCategory(parent1.getId());
    Assert.assertEquals(0, parent1Children.size());

    parent2Children = modelClient.catalogServices().findByCatalogCategory(parent2.getId());
    Assert.assertEquals(1, parent2Children.size());
    Assert.assertEquals(service.getId(), parent2Children.get(0).getId());
  }
 public CatalogService createCatalogService(
     ServiceDef serviceDef, CatalogCategory parentCategory) {
   CatalogBuilder builder = new CatalogBuilder(client, serviceDescriptors);
   NamedURI namedUri = new NamedURI(parentCategory.getId(), parentCategory.getLabel());
   CatalogService newService = builder.createService(serviceDef, namedUri);
   newService.setSortedIndex(null);
   client.save(newService);
   return newService;
 }
  @Test
  public void testFindByCatalogCategory() {

    _logger.info("Starting FindByCatalogCategory test");

    ModelClient modelClient = getModelClient();

    CatalogCategory root = createCategoryWithLabel("rooty");
    modelClient.save(root);

    CatalogService s1 = createWithLabel("s1");
    s1.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(s1);

    CatalogCategory c1 = createCategoryWithLabel("asdf");
    c1.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(c1);

    CatalogService s2 = createWithLabel("s2");
    s2.setCatalogCategoryId(new NamedURI(c1.getId(), c1.getLabel()));
    modelClient.save(s2);

    CatalogService s3 = createWithLabel("s3");
    s3.setCatalogCategoryId(new NamedURI(c1.getId(), c1.getLabel()));
    modelClient.save(s3);

    CatalogCategory c2 = createCategoryWithLabel("asdf2");
    c2.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(c2);

    CatalogService s4 = createWithLabel("s4");
    s4.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s4);

    CatalogService s5 = createWithLabel("s5");
    s5.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s5);

    CatalogService s6 = createWithLabel("s6");
    s6.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s6);

    List<CatalogService> results =
        modelClient.catalogServices().findByCatalogCategory(root.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(1, results.size());

    results = modelClient.catalogServices().findByCatalogCategory(c1.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(2, results.size());

    results = modelClient.catalogServices().findByCatalogCategory(c2.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(3, results.size());
  }
 private void setParent(CatalogCategory parent, CatalogService child) {
   child.setCatalogCategoryId(new NamedURI(parent.getId(), parent.getLabel()));
 }