Merge "Bug-835:Reserve ports should be logical ports"
[controller.git] / opendaylight / northbound / commons / src / test / java / org / opendaylight / controller / northbound / commons / query / QueryContextTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.northbound.commons.query;
9
10 import java.io.ByteArrayOutputStream;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.xml.bind.JAXBContext;
15 import javax.xml.bind.Marshaller;
16
17 import org.junit.Assert;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.northbound.commons.types.StringList;
21
22 public class QueryContextTest {
23
24     protected static final List<PersonBean> people = new ArrayList<PersonBean>();
25     protected static final List<BookBean> books = new ArrayList<BookBean>();
26
27     public static void p(String msg) {
28         System.out.println("=== " + msg);
29     }
30
31     @BeforeClass
32     public static void load() {
33         people.add(new PersonBean(100, "John", "Doe", "San Jose"));
34         people.add(new PersonBean(200, "Foo", "Bar", "San Francisco"));
35         people.add(new PersonBean(300, "A", "B", "San Francisco"));
36         people.add(new PersonBean(400, "X", "Y", "New York"));
37
38         books.add(new BookBean("Book1", "A001", people.get(0)));
39         books.add(new BookBean("Book2", "A002", people.get(1)));
40         books.add(new BookBean("Book3", "A003", people.get(2)));
41
42         ReviewBean review1 = new ReviewBean("cool", people.get(2));
43         ReviewBean review2 = new ReviewBean("kewl", people.get(3));
44
45         books.get(0).addReview(review1).addReview(review2);
46         books.get(1).addReview(review1);
47         books.get(2).addReview(review2).addReview(review1);
48
49     }
50
51     @Test
52     public void testQueryContext() {
53         QueryContext queryContext = new QueryContextImpl();
54         Assert.assertNotNull(queryContext);
55     }
56
57     @Test
58     public void testSimpleQuery() throws QueryException {
59         QueryContext queryContext = new QueryContextImpl();
60         Query<PersonBean> query = queryContext.createQuery(
61                 "person.id==200", PersonBean.class);
62         Assert.assertNotNull(query);
63
64         List<PersonBean> found = query.find(people);
65         Assert.assertNotNull(found);
66         Assert.assertTrue(found.size() == 1);
67         Assert.assertEquals("Foo", found.get(0).firstName);
68     }
69
70     @Test
71     public void testAndQuery() throws QueryException {
72         QueryContext queryContext = new QueryContextImpl();
73         Query<PersonBean> query = queryContext.createQuery(
74                 "person.id!=200;(person.city='San.*')", PersonBean.class);
75         Assert.assertNotNull(query);
76
77         List<PersonBean> found = query.find(people);
78         Assert.assertNotNull(found);
79         Assert.assertTrue(found.size() == 2);
80         Assert.assertEquals("John", found.get(0).firstName);
81         Assert.assertEquals("A", found.get(1).firstName);
82     }
83
84     @Test
85     public void testOrQuery() throws QueryException {
86         QueryContext queryContext = new QueryContextImpl();
87         Query<PersonBean> query = queryContext.createQuery(
88                 "person.id==200,(person.city='San.*')", PersonBean.class);
89         Assert.assertNotNull(query);
90
91         List<PersonBean> found = query.find(people);
92         Assert.assertNotNull(found);
93         Assert.assertTrue(found.size() == 3);
94         Assert.assertEquals("John", found.get(0).firstName);
95         Assert.assertEquals("Foo", found.get(1).firstName);
96         Assert.assertEquals("A", found.get(2).firstName);
97     }
98
99     @Test
100     public void testXmlElementWrapper() throws QueryException {
101         List<String> emails = new ArrayList<String>();
102         emails.add("john@cisco.com");
103         emails.add("john@gmail.com");
104         people.get(0).setEmail(emails);
105
106         p(toXml(people.get(0)));
107         QueryContext queryContext = new QueryContextImpl();
108         Query<PersonBean> query = queryContext.createQuery(
109                 "person.emails.email==john@cisco.com", PersonBean.class);
110         Assert.assertNotNull(query);
111
112         List<PersonBean> found = query.find(people);
113         Assert.assertNotNull(found);
114         Assert.assertEquals(1,found.size());
115         Assert.assertEquals("John", found.get(0).firstName);
116     }
117
118     @Test
119     public void testXmlWrapperOfWrapper() throws QueryException{
120         WrapperList wrapper = new WrapperList();
121         wrapper.item.add("Test1");
122         wrapper.item.add("Test2");
123
124         books.get(0).addWrapperList(wrapper);
125         books.get(1).addWrapperList(wrapper);
126
127         System.out.println(toXml(books.get(0)));
128         QueryContext queryContext = new QueryContextImpl();
129         Query<BookBean> query = queryContext.createQuery(
130                 "book.parent.child.items.item==Test1", BookBean.class);
131         Assert.assertNotNull(query);
132     }
133
134     @Test
135     public void testXmlElementWrapperListofList() throws QueryException {
136         // create Stringlist
137         List<String> testList = new ArrayList<String>();
138         testList.add("A");
139         testList.add("B");
140         StringList itemList = new StringList(testList);
141         books.get(0).addToTestList(itemList);
142
143         System.out.println(toXml(books.get(0)));
144         QueryContext queryContext = new QueryContextImpl();
145         Query<BookBean> query = queryContext.createQuery(
146                 "book.test.testList.item==A", BookBean.class);
147         Assert.assertNotNull(query);
148     }
149
150     @Test
151     public void testPrimitiveIteratableTypes() throws QueryException {
152         // Load data for this test
153         List<String> sellers = new ArrayList<String>();
154         sellers.add("Amazon");
155
156         books.get(0).setSellerInfo(sellers);
157         sellers.add("Barners & Nobles");
158         books.get(1).setSellerInfo(sellers);
159         sellers.add("Borders");
160         sellers.remove("Amazon");
161         sellers.add("BookShop");
162         books.get(2).setSellerInfo(sellers);
163
164         System.out.println(toXml(books.get(0)));
165
166         QueryContext queryContext = new QueryContextImpl();
167         Query<BookBean> query = queryContext.createQuery(
168                 "book.soldBy==Amazon", BookBean.class);
169         Assert.assertNotNull(query);
170
171         List<BookBean> found = query.find(books);
172         Assert.assertNotNull(found);
173         Assert.assertEquals(2,found.size());
174         Assert.assertEquals("John", found.get(0).getauthor().firstName);
175
176         query = queryContext.createQuery(
177                 "book.soldBy!=Amazon", BookBean.class);
178         Assert.assertNotNull(query);
179
180         found = query.find(books);
181         System.out.println("books" +found);
182         Assert.assertNotNull(found);
183         Assert.assertEquals(1,found.size());
184         Assert.assertEquals("A", found.get(0).getauthor().firstName);
185     }
186
187     @Test
188     public void testCompositeIteratableTypes() throws QueryException {
189         QueryContext queryContext = new QueryContextImpl();
190         Query<BookBean> query = queryContext.createQuery("book.reviews.review.reviewer.firstName==X",
191                 BookBean.class);
192         Assert.assertNotNull(query);
193
194         List<BookBean> found = query.find(books);
195         Assert.assertNotNull(found);
196         Assert.assertEquals(2, found.size());
197         Assert.assertEquals("John", found.get(0).getauthor().firstName);
198
199         query = queryContext.createQuery("book.reviews.review.comment==kewl",
200                 BookBean.class);
201         Assert.assertNotNull(query);
202
203         found = query.find(books);
204         Assert.assertNotNull(found);
205         Assert.assertEquals(2, found.size());
206         p("Book 0" + found.get(0));
207         Assert.assertEquals("John", found.get(0).getauthor().firstName);
208
209         query = queryContext.createQuery("book.reviews.review.reviewer.id>300",
210                 BookBean.class);
211         Assert.assertNotNull(query);
212
213         found = query.find(books);
214         Assert.assertNotNull(found);
215         Assert.assertEquals(2, found.size());
216         p("Book 0" + found.get(0));
217         Assert.assertEquals("John", found.get(0).getauthor().firstName);
218
219         query = queryContext.createQuery("book.reviews.review.reviewer.firstName!=X",
220                 BookBean.class);
221         Assert.assertNotNull(query);
222
223         found = query.find(books);
224         Assert.assertNotNull(found);
225         Assert.assertEquals(1, found.size());
226         p("Book 0" + found.get(0));
227         Assert.assertEquals("Foo", found.get(0).getauthor().firstName);
228     }
229
230     @Test
231     public void testXMLAccessorType() {
232         //Assert.fail("implement");
233     }
234
235     @Test
236     public void testMethodAnnotation() throws QueryException {
237         System.out.println(toXml(books.get(0)));
238         QueryContext queryContext = new QueryContextImpl();
239         Query<BookBean> query = queryContext.createQuery(
240                 "book.isbn==preA003", BookBean.class);
241         Assert.assertNotNull(query);
242
243         List<BookBean> found = query.find(books);
244         Assert.assertNotNull(found);
245         Assert.assertEquals(1,found.size());
246         Assert.assertEquals("A", found.get(0).getauthor().firstName);
247     }
248
249     public static String toXml(Object element) {
250         try {
251             JAXBContext jc = JAXBContext.newInstance(element.getClass());
252             Marshaller marshaller = jc.createMarshaller();
253             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
254
255             ByteArrayOutputStream baos = new ByteArrayOutputStream();
256             marshaller.marshal(element, baos);
257             return baos.toString();
258         } catch (Exception e) {
259             e.printStackTrace();
260         }
261         return "";
262     }
263
264     @Test
265     public void testXMLElementWrapperForCompositeTypes(){
266         //Assert.fail("implement");
267     }
268
269 }