View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.notificationstore.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.nullValue;
42  
43  import java.util.Collection;
44  import java.util.Iterator;
45  
46  /**
47   * Test class for the DemandDAO
48   *
49   */
50  public class DemandDAOTest extends LuteceTestCase
51  {
52      private static final String CUSTOMER_ID_1 = "CustomerId1";
53      private static final String CUSTOMER_ID_2 = "CustomerId1";
54      private static final String DEMAND_ID_1 = "DemandId1";
55      private static final String DEMAND_ID_2 = "DemandId2";
56      private static final String DEMAND_TYPE_ID_1 = "DemandTypeId1";
57      private static final String DEMAND_TYPE_ID_2 = "DemandTypeId2";
58      private static final String DEMAND_SUBTYPE_ID_1 = "DemandSubtypeId1";
59      private static final String DEMAND_SUBTYPE_ID_2 = "DemandSubtypeId2";
60      private static final String DEMAND_REFERENCE_1 = "DemandReference1";
61      private static final String DEMAND_REFERENCE_2 = "DemandReference2";
62      private static final long DEMAND_CREATION_DATE_1 = 1L;
63      private static final long DEMAND_CREATION_DATE_2 = 2L;
64      private static final long DEMAND_CLOSURE_DATE_1 = 1L;
65      private static final long DEMAND_CLOSURE_DATE_2 = 2L;
66      private static final int DEMAND_MAX_STEPS_1 = 1;
67      private static final int DEMAND_MAX_STEPS_2 = 2;
68      private static final int DEMAND_CURRENT_STEP_1 = 1;
69      private static final int DEMAND_CURRENT_STEP_2 = 2;
70      private final IDemandDAO _demandDao;
71  
72      /**
73       * Constructor
74       */
75      public DemandDAOTest( )
76      {
77          _demandDao = new DemandDAO( );
78      }
79  
80      /**
81       * Test case
82       */
83      public void testBusiness( )
84      {
85          // Create test
86          Demand demand = new Demand( );
87          demand.setId( DEMAND_ID_1 );
88          demand.setTypeId( DEMAND_TYPE_ID_1 );
89          demand.setSubtypeId( DEMAND_SUBTYPE_ID_1 );
90          demand.setReference( DEMAND_REFERENCE_1 );
91          demand.setStatusId( EnumGenericStatus.ONGOING.getStatusId( ) );
92  
93          Customer customer = new Customer( );
94          customer.setCustomerId( CUSTOMER_ID_1 );
95          demand.setCustomer( customer );
96          demand.setCreationDate( DEMAND_CREATION_DATE_1 );
97          demand.setClosureDate( DEMAND_CLOSURE_DATE_1 );
98          demand.setMaxSteps( DEMAND_MAX_STEPS_1 );
99          demand.setCurrentStep( DEMAND_CURRENT_STEP_1 );
100 
101         _demandDao.insert( demand );
102 
103         Demand demandStored = _demandDao.loadByDemandIdAndTypeIdAndCustomerId( demand.getId( ), demand.getTypeId( ), demand.getCustomer( ).getCustomerId( ) );
104         assertEquals( demandStored.getId( ), demand.getId( ) );
105         assertEquals( demandStored.getTypeId( ), demand.getTypeId( ) );
106         assertEquals( demandStored.getSubtypeId( ), demand.getSubtypeId( ) );
107         assertEquals( demandStored.getReference( ), demand.getReference( ) );
108         assertEquals( demandStored.getStatusId( ), demand.getStatusId( ) );
109         assertEquals( demandStored.getCustomer( ).getCustomerId( ), demand.getCustomer( ).getCustomerId( ) );
110         assertEquals( demandStored.getCreationDate( ), demand.getCreationDate( ) );
111         assertEquals( demandStored.getClosureDate( ), demand.getClosureDate( ) );
112         assertEquals( demandStored.getMaxSteps( ), demand.getMaxSteps( ) );
113         assertEquals( demandStored.getCurrentStep( ), demand.getCurrentStep( ) );
114 
115         // Update test
116         demand.setId( DEMAND_ID_2 );
117         demand.setTypeId( DEMAND_TYPE_ID_2 );
118         demand.setSubtypeId( DEMAND_SUBTYPE_ID_2 );
119         demand.setReference( DEMAND_REFERENCE_2 );
120         demand.setStatusId( EnumGenericStatus.CLOSED.getStatusId( ) );
121         customer = new Customer( );
122         customer.setCustomerId( CUSTOMER_ID_2 );
123         demand.setCustomer( customer );
124         demand.setCreationDate( DEMAND_CREATION_DATE_2 );
125         demand.setClosureDate( DEMAND_CLOSURE_DATE_2 );
126         demand.setMaxSteps( DEMAND_MAX_STEPS_2 );
127         demand.setCurrentStep( DEMAND_CURRENT_STEP_2 );
128 
129         _demandDao.store( demand );
130 
131         demandStored = _demandDao.loadByDemandIdAndTypeIdAndCustomerId( demand.getId( ), demand.getTypeId( ), demand.getCustomer( ).getCustomerId( ) );
132         assertNull( demandStored );
133 
134         demand.setId( DEMAND_ID_1 );
135         demand.setTypeId( DEMAND_TYPE_ID_1 );
136         demand.setSubtypeId( DEMAND_SUBTYPE_ID_1 );
137         _demandDao.store( demand );
138 
139         demandStored = _demandDao.loadByDemandIdAndTypeIdAndCustomerId( demand.getId( ), demand.getTypeId( ), demand.getCustomer( ).getCustomerId( ) );
140         assertEquals( demandStored.getId( ), demand.getId( ) );
141         assertEquals( demandStored.getTypeId( ), demand.getTypeId( ) );
142         assertEquals( demandStored.getSubtypeId( ), demand.getSubtypeId( ) );
143         assertNotSame( demandStored.getReference( ), demand.getReference( ) );
144         assertEquals( demandStored.getStatusId( ), demand.getStatusId( ) );
145         assertEquals( demandStored.getCustomer( ).getCustomerId( ), demand.getCustomer( ).getCustomerId( ) );
146         assertNotSame( demandStored.getCreationDate( ), demand.getCreationDate( ) );
147         assertEquals( demandStored.getClosureDate( ), demand.getClosureDate( ) );
148         assertNotSame( demandStored.getMaxSteps( ), demand.getMaxSteps( ) );
149         assertEquals( demandStored.getCurrentStep( ), demand.getCurrentStep( ) );
150 
151         // List test
152         Collection<Demand> collectionDemands = _demandDao.loadByCustomerId( CUSTOMER_ID_1 );
153         assertEquals( collectionDemands.size( ), 1 );
154 
155         Iterator<Demand> iteratorDemand = collectionDemands.iterator( );
156         demandStored = iteratorDemand.next( );
157         assertEquals( demandStored.getId( ), DEMAND_ID_1 );
158         assertEquals( demandStored.getTypeId( ), DEMAND_TYPE_ID_1 );
159         assertEquals( demandStored.getSubtypeId( ), DEMAND_SUBTYPE_ID_1 );
160 
161         // List test by reference
162         Collection<Demand> collectionDemands2 = _demandDao.loadByReference( DEMAND_REFERENCE_1 );
163         assertEquals( collectionDemands2.size( ), 1 );
164 
165         Iterator<Demand> iteratorDemand2 = collectionDemands2.iterator( );
166         demandStored = iteratorDemand2.next( );
167         assertEquals( demandStored.getId( ), DEMAND_ID_1 );
168         assertEquals( demandStored.getTypeId( ), DEMAND_TYPE_ID_1 );
169         assertEquals( demandStored.getSubtypeId( ), DEMAND_SUBTYPE_ID_1 );
170 
171         // Delete test
172         _demandDao.delete( DEMAND_ID_1, DEMAND_TYPE_ID_1, CUSTOMER_ID_1 );
173         demandStored = _demandDao.loadByDemandIdAndTypeIdAndCustomerId( DEMAND_ID_1, DEMAND_TYPE_ID_1, CUSTOMER_ID_1 );
174         assertEquals( demandStored, nullValue( ) );
175     }
176 }