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 .unittree .business .unit ;
35
+
36
+ import java .util .ArrayList ;
37
+ import java .util .Arrays ;
38
+ import java .util .Collections ;
39
+ import java .util .Comparator ;
40
+ import java .util .List ;
41
+ import java .util .stream .Collectors ;
42
+
43
+ import org .junit .jupiter .api .AfterEach ;
44
+ import org .junit .jupiter .api .BeforeEach ;
45
+ import org .junit .jupiter .api .Test ;
46
+
47
+ import fr .paris .lutece .test .LuteceTestCase ;
48
+
49
+ public class UnitHomeTest extends LuteceTestCase
50
+ {
51
+ @ BeforeEach
52
+ protected void setUp ( ) throws Exception
53
+ {
54
+ createTestUnitTree ( );
55
+ }
56
+
57
+ @ AfterEach
58
+ protected void tearDown ( ) throws Exception
59
+ {
60
+ //Removing all units created by the previous test (all units except ROOT unit in fact)
61
+ for (Unit unit : UnitHome .findAll ( ) )
62
+ {
63
+ int idUnit = unit .getIdUnit ();
64
+ if ( idUnit != 0 )
65
+ {
66
+ UnitHome .remove ( idUnit );
67
+ }
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Create the unit tree used in this test class. The structure of the tree is as follows :
73
+ *
74
+ * ROOT
75
+ * __________________|____________________
76
+ * | | |
77
+ * 1 2 3
78
+ * _____|_____ _____|_____ ______|______
79
+ * | | | | | | | |
80
+ * 4 5 6 7 8 9 10 11
81
+ * |
82
+ * 12
83
+ */
84
+ private void createTestUnitTree ( )
85
+ {
86
+ //Units creation
87
+ //Level 0
88
+ //ROOT unit is already created by SQL scripts executed before this test class
89
+
90
+ //Level 1
91
+ createUnit ( 1 , 0 , "1" , "unit 1" , "unit 1" );
92
+ createUnit ( 2 , 0 , "2" , "unit 2" , "unit 2" );
93
+ createUnit ( 3 , 0 , "3" , "unit 3" , "unit 3" );
94
+
95
+ //Level 2
96
+ createUnit ( 4 , 1 , "4" , "unit 4" , "unit 4" );
97
+ createUnit ( 5 , 1 , "5" , "unit 5" , "unit 5" );
98
+ createUnit ( 6 , 1 , "6" , "unit 6" , "unit 6" );
99
+ createUnit ( 7 , 2 , "7" , "unit 7" , "unit 7" );
100
+ createUnit ( 8 , 2 , "8" , "unit 8" , "unit 8" );
101
+ createUnit ( 9 , 2 , "9" , "unit 9" , "unit 9" );
102
+ createUnit ( 10 , 3 , "10" , "unit 10" , "unit 10" );
103
+ createUnit ( 11 , 3 , "11" , "unit 11" , "unit 11" );
104
+
105
+ //Level 3
106
+ createUnit ( 12 , 4 , "12" , "unit 12" , "unit 12" );
107
+ }
108
+
109
+ /*
110
+ * Creates a unit
111
+ *
112
+ * @param nIdUnit
113
+ * the unit id
114
+ * @param nIdParent
115
+ * the unit pareny
116
+ * @param strCode
117
+ * the unit code
118
+ * @param strDescription
119
+ * the unit description
120
+ * @param strLabel
121
+ * the unit label
122
+ */
123
+ private void createUnit ( int nIdUnit , int nIdParent , String strCode , String strDescription , String strLabel )
124
+ {
125
+ Unit unit = new Unit ( );
126
+ unit .setIdUnit ( nIdUnit );
127
+ unit .setIdParent ( nIdParent );
128
+ unit .setCode ( strCode );
129
+ unit .setDescription ( strDescription );
130
+ unit .setLabel ( strLabel );
131
+ UnitHome .create ( unit );
132
+ }
133
+
134
+ @ Test
135
+ public void testGetAllSubUnitsIdOfNonExistentUnit ( )
136
+ {
137
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 9999 ) );
138
+ }
139
+
140
+ @ Test
141
+ public void testGetAllSubUnitsIdOfUnitROOT ( )
142
+ {
143
+ assertEquals ( Arrays .asList ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ), getAllSubUnitsId ( 0 ) );
144
+ }
145
+
146
+ @ Test
147
+ public void testGetAllSubUnitsIdOfUnit1 ( )
148
+ {
149
+ assertEquals ( Arrays .asList ( 4 , 5 , 6 , 12 ), getAllSubUnitsId ( 1 ) );
150
+ }
151
+
152
+ @ Test
153
+ public void testGetAllSubUnitsIdOfUnit2 ( )
154
+ {
155
+ assertEquals ( Arrays .asList ( 7 , 8 , 9 ), getAllSubUnitsId ( 2 ) );
156
+ }
157
+
158
+ @ Test
159
+ public void testGetAllSubUnitsIdOfUnit3 ( )
160
+ {
161
+ assertEquals ( Arrays .asList ( 10 , 11 ), getAllSubUnitsId ( 3 ) );
162
+ }
163
+
164
+ @ Test
165
+ public void testGetAllSubUnitsIdOfUnit4 ( )
166
+ {
167
+ assertEquals ( Arrays .asList ( 12 ), getAllSubUnitsId ( 4 ) );
168
+ }
169
+
170
+ @ Test
171
+ public void testGetAllSubUnitsIdOfUnit5 ( )
172
+ {
173
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 5 ) );
174
+ }
175
+
176
+ @ Test
177
+ public void testGetAllSubUnitsIdOfUnit6 ( )
178
+ {
179
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 6 ) );
180
+ }
181
+
182
+ @ Test
183
+ public void testGetAllSubUnitsIdOfUnit7 ( )
184
+ {
185
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 7 ) );
186
+ }
187
+
188
+ @ Test
189
+ public void testGetAllSubUnitsIdOfUnit8 ( )
190
+ {
191
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 8 ) );
192
+ }
193
+
194
+ @ Test
195
+ public void testGetAllSubUnitsIdOfUnit9 ( )
196
+ {
197
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 9 ) );
198
+ }
199
+
200
+ @ Test
201
+ public void testGetAllSubUnitsIdOfUnit10 ( )
202
+ {
203
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 10 ) );
204
+ }
205
+
206
+ @ Test
207
+ public void testGetAllSubUnitsIdOfUnit11 ( )
208
+ {
209
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 11 ) );
210
+ }
211
+
212
+ @ Test
213
+ public void testGetAllSubUnitsIdOfUnit12 ( )
214
+ {
215
+ assertEquals ( Arrays .asList ( ), getAllSubUnitsId ( 12 ) );
216
+ }
217
+
218
+ /**
219
+ * returns and sort all sub units of the unit of the unit id passed in parameter
220
+ *
221
+ * @param idUnit
222
+ * the id unit
223
+ * @return list of sub units ids
224
+ */
225
+ private List <Integer > getAllSubUnitsId ( int idUnit )
226
+ {
227
+ List <Integer > subUnitsIdsList = UnitHome .getAllSubUnitsId ( idUnit )
228
+ .stream ( )
229
+ .collect ( Collectors .toCollection ( ArrayList ::new ) );
230
+ Collections .sort ( subUnitsIdsList , Comparator .comparing ( Integer ::intValue ) );
231
+ return subUnitsIdsList ;
232
+ }
233
+ }
0 commit comments