protected ResourceState obj(Object... properties) { DefaultResourceState state = new DefaultResourceState(); assertThat(properties.length % 2).isEqualTo(0); int count = properties.length / 2; for (int i = 0; i < count; i++) { String key = (String) properties[2 * i]; Object val = properties[2 * i + 1]; state.putProperty(key, val); } return state; }
protected ResourceState resource( String id, String parentUri, Object[] properties, List<ResourceState> members) throws URISyntaxException { DefaultResourceState state = new DefaultResourceState(id); state.uri(new URI(parentUri + "/" + id)); assertThat(properties.length % 2).isEqualTo(0); int count = properties.length / 2; for (int i = 0; i < count; i++) { String key = (String) properties[2 * i]; Object val = properties[2 * i + 1]; state.putProperty(key, val); } for (ResourceState resource : members) { state.members().add(resource); } return state; }
@Test public void testHttpSubscription() throws Exception { RequestContext requestContext = new RequestContext.Builder().build(); // Create a subscription DefaultResourceState subscriptionState = new DefaultResourceState(); subscriptionState.putProperty("path", "/testApp/memory/data/*"); subscriptionState.putProperty( "destination", "http://localhost:8080/testApp/memory/notifications/"); ResourceState createdSubscription = this.client.create(requestContext, "/testApp/subscriptions", subscriptionState); assertThat(createdSubscription).isNotNull(); assertThat(createdSubscription.getProperty("path")).isEqualTo("/testApp/memory/data/*"); assertThat(createdSubscription.getProperty("destination")) .isEqualTo("http://localhost:8080/testApp/memory/notifications/"); // Create an item that is subscribed to DefaultResourceState bobState = new DefaultResourceState(); bobState.putProperty("name", "Bob McWhirter"); ResourceState createdBob = this.client.create(requestContext, "/testApp/memory/data", bobState); String bobId = createdBob.id(); assertThat(bobId).isNotEmpty(); // Give subscription time to deliver Thread.sleep(1000); assertThat(createdBob.uri().toString()).isEqualTo("/testApp/memory/data/" + bobId); // Check that subscription fired, creating target ResourceState notifiedBob = this.client.read(requestContext, "/testApp/memory/notifications/" + bobId); assertThat(notifiedBob).isNotNull(); assertThat(notifiedBob.id()).isEqualTo(bobId); assertThat(notifiedBob.uri().toString()).isEqualTo("/testApp/memory/notifications/" + bobId); assertThat(notifiedBob.getPropertyNames()).hasSize(1); assertThat(notifiedBob.getProperty("name")).isEqualTo("Bob McWhirter"); // Delete a subscribed thing ResourceState deletedBob = this.client.delete(requestContext, createdBob.uri().toString()); // Give it time to propagate Thread.sleep(1000); // ensure delete was notified to subscriber try { this.client.read(requestContext, notifiedBob.uri().toString()); fail("Should have thrown ResourceNotFoundException"); } catch (ResourceNotFoundException e) { // expected and correct } // Delete the subscription ResourceState deletedSubscription = this.client.delete(requestContext, createdSubscription.uri().toString()); assertThat(deletedSubscription).isNotNull(); // Ensure that further notifications do not occur. DefaultResourceState kenState = new DefaultResourceState(); kenState.putProperty("name", "Ken Finnigan"); ResourceState createdKen = this.client.create(requestContext, "/testApp/memory/data", kenState); String kenId = createdKen.id(); assertThat(kenId).isNotEmpty(); Thread.sleep(2000); try { this.client.read(requestContext, "/testApp/memory/notifications/" + kenId); fail("should have thrown ResourceNotFoundException"); } catch (ResourceNotFoundException e) { // expected and corret } }
@Override public ResourceState properties() throws Exception { DefaultResourceState state = new DefaultResourceState(); state.putProperty("blocking", this.blocking); return state; }