/**
   * HW2110-0019(base product): 381.64 Euro, and HW2200-0561(partner product): 86.80 Euro. The
   * ProductOneToOnePerfectPartnerPromotion will be fired if both of them are in the cart and the
   * price is 700 Euro.
   *
   * <ul>
   *   <li>adds the HW2110-0019 in cart, and tests the total price,
   *   <li>updates with the ProductOneToOnePerfectPartnerPromotion, and checks the total price,
   *   <li>now adds the HW2200-0561, and checks the total price,
   *   <li>updates with the ProductOneToOnePerfectPartnerPromotion that should be fired now, and
   *       checks the total price.
   * </ul>
   */
  @Test
  public void testProductOneToOnePerfectPartnerPromotion() throws CalculationException {
    cart = cartService.getSessionCart();
    cartService.addNewEntry(cart, baseProduct, 1, baseProduct.getUnit());
    modelService.save(cart);
    calculationService.calculate(cart);
    assertEquals(
        "before updatePromotions(ProductOneToOnePerfectPartnerPromotion)",
        381.64d,
        cart.getTotalPrice().doubleValue(),
        0.01);

    promotionGroup = promotionsService.getPromotionGroup("prGroup5");
    final Collection<PromotionGroupModel> promotionGroups = new ArrayList<PromotionGroupModel>();
    promotionGroups.add(promotionGroup);
    promotionsService.updatePromotions(
        promotionGroups, cart, false, AutoApplyMode.APPLY_ALL, AutoApplyMode.APPLY_ALL, new Date());
    modelService.refresh(cart);
    assertEquals("without partner product", 381.64d, cart.getTotalPrice().doubleValue(), 0.01);

    cartService.addNewEntry(cart, partnerProduct, 1, partnerProduct.getUnit());
    modelService.saveAll();
    calculationService.calculate(cart);
    assertEquals(
        "with partner product, but without promotion",
        468.44d,
        cart.getTotalPrice().doubleValue(),
        0.01);

    promotionsService.updatePromotions(
        promotionGroups, cart, false, AutoApplyMode.APPLY_ALL, AutoApplyMode.APPLY_ALL, new Date());
    modelService.refresh(cart);
    assertEquals("with partner product now", 400.00d, cart.getTotalPrice().doubleValue(), 0.01);
  }
 public void testCloneCartAction() throws Exception {
   Assert.assertNotNull("cart not null", cartService.getSessionCart());
   Assert.assertNotNull("user not null", cartService.getSessionCart().getUser());
   Assert.assertEquals("DC S No", cartService.getSessionCart().getUser().getUid());
   final ReplenishmentProcessModel replenishmentProcessModel = createReplenishmentProcess();
   cloneCartAction.executeAction(replenishmentProcessModel);
   this.modelService.save(replenishmentProcessModel);
   Assert.assertNotNull(
       processParameterHelper.getProcessParameterByName(replenishmentProcessModel, "cart"));
 }
 public void testRecalculateCartAction() throws Exception {
   Assert.assertNotNull("cart not null", cartService.getSessionCart());
   Assert.assertNotNull("user not null", cartService.getSessionCart().getUser());
   Assert.assertEquals("DC S No", cartService.getSessionCart().getUser().getUid());
   final ReplenishmentProcessModel replenishmentProcessModel = createReplenishmentProcess();
   Assert.assertFalse(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   processParameterHelper.setProcessParameter(
       replenishmentProcessModel, "cart", cartService.getSessionCart());
   modelService.save(replenishmentProcessModel);
   Assert.assertTrue(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   calculateCartAction.executeAction(replenishmentProcessModel);
 }
 public void testAuthorizePaymentAction() throws Exception {
   Assert.assertNotNull("cart not null", cartService.getSessionCart());
   Assert.assertNotNull("user not null", cartService.getSessionCart().getUser());
   Assert.assertEquals("DC S No", cartService.getSessionCart().getUser().getUid());
   Assert.assertNotNull(cartService.getSessionCart().getPaymentInfo());
   Assert.assertThat(
       cartService.getSessionCart().getPaymentInfo(),
       instanceOf(CreditCardPaymentInfoModel.class));
   final ReplenishmentProcessModel replenishmentProcessModel = createReplenishmentProcess();
   Assert.assertFalse(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   processParameterHelper.setProcessParameter(
       replenishmentProcessModel, "cart", cartService.getSessionCart());
   modelService.save(replenishmentProcessModel);
   Assert.assertTrue(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   Assert.assertEquals(
       AbstractSimpleDecisionAction.Transition.OK,
       authorizePaymentAction.executeAction(replenishmentProcessModel));
 }
  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");
  }
  /**
   * Tests promotion with voucher that has product restriction. (currency is Euro)
   *
   * <ul>
   *   <li>product(HW1230-0001) price = 769.00 --> 2x = 1538.00
   *   <li>product(HW2110-0012) price = 81.08 --> 2x = 162.16
   *   <li>so the cart contains products that cost 1700.16 Euro without promotion/voucher.
   *   <li>redeems the voucher that is 10% discount with product restriction: only valid for
   *       [HW1230-0001]
   *   <li>applies the fixed price promotion that is only valid for [HW1230-0001], and the fixed
   *       price is 500
   *   <li>update promotions, and the total discount should be (500 * 2) * 10% = 100
   *   <li>total price of the cart: (500 * 2 + 81.08 * 2) - 100 = 1062.16
   * </ul>
   */
  @Test
  public void testPromotionWithProductRestrictionVoucher()
      throws CalculationException, JaloPriceFactoryException {
    // PRO-70
    promotionGroup = promotionsService.getPromotionGroup("prGroup20");
    final ProductModel productSony = productService.getProductForCode(catVersion, "HW1230-0001");

    cartService.addNewEntry(cart, productSony, 2, productSony.getUnit());
    cartService.addNewEntry(cart, product1, 2, product1.getUnit());
    modelService.save(cart);
    calculationService.calculate(cart);
    modelService.refresh(cart);
    assertEquals(
        "should be 1700.16 euro without promotion/voucher",
        1700.16,
        cart.getTotalPrice().doubleValue(),
        0.01);

    voucherService.redeemVoucher(restrictionProductVoucher, cart);
    modelService.save(cart);

    final List<PromotionGroupModel> groups = new ArrayList<PromotionGroupModel>();
    groups.add(promotionGroup);
    promotionsService.updatePromotions(
        groups,
        cart,
        false,
        AutoApplyMode.APPLY_ALL,
        AutoApplyMode.APPLY_ALL,
        new java.util.Date());
    modelService.refresh(cart);
    assertEquals(
        "should be 100 euro for fixed price discount",
        100.0,
        cart.getTotalDiscounts().doubleValue(),
        0.01);
    assertEquals(
        "should be 1062.16 euro for total", 1062.16, cart.getTotalPrice().doubleValue(), 0.01);
  }
 public void testConfirmationAction() throws Exception {
   Assert.assertNotNull(mockEventService);
   confirmationAction.setEventService(mockEventService);
   final CartModel sessionCart = cartService.getSessionCart();
   modelService.refresh(sessionCart);
   sessionCart.setCalculated(Boolean.TRUE);
   modelService.save(sessionCart);
   Assert.assertEquals(Boolean.TRUE, sessionCart.getCalculated());
   Assert.assertNotNull("cart not null", sessionCart);
   Assert.assertNotNull("user not null", sessionCart.getUser());
   Assert.assertEquals("DC S No", sessionCart.getUser().getUid());
   Assert.assertNotNull(sessionCart.getPaymentInfo());
   final ReplenishmentProcessModel replenishmentProcessModel = createReplenishmentProcess();
   Assert.assertFalse(
       processParameterHelper.containsParameter(replenishmentProcessModel, "order"));
   processParameterHelper.setProcessParameter(
       replenishmentProcessModel,
       "order",
       commerceCheckoutService.placeOrder(cartService.getSessionCart()));
   modelService.save(replenishmentProcessModel);
   Assert.assertTrue(processParameterHelper.containsParameter(replenishmentProcessModel, "order"));
   confirmationAction.executeAction(replenishmentProcessModel);
 }
  @Test
  public void testOnEvent() throws Exception {
    // get admin user
    final UserModel user = userService.getAdminUser();

    // create session cart, anonymous user is set on cart
    cartService.getSessionCart();

    // set admin user as current
    userService.setCurrentUser(user);

    // AfterSessionUserChangeEvent processed in background
    // ....

    // get current cart
    final CartModel cart = cartService.getSessionCart();

    // refresh cart to ensure that cart user is persisted
    modelService.refresh(cart);

    assertNotNull("Cart is null.", cart);
    assertNotNull("Cart user is null.", cart.getUser());
    assertEquals("Cart user differs.", user, cart.getUser());
  }
  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);

    strategy = new PickUpInStoreCheckoutFlowStrategy();
    strategy.setDefaultStrategy(defaultStrategy);
    strategy.setMultiStepCheckoutFlowStrategy(multiStepCheckoutFlowStrategy);
    strategy.setPickupStrategy(pickupStrategy);

    given(pickupStrategy.getPickupInStoreMode()).willReturn(PickupInStoreMode.BUY_AND_COLLECT);
    given(multiStepCheckoutFlowStrategy.getCheckoutFlow()).willReturn(CheckoutFlowEnum.MULTISTEP);
    given(defaultStrategy.getCheckoutFlow()).willReturn(CheckoutFlowEnum.MULTISTEP);

    given(cartService.getSessionCart()).willReturn(cartModel);
    given(cartModel.getEntries()).willReturn(Collections.singletonList(cartEntry));
  }
 public void testPlaceOrderAction() throws Exception {
   final CartModel sessionCart = cartService.getSessionCart();
   modelService.refresh(sessionCart);
   sessionCart.setCalculated(Boolean.TRUE);
   modelService.save(sessionCart);
   Assert.assertEquals(Boolean.TRUE, sessionCart.getCalculated());
   Assert.assertNotNull("cart not null", sessionCart);
   Assert.assertNotNull("user not null", sessionCart.getUser());
   Assert.assertEquals("DC S No", sessionCart.getUser().getUid());
   Assert.assertNotNull(sessionCart.getPaymentInfo());
   Assert.assertThat(sessionCart.getPaymentInfo(), instanceOf(CreditCardPaymentInfoModel.class));
   final ReplenishmentProcessModel replenishmentProcessModel = createReplenishmentProcess();
   Assert.assertFalse(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   processParameterHelper.setProcessParameter(replenishmentProcessModel, "cart", sessionCart);
   modelService.save(replenishmentProcessModel);
   Assert.assertTrue(processParameterHelper.containsParameter(replenishmentProcessModel, "cart"));
   placeOrderAction.execute(replenishmentProcessModel);
 }
  /**
   * Puts two products in the cart, applies one 10% product promotion, and redeems
   *
   * <ul>
   *   <li>1. one voucher without free shipping or
   *   <li>2. one voucher with free shipping or
   *   <li>3. two vouchers, one with free shipping and the other one without.
   * </ul>
   */
  @Test
  public void testPromotionWithVoucher() throws JaloPriceFactoryException {
    // PRO-64
    promotionGroup = promotionsService.getPromotionGroup("prGroup10");
    final List<PromotionGroupModel> groups = new ArrayList<PromotionGroupModel>();
    groups.add(promotionGroup);

    cartService.addNewEntry(cart, product1, 2, product1.getUnit());
    deliveryModeDhl = deliveryModeService.getDeliveryModeForCode("dhl");
    cart.setDeliveryMode(deliveryModeDhl);
    cart.setDeliveryAddress(user.getDefaultShipmentAddress());

    /**
     * General information:
     *
     * <ul>
     *   <li>product(HW2110-0012) price = 81.08 --> 2x = 162.16, delivery cost 8.0, totally 170.16
     *   <li>applied 10% ProductPercentageDiscountPromotion, 162.16 * 0.9 = 145.94
     * </ul>
     *
     * Three different situations with vouchers:
     *
     * <ul>
     *   <li>1. 5% voucher without free shipping, 145.94 * 0.95 = 138.64, delivery cost 8.0, totally
     *       146.64
     *   <li>2. 20% voucher with free shipping, 145.94 * 0.8 = 116.75, free shipping, totally 116.75
     *   <li>3. both the vouchers above, 145.94 * (1 - 0.05 - 0.20) = 109.45, free shipping, totally
     *       109.45
     * </ul>
     */
    applyVouchersAndPromotions(new String[] {voucherCode}, groups, 146.64);

    applyVouchersAndPromotions(new String[] {freeShippingVoucherCode}, groups, 116.75);

    applyVouchersAndPromotions(new String[] {voucherCode, freeShippingVoucherCode}, groups, 109.45);
  }
  @Before
  public void beforeTest() throws Exception {
    MockitoAnnotations.initMocks(this);
    // inject a mock payment provider
    cardPaymentService.setCommandFactoryRegistry(mockupCommandFactoryRegistry);
    paymentService.setCardPaymentService(cardPaymentService);
    commerceCheckoutService.setPaymentService(paymentService);

    createCoreData();
    createDefaultCatalog();

    importCsv("/energizercore/test/testOrganizations.csv", "utf-8");
    importCsv("/energizercore/test/testB2BCommerceCart.csv", "utf-8");

    final CartModel modelByExample = new CartModel();
    modelByExample.setCode("dc_shhCart_b2bas");

    final CartModel cart = flexibleSearchService.getModelByExample(modelByExample);
    Assert.assertNotNull(cart);
    cartService.setSessionCart(cart);
    userService.setCurrentUser(cart.getUser());

    if (flexibleSearchService
        .search(
            "SELECT {"
                + ServicelayerJobModel.PK
                + "} FROM {"
                + ServicelayerJobModel._TYPECODE
                + "} WHERE "
                + "{"
                + ServicelayerJobModel.SPRINGID
                + "}=?springid",
            Collections.singletonMap("springid", "b2bAcceleratorCartToOrderJob"))
        .getResult()
        .isEmpty()) {
      final ServicelayerJobModel servicelayerJobModel =
          modelService.create(ServicelayerJobModel.class);
      servicelayerJobModel.setCode("b2bAcceleratorCartToOrderJob");
      servicelayerJobModel.setSpringId("b2bAcceleratorCartToOrderJob");
      modelService.save(servicelayerJobModel);
    }

    final Date startDate = new Date();
    final Integer day = Integer.valueOf(5);
    final Integer week = Integer.valueOf(2);
    final List<DayOfWeek> days = new ArrayList<DayOfWeek>();
    days.add(DayOfWeek.TUESDAY);
    days.add(DayOfWeek.FRIDAY);
    triggerModel = modelService.create(TriggerModel.class);
    triggerModel.setRelative(Boolean.TRUE);
    triggerModel.setActivationTime(startDate);
    triggerModel.setDay(day);
    triggerModel.setWeekInterval(week);
    triggerModel.setDaysOfWeek(days);

    cartToOrderCronJob = modelService.create(CartToOrderCronJobModel.class);
    cartToOrderCronJob.setCart(cartService.getSessionCart());
    cartToOrderCronJob.setDeliveryAddress(userService.getCurrentUser().getDefaultShipmentAddress());
    cartToOrderCronJob.setPaymentAddress(userService.getCurrentUser().getDefaultPaymentAddress());
    cartToOrderCronJob.setPaymentInfo(cartService.getSessionCart().getPaymentInfo());
    setCronJobToTrigger(cartToOrderCronJob, Collections.singletonList(triggerModel));
    cartToOrderCronJob.setJob(cronJobService.getJob("b2bAcceleratorCartToOrderJob"));
    modelService.save(cartToOrderCronJob);
    final BaseSiteModel site = baseSiteService.getBaseSiteForUID("b2bstoretemplate");
    Assert.assertNotNull("no site found for id 'site'", site);
    baseSiteService.setCurrentBaseSite(site, false);
  }
  protected OrderModel placeTestOrder(final boolean valid)
      throws InvalidCartException, CalculationException {
    final CartModel cart = cartService.getSessionCart();
    final UserModel user = userService.getCurrentUser();
    cartService.addNewEntry(cart, productService.getProductForCode("testProduct1"), 1, null);
    cartService.addNewEntry(cart, productService.getProductForCode("testProduct2"), 2, null);
    cartService.addNewEntry(cart, productService.getProductForCode("testProduct3"), 3, null);

    final AddressModel deliveryAddress = new AddressModel();
    deliveryAddress.setOwner(user);
    deliveryAddress.setFirstname("Der");
    deliveryAddress.setLastname("Buck");
    deliveryAddress.setTown("Muenchen");
    deliveryAddress.setCountry(commonI18NService.getCountry("DE"));
    modelService.save(deliveryAddress);

    final DebitPaymentInfoModel paymentInfo = new DebitPaymentInfoModel();
    paymentInfo.setOwner(cart);
    paymentInfo.setBank("MeineBank");
    paymentInfo.setUser(user);
    paymentInfo.setAccountNumber("34434");
    paymentInfo.setBankIDNumber("1111112");
    paymentInfo.setBaOwner("Ich");
    paymentInfo.setCode("testPaymentInfo1");
    modelService.save(paymentInfo);

    cart.setDeliveryMode(deliveryService.getDeliveryModeForCode("free"));
    cart.setDeliveryAddress(deliveryAddress);
    cart.setPaymentInfo(paymentInfo);

    final CardInfo card = new CardInfo();
    card.setCardType(CreditCardType.VISA);
    card.setCardNumber("4111111111111111");
    card.setExpirationMonth(Integer.valueOf(12));
    if (valid) {
      card.setExpirationYear(Integer.valueOf(Calendar.getInstance().get(Calendar.YEAR) + 2));
    } else {
      card.setExpirationYear(Integer.valueOf(Calendar.getInstance().get(Calendar.YEAR) - 2));
    }

    final PaymentTransactionModel paymentTransaction =
        paymentService
            .authorize(
                "code4" + codeNo++,
                BigDecimal.ONE,
                Currency.getInstance("EUR"),
                deliveryAddress,
                deliveryAddress,
                card)
            .getPaymentTransaction();

    cart.setPaymentTransactions(Collections.singletonList(paymentTransaction));
    modelService.save(cart);
    calculationService.calculate(cart);

    final CommerceCheckoutParameter parameter = new CommerceCheckoutParameter();
    parameter.setEnableHooks(true);
    parameter.setCart(cart);
    parameter.setSalesApplication(SalesApplication.WEB);

    return commerceCheckoutService.placeOrder(parameter).getOrder();
  }