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.Calendar;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import net.sf.oness.common.model.temporal.Date;
26  import net.sf.oness.common.model.util.CollectionUtils;
27  import net.sf.oness.common.model.util.PaginatedList;
28  import net.sf.oness.common.webapp.controller.action.ActionUtils;
29  import net.sf.oness.common.webapp.controller.testing.JMockStrutsTestCase;
30  import net.sf.oness.order.model.bo.DeliveryDocket;
31  import net.sf.oness.order.model.bo.DeliveryDocketItem;
32  import net.sf.oness.order.model.facade.OrderFacadeDelegate;
33  import net.sf.oness.order.model.to.DeliveryDocketWithParty;
34  import net.sf.oness.order.webapp.controller.actionform.MasterActionForm;
35  import net.sf.oness.party.model.party.bo.Person;
36  
37  import org.jmock.Mock;
38  
39  /***
40   * @author Carlos Sanchez
41   * @version $Revision: 1.2 $
42   */
43  public class DeliveryDocketActionTest extends JMockStrutsTestCase {
44  
45      private Mock mockFacade;
46  
47      private DeliveryDocket deliveryDocket;
48  
49      private DeliveryDocketItem deliveryDocketItem;
50  
51      private DeliveryDocketWithParty deliveryDocketWithParty;
52  
53      private Map deliveryDocketItems;
54  
55      /***
56       * @see junit.framework.TestCase#setUp()
57       */
58      protected void setUp() throws Exception {
59          super.setUp();
60          mockFacade = new Mock(OrderFacadeDelegate.class);
61          getMockWebApplicationContext().expects(atLeastOnce()).method("getBean")
62                  .with(eq("orderFacadeDelegate")).will(
63                          returnValue(mockFacade.proxy()));
64  
65          deliveryDocket = new DeliveryDocket();
66          deliveryDocket.setType(new Integer(1));
67          deliveryDocket.setPartyId(new Long(1));
68          deliveryDocket.setDate(new Date(2004, Calendar.JANUARY, 1));
69          deliveryDocket.setItems(CollectionUtils
70                  .newList(new DeliveryDocketItem()));
71  
72          deliveryDocketItem = new DeliveryDocketItem();
73          deliveryDocketItem.setProductId(new Long(1));
74          deliveryDocketItem.setQuantity(new BigDecimal("1"));
75  
76          deliveryDocketItems = new HashMap();
77          deliveryDocketItems.put(new Long(1), deliveryDocketItem);
78  
79          deliveryDocketWithParty = new DeliveryDocketWithParty();
80          deliveryDocketWithParty.setDeliveryDocket(deliveryDocket);
81          deliveryDocketWithParty.setParty(new Person());
82          deliveryDocketWithParty.setItems(CollectionUtils
83                  .newList(deliveryDocketItem));
84  
85      }
86  
87      final public void testCreate() {
88          /* setup mock object */
89          mockFacade.expects(once()).method("createDeliveryDocket").with(
90                  eq(deliveryDocket), eq(deliveryDocketItems)).will(
91                  returnValue(deliveryDocketWithParty));
92  
93          create("01/01/2004");
94  
95          struts().verifyNoActionErrors();
96  
97          MasterActionForm form = (MasterActionForm) struts().getActionForm();
98          assertNotNull(form.getDateAsString());
99  
100         assertNotNull("request attribute", struts().getRequest().getAttribute(
101                 "deliveryDocket"));
102         assertNotNull("request attribute", struts().getRequest().getAttribute(
103                 "party"));
104         mockFacade.verify();
105     }
106 
107     /*** check with an incorrect date */
108     final public void testCreateWithIncorrectDate() {
109         getMockWebApplicationContext().reset();
110         mockFacade.verify();
111 
112         struts().clearRequestParameters();
113 
114         create("01/01/");
115 
116         struts().verifyActionErrors(new String[] { "errors.invalid" });
117     }
118 
119     final private void create(String dateAsString) {
120         DeliveryDocket returned = (DeliveryDocket) deliveryDocket.clone();
121         returned.setId(new Long(1));
122 
123         struts().setRequestPathInfo("/deliveryDocket/create");
124         struts().addRequestParameter("dateAsString", dateAsString);
125         struts().addRequestParameter("partyId",
126                 deliveryDocket.getPartyId().toString());
127         struts().addRequestParameter("type", "1");
128         struts().addRequestParameter("itemsSize", "1");
129         struts().addRequestParameter("items[0].id", "1");
130         struts().addRequestParameter("items[0].productId", "1");
131         struts().addRequestParameter("items[0].quantity", "1");
132 
133         struts().actionPerform();
134     }
135 
136     final public void testFind() throws Exception {
137         DeliveryDocket criteria = new DeliveryDocket();
138         criteria.setPartyId(new Long(1));
139         List list = new ArrayList(5);
140         for (int i = 0; i < 5; i++) {
141             list.add(criteria.clone());
142         }
143 
144         /* setup mock object */
145         mockFacade.expects(once()).method("findDeliveryDocket").with(
146                 eq(criteria), eq(0), eq(5)).will(
147                 returnValue(new PaginatedList(list, 0, 5, 5)));
148         struts().setRequestPathInfo("/deliveryDocket/find");
149         struts().addRequestParameter("firstElement", "0");
150         struts().addRequestParameter("maxElements", "5");
151         struts().addRequestParameter("partyId",
152                 criteria.getPartyId().toString());
153         struts().actionPerform();
154         struts().verifyForward("success");
155         struts().verifyNoActionErrors();
156 
157         PaginatedList results = (PaginatedList) struts().getRequest()
158                 .getAttribute(ActionUtils.RESULTS_ATTRIBUTE_NAME);
159 
160         assertEquals(list.size(), results.size());
161 
162         mockFacade.verify();
163     }
164 
165     final public void testShow() {
166         deliveryDocket.setId(new Long(1));
167 
168         /* setup mock object */
169         mockFacade.expects(once()).method("findDeliveryDocketWithDetails")
170                 .with(eq(new Long(1))).will(
171                         returnValue(deliveryDocketWithParty));
172         struts().setRequestPathInfo("/deliveryDocket/show");
173         struts().addRequestParameter("id", "1");
174         struts().actionPerform();
175         struts().verifyForward("success");
176         struts().verifyNoActionErrors();
177 
178         assertNotNull("request attribute", struts().getRequest().getAttribute(
179                 "deliveryDocket"));
180         mockFacade.verify();
181     }
182 
183 }