X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnorthbound%2Fcommons%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnorthbound%2Fcommons%2Fquery%2FXMLAccessorTypeTest.java;fp=opendaylight%2Fnorthbound%2Fcommons%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnorthbound%2Fcommons%2Fquery%2FXMLAccessorTypeTest.java;h=25cb69214eb2ffbca95a3b3510b2041f46204d04;hb=31c83799d67d0bf4012aefedaba5356ebfaad8ab;hp=0000000000000000000000000000000000000000;hpb=b1e455ac4685602b7b3290192906c607d2c92c71;p=controller.git diff --git a/opendaylight/northbound/commons/src/test/java/org/opendaylight/controller/northbound/commons/query/XMLAccessorTypeTest.java b/opendaylight/northbound/commons/src/test/java/org/opendaylight/controller/northbound/commons/query/XMLAccessorTypeTest.java new file mode 100644 index 0000000000..25cb69214e --- /dev/null +++ b/opendaylight/northbound/commons/src/test/java/org/opendaylight/controller/northbound/commons/query/XMLAccessorTypeTest.java @@ -0,0 +1,374 @@ +package org.opendaylight.controller.northbound.commons.query; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; + +import org.junit.Assert; +import org.junit.Test; + +public class XMLAccessorTypeTest { + + @Test + public void testPublicAccessType() throws Exception { + // create bean + List testList = new ArrayList(); + testList.add(new PublicAccessBean("John", "Scott", "private", 1, + "transient", "elem1")); + testList.add(new PublicAccessBean("Foo", "Bar", "private1", 2, + "transient1", "elem2")); + QueryContextTest.p(QueryContextTest.toXml(testList.get(0))); + + QueryContext queryContext = new QueryContextImpl(); + Assert.assertNotNull(queryContext); + // search for public field + Query query = queryContext.createQuery( + "publicbean.firstName==Foo", PublicAccessBean.class); + Assert.assertNotNull(query); + + List found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("Foo", found.get(0).firstName); + + // search for public getter + query = queryContext.createQuery("publicbean.privateGetterField<2", + PublicAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + + // test for transient field + query = queryContext.createQuery("publicbean.transientField='trans*'", + PublicAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(0, found.size()); + + // test for private field + query = queryContext.createQuery("publicbean.privateField==private", + PublicAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(0, found.size()); + + // test for XML Element + query = queryContext.createQuery("publicbean.element==elem1", + PublicAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + } + + @Test + public void testFieldAccessType() throws QueryException { + // create bean + List testList = new ArrayList(); + testList.add(new FieldAccessBean("John", "Scott", "private", 1, "elem1")); + testList.add(new FieldAccessBean("Foo", "Bar", "private1", 2, "elem2")); + + QueryContextTest.p(QueryContextTest.toXml(testList.get(0))); + QueryContext queryContext = new QueryContextImpl(); + Assert.assertNotNull(queryContext); + // test private field + Query query = queryContext.createQuery( + "field.privateField==private", FieldAccessBean.class); + Assert.assertNotNull(query); + + List found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + + // test public field + query = queryContext.createQuery("field.firstName==Foo", + FieldAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("Foo", found.get(0).firstName); + + // test annotated field + query = queryContext.createQuery("field.element==elem2", + FieldAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("Foo", found.get(0).firstName); + + // test annotated method + query = queryContext.createQuery("field.privateGetterField==11", + FieldAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + } + + @Test + public void testPropertyAccessType() throws QueryException { + // create bean + List testList = new ArrayList(); + testList.add(new PropertyAccessBean("John", "Scott", "private", 1, "elem1", + "transient1")); + testList.add(new PropertyAccessBean("Foo", "Bar", "private1", 2, "elem2", + "transient2")); + + QueryContextTest.p(QueryContextTest.toXml(testList.get(0))); + QueryContext queryContext = new QueryContextImpl(); + Assert.assertNotNull(queryContext); + // test public getter public field + Query query = queryContext.createQuery( + "property.firstName==John", PropertyAccessBean.class); + Assert.assertNotNull(query); + + List found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + + // test public field no getter + query = queryContext.createQuery("property.lastName==Bar", + PropertyAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(0, found.size()); + + // test annotated field + query = queryContext.createQuery("property.element==elem2", + PropertyAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("Foo", found.get(0).firstName); + + // test annotated method + query = queryContext.createQuery("property.field==private", + PropertyAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).firstName); + + // test transient method + query = queryContext.createQuery("property.transientField==transient1", + PropertyAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(0, found.size()); + } + + @Test + public void testNoneAccessType() throws QueryException { + // create bean + List testList = new ArrayList(); + testList.add(new NoneAccessBean("John", "Scott", "private")); + testList.add(new NoneAccessBean("Foo", "Bar", "private1")); + + QueryContextTest.p(QueryContextTest.toXml(testList.get(0))); + QueryContext queryContext = new QueryContextImpl(); + Assert.assertNotNull(queryContext); + // test annotated field + Query query = queryContext.createQuery( + "test.firstName==John", NoneAccessBean.class); + Assert.assertNotNull(query); + + List found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).getFirstName()); + // test unannotated field + query = queryContext + .createQuery("test.lastName==Bar", NoneAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(0, found.size()); + // test annotated method + query = queryContext.createQuery("test.testField==private", + NoneAccessBean.class); + Assert.assertNotNull(query); + + found = query.find(testList); + Assert.assertNotNull(found); + Assert.assertEquals(1, found.size()); + Assert.assertEquals("John", found.get(0).getFirstName()); + + } + +} + +// default ( public memeber ) +@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) +@XmlRootElement(name = "publicbean") +class PublicAccessBean { + + public String firstName; + public String lastName; + private String privateField; + private int privateGetterField; + @XmlTransient + public String transientField; + @XmlElement(name = "element") + private String xmlElem; + + public PublicAccessBean() { + } + + public PublicAccessBean(String firstName, String lastName, + String privateField, int privateGetterField, String transientField, + String xmlElem) { + this.firstName = firstName; + this.lastName = lastName; + this.privateField = privateField; + this.privateGetterField = privateGetterField; + this.transientField = transientField; + this.xmlElem = xmlElem; + } + + public int getPrivateGetterField() { + return privateGetterField; + } + + public void setPrivateGetterField(int field) { + this.privateGetterField = field; + } +} + +// default ( public memeber ) +@XmlAccessorType(XmlAccessType.FIELD) +@XmlRootElement(name = "field") +class FieldAccessBean { + + public String firstName; + public String lastName; + private String privateField; + private int test; + @XmlElement(name = "element") + private String xmlElem; + + public FieldAccessBean() { + } + + public FieldAccessBean(String firstName, String lastName, + String privateField, int privateGetterField, String xmlElem) { + this.firstName = firstName; + this.lastName = lastName; + this.privateField = privateField; + this.xmlElem = xmlElem; + this.test = privateGetterField; + } + + public String getPrivateField() { + return privateField; + } + + @XmlElement(name = "privateGetterField") + public int getPrivateGetterField() { + return test + 10; + } +} + +// default ( public memeber ) +@XmlAccessorType(XmlAccessType.PROPERTY) +@XmlRootElement(name = "property") +class PropertyAccessBean { + + public String firstName; + public String lastName; + private String privateField; + private int privateGetterField; + @XmlElement(name = "element") + private String xmlElem; + private String transientField; + + public PropertyAccessBean() { + } + + public PropertyAccessBean(String firstName, String lastName, + String privateField, int privateGetterField, String xmlElem, + String transientField) { + this.firstName = firstName; + this.lastName = lastName; + this.privateField = privateField; + this.privateGetterField = privateGetterField; + this.xmlElem = xmlElem; + this.transientField = transientField; + } + + public int getPrivateGetterField() { + return privateGetterField; + } + + @XmlElement(name = "field") + public String getPrivateField() { + return privateField; + } + + public String getFirstName() { + return firstName; + } + + @XmlTransient + public String getTransientField() { + return transientField; + } +} + +// default ( public memeber ) +@XmlAccessorType(XmlAccessType.NONE) +@XmlRootElement(name = "test") +class NoneAccessBean { + @XmlElement + private String firstName; + public String lastName; + private String testField; + + public NoneAccessBean() { + } + + public NoneAccessBean(String firstName, String lastName, String testField) { + this.firstName = firstName; + this.lastName = lastName; + this.testField = testField; + } + + @XmlElement(name = "testField") + public String getTestField() { + return testField; + } + + public String getFirstName() { + return firstName; + } +}