public static Properties getProperties(String resourceFile, boolean isResource) { Properties props = new Properties(); String propertiesString = getResourceAsString(resourceFile, isResource); try { props.load(new StringReader(propertiesString)); } catch (IOException e) { throw new RuntimeException("Unable to load resources from string", e); } return props; }
Properties properties = FileUtil.getProperties(securityProperties, false);
public static String getResourceAsString(String resourceFile, boolean isResource) { try (InputStream is = getInputStream(resourceFile, isResource); BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) { return buffer.lines().collect(Collectors.joining(System.lineSeparator())); } catch (IOException e) { throw new RuntimeException("Unable to load resources from disk", e); } }
@Test public void testSwitchToUnixLineEnds() { String expectedOutput = "test\n"; String actualOutput = switchToUnixLineEnds("test\r\n"); assertEquals(expectedOutput, actualOutput); actualOutput = switchToUnixLineEnds(null); assertNull(actualOutput); } }
private void startExternalContainerImageIconResourceServices(String resourcesPath) { Path iconResourcePath = Paths.get(resourcesPath, ManagementUriParts.CONTAINER_ICONS_RESOURCE_PATH); if (!iconResourcePath.toFile().exists()) { logInfo("Skip loading container icons from user resource path"); return; } else { logInfo("Loading container icons from user resource path"); } List<File> files = FileUtils.findFiles(iconResourcePath, new HashSet<>(), false); for (File f : files) { String subPath = f.getAbsolutePath().replace( iconResourcePath.toAbsolutePath().toString(), ""); Path servicePath = Paths .get(ManagementUriParts.CONTAINER_ICONS_RESOURCE_PATH.substring(1), subPath); String servicePathString = FileUtil.getForwardSlashesPathString(servicePath); Operation post = Operation .createPost(UriUtils.buildUri(getHost(), servicePathString)); RestrictiveFileContentService fcs = new RestrictiveFileContentService(f); getHost().startService(post, fcs); } } }
@SuppressWarnings("unchecked") public static List<FavoriteImage> buildDefaultFavoriteImages(ServiceHost host) { List<FavoriteImage> images = new ArrayList<>(); try { List<?> jsonImages = Utils.fromJson(FileUtil.getClasspathResourceAsString(POPULAR_IMAGES_FILE), List.class); host.log(Level.INFO, "Default favorite images loaded."); Function<FavoriteImage, String> createSelfLink = state -> { return UriUtils.buildUriPath(FavoriteImagesService.FACTORY_LINK, new StringBuilder().append(state.registry.replaceFirst("https?://", "") .replaceAll("\\.", "-")) .append('-') .append(state.name.replaceAll("/", "-") .replaceAll("\\.", "-")) .toString()); }; jsonImages.forEach(i -> { Map<String, String> imgObj = (Map<String, String>) i; FavoriteImage state = new FavoriteImage(); state.name = imgObj.get(FavoriteImage.FIELD_NAME_NAME); state.description = imgObj.get(FavoriteImage.FIELD_NAME_DESCRIPTION); state.registry = imgObj.get(FavoriteImage.FIELD_NAME_REGISTRY); state.documentSelfLink = createSelfLink.apply(state); images.add(state); }); } catch (NullPointerException | JsonSyntaxException e) { host.log(Level.WARNING, "Unable to load default favorite images. " + "Either the file is missing or it is malformed"); } return images; }
String expectedMySqlService = getContent("kubernetes.mysql.service.yaml"); assertEquals(switchToUnixLineEnds(expectedMysqlDeployment).trim(), mysqlDeploymentSerialized); assertEquals(switchToUnixLineEnds(expectedWordpressService).trim(), wordpressServiceSerialized); assertEquals(switchToUnixLineEnds(expectedWordpressDeployment).trim(), wordpressDeploymentSerialized); assertEquals(switchToUnixLineEnds(expectedMySqlService).trim(), mysqlServiceSerialized); assertEquals(switchToUnixLineEnds(builder.toString()).trim(), kubernetesTemplateSerialized);
@Test public void testGetForwardSlashesPathString() { Path path = null; String pathString = FileUtil.getForwardSlashesPathString(path); assertNull(pathString); path = Paths.get(TEST_BACK_SLASHES_PATH_STRING); pathString = FileUtil.getForwardSlashesPathString(path); assertEquals(TEST_FORWARD_SLASHES_PATH_STRING, pathString); }
private void getDefaultPopularImages(Operation get) { get.setBody(FileUtil.getClasspathResourceAsString(POPULAR_IMAGES_FILE)); get.setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON); get.complete(); }
private static String getContent(String filename) { return FileUtil.getResourceAsString("/compose/" + filename, true); }
@Override public boolean isReady() { try { SimpleEntry<Integer, String> response = doGet(uri, headers); assertEquals(HttpURLConnection.HTTP_OK, (int) response.getKey()); String body = switchToUnixLineEnds(response.getValue()); for (ManagementHost host2 : hosts) { assertTrue("Host " + host2.getUri() + " should be present!", body.contains("\"groupReference\": \"" + host2.getUri() + "/core/node-groups/default\",\n \"status\": \"AVAILABLE\"")); } } catch (Throwable e) { host.log(Level.WARNING, "Request to [%s] failed with: [%s].", NODE_GROUPS, e.getMessage()); return false; } return true; }
Properties customProps = FileUtil.getProperties(fileName, false); @SuppressWarnings("unchecked") Enumeration<String> customEnums = (Enumeration<String>) customProps
private static String getContent(String filename) { return FileUtil.getResourceAsString("/compose/" + filename, true); }
@Test public void testGetProperties() { Properties props = FileUtil.getProperties(TEST_RESOURCE_PROPERTIES_FILE, true); assertNotNull(props); assertTrue(props.containsKey(BUILD_NUMBER_PROPERTY_KEY)); assertTrue(props.get(BUILD_NUMBER_PROPERTY_KEY).equals("1")); }
public static String getContent(String filename) { return FileUtil.getResourceAsString("/compose/" + filename, true); }
private SslTrustCertificateState createSslTrustCert() throws Throwable { if (this.sslTrustCert != null) { return this.sslTrustCert; } String sslTrustPem = FileUtil.getResourceAsString("/certs/ca.pem", true).trim(); SslTrustCertificateState sslTrustCert = new SslTrustCertificateState(); sslTrustCert.certificate = sslTrustPem; return createEntity(sslTrustCert, SslTrustCertificateService.FACTORY_LINK); }
private void startExternalPopularImagesService(String resourcesPath) { Path filePath = Paths.get(resourcesPath, POPULAR_IMAGES_FILE); if (!filePath.toFile().exists()) { logInfo("Skip loading popular images from user resource path."); return; } else { logInfo("Loading popular images from user resource path..."); } File file = filePath.toFile(); try { Utils.fromJson(FileUtil.getResourceAsString(file.getAbsolutePath(), false), Collection.class); } catch (Exception e) { logWarning("Error validating popular images file content: %s", e.getMessage()); return; } Operation post = Operation.createPost(UriUtils.buildUri(getHost(), EXTERNAL_LINK)); FileContentService fcs = new FileContentService(file); getHost().startService(post, fcs); }
@Test public void testGetResourceAsString() { String resourceString = FileUtil.getResourceAsString(TEST_RESOURCE_PROPERTIES_FILE, true); assertNotNull(resourceString); assertTrue(resourceString.contains(BUILD_NUMBER_PROPERTY_KEY)); }