Skip to content

Commit

Permalink
Add MergeQuery.ofYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
chacham committed Nov 25, 2021
1 parent 16ae4cc commit f2332e2
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 @@ -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
Expand Down Expand Up @@ -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<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 f2332e2

Please sign in to comment.