1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.order.webapp.controller.action;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  
22  import net.sf.oness.common.model.temporal.Date;
23  import net.sf.oness.common.webapp.controller.testing.JMockStrutsTestCase;
24  import net.sf.oness.order.model.bo.Order;
25  import net.sf.oness.order.model.bo.OrderItem;
26  import net.sf.oness.order.model.facade.OrderFacadeDelegate;
27  import net.sf.oness.order.model.to.OrderWithParty;
28  import net.sf.oness.order.webapp.controller.actionform.MasterActionForm;
29  import net.sf.oness.party.model.party.bo.Person;
30  
31  import org.jmock.Mock;
32  
33  /***
34   * @author Carlos Sanchez
35   * @version $Revision: 1.6 $
36   */
37  public class OrderItemActionTest extends JMockStrutsTestCase {
38  
39      private Mock mockFacade;
40  
41      private OrderItem orderItem;
42  
43      private OrderWithParty orderWithParty;
44  
45      /***
46       * @see junit.framework.TestCase#setUp()
47       */
48      protected void setUp() throws Exception {
49          super.setUp();
50          mockFacade = new Mock(OrderFacadeDelegate.class);
51          getMockWebApplicationContext().expects(atLeastOnce()).method("getBean")
52                  .with(eq("orderFacadeDelegate")).will(
53                          returnValue(mockFacade.proxy()));
54  
55          orderItem = new OrderItem();
56          orderItem.setQuantity(new BigDecimal("10"));
57          orderItem.setPrice(null);
58          orderItem.setProductId(new Long(1));
59  
60          orderWithParty = new OrderWithParty();
61          Order order = new Order();
62          order.setDate(Date.now());
63          orderWithParty.setOrder(order);
64          orderWithParty.setParty(new Person());
65          orderWithParty.setItems(new ArrayList(0));
66      }
67  
68      public void testCreate() {
69  
70          /* Test normal operation */
71          Long orderId = new Long(1);
72          Collection c = new ArrayList(2);
73          c.add(orderItem);
74          OrderItem oi = (OrderItem) orderItem.clone();
75          oi.setProductId(new Long(2));
76          oi.setQuantity(new BigDecimal("20"));
77          c.add(oi);
78  
79          /* setup mock object */
80          mockFacade.expects(once()).method("createOrderItems").with(eq(c),
81                  eq(orderId)).will(returnValue(orderWithParty));
82          struts().setRequestPathInfo("/orderItem/create");
83          struts().addRequestParameter("itemsSize", "2");
84          struts().addRequestParameter("items[0].productId", "1");
85          struts().addRequestParameter("items[0].quantity", "10");
86          struts().addRequestParameter("items[1].productId", "2");
87          struts().addRequestParameter("items[1].quantity", "20");
88          //        struts().addRequestParameter("price",
89          // orderItem.getPrice().toString());
90  
91          struts().getRequest().getSession().setAttribute(
92                  AbstractAction.CURRENT_ORDER_ID_SESSION_ATTRIBUTE, orderId);
93  
94          struts().actionPerform();
95          struts().verifyNoActionErrors();
96  
97          assertNotNull(((MasterActionForm) struts().getActionForm()).getDate());
98  
99          mockFacade.verify();
100     }
101 
102     public void testWhenNoCurrentOrder() {
103         getMockWebApplicationContext().reset();
104 
105         /* Test when there's no current order */
106         struts().setRequestPathInfo("/orderItem/create");
107         struts().addRequestParameter("itemsSize", "1");
108         struts().addRequestParameter("items[0].productId", "1");
109         struts().addRequestParameter("items[0].quantity", "10");
110         struts().actionPerform();
111         struts().verifyActionErrors(new String[] { "errors.noOrder" });
112     }
113 }