@Test public void testWithAppPolicy() throws Exception { Entity app = createAndStartApplication(loadYaml("test-app-with-policy.yaml")); waitForApplicationTasks(app); Assert.assertEquals(app.getDisplayName(), "test-app-with-policy"); log.info("App started:"); Entities.dumpInfo(app); Assert.assertEquals(app.policies().size(), 1); Policy policy = app.policies().iterator().next(); Assert.assertTrue(policy instanceof TestPolicy); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_NAME), "Name from YAML"); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); Map<?, ?> leftoverProperties = ((TestPolicy) policy).getLeftoverProperties(); Assert.assertEquals(leftoverProperties.get("policyLiteralValue1"), "Hello"); Assert.assertEquals(leftoverProperties.get("policyLiteralValue2"), "World"); Assert.assertEquals(leftoverProperties.size(), 2); }
@Test public void testWithEntityPolicy() throws Exception { Entity app = createAndStartApplication(loadYaml("test-entity-with-policy.yaml")); waitForApplicationTasks(app); Assert.assertEquals(app.getDisplayName(), "test-entity-with-policy"); log.info("App started:"); Entities.dumpInfo(app); Assert.assertEquals(app.policies().size(), 0); Assert.assertEquals(app.getChildren().size(), 1); Entity child = app.getChildren().iterator().next(); Assert.assertEquals(child.policies().size(), 1); Policy policy = child.policies().iterator().next(); Assert.assertNotNull(policy); Assert.assertTrue(policy instanceof TestPolicy, "policy=" + policy + "; type=" + policy.getClass()); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_NAME), "Name from YAML"); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); Assert.assertEquals(((TestPolicy) policy).getLeftoverProperties(), ImmutableMap.of("policyLiteralValue1", "Hello", "policyLiteralValue2", "World")); Assert.assertEquals(policy.getConfig(TestPolicy.TEST_ATTRIBUTE_SENSOR), TestEntity.NAME); }
private Policy getPolicy(Entity entity) { Assert.assertEquals(entity.policies().size(), 1); Policy policy = entity.policies().iterator().next(); Assert.assertTrue(policy instanceof TestReferencingPolicy); return policy; }
assertV2MethodCall(v2); assertV2EffectorCall(v2); Assert.assertEquals(v2.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(v2.policies())); Assert.assertEquals(v2_evil.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(v2_evil.policies())); Assert.assertEquals(v1.policies().size(), 0, "Wrong number of policies: "+MutableList.copyOf(v1.policies()));
Assert.assertEquals(me.policies().size(), 0, "Wrong number of policies: "+MutableList.copyOf(me.policies()));
@Test public void testMoreEntitiesV1Policy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V1_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); RegisteredType cp = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION, BROOKLYN_TEST_OSGI_ENTITIES_URL); me.policies().add(getPolicySpec(cp)); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // must be the actual source bundle Assert.assertFalse(catalogItemId.equals(me.getCatalogItemId()), "catalog item id is: "+catalogItemId); Assert.assertTrue(catalogItemId.equals(CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION)), "catalog item id is: "+catalogItemId); }
@Test public void testMoreEntitiesV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V2_URL, BROOKLYN_TEST_OSGI_ENTITIES_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); Assert.assertEquals(me.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION)); assertV2MethodCall(me); assertV2EffectorCall(me); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // allow either me's bundle (more) or the actual source bundle Assert.assertTrue(catalogItemId.equals(me.getCatalogItemId()) || catalogItemId.startsWith("brooklyn-test-osgi-entities")); }
@Test public void testMoreEntityV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("more-entity-v2-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); OsgiVersionMoreEntityTest.assertV2EffectorCall(moreEntity); OsgiVersionMoreEntityTest.assertV2MethodCall(moreEntity); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded from the java so should have the base more-entity catalog id Assert.assertEquals(policy.getCatalogItemId(), "more-entity:1.0"); }
/** TODO we get warnings from {@link BrooklynEntityMatcher#extractValidConfigFlagsOrKeys}; * if we passed the correct loader at that point we could avoid those warnings. */ @Test public void testMoreEntityV1WithPolicy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("simple-policy-osgi-catalog.yaml")); addCatalogItems(getLocalResource("more-entity-v1-with-policy-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded by yaml w ref to catalog, so should have the simple-policy catalog-id Assert.assertEquals(policy.getCatalogItemId(), "simple-policy:1.0"); }
@Test public void testMoreEntitiesV1ThenV2GivesV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V1_URL); addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V2_URL, BROOKLYN_TEST_OSGI_ENTITIES_URL); // test load and instantiate Entity me = addItemFromCatalog( mgmt.getTypeRegistry().get(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION) ); assertV2MethodCall(me); assertV2EffectorCall(me); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); }
@Override public Integer get() { return child.policies().size(); } }, Predicates.<Integer> equalTo(1));