diff --git a/common/src/main/java/com/linecorp/centraldogma/common/MergeQuery.java b/common/src/main/java/com/linecorp/centraldogma/common/MergeQuery.java index 7660d61d7a..f427d09497 100644 --- a/common/src/main/java/com/linecorp/centraldogma/common/MergeQuery.java +++ b/common/src/main/java/com/linecorp/centraldogma/common/MergeQuery.java @@ -53,6 +53,26 @@ static MergeQuery ofJson(Iterable mergeSources) { return new JsonMergeQuery(IDENTITY, mergeSources, ImmutableList.of()); } + /** + * Returns a newly-created {@link MergeQuery} that merges the YAML contents as specified in the + * {@code mergeSources}. + * + * @param mergeSources the paths of YAML files being merged and indicates whether it is optional + */ + static MergeQuery ofYaml(MergeSource... mergeSources) { + return ofYaml(ImmutableList.copyOf(requireNonNull(mergeSources, "mergeSources"))); + } + + /** + * Returns a newly-created {@link MergeQuery} that merges the YAML contents as specified in the + * {@code mergeSources}. + * + * @param mergeSources the paths of YAML files being merged and indicates whether it is optional + */ + static MergeQuery ofYaml(Iterable mergeSources) { + return new JsonMergeQuery(IDENTITY, mergeSources, ImmutableList.of()); + } + /** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. Then, the specified diff --git a/it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java b/it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java index d0124d1557..430030a373 100644 --- a/it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java +++ b/it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java @@ -50,7 +50,12 @@ protected void scaffold(CentralDogma client) { client.push("myPro", "myRepo", Revision.HEAD, "Initial files", Change.ofJsonUpsert("/foo.json", "{ \"a\": \"bar\" }"), Change.ofJsonUpsert("/foo1.json", "{ \"b\": \"baz\" }"), - Change.ofJsonUpsert("/foo2.json", "{ \"a\": \"new_bar\" }")).join(); + Change.ofJsonUpsert("/foo2.json", "{ \"a\": \"new_bar\" }"), + Change.ofYamlUpsert("/bar.yml", "c:\n" + + " z: \"foo\""), + Change.ofYamlUpsert("/bar1.yml", "c:\n" + + " x: \"qux\""), + Change.ofYamlUpsert("/bar2.yml", "d: \"bar\"")).join(); } @Override @@ -96,6 +101,56 @@ void mergeJsonFiles(ClientType clientType) { .hasCauseInstanceOf(EntryNotFoundException.class); } + @ParameterizedTest + @EnumSource(ClientType.class) + void mergeYamlFiles(ClientType clientType) { + final CentralDogma client = clientType.client(dogma); + final MergedEntry merged = client.mergeFiles("myPro", "myRepo", Revision.HEAD, + MergeSource.ofRequired("/bar.yml"), + MergeSource.ofRequired("/bar1.yml"), + MergeSource.ofRequired("/bar2.yml"), + MergeSource.ofOptional("/bar3.yml")).join(); + + assertThat(merged.paths()).containsExactly("/bar.yml", "/bar1.yml", "/bar2.yml"); + assertThat(merged.revision()).isEqualTo(new Revision(2)); + assertThatJson(merged.content()).isEqualTo('{' + + " \"c\": {" + + " \"z\": \"foo\"," + + " \"x\": \"qux\"" + + " }," + + " \"d\": \"bar\"" + + '}'); + + /* TODO: Make it work. For now, ContentServiceV1 cannot differentiate + * MergeQuery.ofJson(which is MergeQuery) and MergeQuery.ofYaml(which is also + * MergeQuery), so merged entry type is always EntryType.JSON + assertThat(merged.type()).isEqualTo(EntryType.YAML); + */ + + // Check again to see if the original files are changed. + // Content is YAML but uses as JsonNode, so use assertThatJson + assertThatJson(client.getFile("myPro", "myRepo", Revision.HEAD, Query.ofYaml("/bar.yml")) + .join() + .content()) + .isEqualTo("{ \"c\": { \"z\": \"foo\" } }"); + assertThatJson(client.getFile("myPro", "myRepo", Revision.HEAD, Query.ofYaml("/bar1.yml")) + .join() + .content()) + .isEqualTo("{ \"c\": { \"x\": \"qux\" } }"); + assertThatJson(client.getFile("myPro", "myRepo", Revision.HEAD, Query.ofYaml("/bar2.yml")) + .join() + .content()) + .isEqualTo("{ \"d\": \"bar\" }"); + + assertThatThrownBy(() -> client.mergeFiles("myPro", "myRepo", Revision.HEAD, + MergeSource.ofRequired("/bar.yml"), + MergeSource.ofRequired("/bar1.yml"), + MergeSource.ofRequired("/bar2.yml"), + MergeSource.ofRequired("/bar3.yml")).join()) + .isInstanceOf(CompletionException.class) + .hasCauseInstanceOf(EntryNotFoundException.class); + } + @ParameterizedTest @EnumSource(ClientType.class) void exceptionWhenOnlyOptionalFilesAndDoNotExist(ClientType clientType) {