private PlatformUser givenUser() {
   Organization org = new Organization();
   org.setOrganizationId(ORGANIZATION_ID);
   PlatformUser user = new PlatformUser();
   user.setUserId(USER_ID);
   user.setOrganization(org);
   return user;
 }
 private PlatformUser givenUser(String locale) {
   Organization org = new Organization();
   org.setOrganizationId(ORGANIZATION_ID);
   org.setName(ORGANIZATION_NAME);
   PlatformUser user = new PlatformUser();
   user.setUserId(USER_ID);
   user.setOrganization(org);
   user.setLocale(locale);
   return user;
 }
  private List<PlatformUser> givenUsers() {
    List<PlatformUser> users = new ArrayList<PlatformUser>();
    PlatformUser user1 = new PlatformUser();
    user1.setUserId(USERID_1);
    PlatformUser user2 = new PlatformUser();
    user2.setUserId(USERID_2);
    users.add(user1);
    users.add(user2);

    return users;
  }
  private PlatformUser createReseller(Organization custOrg) {
    PlatformUser resellerAdmin = new PlatformUser();

    Organization resellerOrg = new Organization();
    resellerOrg.setOrganizationId(RESELLER_ORGID);
    addRole(resellerOrg, OrganizationRoleType.RESELLER);

    createOrgReference(resellerOrg, custOrg, OrganizationReferenceType.RESELLER_TO_CUSTOMER);

    resellerAdmin.setOrganization(resellerOrg);
    return resellerAdmin;
  }
  private PlatformUser createBroker(Organization custOrg) {
    PlatformUser brokerAdmin = new PlatformUser();

    Organization brokerOrg = new Organization();
    brokerOrg.setOrganizationId(BROKER_ORGID);
    addRole(brokerOrg, OrganizationRoleType.BROKER);

    createOrgReference(brokerOrg, custOrg, OrganizationReferenceType.BROKER_TO_CUSTOMER);

    brokerAdmin.setOrganization(brokerOrg);
    return brokerAdmin;
  }
  private PlatformUser createSupplier(Organization custOrg) {
    PlatformUser supplierAdmin = new PlatformUser();

    Organization supplierOrg = new Organization();
    supplierOrg.setOrganizationId(SUPPLIER_ORGID);
    addRole(supplierOrg, OrganizationRoleType.SUPPLIER);

    createOrgReference(supplierOrg, custOrg, OrganizationReferenceType.SUPPLIER_TO_CUSTOMER);

    supplierAdmin.setOrganization(supplierOrg);
    return supplierAdmin;
  }
 @Before
 public void setup() throws Exception {
   dsMock = mock(DataService.class);
   Organization org = new Organization();
   org.setOrganizationId(ORGANIZATIONID);
   PlatformUser user = new PlatformUser();
   user.setUserId(USERID_1);
   user.setLocale("en");
   user.setOrganization(org);
   when(dsMock.getCurrentUser()).thenReturn(user);
   marketplace = new Marketplace();
   marketplace.setKey(1000l);
   doReturn(marketplace).when(dsMock).getReferenceByBusinessKey(any(Marketplace.class));
 }
  @Override
  public VOUser convert(PlatformUser platformUser) {
    if (platformUser == null) {
      return null;
    }

    VOUser user = new VOUser();

    // Base user data
    user.setKey(platformUser.getKey());
    user.setVersion(platformUser.getVersion());
    user.setUserId(platformUser.getUserId());

    return user;
  }
  @Test
  public void isDefaultServiceConfigurationChanged_Reseller() {
    user.setOrganization(reseller);
    VOPaymentType voPt = createPaymentType(PaymentType.CREDIT_CARD);

    assertTrue(pcf.isDefaultServiceConfigurationChanged(Collections.singleton(voPt)));
  }
  @Test
  public void isDefaultServiceConfigurationChanged_Reseller_Negative() {
    user.setOrganization(reseller);
    VOPaymentType voPt = createPaymentType(PaymentType.INVOICE);

    assertFalse(pcf.isDefaultServiceConfigurationChanged(Collections.singleton(voPt)));
  }
  @Test(expected = OperationNotPermittedException.class)
  public void filterCustomerConfiguration_Customer_NotPermitted() throws Exception {
    user.setOrganization(noCustomer);
    VOOrganizationPaymentConfiguration conf =
        createCustomerConfiguration(noCustomer.getOrganizationId(), PaymentType.INVOICE);

    pcf.filterCustomerConfiguration(Arrays.asList(conf));
  }
  @Test
  public void testSetPaymentIdentificationForOrganizationValidateActivationOfSubs()
      throws Exception {
    // create org and sub with status suspended
    final Organization org =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization org =
                    Organizations.createOrganization(mgr, OrganizationRoleType.CUSTOMER);

                return org;
              }
            });

    OrganizationRefToPaymentType orgPayType = addPaymentTypeToOrganization(org, CREDIT_CARD);
    final PaymentInfo pi = getPaymentInfoForOrg(orgPayType);

    container.login(String.valueOf(supplierUser.getKey()));
    enablePaymentTypes(org.getOrganizationId(), OrganizationRoleType.CUSTOMER);
    Subscription sub =
        runTX(
            new Callable<Subscription>() {
              @Override
              public Subscription call() throws Exception {
                prepareProducts(null);
                Organization supplier = new Organization();
                supplier.setOrganizationId(supplierId);
                supplier = (Organization) mgr.getReferenceByBusinessKey(supplier);
                Subscription sub =
                    Subscriptions.createSubscription(
                        mgr, org.getOrganizationId(), "testProd1", "subId", supplier);
                sub.setStatus(SubscriptionStatus.SUSPENDED);
                sub.setPaymentInfo(pi);
                return sub;
              }
            });
    final long subKey = sub.getKey();

    VOPaymentData pd =
        getData(
            pi.getKey(), "someIdFromPSP", null, null, org.getKey(), pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    // validate subscription status is active
    sub =
        runTX(
            new Callable<Subscription>() {
              @Override
              public Subscription call() throws Exception {
                return mgr.getReference(Subscription.class, subKey);
              }
            });
    Assert.assertEquals(
        "subscription was not activated", SubscriptionStatus.ACTIVE, sub.getStatus());
    Assert.assertTrue(instanceActivated);
  }
  @Test
  public void filterCustomerConfiguration_Reseller_Change() throws Exception {
    user.setOrganization(reseller);
    VOOrganizationPaymentConfiguration conf =
        createCustomerConfiguration(customer.getOrganizationId(), PaymentType.CREDIT_CARD);
    List<VOOrganizationPaymentConfiguration> input = Arrays.asList(conf);

    List<VOOrganizationPaymentConfiguration> result = pcf.filterCustomerConfiguration(input);

    assertEquals(input, result);
  }
  @Test
  public void filterCustomerConfiguration_Reseller_NoChange() throws Exception {
    user.setOrganization(reseller);
    VOOrganizationPaymentConfiguration conf =
        createCustomerConfiguration(customer.getOrganizationId(), PaymentType.INVOICE);

    List<VOOrganizationPaymentConfiguration> result =
        pcf.filterCustomerConfiguration(Arrays.asList(conf));

    assertEquals(new ArrayList<VOOrganizationPaymentConfiguration>(), result);
  }
  @Test
  public void getSubcsriptionsForManagers() throws Exception {
    // given
    Set<SubscriptionStatus> states =
        EnumSet.of(
            SubscriptionStatus.ACTIVE,
            SubscriptionStatus.PENDING,
            SubscriptionStatus.EXPIRED,
            SubscriptionStatus.PENDING_UPD,
            SubscriptionStatus.SUSPENDED,
            SubscriptionStatus.SUSPENDED_UPD);
    customerOrg = createCustomer();
    currentUser = createSupplier(customerOrg);
    // when
    List<Subscription> list = subscriptionServiceBean.getSubscriptionsForManagers();

    // then
    verify(subscriptionDao, times(1))
        .getSubscriptionsForMyBrokerCustomers(eq(currentUser.getOrganization()));
    verify(subscriptionDao, times(1))
        .getSubscriptionsForMyCustomers(eq(currentUser.getOrganization()), eq(states));
    assertNotNull(list);
  }
  @Test
  public void executeQueryLoadSubIdsAndOrgsForMyBrokerCustomers() throws Exception {
    // given
    customerOrg = createCustomer();
    currentUser = createSupplier(customerOrg);
    // when
    List<Subscription> list =
        subscriptionServiceBean.executeQueryLoadSubIdsAndOrgsForMyBrokerCustomers();

    // then
    verify(subscriptionDao, times(1))
        .getSubscriptionsForMyBrokerCustomers(eq(currentUser.getOrganization()));
    assertNotNull(list);
  }
  @Test
  public void isDefaultServiceConfigurationChanged_Reseller_Empty() {
    user.setOrganization(reseller);

    assertTrue(pcf.isDefaultServiceConfigurationChanged(new HashSet<VOPaymentType>()));
  }
  private PlatformUser givenUser() {
    PlatformUser user = new PlatformUser();
    user.setUserId(USERID_1);

    return user;
  }
  @Before
  public void setup() throws Exception {
    ds = mock(DataService.class);

    pt = new PaymentType();
    pt.setPaymentTypeId(PaymentType.INVOICE);
    pt.setKey(12345);

    Organization op = new Organization();
    op.setOrganizationId(OrganizationRoleType.PLATFORM_OPERATOR.name());

    supplier = new Organization();
    supplier.setOrganizationId("supplier");
    Organizations.grantOrganizationRole(supplier, OrganizationRoleType.SUPPLIER);

    reseller = new Organization();
    reseller.setOrganizationId("reseller");
    Organizations.grantOrganizationRole(reseller, OrganizationRoleType.RESELLER);

    OrganizationReference ref =
        OrganizationReferences.addReference(
            op, supplier, OrganizationReferenceType.PLATFORM_OPERATOR_TO_SUPPLIER);
    OrganizationReferences.enablePayment(ref, pt)
        .setOrganizationRole(new OrganizationRole(OrganizationRoleType.SUPPLIER));

    ref =
        OrganizationReferences.addReference(
            op, reseller, OrganizationReferenceType.PLATFORM_OPERATOR_TO_RESELLER);
    OrganizationReferences.enablePayment(ref, pt)
        .setOrganizationRole(new OrganizationRole(OrganizationRoleType.RESELLER));

    customer = new Organization();
    customer.setOrganizationId("customer");

    noCustomer = new Organization();
    noCustomer.setOrganizationId("noCustomer");

    ref =
        OrganizationReferences.addReference(
            supplier, customer, OrganizationReferenceType.SUPPLIER_TO_CUSTOMER);
    OrganizationReferences.enablePayment(ref, pt);

    ref =
        OrganizationReferences.addReference(
            reseller, customer, OrganizationReferenceType.RESELLER_TO_CUSTOMER);
    OrganizationReferences.enablePayment(ref, pt);

    product = new Product();
    product.setKey(9876);
    product.setVendor(supplier);
    product.setType(ServiceType.TEMPLATE);
    product.setPaymentTypes(Arrays.asList(new ProductToPaymentType(product, pt)));

    pcf = new PaymentConfigurationFilter(ds);

    user = new PlatformUser();
    user.setOrganization(supplier);

    when(ds.getCurrentUser()).thenReturn(user);
    when(ds.getReference(eq(Product.class), eq(product.getKey()))).thenReturn(product);
    when(ds.getReference(eq(Product.class), eq(PROD_NOT_FOUND_KEY)))
        .thenThrow(new ObjectNotFoundException(ClassEnum.SERVICE, "product"));

    when(ds.getReferenceByBusinessKey(any(DomainObject.class)))
        .thenAnswer(
            new Answer<DomainObject<?>>() {

              public DomainObject<?> answer(InvocationOnMock invocation) throws Throwable {
                Object object = invocation.getArguments()[0];
                if (object instanceof Organization) {
                  Organization o = (Organization) object;
                  if (customer.getOrganizationId().equals(o.getOrganizationId())) {
                    return customer;
                  }
                  if (noCustomer.getOrganizationId().equals(o.getOrganizationId())) {
                    return noCustomer;
                  }
                  throw new ObjectNotFoundException(ClassEnum.ORGANIZATION, o.getOrganizationId());
                }
                throw new ObjectNotFoundException(
                    ClassEnum.ORGANIZATION, object.getClass().getName());
              }
            });
  }