@Test public void shouldTransformEmptyRoot() throws IOException { JsonConfigCollection empty = readRootYaml("empty"); assertThat(empty.getJsonObject().isJsonObject(), is(true)); assertThat(empty.getJsonObject().get("environments").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("pipelines").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("environments").getAsJsonArray().size(), is(0)); assertThat(empty.getJsonObject().get("pipelines").getAsJsonArray().size(), is(0)); }
private GoPluginApiResponse handlingErrors(Supplier<GoPluginApiResponse> exec) { try { return exec.get(); } catch (ParsedRequest.RequestParseException e) { return badRequest(e.getMessage()); } catch (Exception e) { LOGGER.error("Unexpected error occurred in YAML configuration plugin.", e); JsonConfigCollection config = new JsonConfigCollection(); config.addError(new PluginError(e.toString(), "YAML config plugin")); return error(gson.toJson(config.getJsonObject())); } }
private GoPluginApiResponse handleParseContentRequest(GoPluginApiRequest request) { return handlingErrors(() -> { ParsedRequest parsed = ParsedRequest.parse(request); YamlConfigParser parser = new YamlConfigParser(); Map<String, String> contents = parsed.getParam("contents"); JsonConfigCollection result = new JsonConfigCollection(); contents.forEach((filename, content) -> { parser.parseStream(result, new ByteArrayInputStream(content.getBytes()), filename); }); result.updateTargetVersionFromFiles(); return success(gson.toJson(result.getJsonObject())); }); }
@Test public void shouldTransformRootWhen2PipelinesAnd2Environments() throws IOException { 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 shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty() { JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.get("environments") instanceof JsonArray, is(true)); assertThat(jsonObject.getAsJsonArray("environments"), is(new JsonArray())); }
@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")); assertThat(pipelines.get(1).getAsJsonObject().get("name").getAsString(), is("pipe2")); assertThat(pipelines.get(2).getAsJsonObject().get("name").getAsString(), is("pipe3")); }
@Test public void shouldReturnEnvironmentsInJsonObject() { jsonCollection.addEnvironment(devEnv, "dev.json"); JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.getAsJsonArray("environments").size(), is(1)); } }
@Test public void shouldAppendPipelinesToPipelinesCollection() { jsonCollection.addPipeline(pipe1, "pipe1.json"); jsonCollection.addPipeline(pipe2, "pipe2.json"); JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.getAsJsonArray("pipelines").size(), is(2)); }
@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 shouldReturnTargetVersion() { JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.get("target_version") instanceof JsonPrimitive, is(true)); assertThat(jsonObject.getAsJsonPrimitive("target_version").getAsInt(), is(1)); }
private GoPluginApiResponse handleParseDirectoryRequest(GoPluginApiRequest request) { return handlingErrors(() -> { ParsedRequest parsed = ParsedRequest.parse(request); File baseDir = new File(parsed.getStringParam("directory")); String pattern = parsed.getConfigurationKey(PLUGIN_SETTINGS_FILE_PATTERN); if (isBlank(pattern)) { pattern = getFilePattern(); } YamlConfigParser parser = new YamlConfigParser(); String[] files = new AntDirectoryScanner().getFilesMatchingPattern(baseDir, pattern); JsonConfigCollection config = parser.parseFiles(baseDir, files); config.updateTargetVersionFromFiles(); return success(gson.toJson(config.getJsonObject())); }); }
JsonObject result = collection.getJsonObject(); result.remove("environments"); result.remove("pipelines");