Java Code Examples for org.apache.brooklyn.util.text.Strings#lines()

The following examples show how to use org.apache.brooklyn.util.text.Strings#lines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatalogYamlRebindTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteEmptyBundleRemovedFromPersistence() throws Exception {
    recreateOrigManagementContextWithOsgi();
    
    String bom = Strings.lines(
            "brooklyn.catalog:",
            "  itemType: entity",
            "  items:",
            "  - id: sample",
            "    item:",
            "      type: " + BasicEntity.class.getName());
    addCatalogItems(bom);
    addCatalogItems(bom);
    rebind();
    // should only contain one bundle / bundle.jar pair
    Asserts.assertSize(Arrays.asList( new File(mementoDir, "bundles").list() ), 2);
}
 
Example 2
Source File: CatalogMakeOsgiBundleTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testCatalogBomLoadsFileInBundle() throws Exception {
    bm.setDefaultClassForLoading(getClass());
    File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1");
    
    // add a file in the bundle
    String customText = "Sample data "+Identifiers.makeRandomId(4);
    jf = bm.copyAdding(jf, MutableMap.of(
            new ZipEntry("sample.txt"), (InputStream) new ByteArrayInputStream(customText.getBytes())));
    
    installBundle(jf);

    String yaml = Strings.lines("name: simple-app-yaml",
            "services:",
            "- type: " + "basic1",
            "  brooklyn.initializers:",
            "  - type: "+GetFileContentsEffector.class.getName());
    Entity app = createAndStartApplication(yaml);
    Entity basic1 = Iterables.getOnlyElement( app.getChildren() );
    
    // check the file put in the bundle gets loaded without needing to do anything special
    String contents = basic1.invoke(GetFileContentsEffector.GET_FILE_CONTENTS, MutableMap.of(GetFileContentsEffector.FILENAME.getName(), "classpath://sample.txt")).get();
    Asserts.assertEquals(contents, customText);
}
 
Example 3
Source File: Yamls.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** Returns the original YAML with the found item replaced by the given replacement YAML.
 * @param replacement YAML to put in for the found item;
 * this YAML typically should not have any special indentation -- if required when replacing it will be inserted.
 * <p>
 * if replacing an inline map entry, the supplied entry must follow the structure being replaced;
 * for example, if replacing the value in <code>key: value</code> with a map,
 * supplying a replacement <code>subkey: value</code> would result in invalid yaml;
 * the replacement must be supplied with a newline, either before the subkey or after.
 * (if unsure we believe it is always valid to include an initial newline or comment with newline.)
 */
public String getFullYamlTextWithExtractReplaced(String replacement) {
    if (!found()) throw new IllegalStateException("Cannot perform replacement when item was not matched.");
    String result = yaml.substring(0, getStartOfThis());
    
    String[] newLines = replacement.split("\n");
    for (int i=1; i<newLines.length; i++)
        newLines[i] = Strings.makePaddedString("", getStartColumnOfThis(), "", " ") + newLines[i];
    result += Strings.lines(newLines);
    if (replacement.endsWith("\n")) result += "\n";
    
    int end = getEndOfThis();
    result += yaml.substring(end);
    
    return result;
}
 
Example 4
Source File: CatalogScanOsgiTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
static String bomForLegacySiblingLibraries() {
    return Strings.lines("brooklyn.catalog:",
        "    bundle: test-items",
        "    version: 2.0-test_java",
        "    items:",
        "    - scanJavaAnnotations: true",
        "      item:",
        "        id: here-item",
        "        type: "+OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY,
        "      libraries:",
        "      - classpath://" + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH,
        "      - classpath://" + OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH);
}
 
Example 5
Source File: CatalogScanOsgiTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
static String bomAnonymous() {
    return Strings.lines("brooklyn.catalog:",
        "    items:",
        "    - item:",
        "        id: sample",
        "        type: "+BasicEntity.class.getName());
}
 
Example 6
Source File: CatalogOsgiVersionMoreEntityRebindTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public void testClassAccessAfterUpgrade() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH);
    
    // install dependency
    ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( 
        new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_OSGI_ENTITIES_URL) ).checkNoError();

    // now the v2 bundle
    OsgiBundleInstallationResult b2a = ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( 
        new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_URL) ).get();

    Assert.assertEquals(b2a.getVersionedName().toString(), BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL+":"+"0.2.0");
    Assert.assertEquals(b2a.getCode(), OsgiBundleInstallationResult.ResultCode.INSTALLED_NEW_BUNDLE);
    
    String yaml = Strings.lines("name: simple-app-yaml",
            "services:",
            "- type: " + BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
    Entity app = createAndStartApplication(yaml);
    Entity more = Iterables.getOnlyElement( app.getChildren() );
    
    Assert.assertEquals(
        more.invoke(Effectors.effector(String.class, "sayHI").buildAbstract(), MutableMap.of("name", "Bob")).get(),
        "HI BOB FROM V2");
    
    // unforced upgrade should report already installed
    ReferenceWithError<OsgiBundleInstallationResult> installEvilTwin = ((ManagementContextInternal)mgmt()).getOsgiManager().get().install(
        new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_EVIL_TWIN_URL) );
    Assert.assertTrue(installEvilTwin.hasError());
    Assert.assertEquals(installEvilTwin.getWithoutError().getCode(),
        OsgiBundleInstallationResult.ResultCode.ERROR_PREPARING_BUNDLE);
    
    // force upgrade
    OsgiBundleInstallationResult b2b = ((ManagementContextInternal)mgmt()).getOsgiManager().get().install(b2a.getMetadata(), 
        new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_EVIL_TWIN_URL), true, true, true).get();
    Assert.assertEquals(b2a.getBundle(), b2b.getBundle());
    Assert.assertEquals(b2b.getCode(), OsgiBundleInstallationResult.ResultCode.UPDATED_EXISTING_BUNDLE);

    // calls to things previously instantiated get the old behaviour
    Assert.assertEquals(
        more.invoke(Effectors.effector(String.class, "sayHI").buildAbstract(), MutableMap.of("name", "Claudia")).get(),
        "HI CLAUDIA FROM V2");
    
    // but new deployment gets the new behaviour 
    StartableApplication app2 = (StartableApplication) createAndStartApplication(yaml);
    Entity more2 = Iterables.getOnlyElement( app2.getChildren() );
    Assert.assertEquals(
        more2.invoke(Effectors.effector(String.class, "sayHI").buildAbstract(), MutableMap.of("name", "Daphne")).get(),
        "HO DAPHNE FROM V2 EVIL TWIN");
    app2.stop();
    
    // and after rebind on the old we get new behaviour
    StartableApplication app1 = rebind();
    Entity more1 = Iterables.getOnlyElement( app1.getChildren() );
    Assert.assertEquals(
        more1.invoke(Effectors.effector(String.class, "sayHI").buildAbstract(), MutableMap.of("name", "Eric")).get(),
        "HO ERIC FROM V2 EVIL TWIN");
}
 
Example 7
Source File: BrooklynLauncherRebindCatalogOsgiTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testRebindUpgradeReferencedEntityFromCatalogAndDeployment() throws Exception {
    File initialBomFileV2 = prepForRebindRemovedItemTestReturningBomV2(
        CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType(), true);
    VersionedName bundleName = new VersionedName("referencer", "1.0");
    String bundleBom = Strings.lines("brooklyn.catalog:",
        "  version: 1",  // without this the type gets the osgi version, 1.0.0 (not 1.0)
        "  items:",
        "  - id: references-simple-entity",
        "    item:",
        "      type: simple-entity:1.0",
        "      brooklyn.children:",
        "      - type: simple-entity:1.0");
    File zipIn = newTmpBundle(ImmutableMap.of(BasicBrooklynCatalog.CATALOG_BOM, bundleBom.getBytes(StandardCharsets.UTF_8)), bundleName);
    ((ManagementContextInternal)launcherLast.getManagementContext()).getOsgiManager().get().install(new FileInputStream(zipIn)).checkNoError();
    
    startupAssertions = () -> {
        String v = launcherT2==null ? "1.0.0" : "2.0.0";
        VersionedName bv = new VersionedName("org.example.testRebindGetsInitialOsgiCatalog", v);
        VersionedName iv = new VersionedName("simple-entity", v);
        VersionedName rv = new VersionedName("references-simple-entity", "1");
        assertManagedBundle(launcherLast, bv, MutableSet.of(iv));
        if (launcherT2==null || !CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType()) {
            assertCatalogConsistsOfIds(launcherLast, MutableList.copyOf(COM_EXAMPLE_BUNDLE_CATALOG_IDS).append(iv).append(rv));
        } else {
            assertCatalogConsistsOfIds(launcherLast, MutableList.of(iv, rv));
        }
    };
    startupAssertions.run();
    
    createAndStartApplication(launcherLast.getManagementContext(), 
        "services: [ { type: references-simple-entity } ]");
    

    startT2(newLauncherForTests(initialBomFileV2.getAbsolutePath()));
    promoteT2IfStandby();
    
    Entity entity = Iterables.getOnlyElement( Iterables.getOnlyElement(launcherLast.getManagementContext().getApplications()).getChildren() );
    
    Assert.assertEquals(entity.getCatalogItemId(), "references-simple-entity:1");
    // assert it was updated
    if (CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType()) {
        Assert.assertEquals(Entities.deproxy(entity).getClass().getName(), BasicEntityImpl.class.getName());
    } else {
        Assert.assertEquals(Entities.deproxy(entity).getClass().getName(), "com.example.brooklyn.test.osgi.entities.SimpleEntityImpl");
    }
    Entity child = Iterables.getOnlyElement( entity.getChildren() );
    Assert.assertEquals(child.getCatalogItemId(), "simple-entity:2.0.0");
    if (CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType()) {
        Assert.assertEquals(Entities.deproxy(child).getClass().getName(), BasicEntityImpl.class.getName());
    } else {
        Assert.assertEquals(Entities.deproxy(child).getClass().getName(), "com.example.brooklyn.test.osgi.entities.SimpleEntityImpl");
    }

    // new deployment makes the correct java item
    Application app2 = createAndStartApplication(launcherLast.getManagementContext(), 
        "services: [ { type: references-simple-entity } ]");
    entity = Iterables.getOnlyElement( app2.getChildren() );
    Assert.assertEquals(entity.getCatalogItemId(), "references-simple-entity:1");
    Assert.assertEquals(Entities.deproxy(entity).getClass().getName(), BasicEntityImpl.class.getName());
    child = Iterables.getOnlyElement( entity.getChildren() );
    Assert.assertEquals(child.getCatalogItemId(), "simple-entity:2.0.0");
    Assert.assertEquals(Entities.deproxy(child).getClass().getName(), BasicEntityImpl.class.getName());
}
 
Example 8
Source File: RegisteredTypesTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private String createErrorMessage(String issue, BasicRegisteredType planA, BasicRegisteredType planB, String messageOnError) {
    return Strings.lines(issue + messageOnError, planA.toString(), planB.toString());
}