Esempio n. 1
0
  // If Delegates should be called only on remote events,
  // then Simple* should never call them, thus not test required.
  // TODO add test for Port events when we have them
  @Ignore("Ignore until Delegate spec. is clear.")
  @Test
  public final void testEvents() throws InterruptedException {
    final CountDownLatch addLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkAdd =
        event -> {
          assertEquals(DEVICE_ADDED, event.type());
          assertDevice(DID1, SW1, event.subject());
          addLatch.countDown();
        };
    final CountDownLatch updateLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkUpdate =
        event -> {
          assertEquals(DEVICE_UPDATED, event.type());
          assertDevice(DID1, SW2, event.subject());
          updateLatch.countDown();
        };
    final CountDownLatch removeLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkRemove =
        event -> {
          assertEquals(DEVICE_REMOVED, event.type());
          assertDevice(DID1, SW2, event.subject());
          removeLatch.countDown();
        };

    DeviceDescription description =
        new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID);
    deviceStore.setDelegate(checkAdd);
    deviceStore.createOrUpdateDevice(PID, DID1, description);
    assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));

    DeviceDescription description2 =
        new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID);
    deviceStore.unsetDelegate(checkAdd);
    deviceStore.setDelegate(checkUpdate);
    deviceStore.createOrUpdateDevice(PID, DID1, description2);
    assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));

    deviceStore.unsetDelegate(checkUpdate);
    deviceStore.setDelegate(checkRemove);
    deviceStore.removeDevice(DID1);
    assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
  }
Esempio n. 2
0
  @Test
  public final void testRemoveDevice() {
    putDevice(DID1, SW1, A1);
    List<PortDescription> pds =
        Arrays.<PortDescription>asList(new DefaultPortDescription(P1, true, A2));
    deviceStore.updatePorts(PID, DID1, pds);
    putDevice(DID2, SW1);

    assertEquals(2, deviceStore.getDeviceCount());
    assertEquals(1, deviceStore.getPorts(DID1).size());
    assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
    assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);

    Capture<InternalDeviceEvent> message = new Capture<>();
    Capture<MessageSubject> subject = new Capture<>();
    Capture<Function<InternalDeviceEvent, byte[]>> encoder = new Capture<>();

    resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);

    DeviceEvent event = deviceStore.removeDevice(DID1);
    assertEquals(DEVICE_REMOVED, event.type());
    assertDevice(DID1, SW1, event.subject());

    assertEquals(1, deviceStore.getDeviceCount());
    assertEquals(0, deviceStore.getPorts(DID1).size());
    verify(clusterCommunicator);
    // TODO: verify broadcast message
    assertTrue(message.hasCaptured());

    // putBack Device, Port w/o annotation
    putDevice(DID1, SW1);
    List<PortDescription> pds2 =
        Arrays.<PortDescription>asList(new DefaultPortDescription(P1, true));
    deviceStore.updatePorts(PID, DID1, pds2);

    // annotations should not survive
    assertEquals(2, deviceStore.getDeviceCount());
    assertEquals(1, deviceStore.getPorts(DID1).size());
    assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
    assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
  }