@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 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 testReferencesForPurchasedInCategory() {
    final UserModel user = userService.getUserForUID("dejol");
    final CategoryModel category = categoryService.getCategoryForCode("cameras");

    List<ProductModel> result =
        b2bSimpleSuggestionService.getReferencesForPurchasedInCategory(
            category, user, Collections.EMPTY_LIST, false, null);
    Assert.assertEquals(4, result.size());
    result =
        b2bSimpleSuggestionService.getReferencesForPurchasedInCategory(
            category, user, Collections.EMPTY_LIST, false, NumberUtils.INTEGER_ONE);
    Assert.assertEquals(1, result.size());
    result =
        b2bSimpleSuggestionService.getReferencesForPurchasedInCategory(
            category, user, Arrays.asList(ProductReferenceTypeEnum.SIMILAR), false, null);
    Assert.assertEquals(1, result.size());
    result =
        b2bSimpleSuggestionService.getReferencesForPurchasedInCategory(
            category, user, Arrays.asList(ProductReferenceTypeEnum.ACCESSORIES), false, null);
    Assert.assertEquals(2, result.size());
    result =
        b2bSimpleSuggestionService.getReferencesForPurchasedInCategory(
            category, user, Arrays.asList(ProductReferenceTypeEnum.ACCESSORIES), true, null);
    Assert.assertEquals(1, result.size());
    final ProductModel product = result.get(0);
    Assert.assertEquals("adapterDC", product.getCode());
    Assert.assertEquals("adapter", product.getName());
  }
  @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);
  }
 /*
  * (non-Javadoc)
  *
  * @see de.hybris.platform.b2bacceleratorfacades.company.CompanyB2BCommerceFacade#getContactNumber(java.lang.String)
  */
 @Override
 public String getContactNumber(final String uuid, final CustomerData customerData) {
   final EnergizerB2BCustomerModel model =
       userService.getUserForUID(uuid, EnergizerB2BCustomerModel.class);
   contactNumberPopulator.populate(model, customerData);
   return customerData.getContactNumber();
 }
  @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()));
  }
  private void prepareData() {
    catVersion = catalogVersionService.getCatalogVersion("hwcatalog", "Online");
    catalogVersionService.addSessionCatalogVersion(catVersion);

    user = userService.getUserForUID("ariel");
    userService.setCurrentUser(user);
    cart = cartService.getSessionCart();
    commonI18NService.setCurrentCurrency(commonI18NService.getCurrency("EUR"));
    product1 = productService.getProductForCode(catVersion, "HW2110-0012");
  }
  @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 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 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 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();
  }
  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    final CatalogVersionModel version =
        catalogVersionService.getCatalogVersion("hwcatalog", "Online");
    catalogVersionService.addSessionCatalogVersion(version);
    baseProduct = productService.getProductForCode(version, "HW2110-0019");
    partnerProduct = productService.getProductForCode(version, "HW2200-0561");
    user = userService.getUserForUID("demo");
    userService.setCurrentUser(user);
    currency = commonI18NService.getCurrency("EUR");
    commonI18NService.setCurrentCurrency(currency);
  }
 @Override
 public void updateCustomer(final CustomerData customerData) throws DuplicateUidException {
   validateParameterNotNullStandardMessage("customerData", customerData);
   final EnergizerB2BCustomerModel energizerB2BCustomerModel;
   if (StringUtils.isEmpty(customerData.getUid())) {
     energizerB2BCustomerModel = this.getModelService().create(EnergizerB2BCustomerModel.class);
     energizerB2BCustomerModel.setRegistrationEmailFlag(Boolean.TRUE);
   } else {
     energizerB2BCustomerModel =
         (EnergizerB2BCustomerModel) userService.getUserForUID(customerData.getUid());
   }
   if (null != customerData && null != energizerB2BCustomerModel) {
     energizerCustomerReversePopulator.populate(customerData, energizerB2BCustomerModel);
     companyB2BCommerceService.saveModel(energizerB2BCustomerModel);
   }
 }
 /** As per enhancement added new group b2bviewergroup */
 @Override
 public void populateUserRoles(final SearchPageData<CustomerData> b2bCustomer) {
   List<String> roles = null;
   for (int i = 0; i < b2bCustomer.getResults().size(); i++) {
     roles = new ArrayList<String>();
     final EnergizerB2BCustomerModel customerModel =
         userService.getUserForUID(
             b2bCustomer.getResults().get(i).getUid(), EnergizerB2BCustomerModel.class);
     final Set<PrincipalGroupModel> roleModels =
         new HashSet<PrincipalGroupModel>(customerModel.getGroups());
     for (final PrincipalGroupModel role : roleModels) {
       // only display allowed usergroups
       if (energizerGroupsLookUpStrategy.getUserGroups().contains(role.getUid())) {
         roles.add(role.getUid());
       }
     }
     b2bCustomer.getResults().get(i).setRoles(roles);
   }
 }
 /** As per enhancement added new group b2bviewergroup */
 public void populateRolesByCustomer(final String uuid, final CustomerData target) {
   final List<String> roles = new ArrayList<String>();
   final EnergizerB2BCustomerModel model =
       userService.getUserForUID(uuid, EnergizerB2BCustomerModel.class);
   final Set<PrincipalGroupModel> roleModels = new HashSet<PrincipalGroupModel>(model.getGroups());
   CollectionUtils.filter(
       roleModels,
       PredicateUtils.notPredicate(PredicateUtils.instanceofPredicate(B2BUnitModel.class)));
   CollectionUtils.filter(
       roleModels,
       PredicateUtils.notPredicate(PredicateUtils.instanceofPredicate(B2BUserGroupModel.class)));
   for (final PrincipalGroupModel role : roleModels) {
     // only display allowed usergroups
     if (energizerGroupsLookUpStrategy.getUserGroups().contains(role.getUid())) {
       roles.add(role.getUid());
     }
   }
   target.setRoles(roles);
 }
 /**
  * Sets the user in the session and updates the branch in session context.
  *
  * @param userId
  * @return A {@link de.hybris.platform.core.model.user.UserModel}
  */
 public B2BCustomerModel login(final String userId) {
   final B2BCustomerModel user = userService.getUserForUID(userId, B2BCustomerModel.class);
   org.junit.Assert.assertNotNull(userId + " user is null", user);
   login(user);
   return user;
 }
  @Test
  public void testPerform() {

    attachments = Arrays.asList(customer, b2BRegistrationModel);

    Mockito.doAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(final InvocationOnMock invocation) {
                customers = new LinkedList<>();

                return null;
              }
            })
        .when(modelService)
        .remove(customer);

    Mockito.doAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(final InvocationOnMock invocation) {
                customers = new ArrayList<CustomerModel>();
                customers.add(b2BCustomer);

                return null;
              }
            })
        .when(modelService)
        .save(b2BCustomer);

    Mockito.doAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(final InvocationOnMock invocation) {
                attachments = new LinkedList<ItemModel>();
                attachments =
                    Arrays.asList(createB2BCustomerModel(customers.get(0)), b2BRegistrationModel);

                return null;
              }
            })
        .when(workflowAttachmentService)
        .addItems(any(WorkflowModel.class), any(attachments.getClass()));

    workflowAttachmentService.addItems(workflowActionModel.getWorkflow(), attachments);

    when(userService.getUserForUID(b2BRegistrationModel.getEmail(), CustomerModel.class))
        .thenReturn(customer);

    final List<ItemModel> customerAsList = new LinkedList<ItemModel>();
    customerAsList.add(customer);
    when(workflowAttachmentService.getAttachmentsForAction(
            workflowActionModel, CustomerModel.class.getName()))
        .thenReturn(customerAsList);

    final List<ItemModel> b2BRegistrationModelAsList = new LinkedList<ItemModel>();
    b2BRegistrationModelAsList.add(b2BRegistrationModel);
    when(workflowAttachmentService.getAttachmentsForAction(
            workflowActionModel, B2BRegistrationModel.class.getName()))
        .thenReturn(b2BRegistrationModelAsList);

    when(modelService.create(B2BCustomerModel.class)).thenReturn(b2BCustomer);

    final WorkflowModel workflowModel = new WorkflowModel();
    workflowModel.setActions(Arrays.asList(workflowActionModel));

    workflowActionModel.setWorkflow(workflowModel);

    final WorkflowDecisionModel decision =
        registrationApprovedAutomatedWorkflowTemplateJob.perform(workflowActionModel);

    assertEquals("The right decision shoule be returned", decision, workflowDecisionModel);

    assertTrue(
        "B2BCustomer should have been created", attachments.get(0) instanceof B2BCustomerModel);

    assertEquals(
        "B2BCustomer should have been assigned a B2BUnit",
        ((B2BCustomerModel) attachments.get(0)).getDefaultB2BUnit().getName(),
        b2bUnitModel.getName());

    assertTrue(
        "b2BRegistrationModel should still be in workflow attachment",
        attachments.get(1).getClass().getName().endsWith("B2BRegistrationModel"));
  }