示例#1
0
  // @Test
  public void testSubnetCascadingDelete() {
    // Create a subnet with several configuration pieces: option 3,
    // option 121, and a host assignment. Then delete it.
    ClientResponse response;

    DtoDhcpSubnet subnet = new DtoDhcpSubnet();
    subnet.setSubnetPrefix("172.31.0.0");
    subnet.setSubnetLength(24);
    subnet.setServerAddr("172.31.0.118");
    response =
        resource()
            .uri(bridge.getDhcpSubnets())
            .type(APPLICATION_DHCP_SUBNET_JSON)
            .post(ClientResponse.class, subnet);
    assertEquals(201, response.getStatus());
    subnet =
        resource()
            .uri(response.getLocation())
            .accept(APPLICATION_DHCP_SUBNET_JSON)
            .get(DtoDhcpSubnet.class);

    DtoDhcpHost host1 = new DtoDhcpHost();
    host1.setMacAddr("02:33:44:55:00:00");
    host1.setIpAddr("172.31.0.11");
    host1.setName("saturn");
    response =
        resource()
            .uri(subnet.getHosts())
            .type(APPLICATION_DHCP_HOST_JSON)
            .post(ClientResponse.class, host1);
    assertEquals(201, response.getStatus());

    // List the subnets
    response =
        resource()
            .uri(bridge.getDhcpSubnets())
            .accept(APPLICATION_DHCP_SUBNET_COLLECTION_JSON)
            .get(ClientResponse.class);
    assertEquals(200, response.getStatus());
    DtoDhcpSubnet[] subnets = response.getEntity(DtoDhcpSubnet[].class);
    assertThat("We expect 1 listed subnets.", subnets, arrayWithSize(1));
    assertThat(
        "We expect the listed subnets to match the one we created.",
        subnets,
        arrayContainingInAnyOrder(subnet));

    // Now delete the subnet.
    response = resource().uri(subnet.getUri()).delete(ClientResponse.class);
    assertEquals(204, response.getStatus());
    // Show that the list of DHCP subnet configurations is empty.
    response =
        resource()
            .uri(bridge.getDhcpSubnets())
            .accept(APPLICATION_DHCP_SUBNET_COLLECTION_JSON)
            .get(ClientResponse.class);
    assertEquals(200, response.getStatus());
    subnets = response.getEntity(DtoDhcpSubnet[].class);
    assertThat("We expect 0 listed subnets.", subnets, arrayWithSize(0));
  }
示例#2
0
  @Test
  public void testHosts() {
    // In this test remember that there will be multiple host definitions.
    // The system enforces that there is only one host def per mac address.
    ClientResponse response;

    DtoDhcpSubnet subnet = new DtoDhcpSubnet();
    subnet.setSubnetPrefix("172.31.0.0");
    subnet.setSubnetLength(24);
    subnet.setServerAddr("172.31.0.118");
    response =
        resource()
            .uri(bridge.getDhcpSubnets())
            .type(APPLICATION_DHCP_SUBNET_JSON)
            .post(ClientResponse.class, subnet);
    assertEquals(201, response.getStatus());
    subnet =
        resource()
            .uri(response.getLocation())
            .accept(APPLICATION_DHCP_SUBNET_JSON)
            .get(DtoDhcpSubnet.class);

    DtoDhcpHost host1 = new DtoDhcpHost();
    List<DtoExtraDhcpOpt> opts = new ArrayList<>();
    opts.add(new DtoExtraDhcpOpt("name1", "val1"));
    opts.add(new DtoExtraDhcpOpt("name2", "val2"));
    opts.add(new DtoExtraDhcpOpt("name3", "val3"));
    host1.setMacAddr("02:33:44:55:00:00");
    host1.setIpAddr("172.31.0.11");
    host1.setName("saturn");
    host1.setExtraDhcpOpts(opts);
    response =
        resource()
            .uri(subnet.getHosts())
            .type(APPLICATION_DHCP_HOST_JSON_V2)
            .post(ClientResponse.class, host1);
    assertEquals(201, response.getStatus());
    host1 =
        resource()
            .uri(response.getLocation())
            .accept(APPLICATION_DHCP_HOST_JSON_V2)
            .get(DtoDhcpHost.class);
    assertEquals("02:33:44:55:00:00", host1.getMacAddr());
    assertEquals("172.31.0.11", host1.getIpAddr());
    assertEquals("saturn", host1.getName());
    assertEquals(opts, host1.getExtraDhcpOpts());

    DtoDhcpHost host2 = new DtoDhcpHost();
    host2.setMacAddr("02:33:44:55:00:01");
    host2.setIpAddr("172.31.0.12");
    host2.setName("jupiter");
    List<DtoExtraDhcpOpt> opts2 = new ArrayList<>();
    opts2.add(new DtoExtraDhcpOpt("name1", "val1"));
    opts2.add(new DtoExtraDhcpOpt("name2", "val2"));
    opts2.add(new DtoExtraDhcpOpt("name3", "val3"));
    host2.setExtraDhcpOpts(opts2);
    response =
        resource()
            .uri(subnet.getHosts())
            .type(APPLICATION_DHCP_HOST_JSON_V2)
            .post(ClientResponse.class, host2);
    assertEquals(201, response.getStatus());
    host2 =
        resource()
            .uri(response.getLocation())
            .accept(APPLICATION_DHCP_HOST_JSON_V2)
            .get(DtoDhcpHost.class);
    assertEquals("02:33:44:55:00:01", host2.getMacAddr());
    assertEquals("172.31.0.12", host2.getIpAddr());
    assertEquals("jupiter", host2.getName());
    assertEquals(opts, host2.getExtraDhcpOpts());

    // Now list all the host static assignments.
    response =
        resource()
            .uri(subnet.getHosts())
            .accept(APPLICATION_DHCP_HOST_COLLECTION_JSON_V2)
            .get(ClientResponse.class);
    assertEquals(200, response.getStatus());

    DtoDhcpHost[] hosts = response.getEntity(DtoDhcpHost[].class);
    assertThat("We expect 2 listed hosts.", hosts, arrayWithSize(2));
    assertThat(
        "We expect the listed hosts to match those we created.",
        hosts,
        arrayContainingInAnyOrder(host2, host1));

    // Now try to create a new host with host1's mac address. This should
    // fail.
    host1.setIpAddr("172.31.0.13");
    response =
        resource()
            .uri(subnet.getHosts())
            .type(APPLICATION_DHCP_HOST_JSON_V2)
            .post(ClientResponse.class, host1);
    assertEquals(409, response.getStatus());

    // Try again, this time using an UPDATE operation.
    response =
        resource()
            .uri(host1.getUri())
            .type(APPLICATION_DHCP_HOST_JSON_V2)
            .put(ClientResponse.class, host1);
    assertEquals(204, response.getStatus());
    host1 =
        resource().uri(host1.getUri()).accept(APPLICATION_DHCP_HOST_JSON_V2).get(DtoDhcpHost.class);
    assertEquals("02:33:44:55:00:00", host1.getMacAddr());
    assertEquals("172.31.0.13", host1.getIpAddr());
    assertEquals("saturn", host1.getName());

    // There should still be exactly 2 host assignments.
    response =
        resource()
            .uri(subnet.getHosts())
            .accept(APPLICATION_DHCP_HOST_COLLECTION_JSON_V2)
            .get(ClientResponse.class);
    assertEquals(200, response.getStatus());
    hosts = response.getEntity(DtoDhcpHost[].class);
    assertThat("We expect 2 listed hosts.", hosts, arrayWithSize(2));
    assertThat(
        "We expect the listed hosts to match those we created.",
        hosts,
        arrayContainingInAnyOrder(host1, host2));

    // Now delete one of the host assignments.
    response = resource().uri(host1.getUri()).delete(ClientResponse.class);
    assertEquals(204, response.getStatus());
    // There should now be only 1 host assignment.
    response =
        resource()
            .uri(subnet.getHosts())
            .accept(APPLICATION_DHCP_HOST_COLLECTION_JSON_V2)
            .get(ClientResponse.class);
    assertEquals(200, response.getStatus());
    hosts = response.getEntity(DtoDhcpHost[].class);
    assertThat("We expect 1 listed host after the delete", hosts, arrayWithSize(1));
    assertThat(
        "The listed hosts should be the one that wasn't deleted.",
        hosts,
        arrayContainingInAnyOrder(host2));
  }