/** * Test case for {@link VTNEtherMatch#verify()}. * * @throws Exception An error occurred. */ @Test public void testVerify() throws Exception { Unmarshaller um = createUnmarshaller(VTNEtherMatch.class); // Invalid ether types. Integer[] badTypes = { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -0x70000000, -0x10000000, -0x10000, -0xffff, -10, -3, -2, -1, 0x10000, 0x10001, 0x20000, 0x10000000, 0x50000000, Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, }; for (Integer type : badTypes) { XmlNode root = new XmlNode(XML_ROOT).add(new XmlNode("ether-type", type)); String xml = root.toString(); VTNEtherMatch ematch = unmarshal(um, xml, VTNEtherMatch.class); try { ematch.verify(); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("Invalid Ethernet type: " + type, st.getDescription()); } } // Invalid VLAN ID. Integer[] badVlanIds = { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -0x70000000, -0x10000000, -0x10000, -0xffff, -10, -3, -2, -1, 0x1000, 0x1001, 0x1002, 0x20000, 0xffffff, 0x60000000, Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, }; for (Integer vid : badVlanIds) { XmlNode root = new XmlNode(XML_ROOT).add(new XmlNode("vlan-id", vid)); String xml = root.toString(); VTNEtherMatch ematch = unmarshal(um, xml, VTNEtherMatch.class); try { ematch.verify(); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("Invalid VLAN ID: " + vid, st.getDescription()); } } // Invalid VLAN priority. Short[] badPcps = { Short.MIN_VALUE, -30000, -20000, -10000, -0x100, -3, -2, -1, 8, 9, 10, 0x100, 300, 10000, Short.MAX_VALUE - 1, Short.MAX_VALUE, }; for (Short pcp : badPcps) { XmlNode root = new XmlNode(XML_ROOT).add(new XmlNode("vlan-pcp", pcp)); String xml = root.toString(); VTNEtherMatch ematch = unmarshal(um, xml, VTNEtherMatch.class); try { ematch.verify(); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("Invalid VLAN priority: " + pcp, st.getDescription()); } } // Specifying VLAN priority without VLAN ID. Integer[] untagged = {null, EtherHeader.VLAN_NONE}; for (Integer vid : untagged) { for (byte pcp = 0; pcp <= 7; pcp++) { XmlNode root = new XmlNode(XML_ROOT).add(new XmlNode("vlan-pcp", pcp)); if (vid != null) { root.add(new XmlNode("vlan-id", vid)); } String xml = root.toString(); VTNEtherMatch ematch = unmarshal(um, xml, VTNEtherMatch.class); try { ematch.verify(); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("VLAN priority requires a valid VLAN ID.", st.getDescription()); } } } }
/** * Ensure that {@link VTNEtherMatch} is mapped to XML root element. * * @throws Exception An error occurred. */ @Test public void testJAXB() throws Exception { Unmarshaller um = createUnmarshaller(VTNEtherMatch.class); EtherMatchParams params = new EtherMatchParams(); // Empty match. String xml = new XmlNode(XML_ROOT).toString(); VTNEtherMatch ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); // Specifying all fields. EtherAddress src = new EtherAddress(0x001122334455L); EtherAddress dst = new EtherAddress(0x0abcdef12345L); Integer etype = Integer.valueOf(0x806); Integer vid = Integer.valueOf(4095); Short pcp = Short.valueOf((short) 7); params.setSourceAddress(src); params.setDestinationAddress(dst); params.setEtherType(etype); params.setVlanId(vid); params.setVlanPriority(pcp); final String tagSrc = "source-address"; final String tagDst = "destination-address"; final String tagType = "ether-type"; final String tagVid = "vlan-id"; final String tagPcp = "vlan-pcp"; xml = new XmlNode(XML_ROOT) .add(new XmlNode(tagSrc, src.getText())) .add(new XmlNode(tagDst, dst.getText())) .add(new XmlNode(tagType, etype)) .add(new XmlNode(tagVid, vid)) .add(new XmlNode(tagPcp, pcp)) .toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); // Specifying single field. params.reset().setSourceAddress(src); xml = new XmlNode(XML_ROOT).add(new XmlNode(tagSrc, src.getText())).toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); params.reset().setDestinationAddress(dst); xml = new XmlNode(XML_ROOT).add(new XmlNode(tagDst, dst.getText())).toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); params.reset().setEtherType(etype); xml = new XmlNode(XML_ROOT).add(new XmlNode(tagType, etype)).toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); params.reset().setVlanId(vid); xml = new XmlNode(XML_ROOT).add(new XmlNode(tagVid, vid)).toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); params.setVlanPriority(pcp); xml = new XmlNode(XML_ROOT) .add(new XmlNode(tagVid, vid)) .add(new XmlNode(tagPcp, pcp)) .toString(); ematch = unmarshal(um, xml, VTNEtherMatch.class); ematch.verify(); assertEquals(params.toVTNEtherMatch(), ematch); // Ensure that broken values in XML can be detected. jaxbErrorTest(VTNEtherMatch.class, getXmlDataTypes(XML_ROOT)); }
/** * Test case for the following methods. * * <ul> * <li>{@link * VTNEtherMatch#VTNEtherMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.VtnEtherMatchFields)} * <li>{@link VTNEtherMatch#create(Match)} * <li>{@link VTNEtherMatch#setMatch(MatchBuilder)} * <li>Getter methods. * <li>JAXB bindings. * </ul> * * @throws Exception An error occurred. */ @Test public void testConstructor3() throws Exception { EtherAddress[] srcs = { null, new EtherAddress(1L), new EtherAddress(0x000102030405L), }; EtherAddress[] dsts = { null, new EtherAddress(0xf0f1f2f3f4f5L), new EtherAddress(0xa8b9cadbecfdL), }; Integer[] types = {null, 0x800, 0x86dd}; Integer[] vlans = {null, 0, 1, 4095}; Short[] priorities = {0, 3, 7}; EtherMatchParams params = new EtherMatchParams(); Class<VTNEtherMatch> mtype = VTNEtherMatch.class; for (EtherAddress src : srcs) { params.setSourceAddress(src); for (EtherAddress dst : dsts) { params.setDestinationAddress(dst); for (Integer type : types) { params.setEtherType(type); for (Integer vlan : vlans) { params.setVlanId(vlan).setVlanPriority((Short) null); VtnEtherMatch vem = params.toVtnEtherMatch(); VTNEtherMatch ematch = new VTNEtherMatch(vem); params.verify(ematch); // JAXB test. VTNEtherMatch jaxb = jaxbTest(ematch, mtype, XML_ROOT); jaxb.verify(); VtnEtherMatch vem1 = jaxb.toVtnEtherMatchBuilder().build(); assertEquals(vem, vem1); if (vlan == null || vlan.intValue() == EtherHeader.VLAN_NONE) { continue; } for (Short pri : priorities) { params.setVlanPriority(pri); vem = params.toVtnEtherMatch(); ematch = new VTNEtherMatch(vem); params.verify(ematch); // JAXB test. jaxb = jaxbTest(ematch, mtype, XML_ROOT); jaxb.verify(); vem1 = jaxb.toVtnEtherMatchBuilder().build(); assertEquals(vem, vem1); } } } } } // Invalid ether types. Long[] badTypes = { 0x10000L, 0x10001L, 0x20000L, 0x7fffffffL, 0x80000000L, 0xaaaaaaaaL, 0xccccccccL, 0xffff0000L, 0xffffffffL, }; for (Long type : badTypes) { VtnEtherMatch vem = new VtnEtherMatchBuilder().setEtherType(new EtherType(type)).build(); try { new VTNEtherMatch(vem); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("Invalid Ethernet type: " + type, st.getDescription()); } } // Specifying VLAN priority without VLAN ID. Integer[] untagged = {null, EtherHeader.VLAN_NONE}; for (Integer vid : untagged) { params.setVlanId(vid); for (byte pcp = 0; pcp <= 7; pcp++) { params.setVlanPriority(pcp); VtnEtherMatch vem = params.toVtnEtherMatch(); try { new VTNEtherMatch(vem); unexpected(); } catch (RpcException e) { assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag()); Status st = e.getStatus(); assertEquals(StatusCode.BADREQUEST, st.getCode()); assertEquals("VLAN priority requires a valid VLAN ID.", st.getDescription()); } } } }