@Test
  public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() throws Exception {
    policy.subscribe(entity, TestEntity.SEQUENCE, listener);
    policy.subscribe(entity, TestEntity.NAME, listener);
    policy.subscribe(entity, TestEntity.MY_NOTIF, listener);
    policy.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
    policy.unsubscribe(entity);

    entity.setAttribute(TestEntity.SEQUENCE, 123);
    entity.setAttribute(TestEntity.NAME, "myname");
    entity.emit(TestEntity.MY_NOTIF, 456);
    otherEntity.setAttribute(TestEntity.SEQUENCE, 789);

    Thread.sleep(SHORT_WAIT_MS);
    Asserts.succeedsEventually(
        new Runnable() {
          @Override
          public void run() {
            assertEquals(
                listener.events,
                ImmutableList.of(
                    new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 789)));
          }
        });
  }
  public ProxyPolicy(AbstractPolicy policy) {
    if (policy == null) throw new IllegalArgumentException("policy may not be null");

    this.id = policy.getId().toString();
    this.target = policy.getTarget();

    if (policy instanceof Policy) this.refType = PolicyReference.POLICY_REFERENCE;
    if (policy instanceof PolicySet) this.refType = PolicyReference.POLICYSET_REFERENCE;
  }
  @Test
  public void testSubscriptionReceivesEvents() throws Exception {
    policy.subscribe(entity, TestEntity.SEQUENCE, listener);
    policy.subscribe(entity, TestEntity.NAME, listener);
    policy.subscribe(entity, TestEntity.MY_NOTIF, listener);

    otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
    entity.setAttribute(TestEntity.SEQUENCE, 123);
    entity.setAttribute(TestEntity.NAME, "myname");
    entity.emit(TestEntity.MY_NOTIF, 789);

    Asserts.succeedsEventually(
        new Runnable() {
          @Override
          public void run() {
            assertEquals(
                listener.events,
                ImmutableList.of(
                    new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
                    new BasicSensorEvent<String>(TestEntity.NAME, entity, "myname"),
                    new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, entity, 789)));
          }
        });
  }
  @Test
  public void testUnsubscribeUsingHandleStopsEvents() throws Exception {
    SubscriptionHandle handle1 = policy.subscribe(entity, TestEntity.SEQUENCE, listener);
    SubscriptionHandle handle2 = policy.subscribe(entity, TestEntity.NAME, listener);
    SubscriptionHandle handle3 = policy.subscribe(otherEntity, TestEntity.SEQUENCE, listener);

    policy.unsubscribe(entity, handle2);

    entity.setAttribute(TestEntity.SEQUENCE, 123);
    entity.setAttribute(TestEntity.NAME, "myname");
    otherEntity.setAttribute(TestEntity.SEQUENCE, 456);

    Asserts.succeedsEventually(
        new Runnable() {
          @Override
          public void run() {
            assertEquals(
                listener.events,
                ImmutableList.of(
                    new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
                    new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
          }
        });
  }