1 5 package com.openedit.modules.cart; 6 7 import java.util.Iterator ; 8 9 import org.openedit.money.Money; 10 11 import com.openedit.WebPageRequest; 12 import com.openedit.store.BaseOrderGenerator; 13 import com.openedit.store.Cart; 14 import com.openedit.store.CartItem; 15 import com.openedit.store.InventoryItem; 16 import com.openedit.store.Option; 17 import com.openedit.store.Order; 18 import com.openedit.store.OrderGenerator; 19 import com.openedit.store.Price; 20 import com.openedit.store.Product; 21 import com.openedit.store.ShippingMethod; 22 import com.openedit.store.Store; 23 import com.openedit.store.StoreException; 24 import com.openedit.store.StoreTestCase; 25 import com.openedit.store.adjustments.SaleAdjustment; 26 import com.openedit.store.customer.Customer; 27 import com.openedit.store.shipping.WeightBasedShippingMethod; 28 29 33 public class CartTest extends StoreTestCase 34 { 35 public CartTest(String arg0) 36 { 37 super(arg0); 38 } 39 40 public void testUpdateItemQuantity() throws Exception 41 { 42 CartModule cartModule = (CartModule) getFixture().getModuleManager() 43 .getModule("CartModule"); 44 45 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 46 Store store = cartModule.getStore(context); 47 assertNotNull(store); 48 49 final String RED_ITEM_SKU = "2001"; 50 final String BLACK_ITEM_SKU = "2002"; 51 52 context.setRequestParameter("productid", "2"); 53 context.setRequestParameter("color", "red"); 54 context.setRequestParameter("size", "NB"); 55 cartModule.updateCart(context); 56 57 Cart cart = (Cart) context.getSessionValue("cart"); 58 assertNotNull(cart); 59 assertEquals(1, cart.getNumItems()); 60 61 CartItem firstItem = (CartItem) cart.getItemIterator().next(); 62 assertEquals(RED_ITEM_SKU, firstItem.getSku()); 63 firstItem.setQuantity(3); 64 65 WebPageRequest updatecontext = getFixture().createPageRequest("/store/index.html"); 67 updatecontext.putSessionValue("cart", cart); 68 69 updatecontext.setRequestParameter("reloadcart", "true"); 70 updatecontext.setRequestParameter("productid.1", "2"); 71 updatecontext.setRequestParameter("quantity.1", "5"); 72 updatecontext.setRequestParameter("color.1", "black"); 73 updatecontext.setRequestParameter("size.1", "large"); cartModule.updateCart(updatecontext); 75 76 assertEquals(1, cart.getNumItems()); 77 Iterator iter = cart.getItemIterator(); 78 firstItem = (CartItem) iter.next(); 79 assertEquals(BLACK_ITEM_SKU, firstItem.getSku()); 80 assertEquals(5, firstItem.getQuantity()); 81 82 updatecontext.setRequestParameter("remove.1", "x"); 83 cartModule.updateCart(updatecontext); 84 85 assertEquals(0, cart.getNumItems()); 86 } 87 88 public void testUpdateNoSizeNoColor() throws Exception 89 { 90 CartModule cartModule = (CartModule) getFixture().getModuleManager() 91 .getModule("CartModule"); 92 93 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 94 95 Store store = cartModule.getStore(context); 96 assertNotNull(store); 97 98 context.setRequestParameter("productid", "3"); 99 cartModule.updateCart(context); 102 103 Cart cart = (Cart) context.getSessionValue("cart"); 104 assertNotNull(cart); 105 assertEquals(1, cart.getNumItems()); 106 107 final String NOSIZE_ITEM_SKU = "1234"; 108 109 CartItem firstItem = (CartItem) cart.getItemIterator().next(); 110 assertEquals(NOSIZE_ITEM_SKU, firstItem.getSku()); 111 firstItem.setQuantity(3); 112 113 WebPageRequest updatecontext = getFixture().createPageRequest("/store/index.html"); 114 updatecontext.putSessionValue("cart", cart); 115 116 updatecontext.setRequestParameter("reloadcart", "true"); 118 updatecontext.setRequestParameter("productid.1", "3"); 119 updatecontext.setRequestParameter("quantity.1", "5"); 120 cartModule.updateCart(updatecontext); 123 124 assertEquals(1, cart.getNumItems()); 125 firstItem = (CartItem) cart.getItemIterator().next(); 126 assertEquals(NOSIZE_ITEM_SKU, firstItem.getSku()); 127 assertEquals(5, firstItem.getQuantity()); 128 129 updatecontext.setRequestParameter("remove.1", "x"); 130 cartModule.updateCart(updatecontext); 131 132 assertEquals(0, cart.getNumItems()); 133 } 134 135 public void testUpdateSpecialRequest() throws Exception 136 { 137 CartModule cartModule = (CartModule) getFixture().getModuleManager() 138 .getModule("CartModule"); 139 140 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 141 Store store = cartModule.getStore(context); 142 assertNotNull(store); 143 144 context.setRequestParameter("productid", "3"); 145 context.setRequestParameter("option.specialrequest", "My special order"); 146 cartModule.updateCart(context); 147 148 Cart cart = (Cart) context.getSessionValue("cart"); 149 assertNotNull(cart); 150 assertEquals(1, cart.getNumItems()); 151 152 final String ITEM_SKU = "1234"; 153 154 CartItem firstItem = (CartItem) cart.getItems().get(0); 155 assertEquals("My special order", firstItem.getOption("specialrequest").getValue()); 156 assertEquals(ITEM_SKU, firstItem.getSku()); 157 assertEquals(1, firstItem.getQuantity()); 158 159 firstItem.setQuantity(3); 160 161 WebPageRequest updatecontext = getFixture().createPageRequest(); 162 updatecontext.putSessionValue("cart", cart); 163 updatecontext.setRequestParameter("reloadcart", "true"); 164 updatecontext.setRequestParameter("productid.1", "3"); 166 updatecontext.setRequestParameter("quantity.1", "5"); 167 updatecontext.setRequestParameter("option.1.specialrequest", "My special order"); 168 cartModule.updateCart(updatecontext); 169 170 assertEquals(1, cart.getNumItems()); 172 firstItem = (CartItem) cart.getItems().get(0); 173 assertEquals(ITEM_SKU, firstItem.getSku()); 174 assertEquals(5, firstItem.getQuantity()); 175 assertEquals("My special order", firstItem.getOption("specialrequest").getValue()); 176 } 177 178 public void testSameProductTwoColors() throws Exception 179 { 180 CartModule cartModule = (CartModule) getFixture().getModuleManager() 181 .getModule("CartModule"); 182 183 WebPageRequest context = getFixture().createPageRequest(); 184 Store store = cartModule.getStore(context); 185 assertNotNull(store); 186 187 context.setRequestParameter("productid", "2"); 188 context.setRequestParameter("size", "1"); 189 context.setRequestParameter("color", "red"); 190 cartModule.updateCart(context); 191 Cart cart = (Cart) context.getSessionValue("cart"); 192 193 context.setRequestParameter("productid", "2"); 194 context.setRequestParameter("size", "1"); 195 context.setRequestParameter("color", "blue"); 196 cartModule.updateCart(context); 197 198 assertNotNull(cart); 199 assertEquals(2, cart.getNumItems()); 200 201 CartItem item = (CartItem) cart.getItems().get(0); 202 assertEquals("2003", item.getSku()); 203 assertTrue(item.isColor("red")); 204 205 item = (CartItem) cart.getItems().get(1); 206 assertEquals("2004", item.getSku()); 207 208 assertEquals("blue", item.getColor().getValue()); 209 210 assertTrue(item.isColor("blue")); 211 } 212 213 public void testListColors() throws Exception 215 { 216 Store store = getStore(); 217 Product two = store.getProduct("2"); 218 } 220 221 public void testShippingAndOptions() throws Exception 222 { 223 CartModule cartModule = (CartModule) getFixture().getModuleManager() 224 .getModule("CartModule"); 225 226 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 227 Store store = cartModule.getStore(context); 228 assertNotNull(store); 229 assertTrue(store.getAllShippingMethods().size() > 0); 230 231 context.setRequestParameter("productid", "1"); 232 context.setRequestParameter("option.domainname", "http://domain.com"); 233 cartModule.updateCart(context); 234 235 Cart cart = (Cart) context.getSessionValue("cart"); 236 assertNotNull(cart); 237 assertEquals(cart.getNumItems(), 1); 238 239 ShippingMethod standard = store.findShippingMethod("1"); 240 ShippingMethod nextDay = store.findShippingMethod("3"); 241 ShippingMethod over200 = store.findShippingMethod("4"); 242 assertTrue(cart.getAvailableShippingMethods().contains(standard)); 243 assertTrue(cart.getAvailableShippingMethods().contains(nextDay)); 244 assertFalse(cart.getAvailableShippingMethods().contains(over200)); 245 246 Money costs = standard.getCost(cart); 247 assertEquals(new Money("6.00"), costs); 248 249 Money shippingCostForNextDay = nextDay.getCost(cart); 250 assertEquals(new Money("2.00"), shippingCostForNextDay); 251 252 costs = over200.getCost(cart); 253 assertEquals(new Money("0.00"), costs); 254 255 CartItem firstItem = (CartItem) cart.getItemIterator().next(); 256 firstItem.setQuantity(12); 257 258 Option option = firstItem.getOption("domainname"); 259 assertNotNull(option); 260 assertEquals(option.getValue(), "http://domain.com"); 261 262 cart.setCustomer(createCustomer()); 263 context.putSessionValue("cart", cart); 264 int quantityInStock = firstItem.getInventoryItem().getQuantityInStock(); 266 267 Order order = cartModule.processOrder(context); 268 assertTrue(order.getOrderState().isOk()); 269 CartItem item = (CartItem) order.getItems().get(0); 270 271 assertEquals(item.getInventoryItem().getQuantityInStock(), quantityInStock 272 - firstItem.getQuantity()); 273 assertEquals(cart.getShippingMethod(), over200); 274 275 option = item.getOption("domainname"); 276 assertNotNull(option); 277 assertEquals(option.getValue(), "http://domain.com"); 278 279 } 280 281 public void testShipByWeight() throws Exception 282 { 283 284 CartModule cartModule = (CartModule) getFixture().getModuleManager() 285 .getModule("CartModule"); 286 287 WebPageRequest context = getFixture().createPageRequest(); 288 Store store = cartModule.getStore(context); 289 assertNotNull(store); 290 assertTrue(store.getAllShippingMethods().size() > 0); 291 292 context.setRequestParameter("productid", "2"); 293 context.setRequestParameter("color", "red"); 294 context.setRequestParameter("size", "NB"); 295 cartModule.updateCart(context); 296 297 Cart cart = (Cart) context.getSessionValue("cart"); 298 assertNotNull(cart); 299 assertTrue(cart.getNumItems() > 0); 300 301 WeightBasedShippingMethod weight = (WeightBasedShippingMethod) store 302 .findShippingMethod("5"); 303 304 Money shippingCostForNextDay = weight.getCost(cart); 306 assertEquals(new Money("3.00"), shippingCostForNextDay); 307 308 } 309 310 public void testCoupon() throws Exception 311 { 312 CartModule cartModule = (CartModule) getFixture().getModuleManager() 315 .getModule("CartModule"); 316 317 WebPageRequest context = getFixture().createPageRequest(); 318 Cart cart = cartModule.getCart(context); 319 320 context.setRequestParameter("productid", "2"); 321 context.setRequestParameter("color", "red"); 322 context.setRequestParameter("size", "NB"); 323 cartModule.updateCart(context); 324 assertEquals(1, cart.getNumItems()); 325 326 assertEquals(19.95, cart.getSubTotal().doubleValue(), .01); 328 context.setRequestParameter("couponcode", "1234"); 329 cartModule.addCoupon(context); 330 assertEquals(14.95, cart.getSubTotal().doubleValue(), .01); 331 332 } 333 334 public void testHandling() throws Exception 335 { 336 CartModule cartModule = (CartModule) getFixture().getModuleManager() 337 .getModule("CartModule"); 338 339 WebPageRequest context = getFixture().createPageRequest(); 340 Store store = cartModule.getStore(context); 341 assertNotNull(store); 342 343 context.setRequestParameter("productid", "2"); 344 345 cartModule.updateCart(context); 346 347 Cart cart = (Cart) context.getSessionValue("cart"); 348 assertNotNull(cart); 349 assertTrue(cart.getNumItems() > 0); 350 351 ShippingMethod standard = store.findShippingMethod("1"); 352 assertNotNull(standard); 353 ShippingMethod threeDay = store.findShippingMethod("2"); 354 assertNotNull(threeDay); 355 356 CartItem firstItem = (CartItem) cart.getItemIterator().next(); 357 firstItem.setQuantity(2); 358 359 cart.setShippingMethod(standard); 360 assertEquals(new Money("36.00"), cart.getTotalShipping()); 361 assertFalse(cart.isAdditionalShippingCosts()); 362 363 cart.setShippingMethod(threeDay); 364 Money totalShipping = cart.getTotalShipping(); 365 assertEquals(new Money("62.00"), totalShipping); 366 assertTrue(cart.isAdditionalShippingCosts()); 367 } 368 369 public void testCustomPrice() throws Exception 370 { 371 374 final double INVOICE_PRICE = 15557.81; 375 376 CartModule cartModule = (CartModule) getFixture().getModuleManager() 377 .getModule("CartModule"); 378 379 WebPageRequest context = getFixture().createPageRequest(); 380 Store store = cartModule.getStore(context); 381 assertNotNull(store); 382 383 context.setRequestParameter("productid", "1"); 384 context.setRequestParameter("price", String.valueOf(INVOICE_PRICE)); 385 cartModule.updateCart(context); 386 387 Cart cart = (Cart) context.getSessionValue("cart"); 388 assertNotNull(cart); 389 assertTrue(cart.getNumItems() > 0); 390 assertEquals(new Money("19.95"), cart.getSubTotal()); 391 cart.removeAllItems(); 392 393 Product product = store.getProduct("1"); 394 product.setCustomPrice(true); 395 context.setRequestParameter("productid", "1"); 396 context.setRequestParameter("price", String.valueOf(INVOICE_PRICE)); 397 cartModule.updateCart(context); 398 product.setCustomPrice(false); 399 assertEquals(new Money(INVOICE_PRICE), cart.getSubTotal()); 400 } 401 402 public void XXXtestBackOrder() throws StoreException 403 { 404 CartItem cheapToy = createCheapToyCartItem(); 405 cheapToy.setQuantity(cheapToy.getInventoryItem().getQuantityInStock() + 1); 407 assertTrue(cheapToy.isBackOrdered()); 408 } 409 410 public void testCart() throws StoreException 411 { 412 Product collegeTextbookProduct = new Product("college textbook"); 413 final double COLLEGE_TEXTBOOK_PRICE = 94.00; 414 collegeTextbookProduct.addTierPrice(1, createPrice(COLLEGE_TEXTBOOK_PRICE)); 415 InventoryItem item = new InventoryItem(); 416 item.setQuantityInStock(10); 417 item.setSku("textbook"); 418 collegeTextbookProduct.addInventoryItem(item); 419 CartItem citem = new CartItem(); 420 citem.setInventoryItem(item); 421 citem.setQuantity(1); 422 Cart cart = new Cart(); 423 cart.addItem(citem); 424 assertEquals(new Money(94.00), cart.getTotalPrice()); 425 426 CartItem cheapToy = createCheapToyCartItem(); 427 cart.addItem(cheapToy); 428 Money m1 = cart.getTotalPrice(); 429 Money m2 = new Money(103.95); 430 assertEquals(m2, m1); 431 432 cart.removeItem(cheapToy); 434 Product brandNewCarProduct = new Product("brand new car"); 435 final double BRAND_NEW_CAR_PRICE = 25000.0; 436 Price newCarPrice = createPrice(BRAND_NEW_CAR_PRICE); 437 brandNewCarProduct.addTierPrice(1, newCarPrice); 438 item = new InventoryItem(); 439 item.setQuantityInStock(10); 440 item.setSku("car"); 441 brandNewCarProduct.addInventoryItem(item); 442 443 CartItem citem2 = new CartItem(); 444 citem2.setInventoryItem(item); 445 citem2.setQuantity(1); 446 447 cart.addItem(citem2); 448 assertEquals(new Money(94.0 + 25000.0), cart.getTotalPrice()); 449 450 Option powerWindows = new Option(); 452 Price powerWindowsPrice = createPrice(123.0); 453 powerWindows.addTierPrice(1, powerWindowsPrice); 454 powerWindows.setId("powerwindows"); 455 citem2.addOption(powerWindows); 456 457 assertEquals(new Money(94.0 + 25000.0 + 123.0), cart.getTotalPrice()); 458 459 cart.removeAllItems(); 460 assertEquals(cart.getTotalPrice(), Money.ZERO); 461 } 462 463 public void testStartCheckOut() throws Exception 464 { 465 CartModule cartModule = (CartModule) getFixture().getModuleManager() 466 .getModule("CartModule"); 467 WebPageRequest context = getFixture().createPageRequest(); 468 context.setRequestParameter("productid", "1"); 469 472 cartModule.updateCart(context); 473 474 Cart cart = (Cart) context.getSessionValue("cart"); 475 assertNotNull(cart); 476 assertTrue(cart.getNumItems() == 1); 477 CartItem item = (CartItem) cart.getItems().get(0); 478 assertEquals(item.getProduct().getId(), "1"); 479 CartItem cheapToy = createCheapToyCartItem(); 480 cart.addItem(cheapToy); 481 assertEquals(cart.getItems().indexOf(cheapToy), 1); 482 cart.removeItem(item); 483 assertEquals(cart.getItems().indexOf(cheapToy), 0); 484 } 485 486 491 492 public void testApplyAdjustments() throws Exception 493 { 494 Product brandNewCar = new Product("brand new car"); 495 496 brandNewCar.addTierPrice(1, createPrice(25000)); 497 brandNewCar.addTierPrice(2, createPrice(23000)); 498 brandNewCar.addTierPrice(4, createPrice(22000)); 499 500 InventoryItem item = new InventoryItem(); 501 item.setSku("car"); 502 brandNewCar.addInventoryItem(item); 503 CartItem car = new CartItem(); 504 car.setQuantity(2); 505 car.setInventoryItem(item); 506 507 Cart cart = new Cart(); 508 cart.addItem(car); 509 assertNotNull(cart); 510 511 Money newSubtotal = cart.getSubTotal(); 512 assertEquals(46000D, newSubtotal.doubleValue(), .001); 513 car.setQuantity(3); 514 515 newSubtotal = cart.getSubTotal(); 516 assertEquals(69000D, newSubtotal.doubleValue(), .001); 517 518 car.setQuantity(5); 519 newSubtotal = cart.getSubTotal(); 520 assertEquals(110000, newSubtotal.doubleValue(), .001); 521 522 SaleAdjustment adjustment = new SaleAdjustment(); 523 adjustment.setPercentDiscount(20); 524 cart.addAdjustment(adjustment); 525 526 assertEquals(88000, cart.getSubTotal().doubleValue(), 0.001); 527 528 } 529 530 534 public void testCheckOut() throws Exception 535 { 536 CartModule cartModule = (CartModule) getFixture().getModuleManager() 537 .getModule("CartModule"); 538 539 WebPageRequest context = getFixture().createPageRequest(); 540 context.setRequestParameter("productid", "1"); 541 context.setRequestParameter("size", "large"); 542 543 cartModule.updateCart(context); 544 545 context.setRequestParameter("firstName", "John"); 546 context.setRequestParameter("lastName", "Doe"); 547 context.setRequestParameter("email", "cburkey@einnovation.com"); 548 context.setRequestParameter("shippingaddress1", "49 East Slum St."); 549 context.setRequestParameter("shippingaddress2", "Apartment 2000"); 550 context.setRequestParameter("shippingcity", "Cincinnati"); 551 context.setRequestParameter("shippingstate", "OH"); 552 context.setRequestParameter("shippingcountry", "USA"); 553 context.setRequestParameter("shippingzipCode", "45202"); 554 cartModule.createCustomer(context); 555 cartModule.updateCustomer(context); 556 557 context.setRequestParameter("billingaddress1", "New Address"); 558 559 context.setRequestParameter("cardType", "Visa"); 560 context.setRequestParameter("cardNumber", "4245123456780909"); 561 context.setRequestParameter("expirationMonth", "12"); 562 context.setRequestParameter("expirationYear", "2007"); 563 cartModule.updateCustomer(context); 564 cartModule.saveCreditPaymentMethodData(context); 565 Cart cart = (Cart) context.getSessionValue("cart"); 566 assertEquals("New Address", cart.getCustomer().getBillingAddress().getAddress1()); 567 568 Money total = cart.getTotalPrice(); 569 assertEquals("$21.35", total.toString()); 570 } 572 573 public void testCreateNewCustomer() throws Exception 574 { 575 CartModule cartModule = (CartModule) getFixture().getModuleManager() 577 .getModule("CartModule"); 578 579 WebPageRequest context = getFixture().createPageRequest(); 580 581 context = getFixture().createPageRequest(); 583 context.setRequestParameter("firstName", "John"); 584 context.setRequestParameter("lastName", "Doe"); 585 context.setRequestParameter("email", "cburkey@einnovation.com"); 586 context.setRequestParameter("shippingaddress1", "49 East Slum St."); 587 context.setRequestParameter("shippingaddress2", "Apartment 2000"); 588 context.setRequestParameter("shippingcity", "Cincinnati"); 589 context.setRequestParameter("shippingstate", "OH"); 590 context.setRequestParameter("shippingcountry", "USA"); 591 context.setRequestParameter("shippingzipCode", "45202"); 592 cartModule.createCustomer(context); 593 cartModule.updateCustomer(context); 594 595 Cart cart = cartModule.getCart(context); 596 Customer customer = cart.getCustomer(); 597 assertTrue(customer.getShippingAddress().getCity().equals("Cincinnati")); 598 assertNotNull(customer.getUserName()); 599 600 String id = customer.getUserName(); 601 602 604 context = getFixture().createPageRequest(); 606 context.setRequestParameter("productid", "1"); 608 context.setRequestParameter("size", "large"); 609 cartModule.updateCart(context); 610 611 context.setRequestParameter("customerId", id); 612 cartModule.loadCustomer(context); 613 614 cart = cartModule.getCart(context); 615 customer = cart.getCustomer(); 616 assertNotNull(customer); 617 assertNotNull(customer.getShippingAddress().getCity()); 618 assertTrue(customer.getShippingAddress().getCity().equals("Cincinnati")); 619 620 context.setRequestParameter("cardType", "Visa"); 621 context.setRequestParameter("cardNumber", "4245123456780909"); 622 context.setRequestParameter("expirationMonth", "12"); 623 context.setRequestParameter("expirationYear", "2007"); 624 cartModule.saveCreditPaymentMethodData(context); 625 Money total = cart.getTotalPrice(); 626 assertEquals("$21.35", total.toString()); 627 628 } 629 630 public void testNextOrderNumber() throws Exception 631 { 632 OrderGenerator orderGenerator = new BaseOrderGenerator(); 633 String orderNumber = orderGenerator.nextOrderNumber(getStore()); 634 assertEquals(10, orderNumber.length()); 635 String nextOrderNumber = orderGenerator.nextOrderNumber(getStore()); 636 System.out.println(orderNumber + ", " + nextOrderNumber); 637 assertFalse(nextOrderNumber.equals(orderNumber)); 638 } 639 640 public void testAddsRequiredOptions() throws Exception 641 { 642 CartModule cartModule = (CartModule) getFixture().getModuleManager() 643 .getModule("CartModule"); 644 645 WebPageRequest context = getFixture().createPageRequest(); 646 Store store = cartModule.getStore(context); 647 assertNotNull(store); 648 649 context.setRequestParameter("productid", "handling"); 650 cartModule.updateCart(context); 651 652 Cart cart = (Cart) context.getSessionValue("cart"); 653 assertNotNull(cart); 654 assertEquals(1, cart.getNumItems()); 655 656 CartItem firstItem = (CartItem) cart.getItems().get(0); 657 assertTrue(firstItem.hasOption("handling")); 658 String totalPrice = cart.getTotalPrice().toShortString(); 659 assertEquals("28.00", totalPrice); 660 } 661 } 662 | Popular Tags |