|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.brooklyn.camp.brooklyn; |
| 20 | + |
| 21 | +import java.util.Collection; |
| 22 | + |
| 23 | +import org.apache.brooklyn.api.catalog.BrooklynCatalog; |
| 24 | +import org.apache.brooklyn.api.entity.Entity; |
| 25 | +import org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest; |
| 26 | +import org.apache.brooklyn.entity.stock.BasicEntity; |
| 27 | +import org.apache.brooklyn.test.support.TestResourceUnavailableException; |
| 28 | +import org.apache.brooklyn.util.osgi.OsgiTestResources; |
| 29 | +import org.testng.Assert; |
| 30 | +import org.testng.annotations.Test; |
| 31 | + |
| 32 | +import com.google.common.collect.Iterables; |
| 33 | + |
| 34 | +public class ReferencedOsgiYamlTest extends AbstractYamlTest { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected boolean disableOsgi() { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testCatalogReferencingYamlUrlFromOsgiBundle() throws Exception { |
| 43 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 44 | + |
| 45 | + addCatalogItems( |
| 46 | + "brooklyn.catalog:", |
| 47 | + " id: yaml.reference", |
| 48 | + " version: " + TEST_VERSION, |
| 49 | + " itemType: entity", |
| 50 | + " libraries:", |
| 51 | + " - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 52 | + " item:", |
| 53 | + " type: classpath://yaml-ref-osgi-entity.yaml"); |
| 54 | + |
| 55 | + String entityName = "YAML -> catalog item -> yaml url (osgi)"; |
| 56 | + Entity app = createAndStartApplication( |
| 57 | + "services:", |
| 58 | + "- name: " + entityName, |
| 59 | + " type: " + ver("yaml.reference")); |
| 60 | + |
| 61 | + checkChildEntitySpec(app, entityName); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Tests that a YAML referenced by URL from a catalog item |
| 66 | + * will have access to the catalog item's bundles. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void testCatalogLeaksBundlesToReferencedYaml() throws Exception { |
| 70 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 71 | + |
| 72 | + String parentCatalogId = "my.catalog.app.id.url.parent"; |
| 73 | + addCatalogItems( |
| 74 | + "brooklyn.catalog:", |
| 75 | + " id: " + parentCatalogId, |
| 76 | + " version: " + TEST_VERSION, |
| 77 | + " itemType: entity", |
| 78 | + " libraries:", |
| 79 | + " - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 80 | + " item:", |
| 81 | + " type: classpath://yaml-ref-bundle-without-libraries.yaml"); |
| 82 | + |
| 83 | + Entity app = createAndStartApplication( |
| 84 | + "services:", |
| 85 | + "- type: " + ver(parentCatalogId)); |
| 86 | + |
| 87 | + Collection<Entity> children = app.getChildren(); |
| 88 | + Assert.assertEquals(children.size(), 1); |
| 89 | + Entity child = Iterables.getOnlyElement(children); |
| 90 | + Assert.assertEquals(child.getEntityType().getName(), "org.apache.brooklyn.test.osgi.entities.SimpleEntity"); |
| 91 | + |
| 92 | + deleteCatalogEntity(parentCatalogId); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testCatalogReference() throws Exception { |
| 97 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 98 | + |
| 99 | + addCatalogItems( |
| 100 | + "brooklyn.catalog:", |
| 101 | + " brooklyn.libraries:", |
| 102 | + " - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 103 | + " items:", |
| 104 | + " - classpath://yaml-ref-parent-catalog.bom"); |
| 105 | + |
| 106 | + assertCatalogReference(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testCatalogReferenceByExplicitUrl() throws Exception { |
| 111 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 112 | + |
| 113 | + addCatalogItems( |
| 114 | + "brooklyn.catalog:", |
| 115 | + " brooklyn.libraries:", |
| 116 | + " - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 117 | + " items:", |
| 118 | + " - include: classpath://yaml-ref-parent-catalog.bom"); |
| 119 | + |
| 120 | + assertCatalogReference(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testCatalogReferenceByMultipleUrls() throws Exception { |
| 125 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 126 | + |
| 127 | + addCatalogItems( |
| 128 | + "brooklyn.catalog:", |
| 129 | + " items:", |
| 130 | + " - include: classpath://yaml-ref-simple.bom", |
| 131 | + " - include: classpath://yaml-ref-more.bom" |
| 132 | + ); |
| 133 | + |
| 134 | + assertCatalogReference(); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testCatalogReferenceByMultipleUrlsSimplerSyntax() throws Exception { |
| 139 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 140 | + |
| 141 | + addCatalogItems( |
| 142 | + "brooklyn.catalog:", |
| 143 | + " items:", |
| 144 | + " - classpath://yaml-ref-simple.bom", |
| 145 | + " - classpath://yaml-ref-more.bom" |
| 146 | + ); |
| 147 | + |
| 148 | + assertCatalogReference(); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testCatalogReferenceSeesPreviousItems() throws Exception { |
| 154 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 155 | + |
| 156 | + addCatalogItems( |
| 157 | + "brooklyn.catalog:", |
| 158 | + " brooklyn.libraries:", |
| 159 | + " - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 160 | + " items:", |
| 161 | + " - id: yaml.nested.catalog.simple", |
| 162 | + " itemType: entity", |
| 163 | + " item:", |
| 164 | + " type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY, |
| 165 | + " - include: classpath://yaml-ref-back-catalog.bom"); |
| 166 | + |
| 167 | + String entityNameSimple = "YAML -> catalog -> catalog (osgi)"; |
| 168 | + Entity app = createAndStartApplication( |
| 169 | + "services:", |
| 170 | + "- name: " + entityNameSimple, |
| 171 | + " type: back-reference"); |
| 172 | + |
| 173 | + Collection<Entity> children = app.getChildren(); |
| 174 | + Assert.assertEquals(children.size(), 1); |
| 175 | + Entity childSimple = Iterables.getOnlyElement(children); |
| 176 | + Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple); |
| 177 | + Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testCatalogReferenceMixesMetaAndUrl() throws Exception { |
| 182 | + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); |
| 183 | + |
| 184 | + addCatalogItems( |
| 185 | + "brooklyn.catalog:", |
| 186 | + " brooklyn.libraries:", |
| 187 | + " - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, |
| 188 | + " items:", |
| 189 | + " - include: classpath://yaml-ref-parent-catalog.bom", |
| 190 | + " items:", |
| 191 | + " - id: yaml.nested.catalog.nested", |
| 192 | + " itemType: entity", |
| 193 | + " item:", |
| 194 | + " type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY); |
| 195 | + |
| 196 | + BrooklynCatalog catalog = mgmt().getCatalog(); |
| 197 | + Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.nested", BrooklynCatalog.DEFAULT_VERSION)); |
| 198 | + Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.simple", BrooklynCatalog.DEFAULT_VERSION)); |
| 199 | + Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.more", BrooklynCatalog.DEFAULT_VERSION)); |
| 200 | + } |
| 201 | + |
| 202 | + protected void assertCatalogReference() throws Exception { |
| 203 | + String entityNameSimple = "YAML -> catalog -> catalog simple (osgi)"; |
| 204 | + String entityNameMore = "YAML -> catalog -> catalog more (osgi)"; |
| 205 | + Entity app = createAndStartApplication( |
| 206 | + "services:", |
| 207 | + "- name: " + entityNameSimple, |
| 208 | + " type: yaml.nested.catalog.simple", |
| 209 | + "- name: " + entityNameMore, |
| 210 | + " type: yaml.nested.catalog.more"); |
| 211 | + |
| 212 | + Collection<Entity> children = app.getChildren(); |
| 213 | + Assert.assertEquals(children.size(), 2); |
| 214 | + Entity childSimple = Iterables.get(children, 0); |
| 215 | + Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple); |
| 216 | + Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY); |
| 217 | + |
| 218 | + Entity childMore = Iterables.get(children, 1); |
| 219 | + Assert.assertEquals(childMore.getDisplayName(), entityNameMore); |
| 220 | + Assert.assertEquals(childMore.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY); |
| 221 | + } |
| 222 | + |
| 223 | + private void checkChildEntitySpec(Entity app, String entityName) { |
| 224 | + Collection<Entity> children = app.getChildren(); |
| 225 | + Assert.assertEquals(children.size(), 1); |
| 226 | + Entity child = Iterables.getOnlyElement(children); |
| 227 | + Assert.assertEquals(child.getDisplayName(), entityName); |
| 228 | + Assert.assertEquals(child.getEntityType().getName(), BasicEntity.class.getName()); |
| 229 | + } |
| 230 | +} |
0 commit comments