@Test
  public void testHasWriteCollectionRight() {
    final UserModel otherUser = userService.getUserForUID("other");
    final UserModel abrode = userService.getUserForUID("abrode");
    final UserModel deJol = userService.getUserForUID("deJol");

    final List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(deJol);

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(2);

    CockpitObjectAbstractCollectionModel writableCollection = null;
    for (final CockpitObjectAbstractCollectionModel collection : collections) {
      if ("writable".equals(collection.getQualifier())) {
        writableCollection = collection;
        break;
      }
    }

    boolean hasRight =
        cockpitCollectionService.hasWriteCollectionRight(otherUser, writableCollection);
    assertThat(hasRight).isFalse();

    hasRight = cockpitCollectionService.hasWriteCollectionRight(abrode, writableCollection);
    assertThat(hasRight).isTrue();

    hasRight = cockpitCollectionService.hasWriteCollectionRight(deJol, writableCollection);
    assertThat(hasRight).isTrue();
  }
  @Test
  public void testGetElements() {
    final UserModel user = userService.getUserForUID("abrode");
    final List<CockpitObjectSpecialCollectionModel> collections =
        cockpitCollectionService.getSpecialCollections(user, "quickcollection");
    CockpitObjectSpecialCollectionModel threeElementsCollection = null;
    for (final CockpitObjectSpecialCollectionModel collection : collections) {
      if ("testSpecialB".equals(collection.getQualifier())) {
        threeElementsCollection = collection;
        break;
      }
    }

    assertThat(threeElementsCollection).isNotNull();

    List<ItemModel> elements =
        cockpitCollectionService.getElements(threeElementsCollection, 0, 100);

    assertThat(elements.size()).isEqualTo(3);
    assertThat(elements.get(0) instanceof ProductModel).isTrue();

    elements = cockpitCollectionService.getElements(threeElementsCollection, 0, 2);

    assertThat(elements.size()).isEqualTo(2);
  }
  @Test
  public void testAddToCollection() {
    final UserModel user = userService.getUserForUID("ahertz");
    List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(user);
    CockpitObjectAbstractCollectionModel collection = null;
    for (final CockpitObjectAbstractCollectionModel coll : collections) {
      if ("testA".equals(coll.getQualifier())) {
        collection = coll;
        break;
      }
    }

    assertThat(collection.getElements().size()).isEqualTo(2);

    final CatalogVersionModel catalogVersion =
        catalogService.getCatalogVersion("testCatalog", "Online");
    final List<ItemModel> elements = new ArrayList<ItemModel>();
    elements.add(productService.getProductForCode(catalogVersion, "testProduct4"));

    final int n = cockpitCollectionService.addToCollection(collection, elements);

    collections = cockpitCollectionService.getCollectionsForUser(user);
    CockpitObjectAbstractCollectionModel changedCollection = null;
    for (final CockpitObjectAbstractCollectionModel coll : collections) {
      if ("testA".equals(coll.getQualifier())) {
        changedCollection = coll;
        break;
      }
    }

    assertThat(changedCollection.getElements().size()).isEqualTo(3);
    assertThat(n).isEqualTo(1);
  }
  @Test
  public void testRemoveFromCollection() {
    final UserModel user = userService.getUserForUID("ahertz");
    List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(user);
    CockpitObjectAbstractCollectionModel collection = null;

    for (final CockpitObjectAbstractCollectionModel coll : collections) {
      if ("testA".equals(coll.getQualifier())) {
        collection = coll;
        break;
      }
    }

    assertThat(collection.getElements().size()).isEqualTo(2);

    final CatalogVersionModel catalogVersion =
        catalogService.getCatalogVersion("testCatalog", "Online");
    final List<ItemModel> elements = new ArrayList<ItemModel>();
    elements.add(productService.getProductForCode(catalogVersion, "testProduct4"));

    int n = cockpitCollectionService.removeFromCollection(collection, elements);

    collections = cockpitCollectionService.getCollectionsForUser(user);
    CockpitObjectAbstractCollectionModel changedCollection = null;
    for (final CockpitObjectAbstractCollectionModel coll : collections) {
      if ("testA".equals(coll.getQualifier())) {
        changedCollection = coll;
        break;
      }
    }
    // should not change, before we tried to remove the element that does not exist in the
    // collection
    assertThat(changedCollection.getElements().size()).isEqualTo(2);
    assertThat(n).isEqualTo(0);

    elements.clear();
    elements.add(productService.getProductForCode(catalogVersion, "testProduct0"));

    n = cockpitCollectionService.removeFromCollection(collection, elements);

    collections = cockpitCollectionService.getCollectionsForUser(user);
    changedCollection = null;
    for (final CockpitObjectAbstractCollectionModel coll : collections) {
      if ("testA".equals(coll.getQualifier())) {
        changedCollection = coll;
        break;
      }
    }
    assertThat(changedCollection.getElements().size()).isEqualTo(1);
    assertThat(n).isEqualTo(1);
  }
  @Test
  public void testGetCollectionsForUser() {
    UserModel user = userService.getUserForUID("ahertz");
    List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(user);

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(3);

    user = userService.getUserForUID("abrode");
    collections = cockpitCollectionService.getCollectionsForUser(user);

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(4);
  }
  @Test
  public void testCloneCollection() {
    final UserModel user = userService.getUserForUID("ahertz");
    final UserModel userB = userService.getUserForUID("abrode");
    final List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(user);
    final CockpitObjectCollectionModel collection =
        (CockpitObjectCollectionModel) collections.get(0);

    final CockpitObjectAbstractCollectionModel clone =
        cockpitCollectionService.cloneCollection(collection, userB);

    assertThat(clone).isNotNull();
    assertThat(clone.getUser()).isEqualTo(userB);
    assertThat(clone.getQualifier()).isEqualTo(collection.getQualifier());
    assertThat(clone.getElements().size())
        .isEqualTo(Integer.valueOf(collection.getElements().size()));
  }
  @Test
  public void testGetSpecialCollections() {
    final UserModel user = userService.getUserForUID("abrode");
    final List<CockpitObjectSpecialCollectionModel> collections =
        cockpitCollectionService.getSpecialCollections(user, "quickcollection");

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(1);
    assertThat(collections.get(0).getQualifier()).isEqualTo("testSpecialB");
    assertThat(collections.get(0).getCollectionType().getCode()).isEqualTo("quickcollection");
    assertThat(collections.get(0).getElements().size()).isEqualTo(3);
  }
  @Test
  public void testGetSpecialCollectionsForUser() {
    final UserModel user = userService.getUserForUID("ahertz");
    final List<CockpitObjectSpecialCollectionModel> collections =
        cockpitCollectionService.getSpecialCollectionsForUser(user);

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(1);
    assertThat(collections.get(0).getQualifier()).isEqualTo("testSpecialA");
    assertThat(collections.get(0).getCollectionType().getCode()).isEqualTo("blacklist");
    assertThat(collections.get(0).getElements().size()).isEqualTo(2);
  }
  @Test
  public void testHasReadCollectionRight() {
    final UserModel otherUser = userService.getUserForUID("other");
    final UserModel abrode = userService.getUserForUID("abrode");
    final UserModel deJol = userService.getUserForUID("deJol");

    final List<CockpitObjectAbstractCollectionModel> collections =
        cockpitCollectionService.getCollectionsForUser(deJol);

    assertThat(collections).isNotNull();
    assertThat(collections.size()).isEqualTo(2);

    final CockpitObjectAbstractCollectionModel collection = collections.get(0);
    boolean hasRight = cockpitCollectionService.hasReadCollectionRight(otherUser, collection);
    assertThat(hasRight).isFalse();

    hasRight = cockpitCollectionService.hasReadCollectionRight(abrode, collection);
    assertThat(hasRight).isTrue();

    hasRight = cockpitCollectionService.hasReadCollectionRight(deJol, collection);
    assertThat(hasRight).isTrue();
  }