From e0f6fbbffa8a50945f830911b65b2624b4cb6e21 Mon Sep 17 00:00:00 2001 From: "K.S. Yim" Date: Fri, 7 Jan 2022 11:53:39 +0900 Subject: [PATCH] Add test case for watcher not notified unless JSON5 content changes --- .../linecorp/centraldogma/it/GetFileTest.java | 13 +------- .../centraldogma/it/TestConstants.java | 21 +++++++++++++ .../linecorp/centraldogma/it/WatchTest.java | 31 +++++++++++++++++-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/it/src/test/java/com/linecorp/centraldogma/it/GetFileTest.java b/it/src/test/java/com/linecorp/centraldogma/it/GetFileTest.java index c6379a2bd3..8830eeefb6 100644 --- a/it/src/test/java/com/linecorp/centraldogma/it/GetFileTest.java +++ b/it/src/test/java/com/linecorp/centraldogma/it/GetFileTest.java @@ -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; @@ -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(); diff --git a/it/src/test/java/com/linecorp/centraldogma/it/TestConstants.java b/it/src/test/java/com/linecorp/centraldogma/it/TestConstants.java index aa210189d9..8630f19482 100644 --- a/it/src/test/java/com/linecorp/centraldogma/it/TestConstants.java +++ b/it/src/test/java/com/linecorp/centraldogma/it/TestConstants.java @@ -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 previousRandomTexts = new HashSet<>(); public static String randomText() { diff --git a/it/src/test/java/com/linecorp/centraldogma/it/WatchTest.java b/it/src/test/java/com/linecorp/centraldogma/it/WatchTest.java index d171ea9817..1bbbe0d5c6 100644 --- a/it/src/test/java/com/linecorp/centraldogma/it/WatchTest.java +++ b/it/src/test/java/com/linecorp/centraldogma/it/WatchTest.java @@ -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; @@ -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; @@ -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 { @@ -780,8 +783,10 @@ class WatchJson5Test { void watchJson5() throws Exception { final CentralDogma client = dogma.client(); - final CompletableFuture> future = client.watchFile( - dogma.project(), dogma.repo1(), Revision.HEAD, Query.ofJson("/test/test1.json5")); + final CompletableFuture> 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); @@ -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> 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); + } } }