forked from tomzo/gocd-yaml-config-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
387 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/cd/go/plugin/config/yaml/JsonConfigCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cd.go.plugin.config.yaml; | ||
|
||
import com.google.gson.*; | ||
|
||
public class JsonConfigCollection { | ||
private static final int TARGET_VERSION = 1; | ||
private final Gson gson; | ||
|
||
private JsonObject mainObject = new JsonObject(); | ||
private JsonArray environments = new JsonArray(); | ||
private JsonArray pipelines = new JsonArray(); | ||
private JsonArray errors = new JsonArray(); | ||
|
||
public JsonConfigCollection() | ||
{ | ||
gson = new Gson(); | ||
|
||
mainObject.add("target_version",new JsonPrimitive(TARGET_VERSION)); | ||
mainObject.add("environments",environments); | ||
mainObject.add("pipelines",pipelines); | ||
mainObject.add("errors",errors); | ||
} | ||
|
||
protected JsonArray getEnvironments() | ||
{ | ||
return environments; | ||
} | ||
|
||
public void addEnvironment(JsonElement environment,String location) { | ||
environments.add(environment); | ||
environment.getAsJsonObject().add("location",new JsonPrimitive(location)); | ||
} | ||
|
||
public JsonObject getJsonObject() | ||
{ | ||
return mainObject; | ||
} | ||
|
||
public void addPipeline(JsonElement pipeline,String location) { | ||
pipelines.add(pipeline); | ||
pipeline.getAsJsonObject().add("location",new JsonPrimitive(location)); | ||
} | ||
|
||
public JsonArray getPipelines() { | ||
return pipelines; | ||
} | ||
|
||
public JsonArray getErrors() { | ||
return errors; | ||
} | ||
|
||
public void addError(PluginError error) { | ||
errors.add(gson.toJsonTree(error)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cd.go.plugin.config.yaml; | ||
|
||
public class PluginError { | ||
private String location; | ||
private String message; | ||
|
||
public PluginError(){} | ||
public PluginError(String message){ | ||
this.message = message; | ||
} | ||
public PluginError(String message,String location) | ||
{ | ||
this.location = location; | ||
this.message = message; | ||
} | ||
|
||
public String getLocation() { | ||
return location; | ||
} | ||
|
||
public void setLocation(String location) { | ||
this.location = location; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cd.go.plugin.config.yaml; | ||
|
||
import com.esotericsoftware.yamlbeans.YamlReader; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Map; | ||
|
||
public class RootParser { | ||
public JsonConfigCollection parseString(InputStreamReader yaml) throws IOException { | ||
YamlReader reader = new YamlReader(yaml); | ||
Object object = reader.read(); | ||
return new JsonConfigCollection(); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/cd/go/plugin/config/yaml/materials/MaterialTransform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cd.go.plugin.config.yaml.materials; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
|
||
public class MaterialTransform { | ||
|
||
public static final String JSON_MATERIAL_TYPE_FIELD = "type"; | ||
public static final String JSON_MATERIAL_NAME_FIELD = "name"; | ||
|
||
public static final String YAML_SHORT_KEYWORD_GIT = "git"; | ||
private final HashSet<String> yamlShortKeywords = new HashSet<String>(); | ||
|
||
public MaterialTransform() { | ||
yamlShortKeywords.add(YAML_SHORT_KEYWORD_GIT); | ||
} | ||
|
||
public JsonObject transform(Object maybeMaterial) { | ||
Map<String,Object> map = (Map<String,Object>)maybeMaterial; | ||
for(Map.Entry<String, Object> entry : map.entrySet()) { | ||
String materialName = entry.getKey(); | ||
JsonObject material = new JsonObject(); | ||
material.addProperty(JSON_MATERIAL_NAME_FIELD, materialName); | ||
Map<String,Object> materialMap = (Map<String,Object>)entry.getValue(); | ||
String materialType = getOptionalString(materialMap,"type"); | ||
if(materialType != null) | ||
material.addProperty(JSON_MATERIAL_TYPE_FIELD,materialType); | ||
|
||
String git = getOptionalString(materialMap,YAML_SHORT_KEYWORD_GIT); | ||
if(git != null) | ||
{ | ||
material.addProperty(JSON_MATERIAL_TYPE_FIELD,YAML_SHORT_KEYWORD_GIT); | ||
material.addProperty("url",git); | ||
} | ||
|
||
for(Map.Entry<String, Object> materialProp : materialMap.entrySet()) { | ||
if(yamlShortKeywords.contains(materialProp.getKey())) | ||
continue; | ||
if(materialProp.getValue() instanceof String) | ||
material.addProperty(materialProp.getKey(),(String)materialProp.getValue()); | ||
} | ||
return material; | ||
} | ||
throw new RuntimeException("expected material hash to have 1 item"); | ||
} | ||
|
||
private String getOptionalString(Map map, String fieldName) { | ||
Object type = map.get(fieldName); | ||
if(type != null) | ||
{ | ||
return (String)type; | ||
} | ||
return null; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/cd/go/plugin/config/yaml/RootParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cd.go.plugin.config.yaml; | ||
|
||
import com.esotericsoftware.yamlbeans.YamlException; | ||
import com.esotericsoftware.yamlbeans.YamlReader; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class RootParserTest { | ||
@Test | ||
public void shouldReadSimpleFile() throws IOException { | ||
YamlReader reader = new YamlReader(TestUtils.createReader("examples/simple.gocd.yaml")); | ||
Object object = reader.read(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cd.go.plugin.config.yaml; | ||
|
||
import com.esotericsoftware.yamlbeans.YamlException; | ||
import com.esotericsoftware.yamlbeans.YamlReader; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.*; | ||
|
||
public class TestUtils { | ||
|
||
public static JsonElement readJsonObject(String path) throws IOException { | ||
JsonParser parser = new JsonParser(); | ||
return parser.parse(TestUtils.createReader(path)); | ||
} | ||
|
||
public static Object readYamlObject(String path) throws IOException { | ||
YamlReader reader = new YamlReader(TestUtils.createReader(path)); | ||
return reader.read(); | ||
} | ||
|
||
public static InputStreamReader createReader(String path) throws IOException { | ||
final InputStream resourceAsStream = getResourceAsStream(path); | ||
return new InputStreamReader(resourceAsStream); | ||
} | ||
|
||
private static String loadString(String path) throws IOException { | ||
final InputStream resourceAsStream = getResourceAsStream(path); | ||
return IOUtils.toString(resourceAsStream); | ||
} | ||
|
||
private static InputStream getResourceAsStream( String resource ) { | ||
final InputStream in | ||
= getContextClassLoader().getResourceAsStream( resource ); | ||
|
||
return in == null ? TestUtils.class.getResourceAsStream( resource ) : in; | ||
} | ||
|
||
private static ClassLoader getContextClassLoader() { | ||
return Thread.currentThread().getContextClassLoader(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/cd/go/plugin/config/yaml/materials/MaterialTransformTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cd.go.plugin.config.yaml.materials; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static cd.go.plugin.config.yaml.TestUtils.readJsonObject; | ||
import static cd.go.plugin.config.yaml.TestUtils.readYamlObject; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class MaterialTransformTest { | ||
private final MaterialTransform parser; | ||
|
||
public MaterialTransformTest() { | ||
parser = new MaterialTransform(); | ||
} | ||
|
||
@Test | ||
public void shouldTransformMinimalGit() throws IOException { | ||
testTransform("minimal.git"); | ||
} | ||
@Test | ||
public void shouldTransformMinimalExplicitGit() throws IOException { | ||
testTransform("minimal-explicit.git","minimal.git"); | ||
} | ||
@Test | ||
public void shouldTransformMinimalNoUrlGit() throws IOException { | ||
testTransform("minimal-nourl.git","minimal.git"); | ||
} | ||
|
||
private void testTransform(String caseFile) throws IOException { | ||
testTransform(caseFile,caseFile); | ||
} | ||
|
||
private void testTransform(String caseFile,String expectedFile) throws IOException { | ||
JsonElement expectedObject = readJsonObject("parts/materials/" + expectedFile + ".json"); | ||
JsonObject jsonObject = parser.transform(readYamlObject("parts/materials/" + caseFile + ".yaml")); | ||
assertThat(jsonObject,is(expectedObject)); | ||
} | ||
|
||
} |
Oops, something went wrong.