From 7e79ea231e0b81645d40f63de6dd25889a54d166 Mon Sep 17 00:00:00 2001 From: "K.S. Yim" Date: Fri, 10 Dec 2021 13:36:04 +0900 Subject: [PATCH] Add JSON5 upsert GitRepository test case --- .../storage/repository/RepositoryUtil.java | 16 +++++++--------- .../repository/git/GitRepositoryTest.java | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/com/linecorp/centraldogma/server/storage/repository/RepositoryUtil.java b/server/src/main/java/com/linecorp/centraldogma/server/storage/repository/RepositoryUtil.java index c866948d04..3694deff76 100644 --- a/server/src/main/java/com/linecorp/centraldogma/server/storage/repository/RepositoryUtil.java +++ b/server/src/main/java/com/linecorp/centraldogma/server/storage/repository/RepositoryUtil.java @@ -141,16 +141,14 @@ static Entry applyQuery(Entry entry, Query query) { if (queryType == IDENTITY || queryType == IDENTITY_TEXT || queryType == IDENTITY_JSON) { return entry; } else if (queryType == JSON_PATH) { - if (entryType == EntryType.JSON5) { - try { - return Entry.of(entry.revision(), query.path(), EntryType.JSON, - query.apply(unsafeCast(Jackson.readTree(entry.contentAsText())))); - } catch (JsonParseException e) { - throw new QueryExecutionException("Invalid JSON5 entry content: " + entry.content() + - " (query: " + query + ')'); - } + try { + // Entry whose type is EntryType.JSON5 is overridden to EntryType.JSON for JsonPath query. + return Entry.of(entry.revision(), query.path(), EntryType.JSON, + query.apply(unsafeCast(entry.contentAsJson()))); + } catch (JsonParseException e) { + throw new QueryExecutionException("Invalid JSON content: " + entry.content() + + " (query: " + query + ')'); } - return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content())); } else { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); diff --git a/server/src/test/java/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepositoryTest.java b/server/src/test/java/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepositoryTest.java index 435d1d4336..70abbc2b0b 100644 --- a/server/src/test/java/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepositoryTest.java +++ b/server/src/test/java/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepositoryTest.java @@ -127,8 +127,10 @@ static void destroy() { private String prefix; private String allPattern; private final String[] jsonPaths = new String[NUM_ITERATIONS]; + private final String[] json5Paths = new String[NUM_ITERATIONS]; private final String[] textPaths = new String[NUM_ITERATIONS]; private final Change[] jsonUpserts = Util.unsafeCast(new Change[NUM_ITERATIONS]); + private final Change[] json5Upserts = Util.unsafeCast(new Change[NUM_ITERATIONS]); private final Change[] textUpserts = Util.unsafeCast(new Change[NUM_ITERATIONS]); private final Change[] jsonPatches = Util.unsafeCast(new Change[NUM_ITERATIONS]); private final Change[] textPatches = Util.unsafeCast(new Change[NUM_ITERATIONS]); @@ -140,11 +142,14 @@ void setUp(TestInfo testInfo) { for (int i = 0; i < NUM_ITERATIONS; i++) { final String jsonPath = prefix + i + ".json"; + final String json5Path = prefix + i + ".json5"; final String textPath = prefix + i + ".txt"; jsonPaths[i] = jsonPath; + json5Paths[i] = json5Path; textPaths[i] = textPath; jsonUpserts[i] = Change.ofJsonUpsert(jsonPath, "{ \"" + i + "\": " + i + " }"); + json5Upserts[i] = Change.ofJson5Upsert(json5Path, "{ \"" + i + "\": '" + i + "' }"); textUpserts[i] = Change.ofTextUpsert(textPath, "value:\n" + i); } @@ -166,6 +171,11 @@ void testJsonUpsert() { testUpsert(jsonUpserts, EntryType.JSON); } + @Test + void testJson5Upsert() { + testUpsert(json5Upserts, EntryType.JSON5); + } + @Test void testTextUpsert() { testUpsert(textUpserts, EntryType.TEXT); @@ -196,10 +206,9 @@ private void testUpsert(Change[] upserts, EntryType entryType) { final Map> entries = Util.unsafeCast(repo.find(headRev, allPattern).join()); for (Change c : upserts) { final String path = c.path(); - if (entryType == EntryType.TEXT) { + if (entryType == EntryType.TEXT || entryType == EntryType.JSON5) { // Text must be sanitized so that the last line ends with \n. - assertThat(entries).containsEntry(path, Entry.of(headRev, path, EntryType.TEXT, - c.content() + "\n")); + assertThat(entries).containsEntry(path, Entry.of(headRev, path, entryType, c.content() + "\n")); } else { assertThat(entries).containsEntry(path, Entry.of(headRev, path, entryType, c.content())); }