View Javadoc
1   /*
2    * Copyright (c) 2002-2016, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.grustoragedb.business;
35  
36  import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
37  import fr.paris.lutece.plugins.grubusiness.business.demand.Demand;
38  import fr.paris.lutece.plugins.grubusiness.business.demand.IDemandDAO;
39  import fr.paris.lutece.plugins.grubusiness.business.web.rs.EnumGenericStatus;
40  import fr.paris.lutece.test.LuteceTestCase;
41  import static org.hamcrest.CoreMatchers.is;
42  import static org.hamcrest.CoreMatchers.not;
43  import static org.hamcrest.CoreMatchers.nullValue;
44  import static org.junit.Assert.assertEquals;
45  
46  import java.util.Collection;
47  import java.util.Iterator;
48  
49  /**
50   * Test class for the DemandDAO
51   *
52   */
53  public class DemandDAOTest extends LuteceTestCase
54  {
55      private static final String CUSTOMER_ID_1 = "CustomerId1";
56      private static final String CUSTOMER_ID_2 = "CustomerId1";
57      private static final String DEMAND_ID_1 = "DemandId1";
58      private static final String DEMAND_ID_2 = "DemandId2";
59      private static final String DEMAND_TYPE_ID_1 = "DemandTypeId1";
60      private static final String DEMAND_TYPE_ID_2 = "DemandTypeId2";
61      private static final String DEMAND_SUBTYPE_ID_1 = "DemandSubtypeId1";
62      private static final String DEMAND_SUBTYPE_ID_2 = "DemandSubtypeId2";
63      private static final String DEMAND_REFERENCE_1 = "DemandReference1";
64      private static final String DEMAND_REFERENCE_2 = "DemandReference2";
65      private static final long DEMAND_CREATION_DATE_1 = 1L;
66      private static final long DEMAND_CREATION_DATE_2 = 2L;
67      private static final long DEMAND_CLOSURE_DATE_1 = 1L;
68      private static final long DEMAND_CLOSURE_DATE_2 = 2L;
69      private static final int DEMAND_MAX_STEPS_1 = 1;
70      private static final int DEMAND_MAX_STEPS_2 = 2;
71      private static final int DEMAND_CURRENT_STEP_1 = 1;
72      private static final int DEMAND_CURRENT_STEP_2 = 2;
73      private final IDemandDAO _demandDao;
74  
75      /**
76       * Constructor
77       */
78      public DemandDAOTest( )
79      {
80          _demandDao = new DemandDAO( );
81      }
82  
83      /**
84       * Test case
85       */
86      public void testBusiness( )
87      {
88          // Create test
89          Demand demand = new Demand( );
90          demand.setId( DEMAND_ID_1 );
91          demand.setTypeId( DEMAND_TYPE_ID_1 );
92          demand.setSubtypeId( DEMAND_SUBTYPE_ID_1 );
93          demand.setReference( DEMAND_REFERENCE_1 );
94          demand.setStatusId( EnumGenericStatus.ENCOURS.getStatusId( ) );
95  
96          Customer customer = new Customer( );
97          customer.setId( CUSTOMER_ID_1 );
98          demand.setCustomer( customer );
99          demand.setCreationDate( DEMAND_CREATION_DATE_1 );
100         demand.setClosureDate( DEMAND_CLOSURE_DATE_1 );
101         demand.setMaxSteps( DEMAND_MAX_STEPS_1 );
102         demand.setCurrentStep( DEMAND_CURRENT_STEP_1 );
103 
104         _demandDao.insert( demand );
105 
106         Demand demandStored = _demandDao.load( demand.getId( ), demand.getTypeId( ) );
107         assertEquals( demandStored.getId( ), demand.getId( ) );
108         assertEquals( demandStored.getTypeId( ),  demand.getTypeId( ) );
109         assertEquals( demandStored.getSubtypeId( ), demand.getSubtypeId( ) );
110         assertEquals( demandStored.getReference( ),  demand.getReference( ) );
111         assertEquals( demandStored.getStatusId( ),  demand.getStatusId( ) );
112         assertEquals( demandStored.getCustomer( ).getId( ),  demand.getCustomer( ).getId( ) );
113         assertEquals( demandStored.getCreationDate( ),  demand.getCreationDate( ) );
114         assertEquals( demandStored.getClosureDate( ),  demand.getClosureDate( ) );
115         assertEquals( demandStored.getMaxSteps( ),  demand.getMaxSteps( ) );
116         assertEquals( demandStored.getCurrentStep( ),  demand.getCurrentStep( ) );
117 
118         // Update test
119         demand.setId( DEMAND_ID_2 );
120         demand.setTypeId( DEMAND_TYPE_ID_2 );
121         demand.setSubtypeId( DEMAND_SUBTYPE_ID_2 );
122         demand.setReference( DEMAND_REFERENCE_2 );
123         demand.setStatusId( EnumGenericStatus.TERMINE.getStatusId( ) );
124         customer = new Customer( );
125         customer.setId( CUSTOMER_ID_2 );
126         demand.setCustomer( customer );
127         demand.setCreationDate( DEMAND_CREATION_DATE_2 );
128         demand.setClosureDate( DEMAND_CLOSURE_DATE_2 );
129         demand.setMaxSteps( DEMAND_MAX_STEPS_2 );
130         demand.setCurrentStep( DEMAND_CURRENT_STEP_2 );
131 
132         _demandDao.store( demand );
133 
134         demandStored = _demandDao.load( demand.getId( ), demand.getTypeId( ) );
135         assertNull( demandStored );
136 
137         demand.setId( DEMAND_ID_1 );
138         demand.setTypeId( DEMAND_TYPE_ID_1 );
139         demand.setSubtypeId( DEMAND_SUBTYPE_ID_1 );
140         _demandDao.store( demand );
141 
142         demandStored = _demandDao.load( demand.getId( ), demand.getTypeId( ) );
143         assertEquals( demandStored.getId( ), demand.getId( ) );
144         assertEquals( demandStored.getTypeId( ), demand.getTypeId( ) );
145         assertEquals( demandStored.getSubtypeId( ), demand.getSubtypeId( ) );
146         assertNotSame( demandStored.getReference( ), demand.getReference( ) );
147         assertEquals( demandStored.getStatusId( ), demand.getStatusId( ) );
148         assertEquals( demandStored.getCustomer( ).getId( ), demand.getCustomer( ).getId( ) );
149         assertNotSame( demandStored.getCreationDate( ), demand.getCreationDate( ) );
150         assertEquals( demandStored.getClosureDate( ), demand.getClosureDate( ) );
151         assertNotSame( demandStored.getMaxSteps( ), demand.getMaxSteps( ) );
152         assertEquals( demandStored.getCurrentStep( ), demand.getCurrentStep( ) );
153 
154         // List test
155         Collection<Demand> collectionDemands = _demandDao.loadByCustomerId( CUSTOMER_ID_1 );
156         assertEquals( collectionDemands.size( ), 1 );
157 
158         Iterator<Demand> iteratorDemand = collectionDemands.iterator( );
159         demandStored = iteratorDemand.next( );
160         assertEquals( demandStored.getId( ), DEMAND_ID_1 );
161         assertEquals( demandStored.getTypeId( ), DEMAND_TYPE_ID_1 );
162         assertEquals( demandStored.getSubtypeId( ), DEMAND_SUBTYPE_ID_1 );
163 
164         // List test by reference
165         Collection<Demand> collectionDemands2 = _demandDao.loadByReference( DEMAND_REFERENCE_1 );
166         assertEquals( collectionDemands2.size( ), 1 );
167 
168         Iterator<Demand> iteratorDemand2 = collectionDemands2.iterator( );
169         demandStored = iteratorDemand2.next( );
170         assertEquals( demandStored.getId( ), DEMAND_ID_1 );
171         assertEquals( demandStored.getTypeId( ), DEMAND_TYPE_ID_1 );
172         assertEquals( demandStored.getSubtypeId( ), DEMAND_SUBTYPE_ID_1 );
173 
174         // Delete test
175         _demandDao.delete( DEMAND_ID_1, DEMAND_TYPE_ID_1 );
176         demandStored = _demandDao.load( DEMAND_ID_1, DEMAND_TYPE_ID_1 );
177         assertEquals( demandStored, nullValue( ) );
178     }
179 }