Salome HOME
Search knowledge is implemented. Unit tests are improved. Lucene index is not used...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / common / TestListingAndOrder.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package test.splat.common; 
11
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.List;
18
19 import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
20 import org.testng.Assert;
21 import org.testng.annotations.Test;
22
23 /**
24  * Abstract class for specific search functionality tests. It is necessary to 
25  * define method, returning expected search list size, array of properties.
26  * 
27  * @author <a href="mailto:vera.polyanskikh@opencascade.com">Vera POLYANSKIKH (VPH)</a>
28  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
29  */
30 @Test(singleThreaded=true, groups = {"service", "functional", "business"})
31 public class TestListingAndOrder extends AbstractTransactionalTestNGSpringContextTests {
32
33
34                 
35                 /**
36                  * Current Specific method name.
37                  */
38                 protected String _methodName;
39                 /**
40                  * Common message suffix used to inform that corresponding method should be overridden if this common functionality is used. 
41                  */             
42                 private static final String NO_METHOD_OVERRIDEN_MSG_SUFFIX = " should be overridden in test class if common test pagination and order engine is used";
43                 
44                 /**
45                  * Error message has been appeared when null object exist in business method result set.
46                  */             
47                 private static final String NULL_OBJECT_EXIST_IN_RESULTSET = "Null object exist in business method result set";
48
49                 //////////////////////////////////////////////////////////////////////////////////////////
50                 //////////////////////////////////////////////////////////////////////////////////////////
51                                                                         /**METHODS TO BE OVERRIDDEN**/
52                                                         /**Throw UnsupportedOperationException if not**/
53                 //////////////////////////////////////////////////////////////////////////////////////////
54                 //////////////////////////////////////////////////////////////////////////////////////////
55                 
56                 /**
57                  * Return expected size of the whole retrieved list or a page of the objects.
58                  * @param methodName - specific search method to be called
59                  * @return size  of list
60                  */
61                 @Test(enabled = false)
62                 public int getExpectedWholeListSize(final String methodName){
63                         // empty default implementation
64                         // should be overridden if test of pagination or order is necessary
65                         throw new UnsupportedOperationException("TestListingAndOrder.getExpectedWholeListSize"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
66                 }
67                 
68
69                 /**
70                  * Returns array of DTO properties which can be used to test order of page or list. 
71                  * @param methodName - specific search method to be called
72                  * @return array of order criteria.
73                  */
74                 @Test(enabled = false)
75                 public String[][] getOrderCriteria(final String methodName){
76                         // empty default implementation
77                         // should be overridden if test of pagination or order is necessary
78                         throw new UnsupportedOperationException("TestListingAndOrder.getOrderCriteria"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
79                 }
80                 
81                 /**
82                  * Wrapper for call of specific not paginated search method.
83                  * @param <T> object type class
84                  * @param methodName - specific search method to be called
85                  * @return template list
86                  */             
87                 @Test(enabled = false)
88                 public <T>List<T>  searchListWrapper(final String methodName){
89                         // empty default implementation
90                         // should be overridden if test of pagination or order is necessary
91                         throw new UnsupportedOperationException("TestListingAndOrder.searchListWrapper"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
92                 }
93                 
94                 //////////////////////////////////////////////////////////////////////////////////////////
95                 //////////////////////////////////////////////////////////////////////////////////////////
96                                                                                 /**END OF SECTION**/
97                 //////////////////////////////////////////////////////////////////////////////////////////
98                 //////////////////////////////////////////////////////////////////////////////////////////
99                 
100
101                 /**
102                  * Returns result of default comparison of two objects by given array of properties.
103                  * @param o1 - object 1
104                  * @param o2 - object 2
105                  * @param methodName - specific search method name
106                  * @param propertyNames - properties to be compared by
107                  * @param ascDirection -  order direction (asc if true, desc - otherwise)
108                  * @return result of comparison.
109                  */
110                 @Test(enabled = false)          
111                 protected  int specificCompare(final Object o1, final Object o2, final String methodName,final String[] propertyNames,final boolean ascDirection){
112                         int result = 0;
113                         int i=0;                        
114                         while(result==0 && i<propertyNames.length){                                             
115                                 result =internalCompare(o1,o2, propertyNames[i]);
116                                 i++;
117                         }
118                         
119                         if(!ascDirection){
120                                 result = result*(-1);
121                         }
122                         return result;
123                 }
124         
125                 /**
126                  * Perform test of not paginated search method.
127                  * @param <T> object type class
128                  * @param methodName - specific search method to be called
129                  */
130                 @Test(enabled = false)
131                 protected <T> void internalTestSearchList(final String methodName) {
132                         setMethodName(methodName);
133                         try {
134                                 // compare the whole set size with expected
135                                 List<T> list = searchList();
136                                 Assert.assertEquals(list.size(),  getExpectedWholeListSize(methodName),"The whole list size is unexpected!");
137                                                                 
138                                 // check ordering by all given criteria
139                                 String[] orderCriteria = getOrderCriteria(_methodName)[0];      
140                                 
141                                 Assert.assertTrue(checkOrdering(list,orderCriteria,true),"Asc Ordering of list by properties "+orderCriteria[0].toString()+" is wrong!");
142
143                         } catch (Exception e) {
144                                 Assert.fail("Exception occured", e);
145                         }
146                 }
147
148                 /**
149                  * Get the methodName.
150                  * @return the methodName
151                  */
152                 @Test(enabled = false)
153                 private String getMethodName() {
154                         return _methodName;
155                 }
156
157
158
159
160                 /**
161                  * Set the methodName.
162                  * @param methodName the methodName to set
163                  */
164                 @Test(enabled = false)
165                 private void setMethodName(final String methodName) {
166                         this._methodName = methodName;
167                 }
168                 
169
170                 /**
171                  * Test of not paginated search method. Specific service method to be called for tests.
172                  * @param <T> object type class
173                  * @return list -template list
174                  */
175                 @Test(enabled = false)
176                 private <T>List<T> searchList() {
177                         return searchListWrapper(_methodName);
178                 }
179
180                 /**
181                  * Order list given list by given order criteria.
182                  * @param <T> - object type class
183                  * @param list - list of the objects
184                  * @param orderCriteria - property to order by
185                  * @param ascDirection - order direction (asc if true, desc - otherwise)
186                  * @return ordered list
187                  */
188                 @Test(enabled = false)
189                 private <T>List<T> getOrderedList(final List<T> list,
190                                                                            final String[] orderCriteria,
191                                                                            final boolean ascDirection){
192                         // check ordering
193
194                         Collections.sort(list,new  Comparator<T>(){                     
195                                 public int compare(final T o1, final T o2){
196                                         return specificCompare(o1,o2,null,orderCriteria,ascDirection);
197                                 }       
198                         });
199                         return list;
200                 }
201
202                 /**
203                  * Check if given list is ordered by given criteria.
204                  * @param <T> - object type class
205                  * @param list - list of the objects
206                  * @param orderCriteria - property to order by
207                  * @param ascDirection - order direction (asc if true, desc - otherwise)
208                  * @return boolean
209                  */
210                 @Test(enabled = false)
211                 private <T> boolean checkOrdering(final List<T> list,
212                                    final String[] orderCriteria,
213                                    final boolean ascDirection){
214                         boolean result = true;
215
216                         List<T> oldList = new ArrayList<T>();
217                         for(T item:list){
218                                 oldList.add(item);
219                         }
220                         List<T> sortedList = getOrderedList(list,orderCriteria,ascDirection);
221                         int i = 0 ;
222                         for(i=0;i<sortedList.size();i++){       
223                                 if(oldList.get(i)==null){
224                                         Assert.fail(NULL_OBJECT_EXIST_IN_RESULTSET);
225                                 }
226                                 if(!oldList.get(i).equals(sortedList.get(i))){
227                                         result = false;
228                                         break;
229                                 }
230                         }
231                         return result;
232                 }
233
234
235                 /**
236                  * Performs internal default compare by given property.
237                  * @param o1 - object 1
238                  * @param o2 - object 2
239                  * @param propertyName - property to be compared by
240                  * @return result
241                  */
242                 @Test(enabled = false)
243                 private  int internalCompare(final Object o1, final Object o2, final String propertyName){
244                         int result = -2;
245                         try {
246                                 // Use getter method for the given property name.
247                                 Method getter = o1.getClass().getMethod(
248                                         "get" + propertyName.substring(0, 1).toUpperCase()
249                                                         + propertyName.substring(1));
250                                 // Get property values by means of the getter method.
251                                 Object v1 = getter.invoke(o1);
252                                 Object v2 = getter.invoke(o2);
253                                 // Compare values.
254                                 if(v1==null && v2==null) {
255                                         result = 0;
256                                 }else{
257                                         if(v1==null && v2!=null) {
258                                                 result = 1;
259                                         }else{
260                                                 if(v1!=null && v2==null){
261                                                         result = -1;
262                                                 }else{
263                                                         if (v1 instanceof Comparable) {
264                                                                 result = ((Comparable)v1).compareTo(v2);
265                                                         }
266                                                 }
267                                         }
268                                 }
269                         }catch (IllegalAccessException e) {
270
271                                 Assert.fail("IllegalAccessException was thrown by BeanUtils.getProperty ", e);
272                         } catch (InvocationTargetException e) {
273
274                                 Assert.fail("InvocationTargetException was thrown by BeanUtils.getProperty ", e);
275                         } catch (NoSuchMethodException e) {
276
277                                 Assert.fail("NoSuchMethodException was thrown by BeanUtils.getProperty: " + e.getMessage(), e);
278                         }
279                         return result;
280                 }       
281 }