2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.northbound.commons.query;
10 import java.io.ByteArrayOutputStream;
11 import java.util.ArrayList;
12 import java.util.List;
14 import javax.xml.bind.JAXBContext;
15 import javax.xml.bind.Marshaller;
17 import org.junit.Assert;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.northbound.commons.types.StringList;
22 public class QueryContextTest {
24 protected static final List<PersonBean> people = new ArrayList<PersonBean>();
25 protected static final List<BookBean> books = new ArrayList<BookBean>();
27 public static void p(String msg) {
28 System.out.println("=== " + msg);
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"));
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)));
42 ReviewBean review1 = new ReviewBean("cool", people.get(2));
43 ReviewBean review2 = new ReviewBean("kewl", people.get(3));
45 books.get(0).addReview(review1).addReview(review2);
46 books.get(1).addReview(review1);
47 books.get(2).addReview(review2).addReview(review1);
52 public void testQueryContext() {
53 QueryContext queryContext = new QueryContextImpl();
54 Assert.assertNotNull(queryContext);
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);
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);
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);
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);
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);
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);
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);
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);
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);
119 public void testXmlWrapperOfWrapper() throws QueryException{
120 WrapperList wrapper = new WrapperList();
121 wrapper.item.add("Test1");
122 wrapper.item.add("Test2");
124 books.get(0).addWrapperList(wrapper);
125 books.get(1).addWrapperList(wrapper);
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);
135 public void testXmlElementWrapperListofList() throws QueryException {
137 List<String> testList = new ArrayList<String>();
140 StringList itemList = new StringList(testList);
141 books.get(0).addToTestList(itemList);
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);
151 public void testPrimitiveIteratableTypes() throws QueryException {
152 // Load data for this test
153 List<String> sellers = new ArrayList<String>();
154 sellers.add("Amazon");
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);
164 System.out.println(toXml(books.get(0)));
166 QueryContext queryContext = new QueryContextImpl();
167 Query<BookBean> query = queryContext.createQuery(
168 "book.soldBy==Amazon", BookBean.class);
169 Assert.assertNotNull(query);
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);
176 query = queryContext.createQuery(
177 "book.soldBy!=Amazon", BookBean.class);
178 Assert.assertNotNull(query);
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);
188 public void testCompositeIteratableTypes() throws QueryException {
189 QueryContext queryContext = new QueryContextImpl();
190 Query<BookBean> query = queryContext.createQuery("book.reviews.review.reviewer.firstName==X",
192 Assert.assertNotNull(query);
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);
199 query = queryContext.createQuery("book.reviews.review.comment==kewl",
201 Assert.assertNotNull(query);
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);
209 query = queryContext.createQuery("book.reviews.review.reviewer.id>300",
211 Assert.assertNotNull(query);
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);
219 query = queryContext.createQuery("book.reviews.review.reviewer.firstName!=X",
221 Assert.assertNotNull(query);
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);
231 public void testXMLAccessorType() {
232 //Assert.fail("implement");
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);
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);
249 public static String toXml(Object element) {
251 JAXBContext jc = JAXBContext.newInstance(element.getClass());
252 Marshaller marshaller = jc.createMarshaller();
253 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
255 ByteArrayOutputStream baos = new ByteArrayOutputStream();
256 marshaller.marshal(element, baos);
257 return baos.toString();
258 } catch (Exception e) {
265 public void testXMLElementWrapperForCompositeTypes(){
266 //Assert.fail("implement");