Skip to content

Commit

Permalink
Add MergeQuery.ofYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
chacham committed Jan 6, 2022
1 parent 93e1136 commit 8c2ebb1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ static MergeQuery<JsonNode> ofJson(Iterable<MergeSource> 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<JsonNode> 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<JsonNode> ofYaml(Iterable<MergeSource> 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
Expand Down
57 changes: 56 additions & 1 deletion it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ protected void scaffold(CentralDogma client) {
.commit("Initial files",
Change.ofJsonUpsert("/foo.json", "{ \"a\": \"bar\" }"),
Change.ofJsonUpsert("/foo1.json", "{ \"b\": \"baz\" }"),
Change.ofJsonUpsert("/foo2.json", "{ \"a\": \"new_bar\" }"))
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\""))
.push().join();
}

Expand Down Expand Up @@ -100,6 +105,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<JsonNode>) and MergeQuery.ofYaml(which is also
* MergeQuery<JsonNode>), 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) {
Expand Down

0 comments on commit 8c2ebb1

Please sign in to comment.