/** * Assert that an object reference is not null. * * @param object The object reference. * * @throws AssertionError if the assertion fails. */ static public void assertNotNull(final Object object) { assertNotNull(object, null); }
@Override public void run() { HostAndPort val = supplier.get(); Asserts.assertNotNull(val); assertSucceeds(assertions, val); }}); setUpAndRunState(true, Lifecycle.RUNNING);
public static void assertContentMatches(String url, String regex) { String contents = HttpTool.getContent(url); Asserts.assertNotNull(contents); Asserts.assertTrue(contents.matches(regex), "Contents does not match expected regex ("+regex+"): "+contents); }
@Override public boolean apply(Lifecycle.Transition transition) { assertNotNull(transition); return Lifecycle.RUNNING.equals(transition.getState()); } });
private String getFileContentsAsString(final String filename) throws Exception { final URL resource = getClass().getClassLoader().getResource(filename); Asserts.assertNotNull(resource); return new String(Files.readAllBytes(Paths.get(resource.toURI())), Charset.defaultCharset()); } }
private void assertHasBasic1() { RegisteredType basic1T = mgmt().getTypeRegistry().get("basic1"); Asserts.assertNotNull(basic1T, "basic1 not in catalog"); }
@Test public void testAddAnonymousBomTwiceDeletesOldEmptyOne() { Map<String, ManagedBundle> b1 = ((ManagementContextInternal)mgmt()).getOsgiManager().get().getManagedBundles(); addCatalogItems(bomAnonymous()); Map<String, ManagedBundle> b2_new = MutableMap.copyOf( ((ManagementContextInternal)mgmt()).getOsgiManager().get().getManagedBundles() ); for (String old: b1.keySet()) b2_new.remove(old); RegisteredType sample = mgmt().getTypeRegistry().get("sample"); Asserts.assertNotNull(sample); Asserts.assertSize(b2_new.values(), 1); addCatalogItems(bomAnonymous()); Map<String, ManagedBundle> b3 = MutableMap.copyOf( ((ManagementContextInternal)mgmt()).getOsgiManager().get().getManagedBundles() ); Map<String, ManagedBundle> b3_new = MutableMap.copyOf(b3); for (String old: b1.keySet()) b3_new.remove(old); for (String old: b2_new.keySet()) b3_new.remove(old); Asserts.assertSize(b3_new.values(), 1); Asserts.assertFalse(b3_new.keySet().contains( Iterables.getOnlyElement(b2_new.keySet()) )); }
@Test public void testPayloadWithContentTypeHeaderJson() throws InterruptedException { server.enqueue(jsonResponse("map-response.json")); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/post")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, ImmutableMap.of("key", "value")) .configure(HttpCommandEffector.EFFECTOR_HTTP_HEADERS, ImmutableMap.of(HttpHeaders.CONTENT_TYPE, "application/json")) .configure(HttpCommandEffector.JSON_PATH, "$.data") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.minutes(1)); assertEquals(output, "{\"key\", \"value\"}"); assertEquals(server.getRequestCount(), 1); assertSent(server, "POST", "/post"); }
@Test public void testPayloadWithContentTypeHeaderYaml() throws InterruptedException { server.enqueue((jsonResponse("map-response.json"))); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/post")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, "my yaml") .configure(HttpCommandEffector.EFFECTOR_HTTP_HEADERS, ImmutableMap.of(HttpHeaders.CONTENT_TYPE, "application/yaml")) .configure(HttpCommandEffector.JSON_PATH, "$.data") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.minutes(1)); assertEquals(server.getRequestCount(), 1); assertEquals(new String(server.takeRequest().getBody()), "my yaml"); }
@Test public void testPayloadWithoutContentTypeHeader() throws InterruptedException { server.enqueue(jsonResponse("map-response.json")); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/post")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, ImmutableMap.of("key", "value")) .configure(HttpCommandEffector.JSON_PATH, "$.data") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); assertEquals(output, "{\"key\", \"value\"}"); assertEquals(server.getRequestCount(), 1); assertSent(server, "POST", "/post"); }
@Test public void testListPayloadWithoutContentTypeHeader() throws InterruptedException { server.enqueue(jsonResponse("list-response.json")); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/post")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, ImmutableList.of("key", "value")) .configure(HttpCommandEffector.JSON_PATH, "$.data") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); assertEquals(output, "[\"key\", \"value\"]"); assertEquals(server.getRequestCount(), 1); assertSent(server, "POST", "/post"); }
@Test public void testScheduledEffectorFiresImmediately() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test(expectedExceptions = PropagatedRuntimeException.class) public void testWhen404() throws InterruptedException { server.enqueue(response404()); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/get?login=myLogin")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") .configure(HttpCommandEffector.JSON_PATH, "$.args.login") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); assertEquals(output, "myLogin"); assertEquals(server.getRequestCount(), 1); assertSent(server, "GET", "/get?login=myLogin"); }
@Test public void testHappyPath() throws InterruptedException { server.enqueue(jsonResponse("login.json")); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/get?login=myLogin")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") .configure(HttpCommandEffector.JSON_PATH, "$.args.login") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); assertEquals(output, "myLogin"); assertEquals(server.getRequestCount(), 1); assertSent(server, "GET", "/get?login=myLogin"); }
@Test public void testPayloadWithContentTypeHeaderXml() throws InterruptedException { server.enqueue(jsonResponse("int-response.json")); httpCommandEffector = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, EFFECTOR_HTTP_COMMAND.getName()) .configure(HttpCommandEffector.EFFECTOR_URI, url("/post")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, 1) .configure(HttpCommandEffector.EFFECTOR_HTTP_HEADERS, ImmutableMap.of(HttpHeaders.CONTENT_TYPE, "application/xml")) .configure(HttpCommandEffector.JSON_PATH, "$.data") ); assertNotNull(httpCommandEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(httpCommandEffector)); Object output = testEntity.invoke(EFFECTOR_HTTP_COMMAND, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); assertEquals(output, "1"); assertEquals(server.getRequestCount(), 1); assertSent(server, "POST", "/post"); }
@Test(groups="Integration") public void testSuspendsAndResumes() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.WAIT, Duration.FIVE_SECONDS) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); policy.suspend(); policy.resume(); Time.sleep(Duration.seconds(5)); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test public void testPeriodicEffectorFires() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.ONE_MILLISECOND) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(PeriodicEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, PeriodicEffectorPolicy.RUNNING, true); assertCallHistoryEventually(entity, "myEffector", 2); }
@Test public void testScheduledEffectorFiresOnSensor() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryNeverContinually(entity, "myEffector"); entity.sensors().set(ScheduledEffectorPolicy.INVOKE_IMMEDIATELY, Boolean.TRUE); assertCallHistoryContainsEventually(entity, "myEffector"); } }
@Test(groups="Integration") public void testScheduledEffectorFiresAfterDelay() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.WAIT, Duration.FIVE_SECONDS) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryNeverContinually(entity, "myEffector"); Time.sleep(Duration.seconds(5)); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test(expectedExceptions = PropagatedRuntimeException.class) public void testWhenOneEffectorFails() throws InterruptedException { server.enqueue(response404()); HttpCommandEffector eff1 = new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, "eff1") .configure(HttpCommandEffector.EFFECTOR_URI, url("/get?login=myLogin")) .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") .configure(HttpCommandEffector.JSON_PATH, "$.args.login")); compositeEffector = new CompositeEffector(ConfigBag.newInstance() .configure(CompositeEffector.EFFECTOR_NAME, EFFECTOR_COMPOSITE.getName()) .configure(CompositeEffector.EFFECTORS, ImmutableList.of("eff1", "eff2")) ); assertNotNull(compositeEffector); TestEntity testEntity = app.createAndManageChild(buildEntitySpec(eff1, compositeEffector)); List<?> results = testEntity.invoke(EFFECTOR_COMPOSITE, ImmutableMap.<String, Object>of()).getUnchecked(Duration.seconds(1)); Asserts.assertEquals(results.size(), 2); }