OverridePropertiesApplier { public JsonNode applyPropertiesOverride( JsonNode devfileNode, Map<String, String> overrideProperties) throws OverrideParameterException { for (Map.Entry<String, String> entry : overrideProperties.entrySet()) { String[] pathSegments = parseSegments(entry.getKey()); if (pathSegments.length < 1) { continue; } validateFirstSegment(pathSegments); String lastSegment = pathSegments[pathSegments.length - 1]; JsonNode currentNode = devfileNode; Iterator<String> pathSegmentsIterator = Stream.of(pathSegments).iterator(); do { String currentSegment = pathSegmentsIterator.next(); JsonNode nextNode = currentNode.path(currentSegment); if (nextNode.isMissingNode() && pathSegmentsIterator.hasNext()) { currentNode = ((ObjectNode) currentNode).putObject(currentSegment); continue; } else if (nextNode.isArray()) { if (!pathSegmentsIterator.hasNext()) { throw new OverrideParameterException( format( "Override property reference '%s' points to an array type object. Please add an item qualifier by name.", entry.getKey())); } String arrayObjectName = pathSegmentsIterator.next(); currentNode = findNodeByName((ArrayNode) nextNode, arrayObjectName, currentSegment); continue; } else { if (pathSegmentsIterator.hasNext()) { currentNode = nextNode; } } } while (pathSegmentsIterator.hasNext()); ((ObjectNode) currentNode).put(lastSegment, entry.getValue()); } return devfileNode; }  JsonNode applyPropertiesOverride(
      JsonNode devfileNode, Map<String, String> overrideProperties);  }
@Test public void shouldOverrideExistingPropertiesInDevfile() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"metadata\": {" + " \"generateName\": \"python\"" + " }" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("apiVersion", "2.0.0"); overrides.put("metadata.generateName", "go"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("apiVersion").textValue(), "2.0.0"); assertEquals(result.get("metadata").get("generateName").textValue(), "go"); }
@Test public void shouldCreateUnExistingOverridePropertiesInDevfile() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("metadata.generateName", "go"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("metadata").get("generateName").textValue(), "go"); }
@Test public void shouldCreateUnExistingAttributesInDevfile() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("attributes.persistVolumes", "true"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("attributes").get("persistVolumes").textValue(), "true"); }
@Test public void shouldUpdateExistingAttributesInDevfile() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"attributes\": {" + " \"persistVolumes\": \"false\"" + " }" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("attributes.persistVolumes", "true"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("attributes").get("persistVolumes").textValue(), "true"); }
@Test public void shouldUpdateExistingAttributesWithDotsInDevfile() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"attributes\": {" + " \"che.persistVolumes\": \"false\"" + " }" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("attributes.che.persistVolumes", "true"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("attributes").get("che.persistVolumes").textValue(), "true"); }
@Test public void shouldRewriteValueInProjectsArrayElementByName() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"projects\": [" + " {" + " \"name\": \"test1\"," + " \"clonePath\": \"/foo/bar1\"" + " }," + " {" + " \"name\": \"test2\"," + " \"clonePath\": \"/foo/bar2\"" + " }" + " ]" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("projects.test1.clonePath", "baz1"); overrides.put("projects.test2.clonePath", "baz2"); JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); assertEquals(result.get("projects").get(0).get("clonePath").textValue(), "baz1"); assertEquals(result.get("projects").get(1).get("clonePath").textValue(), "baz2"); }
@Test( expectedExceptions = OverrideParameterException.class, expectedExceptionsMessageRegExp = "Cannot apply override: object with name 'test3' not found in array of projects.") public void shouldThrowExceptionIfOverrideArrayObjectNotFoundByName() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"projects\": [" + " {" + " \"name\": \"test1\"," + " \"clonePath\": \"/foo/bar1\"" + " }," + " {" + " \"name\": \"test2\"," + " \"clonePath\": \"/foo/bar2\"" + " }" + " ]" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("projects.test3.clonePath", "baz1"); applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); }
@Test( expectedExceptions = OverrideParameterException.class, expectedExceptionsMessageRegExp = "Override property reference 'projects' points to an array type object. Please add an item qualifier by name.") public void shouldThrowExceptionIfOverrideReferenceEndsWithArray() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"projects\": [" + " {" + " \"name\": \"test1\"," + " \"clonePath\": \"/foo/bar1\"" + " }," + " {" + " \"name\": \"test2\"," + " \"clonePath\": \"/foo/bar2\"" + " }" + " ]" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("projects", "baz1"); applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); }
@Test( expectedExceptions = OverrideParameterException.class, expectedExceptionsMessageRegExp = "Override property reference 'projects' points to an array type object. Please add an item qualifier by name.") public void shouldThrowExceptionIfOverrideReferenceIsJustWithArray() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"projects\": [" + " {" + " \"name\": \"test1\"," + " \"clonePath\": \"/foo/bar1\"" + " }" + " ]" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("projects", "baz1"); applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); }
@Test( expectedExceptions = OverrideParameterException.class, expectedExceptionsMessageRegExp = "Override path 'commands.run.foo.bar' starts with an unsupported field pointer. Supported fields are \\{\"apiVersion\",\"metadata\",\"projects\"\\,\"attributes\"\\}.") public void shouldThrowExceptionIfOverrideReferenceUsesUnsupportedField() throws Exception { String json = "{" + "\"apiVersion\": \"1.0.0\"," + "\"projects\": [" + " {" + " \"name\": \"test1\"," + " \"clonePath\": \"/foo/bar1\"" + " }" + " ]" + "}"; Map<String, String> overrides = new HashMap<>(); overrides.put("commands.run.foo.bar", "baz1"); applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides); }