Example #1
0
  /**
   * Set a XML node which represents this node into the given string builder.
   *
   * @param builder A string builder.
   */
  private void setXml(StringBuilder builder) {
    builder.append('<').append(nodeName);
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
      String key = entry.getKey();
      Object value = entry.getValue();
      builder.append(' ').append(key).append("=\"").append(String.valueOf(value)).append("\"");
    }
    builder.append('>');

    for (XmlNode child : children) {
      child.setXml(builder);
    }
    if (nodeValue != null) {
      builder.append(nodeValue);
    }

    builder.append("</").append(nodeName).append('>');
  }
  /**
   * 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());
        }
      }
    }
  }