Support for FIQL queries on REST retreive operations
[controller.git] / opendaylight / northbound / commons / src / test / java / org / opendaylight / controller / northbound / commons / query / XMLAccessorTypeTest.java
1 package org.opendaylight.controller.northbound.commons.query;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.xml.bind.annotation.XmlAccessType;
7 import javax.xml.bind.annotation.XmlAccessorType;
8 import javax.xml.bind.annotation.XmlElement;
9 import javax.xml.bind.annotation.XmlRootElement;
10 import javax.xml.bind.annotation.XmlTransient;
11
12 import org.junit.Assert;
13 import org.junit.Test;
14
15 public class XMLAccessorTypeTest {
16
17     @Test
18     public void testPublicAccessType() throws Exception {
19         // create bean
20         List<PublicAccessBean> testList = new ArrayList<PublicAccessBean>();
21         testList.add(new PublicAccessBean("John", "Scott", "private", 1,
22                 "transient", "elem1"));
23         testList.add(new PublicAccessBean("Foo", "Bar", "private1", 2,
24                 "transient1", "elem2"));
25         QueryContextTest.p(QueryContextTest.toXml(testList.get(0)));
26
27         QueryContext queryContext = new QueryContextImpl();
28         Assert.assertNotNull(queryContext);
29         // search for public field
30         Query<PublicAccessBean> query = queryContext.createQuery(
31                 "publicbean.firstName==Foo", PublicAccessBean.class);
32         Assert.assertNotNull(query);
33
34         List<PublicAccessBean> found = query.find(testList);
35         Assert.assertNotNull(found);
36         Assert.assertEquals(1, found.size());
37         Assert.assertEquals("Foo", found.get(0).firstName);
38
39         // search for public getter
40         query = queryContext.createQuery("publicbean.privateGetterField<2",
41                 PublicAccessBean.class);
42         Assert.assertNotNull(query);
43
44         found = query.find(testList);
45         Assert.assertNotNull(found);
46         Assert.assertEquals(1, found.size());
47         Assert.assertEquals("John", found.get(0).firstName);
48
49         // test for transient field
50         query = queryContext.createQuery("publicbean.transientField='trans*'",
51                 PublicAccessBean.class);
52         Assert.assertNotNull(query);
53
54         found = query.find(testList);
55         Assert.assertNotNull(found);
56         Assert.assertEquals(0, found.size());
57
58         // test for private field
59         query = queryContext.createQuery("publicbean.privateField==private",
60                 PublicAccessBean.class);
61         Assert.assertNotNull(query);
62
63         found = query.find(testList);
64         Assert.assertNotNull(found);
65         Assert.assertEquals(0, found.size());
66
67         // test for XML Element
68         query = queryContext.createQuery("publicbean.element==elem1",
69                 PublicAccessBean.class);
70         Assert.assertNotNull(query);
71
72         found = query.find(testList);
73         Assert.assertNotNull(found);
74         Assert.assertEquals(1, found.size());
75         Assert.assertEquals("John", found.get(0).firstName);
76     }
77
78     @Test
79     public void testFieldAccessType() throws QueryException {
80         // create bean
81         List<FieldAccessBean> testList = new ArrayList<FieldAccessBean>();
82         testList.add(new FieldAccessBean("John", "Scott", "private", 1, "elem1"));
83         testList.add(new FieldAccessBean("Foo", "Bar", "private1", 2, "elem2"));
84
85         QueryContextTest.p(QueryContextTest.toXml(testList.get(0)));
86         QueryContext queryContext = new QueryContextImpl();
87         Assert.assertNotNull(queryContext);
88         // test private field
89         Query<FieldAccessBean> query = queryContext.createQuery(
90                 "field.privateField==private", FieldAccessBean.class);
91         Assert.assertNotNull(query);
92
93         List<FieldAccessBean> found = query.find(testList);
94         Assert.assertNotNull(found);
95         Assert.assertEquals(1, found.size());
96         Assert.assertEquals("John", found.get(0).firstName);
97
98         // test public field
99         query = queryContext.createQuery("field.firstName==Foo",
100                 FieldAccessBean.class);
101         Assert.assertNotNull(query);
102
103         found = query.find(testList);
104         Assert.assertNotNull(found);
105         Assert.assertEquals(1, found.size());
106         Assert.assertEquals("Foo", found.get(0).firstName);
107
108         // test annotated field
109         query = queryContext.createQuery("field.element==elem2",
110                 FieldAccessBean.class);
111         Assert.assertNotNull(query);
112
113         found = query.find(testList);
114         Assert.assertNotNull(found);
115         Assert.assertEquals(1, found.size());
116         Assert.assertEquals("Foo", found.get(0).firstName);
117
118         // test annotated method
119         query = queryContext.createQuery("field.privateGetterField==11",
120                 FieldAccessBean.class);
121         Assert.assertNotNull(query);
122
123         found = query.find(testList);
124         Assert.assertNotNull(found);
125         Assert.assertEquals(1, found.size());
126         Assert.assertEquals("John", found.get(0).firstName);
127     }
128
129     @Test
130     public void testPropertyAccessType() throws QueryException {
131         // create bean
132         List<PropertyAccessBean> testList = new ArrayList<PropertyAccessBean>();
133         testList.add(new PropertyAccessBean("John", "Scott", "private", 1, "elem1",
134                 "transient1"));
135         testList.add(new PropertyAccessBean("Foo", "Bar", "private1", 2, "elem2",
136                 "transient2"));
137
138         QueryContextTest.p(QueryContextTest.toXml(testList.get(0)));
139         QueryContext queryContext = new QueryContextImpl();
140         Assert.assertNotNull(queryContext);
141         // test public getter public field
142         Query<PropertyAccessBean> query = queryContext.createQuery(
143                 "property.firstName==John", PropertyAccessBean.class);
144         Assert.assertNotNull(query);
145
146         List<PropertyAccessBean> found = query.find(testList);
147         Assert.assertNotNull(found);
148         Assert.assertEquals(1, found.size());
149         Assert.assertEquals("John", found.get(0).firstName);
150
151         // test public field no getter
152         query = queryContext.createQuery("property.lastName==Bar",
153                 PropertyAccessBean.class);
154         Assert.assertNotNull(query);
155
156         found = query.find(testList);
157         Assert.assertNotNull(found);
158         Assert.assertEquals(0, found.size());
159
160         // test annotated field
161         query = queryContext.createQuery("property.element==elem2",
162                 PropertyAccessBean.class);
163         Assert.assertNotNull(query);
164
165         found = query.find(testList);
166         Assert.assertNotNull(found);
167         Assert.assertEquals(1, found.size());
168         Assert.assertEquals("Foo", found.get(0).firstName);
169
170         // test annotated method
171         query = queryContext.createQuery("property.field==private",
172                 PropertyAccessBean.class);
173         Assert.assertNotNull(query);
174
175         found = query.find(testList);
176         Assert.assertNotNull(found);
177         Assert.assertEquals(1, found.size());
178         Assert.assertEquals("John", found.get(0).firstName);
179
180         // test transient method
181         query = queryContext.createQuery("property.transientField==transient1",
182                 PropertyAccessBean.class);
183         Assert.assertNotNull(query);
184
185         found = query.find(testList);
186         Assert.assertNotNull(found);
187         Assert.assertEquals(0, found.size());
188     }
189
190     @Test
191     public void testNoneAccessType() throws QueryException {
192         // create bean
193         List<NoneAccessBean> testList = new ArrayList<NoneAccessBean>();
194         testList.add(new NoneAccessBean("John", "Scott", "private"));
195         testList.add(new NoneAccessBean("Foo", "Bar", "private1"));
196
197         QueryContextTest.p(QueryContextTest.toXml(testList.get(0)));
198         QueryContext queryContext = new QueryContextImpl();
199         Assert.assertNotNull(queryContext);
200         // test annotated field
201         Query<NoneAccessBean> query = queryContext.createQuery(
202                 "test.firstName==John", NoneAccessBean.class);
203         Assert.assertNotNull(query);
204
205         List<NoneAccessBean> found = query.find(testList);
206         Assert.assertNotNull(found);
207         Assert.assertEquals(1, found.size());
208         Assert.assertEquals("John", found.get(0).getFirstName());
209         // test unannotated field
210         query = queryContext
211                 .createQuery("test.lastName==Bar", NoneAccessBean.class);
212         Assert.assertNotNull(query);
213
214         found = query.find(testList);
215         Assert.assertNotNull(found);
216         Assert.assertEquals(0, found.size());
217         // test annotated method
218         query = queryContext.createQuery("test.testField==private",
219                 NoneAccessBean.class);
220         Assert.assertNotNull(query);
221
222         found = query.find(testList);
223         Assert.assertNotNull(found);
224         Assert.assertEquals(1, found.size());
225         Assert.assertEquals("John", found.get(0).getFirstName());
226
227     }
228
229 }
230
231 // default ( public memeber )
232 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
233 @XmlRootElement(name = "publicbean")
234 class PublicAccessBean {
235
236   public String firstName;
237   public String lastName;
238   private String privateField;
239   private int privateGetterField;
240   @XmlTransient
241   public String transientField;
242   @XmlElement(name = "element")
243   private String xmlElem;
244
245   public PublicAccessBean() {
246   }
247
248   public PublicAccessBean(String firstName, String lastName,
249       String privateField, int privateGetterField, String transientField,
250       String xmlElem) {
251     this.firstName = firstName;
252     this.lastName = lastName;
253     this.privateField = privateField;
254     this.privateGetterField = privateGetterField;
255     this.transientField = transientField;
256     this.xmlElem = xmlElem;
257   }
258
259   public int getPrivateGetterField() {
260     return privateGetterField;
261   }
262
263   public void setPrivateGetterField(int field) {
264     this.privateGetterField = field;
265   }
266 }
267
268 // default ( public memeber )
269 @XmlAccessorType(XmlAccessType.FIELD)
270 @XmlRootElement(name = "field")
271 class FieldAccessBean {
272
273   public String firstName;
274   public String lastName;
275   private String privateField;
276   private int test;
277   @XmlElement(name = "element")
278   private String xmlElem;
279
280   public FieldAccessBean() {
281   }
282
283   public FieldAccessBean(String firstName, String lastName,
284       String privateField, int privateGetterField, String xmlElem) {
285     this.firstName = firstName;
286     this.lastName = lastName;
287     this.privateField = privateField;
288     this.xmlElem = xmlElem;
289     this.test = privateGetterField;
290   }
291
292   public String getPrivateField() {
293     return privateField;
294   }
295
296   @XmlElement(name = "privateGetterField")
297   public int getPrivateGetterField() {
298     return test + 10;
299   }
300 }
301
302 // default ( public memeber )
303 @XmlAccessorType(XmlAccessType.PROPERTY)
304 @XmlRootElement(name = "property")
305 class PropertyAccessBean {
306
307   public String firstName;
308   public String lastName;
309   private String privateField;
310   private int privateGetterField;
311   @XmlElement(name = "element")
312   private String xmlElem;
313   private String transientField;
314
315   public PropertyAccessBean() {
316   }
317
318   public PropertyAccessBean(String firstName, String lastName,
319       String privateField, int privateGetterField, String xmlElem,
320       String transientField) {
321     this.firstName = firstName;
322     this.lastName = lastName;
323     this.privateField = privateField;
324     this.privateGetterField = privateGetterField;
325     this.xmlElem = xmlElem;
326     this.transientField = transientField;
327   }
328
329   public int getPrivateGetterField() {
330     return privateGetterField;
331   }
332
333   @XmlElement(name = "field")
334   public String getPrivateField() {
335     return privateField;
336   }
337
338   public String getFirstName() {
339     return firstName;
340   }
341
342   @XmlTransient
343   public String getTransientField() {
344     return transientField;
345   }
346 }
347
348 // default ( public memeber )
349 @XmlAccessorType(XmlAccessType.NONE)
350 @XmlRootElement(name = "test")
351 class NoneAccessBean {
352   @XmlElement
353   private String firstName;
354   public String lastName;
355   private String testField;
356
357   public NoneAccessBean() {
358   }
359
360   public NoneAccessBean(String firstName, String lastName, String testField) {
361     this.firstName = firstName;
362     this.lastName = lastName;
363     this.testField = testField;
364   }
365
366   @XmlElement(name = "testField")
367   public String getTestField() {
368     return testField;
369   }
370
371   public String getFirstName() {
372     return firstName;
373   }
374 }