private static String[] convertDependsOn(Object dependsOn) { if (dependsOn == null) { return null; } return YamlMapper.objectMapper().convertValue(dependsOn, String[].class); }
private String serializeContent(String kubeConfig) throws IOException { return YamlMapper.fromJsonToYaml(kubeConfig); }
public String getKubernetesEntityAsJson() throws IOException { return YamlMapper.fromYamlToJson(kubernetesEntity); }
public static Map<String, Object> serializeNestedState(NestedState nestedState) throws IOException { return serializeNestedState(nestedState, YamlMapper.objectMapper(), YamlMapper.objectWriter()); }
boolean isMultiYaml = YamlMapper.isMultiYaml(yaml); if (isMultiYaml) { List<String> yamls = YamlMapper.splitYaml(yaml); template = YamlMapper.objectMapper().readValue(yaml, CommonDescriptionEntity.class); } catch (JsonProcessingException e) { throw new LocalizableValidationException(
@Test public void testSplitYamlWithSingleYaml() { String yamlInput = sampleYamlDefinition; List<String> expectedOutput = new ArrayList<>(); expectedOutput.add(sampleYamlDefinition); List<String> actualOutput = YamlMapper.splitYaml(yamlInput); assertEquals(1, actualOutput.size()); assertEquals(expectedOutput.get(0), actualOutput.get(0)); }
@Test public void testIsValidYaml() { String[] inputs = new String[] { sampleYamlDefinition, "invalid\ninput", "{\"person\":{\"name\":\"test-name\",\"age\":14}}" }; boolean[] expectedOutputs = new boolean[] { true, false, false }; for (int i = 0; i < inputs.length; i++) { assertEquals("Test failed on iteration: " + i, expectedOutputs[i], YamlMapper.isValidYaml(inputs[i])); } }
@Test public void testIsMultiYaml() { String[] in = new String[] { sampleYamlDefinition, sampleYamlDefinition + "\n" + sampleYamlDefinition }; boolean[] out = new boolean[] { false, true }; assertEquals(out[0], YamlMapper.isMultiYaml(in[0])); assertEquals(out[1], YamlMapper.isMultiYaml(in[1])); }
public static String serializeDockerCompose(DockerCompose entity) throws IOException { sanitizeDockerCompose(entity); return YamlMapper.objectWriter().writeValueAsString(entity).trim(); }
public static PropertyFilter createBuiltinFieldFilter() { return SimpleBeanPropertyFilter.serializeAllExcept(getBuiltinFieldNames()); }
public static List<String> splitYaml(String yaml) { assertNotNull(yaml, "yaml"); yaml = removeComments(yaml); List<String> result = new ArrayList<>(); if (!yaml.startsWith("---")) { result.add(yaml); return result; } String[] yamls = yaml.split(YAML_REGEX_VERIFIER); result = Arrays.stream(yamls) .filter(y -> !y.trim().equals("")) .collect(Collectors.toList()); for (int i = 0; i < result.size(); i++) { String tempYaml = "---\n" + result.get(i).trim(); result.set(i, tempYaml); } return result; }
@Test public void testSplitYamlWithMultipleYamls() { String yamlInput = sampleYamlDefinition + "\n" + sampleYamlDefinition; List<String> expectedOutput = new ArrayList<>(); expectedOutput.add(sampleYamlDefinition); expectedOutput.add(sampleYamlDefinition); List<String> actualOutput = YamlMapper.splitYaml(yamlInput); assertEquals(2, actualOutput.size()); assertEquals(expectedOutput.get(0), actualOutput.get(0)); assertEquals(expectedOutput.get(1), actualOutput.get(1)); }
public static Map<String, Object> serializeTemplate(CompositeTemplate template) throws IOException { return serializeTemplate(template, YamlMapper.objectMapper(), YamlMapper.objectWriter()); }
private void validateDescription(KubernetesDescription description) throws IOException { if (!isValidYaml(description.kubernetesEntity)) { throw new LocalizableValidationException("Invalid YAML input.", "compute.template.yaml.invalid"); } BaseKubernetesObject kubernetesEntity = description .getKubernetesEntity(BaseKubernetesObject.class); assertNotNullOrEmpty(kubernetesEntity.apiVersion, "apiVersion"); assertNotNullOrEmpty(kubernetesEntity.kind, "kind"); assertNotNull(kubernetesEntity.metadata, "metadata"); assertNotNullOrEmpty(kubernetesEntity.metadata.name, "metadata.name"); description.type = kubernetesEntity.kind; description.apiVersion = kubernetesEntity.apiVersion; description.name = kubernetesEntity.metadata.name; description.namespace = kubernetesEntity.metadata.namespace; }
public static String serializeCompositeTemplate(CompositeTemplate entity) throws IOException { sanitizeCompositeTemplate(entity, true); Map<String, Object> stringObjectMap = TemplateSerializationUtils.serializeTemplate(entity); if (!isNullOrEmpty(entity.bindings)) { normalizeBindings(stringObjectMap, entity); } return YamlMapper.objectWriter().writeValueAsString(stringObjectMap); }
public <T extends BaseKubernetesObject> T getKubernetesEntity(Class<T> type) throws IOException { return YamlMapper.objectMapper().readValue(kubernetesEntity, type); }
@Test public void testSplitYamlWithComments() { String yamlInput = "# comment 1 \n" + "#comment 2 \n" + sampleYamlDefinition + " # comment 3\n" + sampleYamlDefinition; List<String> expectedOutput = new ArrayList<>(); expectedOutput.add(sampleYamlDefinition); expectedOutput.add(sampleYamlDefinition); List<String> actualOutput = YamlMapper.splitYaml(yamlInput); assertEquals(2, actualOutput.size()); assertEquals(expectedOutput.get(0), actualOutput.get(0)); assertEquals(expectedOutput.get(1), actualOutput.get(1)); }
@Test public void testConvertFromYamlToJson() throws IOException { String yamlInput = "---\n" + "person:\n" + " name: test-name\n" + " age: 14\n"; String expectedJsonOutput = "{\"person\":{\"name\":\"test-name\",\"age\":14}}"; String actualJsonOutput = YamlMapper.fromYamlToJson(yamlInput); assertEquals(expectedJsonOutput, actualJsonOutput); }
@Test public void testConvertFromJsonToYaml() throws IOException { String jsonInput = "{\"person\":{\"name\":\"test-name\",\"age\":14}}"; String expectedYamlOutput = "---\n" + "person:\n" + " name: \"test-name\"\n" + " age: 14\n"; String actualYamlOutput = YamlMapper.fromJsonToYaml(jsonInput); assertEquals(expectedYamlOutput, actualYamlOutput); }
public static String fromJsonToYaml(String json) throws IOException { JsonNode jsonNode = objectMapper().readTree(json); return objectMapper().writeValueAsString(jsonNode); }