@Override
  public void setup(final TestContainer container) throws Exception {
    instanceActivated = false;
    container.login("1");
    container.addBean(new DataServiceBean());
    container.addBean(new ConfigurationServiceStub());
    container.addBean(new AccountServiceStub());
    container.addBean(Mockito.mock(BillingDataRetrievalServiceLocal.class));
    container.addBean(new PortLocatorBean());
    container.addBean(new LocalizerServiceBean());
    container.addBean(
        new ApplicationServiceStub() {

          @Override
          public void activateInstance(Subscription subscription)
              throws TechnicalServiceNotAliveException, TechnicalServiceOperationException {
            instanceActivated = true;
          }
        });
    container.addBean(new PaymentServiceBean());

    mgr = container.get(DataService.class);
    paymentMgmt = container.get(PaymentService.class);

    runTX(
        new Callable<Void>() {
          @Override
          public Void call() throws Exception {
            createOrganizationRoles(mgr);
            createPaymentTypes(mgr);
            SupportedCountries.createSomeSupportedCountries(mgr);
            return null;
          }
        });
    Organization organization =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization organization =
                    Organizations.createOrganization(mgr, OrganizationRoleType.TECHNOLOGY_PROVIDER);
                Organizations.createUserForOrg(mgr, organization, true, "ProvAdmin");
                return organization;
              }
            });
    providerId = organization.getOrganizationId();

    supplier =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization organization =
                    Organizations.createOrganization(mgr, OrganizationRoleType.SUPPLIER);
                supplierUser = Organizations.createUserForOrg(mgr, organization, true, "SuppAdmin");
                return organization;
              }
            });
    supplierId = supplier.getOrganizationId();
  }
  @Test(expected = OperationNotPermittedException.class)
  public void filterCustomerConfiguration_NotPermitted() throws Exception {
    VOOrganizationPaymentConfiguration conf =
        createCustomerConfiguration(noCustomer.getOrganizationId(), PaymentType.INVOICE);

    pcf.filterCustomerConfiguration(Arrays.asList(conf));
  }
  @Test
  public void getTemplateProducts_AllProductTypes() throws Exception {
    // given
    final Product template =
        createTemplateProduct(
            tpSup.getOrganizationId(), "product", "techProduct", ServiceStatus.ACTIVE);
    final Product customerTemplate =
        createCustomerSpecificCopiedProduct(tpSup, template, ServiceStatus.ACTIVE);
    final Product partnerTemplate = createPartnerProductCopy(template, tpSup, ServiceStatus.ACTIVE);
    runTX(
        new Callable<Void>() {
          public Void call() throws Exception {
            Subscriptions.createSubscription(ds, tpSup.getOrganizationId(), template);
            Subscriptions.createSubscription(ds, tpSup.getOrganizationId(), customerTemplate);
            Subscriptions.createPartnerSubscription(
                ds, tpSup.getOrganizationId(), partnerTemplate.getProductId(), "partnerSub", tpSup);
            return null;
          }
        });

    // when
    List<Product> products =
        runTX(
            new Callable<List<Product>>() {
              public List<Product> call() {
                return localService.getTemplateProducts();
              }
            });

    // then
    assertEquals(1, products.size());
    assertEquals(template.getKey(), products.get(0).getKey());
  }
  @Test
  public void getProductsForVendor_FilterInStatuses() throws Exception {
    // give
    givenBrokerOrg();
    container.login(brokerUserKey, UserRoleType.BROKER_MANAGER.name());

    Product templateInactive =
        createTemplateProduct(
            broker.getOrganizationId(),
            "productInactive",
            "techProductInactive",
            ServiceStatus.INACTIVE);
    Product templateActive =
        createTemplateProduct(
            broker.getOrganizationId(), "productActive", "techProductActive", ServiceStatus.ACTIVE);
    createTemplateProduct(
        broker.getOrganizationId(), "productDeleted", "techProductDeleted", ServiceStatus.DELETED);
    createTemplateProduct(
        broker.getOrganizationId(),
        "productObsolete",
        "techProductObsolete",
        ServiceStatus.OBSOLETE);
    Product templateSuspended =
        createTemplateProduct(
            broker.getOrganizationId(),
            "productSuspended",
            "techProductSuspended",
            ServiceStatus.SUSPENDED);
    // when
    List<Product> products =
        runTX(
            new Callable<List<Product>>() {
              public List<Product> call() {
                return localService.getProductsForVendor();
              }
            });
    // then
    assertEquals(3, products.size());
    assertEquals(templateInactive.getKey(), products.get(0).getKey());
    assertEquals(ServiceStatus.INACTIVE, products.get(0).getStatus());

    assertEquals(templateActive.getKey(), products.get(1).getKey());
    assertEquals(ServiceStatus.ACTIVE, products.get(1).getStatus());

    assertEquals(ServiceStatus.SUSPENDED, products.get(2).getStatus());
    assertEquals(templateSuspended.getKey(), products.get(2).getKey());
  }
  @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_Change() throws Exception {
    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_NoChange() throws Exception {
    VOOrganizationPaymentConfiguration conf =
        createCustomerConfiguration(customer.getOrganizationId(), PaymentType.INVOICE);

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

    assertEquals(new ArrayList<VOOrganizationPaymentConfiguration>(), result);
  }
  @Test
  public void getTemplateProducts_AllStates() throws Exception {
    // give
    for (ServiceStatus status : ServiceStatus.values()) {
      createTemplateProduct(
          tpSup.getOrganizationId(), status.name(), "tech_" + status.name(), status);
    }

    // when
    List<Product> products =
        runTX(
            new Callable<List<Product>>() {
              public List<Product> call() {
                return localService.getTemplateProducts();
              }
            });
    // then
    assertEquals(ServiceStatus.values().length, products.size());
  }
 @Test
 public void getProductsForVendor_WithSubscriptionSpecificCopy() throws Exception {
   // given
   givenBrokerOrg();
   container.login(brokerUserKey, UserRoleType.BROKER_MANAGER.name());
   Product template =
       createTemplateProduct(
           broker.getOrganizationId(), "product", "techProduct", ServiceStatus.ACTIVE);
   createSubscriptionSpecificProductCopy(broker, template);
   // when
   List<Product> products =
       runTX(
           new Callable<List<Product>>() {
             public List<Product> call() {
               return localService.getProductsForVendor();
             }
           });
   // then
   assertEquals(1, products.size());
 }
  @Test
  public void getPartnerProductsForTemplate() throws Exception {
    // give
    Organization broker1 = createOrgWithRole(OrganizationRoleType.BROKER);
    Organization broker2 = createOrgWithRole(OrganizationRoleType.BROKER);
    Organization broker3 = createOrgWithRole(OrganizationRoleType.BROKER);
    Organization reseller1 = createOrgWithRole(OrganizationRoleType.RESELLER);
    Organization reseller2 = createOrgWithRole(OrganizationRoleType.RESELLER);

    final Product template =
        createTemplateProduct(
            tpSup.getOrganizationId(), "product", "techProduct", ServiceStatus.ACTIVE);
    createCustomerSpecificCopiedProduct(tpSup, template, ServiceStatus.ACTIVE);
    createSubscriptionSpecificProductCopy(tpSup, template);

    Product brokerActiveProduct = createPartnerProductCopy(template, broker1, ServiceStatus.ACTIVE);
    Product resellerInactiveProduct =
        createPartnerProductCopy(template, reseller1, ServiceStatus.INACTIVE);
    createPartnerProductCopy(template, broker2, ServiceStatus.OBSOLETE);
    createPartnerProductCopy(template, broker3, ServiceStatus.DELETED);
    createPartnerProductCopy(template, reseller2, ServiceStatus.SUSPENDED);

    // when
    List<Product> products =
        runTX(
            new Callable<List<Product>>() {
              public List<Product> call() throws Exception {
                return localService.getPartnerProductsForTemplate(template.getKey());
              }
            });

    // then
    assertEquals(5, products.size());
    assertEquals(brokerActiveProduct.getKey(), products.get(0).getKey());
    assertEquals(resellerInactiveProduct.getKey(), products.get(1).getKey());
  }