/**
   * Test case for the following methods.
   *
   * <ul>
   *   <li>{@link VTNEtherMatch#VTNEtherMatch(EthernetMatch)}
   *   <li>{@link VTNEtherMatch#VTNEtherMatch(EtherAddress,EtherAddress,Integer,Integer,Short)}
   *   <li>{@link VTNEtherMatch#create(Match)}
   *   <li>{@link VTNEtherMatch#setMatch(MatchBuilder)}
   *   <li>Getter methods.
   * </ul>
   *
   * @throws Exception An error occurred.
   */
  @Test
  public void testConstructor2() 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();
    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 ematch = params.toVTNEtherMatch();
            params.verify(ematch);

            VTNEtherMatch ematch1 = new VTNEtherMatch(src, dst, type, vlan, null);
            assertEquals(ematch, ematch1);

            if (vlan == null || vlan.intValue() == EtherHeader.VLAN_NONE) {
              continue;
            }

            for (Short pri : priorities) {
              params.setVlanPriority(pri);
              ematch = params.toVTNEtherMatch();
              params.verify(ematch);
              ematch1 = new VTNEtherMatch(src, dst, type, vlan, pri);
              assertEquals(ematch, ematch1);
            }
          }
        }
      }
    }

    // Create broken EthernetMatch.
    String[] badAddrs = {
      "", "aa:bb:cc:dd:ee:ff:11", "00:11", "bad_address",
    };
    Unmarshaller um = createUnmarshaller(EthernetMatch.class);
    for (String addr : badAddrs) {
      String xml = new XmlNode("ethermatch").setAttribute("src", addr).toString();
      EthernetMatch em = unmarshal(um, xml, EthernetMatch.class);
      assertNotNull(em.getValidationStatus());
      try {
        new VTNEtherMatch(em);
        unexpected();
      } catch (RpcException e) {
        assertEquals(RpcErrorTag.BAD_ELEMENT, e.getErrorTag());
        Status st = e.getStatus();
        assertEquals(StatusCode.BADREQUEST, st.getCode());

        String msg = "Invalid source MAC address: " + addr + ": ";
        String desc = st.getDescription();
        assertTrue("Unexpected error message: " + desc, desc.startsWith(msg));
      }
    }

    // Invalid ether types.
    Integer[] badTypes = {
      Integer.MIN_VALUE, -10, -1, 0x10000, 0x10001, 0x33333333, Integer.MAX_VALUE,
    };
    for (Integer type : badTypes) {
      params.setEtherType(type);
      EthernetMatch em = params.toEthernetMatch();
      assertEquals(null, em.getValidationStatus());
      try {
        new VTNEtherMatch(em);
        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());
      }

      try {
        new VTNEtherMatch(null, null, type, null, null);
        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.
    params.setEtherType(0x800);
    params.setVlanPriority((Short) null);
    Short[] badVlanIds = {
      Short.MIN_VALUE, -30000, -10000, -3, -2, -1, 0x1000, 0x1001, 0x5000, Short.MAX_VALUE,
    };
    for (Short vid : badVlanIds) {
      params.setVlanId(vid);
      EthernetMatch em = params.toEthernetMatch();
      assertEquals(null, em.getValidationStatus());
      try {
        new VTNEtherMatch(em);
        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());
      }

      try {
        new VTNEtherMatch(null, null, null, vid.intValue(), null);
        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.
    params.setVlanId(1);
    Byte[] badPcps = {
      Byte.MIN_VALUE, -100, -50, -2, -1, 8, 9, 10, 50, 100, Byte.MAX_VALUE,
    };
    for (Byte pcp : badPcps) {
      params.setVlanPriority(pcp);
      EthernetMatch em = params.toEthernetMatch();
      assertEquals(null, em.getValidationStatus());
      try {
        new VTNEtherMatch(em);
        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());
      }

      try {
        new VTNEtherMatch(null, null, null, 10, pcp.shortValue());
        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) {
      params.setVlanId(vid);
      for (byte pcp = 0; pcp <= 7; pcp++) {
        params.setVlanPriority(pcp);
        EthernetMatch em = params.toEthernetMatch();
        assertEquals(null, em.getValidationStatus());
        try {
          new VTNEtherMatch(em);
          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());
        }

        try {
          new VTNEtherMatch(null, null, null, null, (short) pcp);
          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());
        }
      }
    }

    Match empty = new MatchBuilder().build();
    assertEquals(null, VTNEtherMatch.create(empty));

    // Invalid VLAN ID match.
    VlanIdBuilder vb = new VlanIdBuilder().setVlanIdPresent(true);
    VlanMatchBuilder vmb = new VlanMatchBuilder().setVlanId(vb.build());
    RpcErrorTag etag = RpcErrorTag.BAD_ELEMENT;
    StatusCode code = StatusCode.BADREQUEST;
    String msg = "Unsupported VLAN ID match: " + vmb.getVlanId();
    try {
      new VTNEtherMatch(null, vmb.build());
      unexpected();
    } catch (RpcException e) {
      assertEquals(etag, e.getErrorTag());
      Status st = e.getStatus();
      assertEquals(code, st.getCode());
      assertEquals(msg, st.getDescription());
    }

    vb.setVlanId(new VlanId(0));
    vmb.setVlanId(vb.build());
    msg = "Unsupported VLAN ID match: " + vmb.getVlanId();
    try {
      new VTNEtherMatch(null, vmb.build());
      unexpected();
    } catch (RpcException e) {
      assertEquals(etag, e.getErrorTag());
      Status st = e.getStatus();
      assertEquals(code, st.getCode());
      assertEquals(msg, st.getDescription());
    }
  }