Skip to content

Commit

Permalink
Add test case for watcher not notified unless JSON5 content changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ks-yim committed Jan 7, 2022
1 parent 2384e54 commit e0f6fbb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
13 changes: 1 addition & 12 deletions it/src/test/java/com/linecorp/centraldogma/it/GetFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.linecorp.centraldogma.it;

import static com.linecorp.centraldogma.it.TestConstants.JSON5_CONTENTS;
import static com.linecorp.centraldogma.testing.internal.ExpectedExceptionAppender.assertThatThrownByWithExpectedException;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -111,18 +112,6 @@ void invalidProject(ClientType clientType) throws Exception {
@Nested
class GetFileJson5Test {

static final String JSON5_CONTENTS =
"{\n" +
" // comments\n" +
" unquoted: 'and you can quote me on that',\n" +
" singleQuotes: 'I can use \"double quotes\" here',\n" +
" lineBreaks: \"Look, Mom! \\\n" +
"No \\\\n's!\",\n" +
" leadingDecimalPoint: .8675309,\n" +
" trailingComma: 'in objects', andIn: ['arrays',],\n" +
" \"backwardsCompatible\": \"with JSON\",\n" +
"}\n";

@Test
void getJson5() throws JsonParseException {
final CentralDogma client = dogma.client();
Expand Down
21 changes: 21 additions & 0 deletions it/src/test/java/com/linecorp/centraldogma/it/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@

package com.linecorp.centraldogma.it;

import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

public final class TestConstants {

public static final String JSON5_CONTENTS;

static {
try {
final URL json5URL = requireNonNull(TestConstants.class.getClassLoader().getResource(
"com/linecorp/centraldogma/it/import/test1.json5"), "json5URL");
JSON5_CONTENTS = new String(Files.readAllBytes(new File(json5URL.toURI()).toPath()),
StandardCharsets.UTF_8);
} catch (Exception e) {
throw new Error(e);
}
}

private static final Set<String> previousRandomTexts = new HashSet<>();

public static String randomText() {
Expand Down
31 changes: 29 additions & 2 deletions it/src/test/java/com/linecorp/centraldogma/it/WatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.linecorp.centraldogma.it;

import static com.linecorp.centraldogma.it.TestConstants.JSON5_CONTENTS;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -40,6 +41,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;

Expand All @@ -57,6 +59,7 @@
import com.linecorp.centraldogma.common.PushResult;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Json5;

class WatchTest {

Expand Down Expand Up @@ -780,8 +783,10 @@ class WatchJson5Test {
void watchJson5() throws Exception {
final CentralDogma client = dogma.client();

final CompletableFuture<Entry<JsonNode>> future = client.watchFile(
dogma.project(), dogma.repo1(), Revision.HEAD, Query.ofJson("/test/test1.json5"));
final CompletableFuture<Entry<JsonNode>> future =
client.forRepo(dogma.project(), dogma.repo1())
.watch(Query.ofJson("/test/test1.json5"))
.start(Revision.HEAD);

assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
.isInstanceOf(TimeoutException.class);
Expand All @@ -805,5 +810,27 @@ void watchJson5() throws Exception {
assertThat(future.get(3, TimeUnit.SECONDS)).isEqualTo(
Entry.ofJson(result.revision(), "/test/test1.json5", "{a: 'foo'}\n"));
}

@Test
void watchJson5_notNotifiedIfJsonContentNotChanged() throws JsonParseException {
final CentralDogma client = dogma.client();

final CompletableFuture<Entry<JsonNode>> future =
client.forRepo(dogma.project(), dogma.repo1())
.watch(Query.ofJson("/test/test1.json5"))
.start(Revision.HEAD);

// Edit file to the plain JSON, so it doesn't change the actual JSON content in it.
final JsonNode plainJson = Json5.readTree(JSON5_CONTENTS);
client.forRepo(dogma.project(), dogma.repo1())
.commit("Edit test1.json5",
Change.ofJsonUpsert("/test/test1.json5", plainJson))
.push(Revision.HEAD)
.join();

// Watcher should not be notified since the JSON content is still the same.
assertThatThrownBy(() -> future.get(1000, TimeUnit.MILLISECONDS))
.isInstanceOf(TimeoutException.class);
}
}
}

0 comments on commit e0f6fbb

Please sign in to comment.