/** convenience for joining lines together */ public static String lines(String ...lines) { if (lines==null) return null; return lines(Arrays.asList(lines)); }
public static String removeLines(String multiline, Predicate<CharSequence> patternToRemove) { if (multiline==null) return null; return lines(Iterables.filter(Arrays.asList(multiline.split("\n")), Predicates.not(patternToRemove))); }
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); }
static String bomAnonymous() { return Strings.lines("brooklyn.catalog:", " items:", " - item:", " id: sample", " type: "+BasicEntity.class.getName()); }
public void testLines() { Assert.assertEquals(Strings.lines("a", "b"), "a\nb"); }
/** 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; }
public void testRemoveLines() { Assert.assertEquals(Strings.removeLines(Strings.lines("a", "b"), StringPredicates.containsLiteralIgnoreCase("A")), "b"); }
@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); }
static void installJavaScanningMoreEntitiesV2(ManagementContext mgmt, Object context) throws FileNotFoundException { // scanning bundle functionality added in 0.12.0, relatively new compared to non-osgi scanning TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); CampYamlLiteTest.installWithoutCatalogBom(mgmt, OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); BundleMaker bm = new BundleMaker(mgmt); File f = Os.newTempFile(context.getClass(), "jar"); Streams.copy(ResourceUtils.create(context).getResourceFromUrl(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH), new FileOutputStream(f)); f = bm.copyRemoving(f, MutableSet.of("catalog.bom")); f = bm.copyAdding(f, MutableMap.of(new ZipEntry("catalog.bom"), new ByteArrayInputStream( Strings.lines( "brooklyn.catalog:", " scanJavaAnnotations: true").getBytes() ) )); ((ManagementContextInternal)mgmt).getOsgiManager().get().install(new FileInputStream(f)).checkNoError(); }
@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); }
@Test(groups={"Integration", "Broken"}) public void testAuthenticationAndHttps() throws Exception { String adminPassword = "p4ssw0rd"; BrooklynNode brooklynNode = app.createAndManageChild(newBrooklynNodeSpecForTest() .configure(BrooklynNode.ENABLED_HTTP_PROTOCOLS, ImmutableList.of("https")) .configure(BrooklynNode.MANAGEMENT_PASSWORD, adminPassword) .configure(BrooklynNode.BROOKLYN_LOCAL_PROPERTIES_CONTENTS, Strings.lines( "brooklyn.webconsole.security.https.required=true", "brooklyn.webconsole.security.users=admin", "brooklyn.webconsole.security.user.admin.password="+adminPassword, "brooklyn.location.localhost.enabled=false") ) ); app.start(locs); log.info("started "+app+" containing "+brooklynNode+" for "+JavaClassNames.niceClassAndMethod()); URI webConsoleUri = brooklynNode.getAttribute(BrooklynNode.WEB_CONSOLE_URI); Assert.assertTrue(webConsoleUri.toString().startsWith("https://"), "web console not https: "+webConsoleUri); Integer httpsPort = brooklynNode.getAttribute(BrooklynNode.HTTPS_PORT); Assert.assertTrue(httpsPort!=null && httpsPort >= 8443 && httpsPort <= 8500); Assert.assertTrue(webConsoleUri.toString().contains(""+httpsPort), "web console not using right https port ("+httpsPort+"): "+webConsoleUri); HttpTestUtils.assertHttpStatusCodeEquals(webConsoleUri.toString(), 401); HttpClient http = HttpTool.httpClientBuilder() .trustAll() .uri(webConsoleUri) .laxRedirect(true) .credentials(new UsernamePasswordCredentials("admin", adminPassword)) .build(); HttpToolResponse response = HttpTool.httpGet(http, webConsoleUri, MutableMap.<String,String>of()); Assert.assertEquals(response.getResponseCode(), 200); }
String yaml = Strings.lines("name: simple-app-yaml", "services:", "- type: " + BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
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);