// Path: src/main/java/cd/go/plugin/config/yaml/JsonConfigCollection.java
// public class JsonConfigCollection {
//     private static final int DEFAULT_VERSION = 1;
//     private final Gson gson;
// 
//     private Set<Integer> uniqueVersions = new HashSet<>();
//     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();
// 
//         updateTargetVersionTo(DEFAULT_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(String message, String location) {
//         this.addError(new PluginError(message, location));
//     }
// 
//     public void addError(PluginError error) {
//         errors.add(gson.toJsonTree(error));
//     }
// 
//     public void append(JsonConfigCollection other) {
//         this.environments.addAll(other.environments);
//         this.pipelines.addAll(other.pipelines);
//         this.errors.addAll(other.errors);
//         this.uniqueVersions.addAll(other.uniqueVersions);
//     }
// 
//     public void updateFormatVersionFound(int version) {
//         uniqueVersions.add(version);
//         updateTargetVersionTo(version);
//     }
// 
//     public void updateTargetVersionFromFiles() {
//         if (uniqueVersions.size() > 1) {
//             throw new RuntimeException("Versions across files are not unique. Found versions: " + uniqueVersions + ". There can only be one version across the whole repository.");
//         }
//         updateTargetVersionTo(uniqueVersions.iterator().hasNext() ? uniqueVersions.iterator().next() : DEFAULT_VERSION);
//     }
// 
//     private void updateTargetVersionTo(int targetVersion) {
//         mainObject.remove("target_version");
//         mainObject.add("target_version", new JsonPrimitive(targetVersion));
//     }
// }
// 
// Path: src/test/java/cd/go/plugin/config/yaml/TestUtils.java
// public static Object readYamlObject(String path) throws IOException {
//     YamlConfig config = new YamlConfig();
//     config.setAllowDuplicates(false);
//     YamlReader reader = new YamlReader(TestUtils.createReader(path), config);
//     return reader.read();
// }


import cd.go.plugin.config.yaml.JsonConfigCollection;
import cd.go.plugin.config.yaml.YamlConfigException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static cd.go.plugin.config.yaml.TestUtils.readYamlObject;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
        JsonConfigCollection collection = readRootYaml("2");
        assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(2));
        assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(2));
    }

    @Test
    public void shouldTransformRootWithCommonSection() throws IOException {
        JsonConfigCollection collection = readRootYaml("common_section");
        assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(1));
        assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(1));
    }

    @Test
    public void shouldRecognizeAndUpdateVersion() throws Exception {
        assertTargetVersion(readRootYaml("version_not_present").getJsonObject(), 1);
        assertTargetVersion(readRootYaml("version_1").getJsonObject(), 1);
        assertTargetVersion(readRootYaml("version_2").getJsonObject(), 2);
    }
    
    @Test
    public void shouldPreserveOrderOfPipelines() throws IOException {
        MaterialTransform materialTransform = mock(MaterialTransform.class);
        StageTransform stageTransform = mock(StageTransform.class);
        EnvironmentVariablesTransform environmentTransform = mock(EnvironmentVariablesTransform.class);
        ParameterTransform parameterTransform = mock(ParameterTransform.class);
        pipelineTransform = new PipelineTransform(materialTransform, stageTransform, environmentTransform, parameterTransform);
        rootTransform = new RootTransform(pipelineTransform, environmentsTransform);
        
        JsonConfigCollection collection = readRootYaml("pipeline_order");
        JsonArray pipelines = collection.getJsonObject().get("pipelines").getAsJsonArray();
// Path: src/main/java/cd/go/plugin/config/yaml/JsonConfigCollection.java
// public class JsonConfigCollection {
//     private static final int DEFAULT_VERSION = 1;
//     private final Gson gson;
// 
//     private Set<Integer> uniqueVersions = new HashSet<>();
//     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();
// 
//         updateTargetVersionTo(DEFAULT_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(String message, String location) {
//         this.addError(new PluginError(message, location));
//     }
// 
//     public void addError(PluginError error) {
//         errors.add(gson.toJsonTree(error));
//     }
// 
//     public void append(JsonConfigCollection other) {
//         this.environments.addAll(other.environments);
//         this.pipelines.addAll(other.pipelines);
//         this.errors.addAll(other.errors);
//         this.uniqueVersions.addAll(other.uniqueVersions);
//     }
// 
//     public void updateFormatVersionFound(int version) {
//         uniqueVersions.add(version);
//         updateTargetVersionTo(version);
//     }
// 
//     public void updateTargetVersionFromFiles() {
//         if (uniqueVersions.size() > 1) {
//             throw new RuntimeException("Versions across files are not unique. Found versions: " + uniqueVersions + ". There can only be one version across the whole repository.");
//         }
//         updateTargetVersionTo(uniqueVersions.iterator().hasNext() ? uniqueVersions.iterator().next() : DEFAULT_VERSION);
//     }
// 
//     private void updateTargetVersionTo(int targetVersion) {
//         mainObject.remove("target_version");
//         mainObject.add("target_version", new JsonPrimitive(targetVersion));
//     }
// }
// 
// Path: src/test/java/cd/go/plugin/config/yaml/TestUtils.java
// public static Object readYamlObject(String path) throws IOException {
//     YamlConfig config = new YamlConfig();
//     config.setAllowDuplicates(false);
//     YamlReader reader = new YamlReader(TestUtils.createReader(path), config);
//     return reader.read();
// }


// Path: src/test/java/cd/go/plugin/config/yaml/transforms/RootTransformTest.java
import cd.go.plugin.config.yaml.JsonConfigCollection;
import cd.go.plugin.config.yaml.YamlConfigException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static cd.go.plugin.config.yaml.TestUtils.readYamlObject;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

        JsonConfigCollection collection = readRootYaml("2");
        assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(2));
        assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(2));
    }

    @Test
    public void shouldTransformRootWithCommonSection() throws IOException {
        JsonConfigCollection collection = readRootYaml("common_section");
        assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(1));
        assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(1));
    }

    @Test
    public void shouldRecognizeAndUpdateVersion() throws Exception {
        assertTargetVersion(readRootYaml("version_not_present").getJsonObject(), 1);
        assertTargetVersion(readRootYaml("version_1").getJsonObject(), 1);
        assertTargetVersion(readRootYaml("version_2").getJsonObject(), 2);
    }
    
    @Test
    public void shouldPreserveOrderOfPipelines() throws IOException {
        MaterialTransform materialTransform = mock(MaterialTransform.class);
        StageTransform stageTransform = mock(StageTransform.class);
        EnvironmentVariablesTransform environmentTransform = mock(EnvironmentVariablesTransform.class);
        ParameterTransform parameterTransform = mock(ParameterTransform.class);
        pipelineTransform = new PipelineTransform(materialTransform, stageTransform, environmentTransform, parameterTransform);
        rootTransform = new RootTransform(pipelineTransform, environmentsTransform);
        
        JsonConfigCollection collection = readRootYaml("pipeline_order");
        JsonArray pipelines = collection.getJsonObject().get("pipelines").getAsJsonArray();
        assertThat(pipelines.get(0).getAsJsonObject().get("name").getAsString(), is("pipe1"));