Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.api.mgmt.Task;
import org.apache.brooklyn.api.typereg.RegisteredType;
import org.apache.brooklyn.camp.brooklyn.spi.creation.CampTypePlanTransformer;
import org.apache.brooklyn.core.catalog.internal.CatalogUtils;
import org.apache.brooklyn.core.entity.Entities;
Expand All @@ -38,6 +39,7 @@
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests.Builder;
import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
import org.apache.brooklyn.core.typereg.RegisteredTypePredicates;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.ResourceUtils;
import org.apache.brooklyn.util.net.Urls;
Expand All @@ -48,6 +50,8 @@
import org.testng.annotations.BeforeMethod;

import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

public abstract class AbstractYamlTest {

Expand Down Expand Up @@ -228,6 +232,22 @@ protected String ver(String id) {
return CatalogUtils.getVersionedId(id, TEST_VERSION);
}

protected String ver(String id, String version) {
return CatalogUtils.getVersionedId(id, version);
}

protected int countCatalogLocations() {
return countCatalogItemsMatching(RegisteredTypePredicates.IS_LOCATION);
}

protected int countCatalogPolicies() {
return countCatalogItemsMatching(RegisteredTypePredicates.IS_POLICY);
}

protected int countCatalogItemsMatching(Predicate<? super RegisteredType> filter) {
return Iterables.size(mgmt().getTypeRegistry().getMatching(filter));
}

public void forceCatalogUpdate() {
forceUpdate = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.brooklyn.camp.brooklyn;

import java.util.Collection;

import org.apache.brooklyn.api.catalog.BrooklynCatalog;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest;
import org.apache.brooklyn.entity.stock.BasicEntity;
import org.apache.brooklyn.test.support.TestResourceUnavailableException;
import org.apache.brooklyn.util.osgi.OsgiTestResources;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.google.common.collect.Iterables;

public class ReferencedOsgiYamlTest extends AbstractYamlTest {

@Override
protected boolean disableOsgi() {
return false;
}

@Test
public void testCatalogReferencingYamlUrlFromOsgiBundle() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" id: yaml.reference",
" version: " + TEST_VERSION,
" itemType: entity",
" libraries:",
" - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" item:",
" type: classpath://yaml-ref-osgi-entity.yaml");

String entityName = "YAML -> catalog item -> yaml url (osgi)";
Entity app = createAndStartApplication(
"services:",
"- name: " + entityName,
" type: " + ver("yaml.reference"));

checkChildEntitySpec(app, entityName);
}

/**
* Tests that a YAML referenced by URL from a catalog item
* will have access to the catalog item's bundles.
*/
@Test
public void testCatalogLeaksBundlesToReferencedYaml() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

String parentCatalogId = "my.catalog.app.id.url.parent";
addCatalogItems(
"brooklyn.catalog:",
" id: " + parentCatalogId,
" version: " + TEST_VERSION,
" itemType: entity",
" libraries:",
" - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" item:",
" type: classpath://yaml-ref-bundle-without-libraries.yaml");

Entity app = createAndStartApplication(
"services:",
"- type: " + ver(parentCatalogId));

Collection<Entity> children = app.getChildren();
Assert.assertEquals(children.size(), 1);
Entity child = Iterables.getOnlyElement(children);
Assert.assertEquals(child.getEntityType().getName(), "org.apache.brooklyn.test.osgi.entities.SimpleEntity");

deleteCatalogEntity(parentCatalogId);
}

@Test
public void testCatalogReference() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" brooklyn.libraries:",
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" items:",
" - classpath://yaml-ref-parent-catalog.bom");

assertCatalogReference();
}

@Test
public void testCatalogReferenceByExplicitUrl() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" brooklyn.libraries:",
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" items:",
" - include: classpath://yaml-ref-parent-catalog.bom");

assertCatalogReference();
}

@Test
public void testCatalogReferenceByMultipleUrls() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" items:",
" - include: classpath://yaml-ref-simple.bom",
" - include: classpath://yaml-ref-more.bom"
);

assertCatalogReference();
}

@Test
public void testCatalogReferenceByMultipleUrlsSimplerSyntax() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" items:",
" - classpath://yaml-ref-simple.bom",
" - classpath://yaml-ref-more.bom"
);

assertCatalogReference();
}


@Test
public void testCatalogReferenceSeesPreviousItems() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" brooklyn.libraries:",
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" items:",
" - id: yaml.nested.catalog.simple",
" itemType: entity",
" item:",
" type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY,
" - include: classpath://yaml-ref-back-catalog.bom");

String entityNameSimple = "YAML -> catalog -> catalog (osgi)";
Entity app = createAndStartApplication(
"services:",
"- name: " + entityNameSimple,
" type: back-reference");

Collection<Entity> children = app.getChildren();
Assert.assertEquals(children.size(), 1);
Entity childSimple = Iterables.getOnlyElement(children);
Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);
}

@Test
public void testCatalogReferenceMixesMetaAndUrl() throws Exception {
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

addCatalogItems(
"brooklyn.catalog:",
" brooklyn.libraries:",
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
" items:",
" - include: classpath://yaml-ref-parent-catalog.bom",
" items:",
" - id: yaml.nested.catalog.nested",
" itemType: entity",
" item:",
" type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);

BrooklynCatalog catalog = mgmt().getCatalog();
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.nested", BrooklynCatalog.DEFAULT_VERSION));
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.simple", BrooklynCatalog.DEFAULT_VERSION));
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.more", BrooklynCatalog.DEFAULT_VERSION));
}

protected void assertCatalogReference() throws Exception {
String entityNameSimple = "YAML -> catalog -> catalog simple (osgi)";
String entityNameMore = "YAML -> catalog -> catalog more (osgi)";
Entity app = createAndStartApplication(
"services:",
"- name: " + entityNameSimple,
" type: yaml.nested.catalog.simple",
"- name: " + entityNameMore,
" type: yaml.nested.catalog.more");

Collection<Entity> children = app.getChildren();
Assert.assertEquals(children.size(), 2);
Entity childSimple = Iterables.get(children, 0);
Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);

Entity childMore = Iterables.get(children, 1);
Assert.assertEquals(childMore.getDisplayName(), entityNameMore);
Assert.assertEquals(childMore.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
}

private void checkChildEntitySpec(Entity app, String entityName) {
Collection<Entity> children = app.getChildren();
Assert.assertEquals(children.size(), 1);
Entity child = Iterables.getOnlyElement(children);
Assert.assertEquals(child.getDisplayName(), entityName);
Assert.assertEquals(child.getEntityType().getName(), BasicEntity.class.getName());
}
}
Loading