@Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    ShoppingOrder existingShoppingOrder =
        _persistence.fetchByPrimaryKey(newShoppingOrder.getPrimaryKey());

    Assert.assertEquals(existingShoppingOrder, newShoppingOrder);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    ShoppingOrder shoppingOrder = _persistence.create(pk);

    Assert.assertNotNull(shoppingOrder);

    Assert.assertEquals(shoppingOrder.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    _persistence.remove(newShoppingOrder);

    ShoppingOrder existingShoppingOrder =
        _persistence.fetchByPrimaryKey(newShoppingOrder.getPrimaryKey());

    Assert.assertNull(existingShoppingOrder);
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newShoppingOrder.getPrimaryKey());

    Map<Serializable, ShoppingOrder> shoppingOrders = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, shoppingOrders.size());
    Assert.assertEquals(newShoppingOrder, shoppingOrders.get(newShoppingOrder.getPrimaryKey()));
  }
  /**
   * Adds the shopping order to the database. Also notifies the appropriate model listeners.
   *
   * @param shoppingOrder the shopping order
   * @return the shopping order that was added
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public ShoppingOrder addShoppingOrder(ShoppingOrder shoppingOrder) {
    shoppingOrder.setNew(true);

    return shoppingOrderPersistence.update(shoppingOrder);
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(ShoppingOrder.class, _dynamicQueryClassLoader);

    dynamicQuery.add(RestrictionsFactoryUtil.eq("orderId", newShoppingOrder.getOrderId()));

    List<ShoppingOrder> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    ShoppingOrder existingShoppingOrder = result.get(0);

    Assert.assertEquals(existingShoppingOrder, newShoppingOrder);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newShoppingOrder.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, ShoppingOrder> shoppingOrders = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, shoppingOrders.size());
    Assert.assertEquals(newShoppingOrder, shoppingOrders.get(newShoppingOrder.getPrimaryKey()));
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    _persistence.clearCache();

    ShoppingOrder existingShoppingOrder =
        _persistence.findByPrimaryKey(newShoppingOrder.getPrimaryKey());

    Assert.assertTrue(
        Objects.equals(
            existingShoppingOrder.getNumber(),
            ReflectionTestUtil.invoke(
                existingShoppingOrder, "getOriginalNumber", new Class<?>[0])));

    Assert.assertTrue(
        Objects.equals(
            existingShoppingOrder.getPpTxnId(),
            ReflectionTestUtil.invoke(
                existingShoppingOrder, "getOriginalPpTxnId", new Class<?>[0])));
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    ShoppingOrder newShoppingOrder = addShoppingOrder();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(ShoppingOrder.class, _dynamicQueryClassLoader);

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("orderId"));

    Object newOrderId = newShoppingOrder.getOrderId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("orderId", new Object[] {newOrderId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingOrderId = result.get(0);

    Assert.assertEquals(existingOrderId, newOrderId);
  }
  protected void forwardCheckout(
      ActionRequest actionRequest, ActionResponse actionResponse, ShoppingOrder order)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);

    ShoppingWebComponentProvider shoppingWebComponentProvider =
        ShoppingWebComponentProvider.getShoppingWebComponentProvider();

    SettingsFactory settingsFactory = shoppingWebComponentProvider.getSettingsFactory();

    ShoppingGroupServiceSettings shoppingGroupServiceSettings =
        settingsFactory.getSettings(
            ShoppingGroupServiceSettings.class,
            new GroupServiceSettingsLocator(
                themeDisplay.getScopeGroupId(), ShoppingConstants.SERVICE_NAME));

    String returnURL =
        ShoppingUtil.getPayPalReturnURL(
            ((ActionResponseImpl) actionResponse).createActionURL(), order);
    String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);

    if (shoppingGroupServiceSettings.usePayPal()) {
      double total =
          ShoppingUtil.calculateTotal(
              cart.getItems(),
              order.getBillingState(),
              cart.getCoupon(),
              cart.getAltShipping(),
              cart.isInsure());

      String redirectURL =
          ShoppingUtil.getPayPalRedirectURL(
              shoppingGroupServiceSettings, order, total, returnURL, notifyURL);

      actionResponse.sendRedirect(redirectURL);
    } else {
      ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

      ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation", serviceContext);

      actionResponse.sendRedirect(returnURL);
    }
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    ShoppingOrder newShoppingOrder1 = addShoppingOrder();
    ShoppingOrder newShoppingOrder2 = addShoppingOrder();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newShoppingOrder1.getPrimaryKey());
    primaryKeys.add(newShoppingOrder2.getPrimaryKey());

    Map<Serializable, ShoppingOrder> shoppingOrders = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, shoppingOrders.size());
    Assert.assertEquals(newShoppingOrder1, shoppingOrders.get(newShoppingOrder1.getPrimaryKey()));
    Assert.assertEquals(newShoppingOrder2, shoppingOrders.get(newShoppingOrder2.getPrimaryKey()));
  }
  protected ShoppingOrder addShoppingOrder() throws Exception {
    long pk = RandomTestUtil.nextLong();

    ShoppingOrder shoppingOrder = _persistence.create(pk);

    shoppingOrder.setGroupId(RandomTestUtil.nextLong());

    shoppingOrder.setCompanyId(RandomTestUtil.nextLong());

    shoppingOrder.setUserId(RandomTestUtil.nextLong());

    shoppingOrder.setUserName(RandomTestUtil.randomString());

    shoppingOrder.setCreateDate(RandomTestUtil.nextDate());

    shoppingOrder.setModifiedDate(RandomTestUtil.nextDate());

    shoppingOrder.setNumber(RandomTestUtil.randomString());

    shoppingOrder.setTax(RandomTestUtil.nextDouble());

    shoppingOrder.setShipping(RandomTestUtil.nextDouble());

    shoppingOrder.setAltShipping(RandomTestUtil.randomString());

    shoppingOrder.setRequiresShipping(RandomTestUtil.randomBoolean());

    shoppingOrder.setInsure(RandomTestUtil.randomBoolean());

    shoppingOrder.setInsurance(RandomTestUtil.nextDouble());

    shoppingOrder.setCouponCodes(RandomTestUtil.randomString());

    shoppingOrder.setCouponDiscount(RandomTestUtil.nextDouble());

    shoppingOrder.setBillingFirstName(RandomTestUtil.randomString());

    shoppingOrder.setBillingLastName(RandomTestUtil.randomString());

    shoppingOrder.setBillingEmailAddress(RandomTestUtil.randomString());

    shoppingOrder.setBillingCompany(RandomTestUtil.randomString());

    shoppingOrder.setBillingStreet(RandomTestUtil.randomString());

    shoppingOrder.setBillingCity(RandomTestUtil.randomString());

    shoppingOrder.setBillingState(RandomTestUtil.randomString());

    shoppingOrder.setBillingZip(RandomTestUtil.randomString());

    shoppingOrder.setBillingCountry(RandomTestUtil.randomString());

    shoppingOrder.setBillingPhone(RandomTestUtil.randomString());

    shoppingOrder.setShipToBilling(RandomTestUtil.randomBoolean());

    shoppingOrder.setShippingFirstName(RandomTestUtil.randomString());

    shoppingOrder.setShippingLastName(RandomTestUtil.randomString());

    shoppingOrder.setShippingEmailAddress(RandomTestUtil.randomString());

    shoppingOrder.setShippingCompany(RandomTestUtil.randomString());

    shoppingOrder.setShippingStreet(RandomTestUtil.randomString());

    shoppingOrder.setShippingCity(RandomTestUtil.randomString());

    shoppingOrder.setShippingState(RandomTestUtil.randomString());

    shoppingOrder.setShippingZip(RandomTestUtil.randomString());

    shoppingOrder.setShippingCountry(RandomTestUtil.randomString());

    shoppingOrder.setShippingPhone(RandomTestUtil.randomString());

    shoppingOrder.setCcName(RandomTestUtil.randomString());

    shoppingOrder.setCcType(RandomTestUtil.randomString());

    shoppingOrder.setCcNumber(RandomTestUtil.randomString());

    shoppingOrder.setCcExpMonth(RandomTestUtil.nextInt());

    shoppingOrder.setCcExpYear(RandomTestUtil.nextInt());

    shoppingOrder.setCcVerNumber(RandomTestUtil.randomString());

    shoppingOrder.setComments(RandomTestUtil.randomString());

    shoppingOrder.setPpTxnId(RandomTestUtil.randomString());

    shoppingOrder.setPpPaymentStatus(RandomTestUtil.randomString());

    shoppingOrder.setPpPaymentGross(RandomTestUtil.nextDouble());

    shoppingOrder.setPpReceiverEmail(RandomTestUtil.randomString());

    shoppingOrder.setPpPayerEmail(RandomTestUtil.randomString());

    shoppingOrder.setSendOrderEmail(RandomTestUtil.randomBoolean());

    shoppingOrder.setSendShippingEmail(RandomTestUtil.randomBoolean());

    _shoppingOrders.add(_persistence.update(shoppingOrder));

    return shoppingOrder;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    ShoppingOrder newShoppingOrder = _persistence.create(pk);

    newShoppingOrder.setGroupId(RandomTestUtil.nextLong());

    newShoppingOrder.setCompanyId(RandomTestUtil.nextLong());

    newShoppingOrder.setUserId(RandomTestUtil.nextLong());

    newShoppingOrder.setUserName(RandomTestUtil.randomString());

    newShoppingOrder.setCreateDate(RandomTestUtil.nextDate());

    newShoppingOrder.setModifiedDate(RandomTestUtil.nextDate());

    newShoppingOrder.setNumber(RandomTestUtil.randomString());

    newShoppingOrder.setTax(RandomTestUtil.nextDouble());

    newShoppingOrder.setShipping(RandomTestUtil.nextDouble());

    newShoppingOrder.setAltShipping(RandomTestUtil.randomString());

    newShoppingOrder.setRequiresShipping(RandomTestUtil.randomBoolean());

    newShoppingOrder.setInsure(RandomTestUtil.randomBoolean());

    newShoppingOrder.setInsurance(RandomTestUtil.nextDouble());

    newShoppingOrder.setCouponCodes(RandomTestUtil.randomString());

    newShoppingOrder.setCouponDiscount(RandomTestUtil.nextDouble());

    newShoppingOrder.setBillingFirstName(RandomTestUtil.randomString());

    newShoppingOrder.setBillingLastName(RandomTestUtil.randomString());

    newShoppingOrder.setBillingEmailAddress(RandomTestUtil.randomString());

    newShoppingOrder.setBillingCompany(RandomTestUtil.randomString());

    newShoppingOrder.setBillingStreet(RandomTestUtil.randomString());

    newShoppingOrder.setBillingCity(RandomTestUtil.randomString());

    newShoppingOrder.setBillingState(RandomTestUtil.randomString());

    newShoppingOrder.setBillingZip(RandomTestUtil.randomString());

    newShoppingOrder.setBillingCountry(RandomTestUtil.randomString());

    newShoppingOrder.setBillingPhone(RandomTestUtil.randomString());

    newShoppingOrder.setShipToBilling(RandomTestUtil.randomBoolean());

    newShoppingOrder.setShippingFirstName(RandomTestUtil.randomString());

    newShoppingOrder.setShippingLastName(RandomTestUtil.randomString());

    newShoppingOrder.setShippingEmailAddress(RandomTestUtil.randomString());

    newShoppingOrder.setShippingCompany(RandomTestUtil.randomString());

    newShoppingOrder.setShippingStreet(RandomTestUtil.randomString());

    newShoppingOrder.setShippingCity(RandomTestUtil.randomString());

    newShoppingOrder.setShippingState(RandomTestUtil.randomString());

    newShoppingOrder.setShippingZip(RandomTestUtil.randomString());

    newShoppingOrder.setShippingCountry(RandomTestUtil.randomString());

    newShoppingOrder.setShippingPhone(RandomTestUtil.randomString());

    newShoppingOrder.setCcName(RandomTestUtil.randomString());

    newShoppingOrder.setCcType(RandomTestUtil.randomString());

    newShoppingOrder.setCcNumber(RandomTestUtil.randomString());

    newShoppingOrder.setCcExpMonth(RandomTestUtil.nextInt());

    newShoppingOrder.setCcExpYear(RandomTestUtil.nextInt());

    newShoppingOrder.setCcVerNumber(RandomTestUtil.randomString());

    newShoppingOrder.setComments(RandomTestUtil.randomString());

    newShoppingOrder.setPpTxnId(RandomTestUtil.randomString());

    newShoppingOrder.setPpPaymentStatus(RandomTestUtil.randomString());

    newShoppingOrder.setPpPaymentGross(RandomTestUtil.nextDouble());

    newShoppingOrder.setPpReceiverEmail(RandomTestUtil.randomString());

    newShoppingOrder.setPpPayerEmail(RandomTestUtil.randomString());

    newShoppingOrder.setSendOrderEmail(RandomTestUtil.randomBoolean());

    newShoppingOrder.setSendShippingEmail(RandomTestUtil.randomBoolean());

    _shoppingOrders.add(_persistence.update(newShoppingOrder));

    ShoppingOrder existingShoppingOrder =
        _persistence.findByPrimaryKey(newShoppingOrder.getPrimaryKey());

    Assert.assertEquals(existingShoppingOrder.getOrderId(), newShoppingOrder.getOrderId());
    Assert.assertEquals(existingShoppingOrder.getGroupId(), newShoppingOrder.getGroupId());
    Assert.assertEquals(existingShoppingOrder.getCompanyId(), newShoppingOrder.getCompanyId());
    Assert.assertEquals(existingShoppingOrder.getUserId(), newShoppingOrder.getUserId());
    Assert.assertEquals(existingShoppingOrder.getUserName(), newShoppingOrder.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingShoppingOrder.getCreateDate()),
        Time.getShortTimestamp(newShoppingOrder.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingShoppingOrder.getModifiedDate()),
        Time.getShortTimestamp(newShoppingOrder.getModifiedDate()));
    Assert.assertEquals(existingShoppingOrder.getNumber(), newShoppingOrder.getNumber());
    AssertUtils.assertEquals(existingShoppingOrder.getTax(), newShoppingOrder.getTax());
    AssertUtils.assertEquals(existingShoppingOrder.getShipping(), newShoppingOrder.getShipping());
    Assert.assertEquals(existingShoppingOrder.getAltShipping(), newShoppingOrder.getAltShipping());
    Assert.assertEquals(
        existingShoppingOrder.getRequiresShipping(), newShoppingOrder.getRequiresShipping());
    Assert.assertEquals(existingShoppingOrder.getInsure(), newShoppingOrder.getInsure());
    AssertUtils.assertEquals(existingShoppingOrder.getInsurance(), newShoppingOrder.getInsurance());
    Assert.assertEquals(existingShoppingOrder.getCouponCodes(), newShoppingOrder.getCouponCodes());
    AssertUtils.assertEquals(
        existingShoppingOrder.getCouponDiscount(), newShoppingOrder.getCouponDiscount());
    Assert.assertEquals(
        existingShoppingOrder.getBillingFirstName(), newShoppingOrder.getBillingFirstName());
    Assert.assertEquals(
        existingShoppingOrder.getBillingLastName(), newShoppingOrder.getBillingLastName());
    Assert.assertEquals(
        existingShoppingOrder.getBillingEmailAddress(), newShoppingOrder.getBillingEmailAddress());
    Assert.assertEquals(
        existingShoppingOrder.getBillingCompany(), newShoppingOrder.getBillingCompany());
    Assert.assertEquals(
        existingShoppingOrder.getBillingStreet(), newShoppingOrder.getBillingStreet());
    Assert.assertEquals(existingShoppingOrder.getBillingCity(), newShoppingOrder.getBillingCity());
    Assert.assertEquals(
        existingShoppingOrder.getBillingState(), newShoppingOrder.getBillingState());
    Assert.assertEquals(existingShoppingOrder.getBillingZip(), newShoppingOrder.getBillingZip());
    Assert.assertEquals(
        existingShoppingOrder.getBillingCountry(), newShoppingOrder.getBillingCountry());
    Assert.assertEquals(
        existingShoppingOrder.getBillingPhone(), newShoppingOrder.getBillingPhone());
    Assert.assertEquals(
        existingShoppingOrder.getShipToBilling(), newShoppingOrder.getShipToBilling());
    Assert.assertEquals(
        existingShoppingOrder.getShippingFirstName(), newShoppingOrder.getShippingFirstName());
    Assert.assertEquals(
        existingShoppingOrder.getShippingLastName(), newShoppingOrder.getShippingLastName());
    Assert.assertEquals(
        existingShoppingOrder.getShippingEmailAddress(),
        newShoppingOrder.getShippingEmailAddress());
    Assert.assertEquals(
        existingShoppingOrder.getShippingCompany(), newShoppingOrder.getShippingCompany());
    Assert.assertEquals(
        existingShoppingOrder.getShippingStreet(), newShoppingOrder.getShippingStreet());
    Assert.assertEquals(
        existingShoppingOrder.getShippingCity(), newShoppingOrder.getShippingCity());
    Assert.assertEquals(
        existingShoppingOrder.getShippingState(), newShoppingOrder.getShippingState());
    Assert.assertEquals(existingShoppingOrder.getShippingZip(), newShoppingOrder.getShippingZip());
    Assert.assertEquals(
        existingShoppingOrder.getShippingCountry(), newShoppingOrder.getShippingCountry());
    Assert.assertEquals(
        existingShoppingOrder.getShippingPhone(), newShoppingOrder.getShippingPhone());
    Assert.assertEquals(existingShoppingOrder.getCcName(), newShoppingOrder.getCcName());
    Assert.assertEquals(existingShoppingOrder.getCcType(), newShoppingOrder.getCcType());
    Assert.assertEquals(existingShoppingOrder.getCcNumber(), newShoppingOrder.getCcNumber());
    Assert.assertEquals(existingShoppingOrder.getCcExpMonth(), newShoppingOrder.getCcExpMonth());
    Assert.assertEquals(existingShoppingOrder.getCcExpYear(), newShoppingOrder.getCcExpYear());
    Assert.assertEquals(existingShoppingOrder.getCcVerNumber(), newShoppingOrder.getCcVerNumber());
    Assert.assertEquals(existingShoppingOrder.getComments(), newShoppingOrder.getComments());
    Assert.assertEquals(existingShoppingOrder.getPpTxnId(), newShoppingOrder.getPpTxnId());
    Assert.assertEquals(
        existingShoppingOrder.getPpPaymentStatus(), newShoppingOrder.getPpPaymentStatus());
    AssertUtils.assertEquals(
        existingShoppingOrder.getPpPaymentGross(), newShoppingOrder.getPpPaymentGross());
    Assert.assertEquals(
        existingShoppingOrder.getPpReceiverEmail(), newShoppingOrder.getPpReceiverEmail());
    Assert.assertEquals(
        existingShoppingOrder.getPpPayerEmail(), newShoppingOrder.getPpPayerEmail());
    Assert.assertEquals(
        existingShoppingOrder.getSendOrderEmail(), newShoppingOrder.getSendOrderEmail());
    Assert.assertEquals(
        existingShoppingOrder.getSendShippingEmail(), newShoppingOrder.getSendShippingEmail());
  }