/** * Returns a newly-created {@link Entry} of a JSON file. * * @param revision the revision of the JSON file * @param path the path of the JSON file * @param content the content of the JSON file * * @throws JsonParseException if the {@code content} is not a valid JSON */ public static Entry<JsonNode> ofJson(Revision revision, String path, String content) throws JsonParseException { return ofJson(revision, path, Jackson.readTree(content)); }
/** * Returns a newly-created {@link Entry} of a JSON file. * * @param revision the revision of the JSON file * @param path the path of the JSON file * @param content the content of the JSON file * * @throws JsonParseException if the {@code content} is not a valid JSON */ public static Entry<JsonNode> ofJson(Revision revision, String path, String content) throws JsonParseException { return ofJson(revision, path, Jackson.readTree(content)); }
/** * Returns a newly-created {@link Entry} of a JSON file. * * @param revision the revision of the JSON file * @param path the path of the JSON file * @param content the content of the JSON file * * @throws JsonParseException if the {@code content} is not a valid JSON */ public static Entry<JsonNode> ofJson(Revision revision, String path, String content) throws JsonParseException { return ofJson(revision, path, Jackson.readTree(content)); }
public static com.linecorp.centraldogma.common.Entry<?> convert( com.linecorp.centraldogma.common.Revision revision, Entry entry) { switch (entry.getType()) { case JSON: try { final JsonNode value = Jackson.readTree(entry.getContent()); return com.linecorp.centraldogma.common.Entry.ofJson(revision, entry.getPath(), value); } catch (IOException e) { throw new UncheckedIOException(e); } case TEXT: return com.linecorp.centraldogma.common.Entry.ofText(revision, entry.getPath(), entry.getContent()); case DIRECTORY: return com.linecorp.centraldogma.common.Entry.ofDirectory(revision, entry.getPath()); default: throw new IllegalArgumentException("unsupported entry type: " + entry.getType()); } }
public static com.linecorp.centraldogma.common.Entry<?> convert( com.linecorp.centraldogma.common.Revision revision, Entry entry) { switch (entry.getType()) { case JSON: try { final JsonNode value = Jackson.readTree(entry.getContent()); return com.linecorp.centraldogma.common.Entry.ofJson(revision, entry.getPath(), value); } catch (IOException e) { throw new UncheckedIOException(e); } case TEXT: return com.linecorp.centraldogma.common.Entry.ofText(revision, entry.getPath(), entry.getContent()); case DIRECTORY: return com.linecorp.centraldogma.common.Entry.ofDirectory(revision, entry.getPath()); default: throw new IllegalArgumentException("unsupported entry type: " + entry.getType()); } }
@Test @SuppressWarnings("unchecked") public void jsonPathQuery() throws JsonParseException { final Repository repo = setMockNames(newCachingRepo()); final Query<JsonNode> query = Query.ofJsonPath("/baz.json", "$.a"); final Entry<JsonNode> queryResult = Entry.ofJson(new Revision(10), query.path(), "{\"a\": \"b\"}"); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(new Revision(10)); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(HEAD); // Uncached when(delegateRepo.getOrNull(any(), any(Query.class))).thenReturn(completedFuture(queryResult)); assertThat(repo.get(HEAD, query).join()).isEqualTo(queryResult); verify(delegateRepo).getOrNull(new Revision(10), query); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.get(HEAD, query).join()).isEqualTo(queryResult); assertThat(repo.get(new Revision(10), query).join()).isEqualTo(queryResult); verify(delegateRepo, never()).getOrNull(any(), any(Query.class)); verifyNoMoreInteractions(delegateRepo); }
@Test @SuppressWarnings("unchecked") public void mergeQuery() throws JsonParseException { final Repository repo = setMockNames(newCachingRepo()); final MergeQuery<JsonNode> query = MergeQuery.ofJson(MergeSource.ofRequired("/foo.json"), MergeSource.ofRequired("/bar.json")); final MergedEntry<JsonNode> queryResult = MergedEntry.of(new Revision(10), JSON, Jackson.readTree("{\"a\": \"bar\"}"), "/foo.json", "/bar.json"); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(new Revision(10)); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(HEAD); // Uncached when(delegateRepo.find(any(), any(), any())) .thenReturn(completedFuture(ImmutableMap.of("/foo.json", Entry.ofJson( new Revision(10), "/foo.json", "{\"a\": \"foo\"}")))) .thenReturn(completedFuture(ImmutableMap.of("/bar.json", Entry.ofJson( new Revision(10), "/bar.json", "{\"a\": \"bar\"}")))); assertThat(repo.mergeFiles(HEAD, query).join()).isEqualTo(queryResult); verify(delegateRepo).find(new Revision(10), "/foo.json", FIND_ONE_WITH_CONTENT); verify(delegateRepo).find(new Revision(10), "/bar.json", FIND_ONE_WITH_CONTENT); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.mergeFiles(HEAD, query).join()).isEqualTo(queryResult); assertThat(repo.mergeFiles(new Revision(10), query).join()).isEqualTo(queryResult); verify(delegateRepo, never()).find(any(), any(), any()); verifyNoMoreInteractions(delegateRepo); }
private Entry<JsonNode> expectedMirrorState(Revision revision, String localPath) throws IOException { final String sha1 = git.getRepository() .exactRef(Constants.R_HEADS + Constants.MASTER) .getObjectId().getName(); return Entry.ofJson(revision, localPath + "mirror_state.json", "{ \"sourceRevision\": \"" + sha1 + "\" }"); }
@Test public void ofDirectory() throws Exception { final Entry<Void> e = Entry.ofDirectory(new Revision(1), "/"); assertThat(e.revision()).isEqualTo(new Revision(1)); assertThat(e.hasContent()).isFalse(); e.ifHasContent(unused -> fail()); assertThatThrownBy(e::content).isInstanceOf(IllegalStateException.class); assertThatThrownBy(e::contentAsJson).isInstanceOf(IllegalStateException.class); assertThatThrownBy(e::contentAsText).isInstanceOf(IllegalStateException.class); assertThatThrownBy(e::contentAsPrettyText).isInstanceOf(IllegalStateException.class); assertThatThrownBy(() -> e.contentAsJson(JsonNode.class)).isInstanceOf(IllegalStateException.class); // directory vs. directory final Entry<Void> e2 = Entry.ofDirectory(new Revision(1), "/"); assertThat(e).isEqualTo(e2); assertThat(e.hashCode()).isEqualTo(e2.hashCode()); assertThat(e).isNotEqualTo(Entry.ofDirectory(new Revision(2), "/")); assertThat(e).isNotEqualTo(Entry.ofDirectory(new Revision(1), "/foo")); // directory vs. text file final Entry<String> e3 = Entry.ofText(new Revision(1), "/a.txt", "foo"); assertThat(e).isNotEqualTo(e3); assertThat(e.hashCode()).isNotEqualTo(e3.hashCode()); // directory vs. JSON file final Entry<JsonNode> e4 = Entry.ofJson(new Revision(1), "/a.json", "{ \"foo\": \"bar\" }"); assertThat(e).isNotEqualTo(e4); assertThat(e.hashCode()).isNotEqualTo(e4.hashCode()); }
@Test public void ofJson() throws Exception { final Entry<JsonNode> e = Entry.ofJson(new Revision(1), "/a.json", "{ \"foo\": \"bar\" }"); assertThat(e.revision()).isEqualTo(new Revision(1)); assertThat(e.hasContent()).isTrue(); e.ifHasContent(content -> assertThatJson(content).isEqualTo("{ \"foo\": \"bar\" }")); assertThatJson(e.content()).isEqualTo("{ \"foo\": \"bar\" }"); assertThat(e.contentAsText()).isEqualTo("{\"foo\":\"bar\"}"); assertThat(e.contentAsPrettyText()).isEqualTo("{\n \"foo\": \"bar\"\n}"); assertThat(e.content()).isSameAs(e.contentAsJson()); assertThat(e.content()).isEqualTo(e.contentAsJson(JsonNode.class)); // JSON file vs. JSON file final Entry<JsonNode> e2 = Entry.ofJson(new Revision(1), "/a.json", "{ \"foo\": \"bar\" }"); assertThat(e).isEqualTo(e2); assertThat(e.hashCode()).isEqualTo(e2.hashCode()); assertThat(e).isNotEqualTo(Entry.ofJson(new Revision(2), "/a.json", "{ \"foo\": \"bar\" }")); assertThat(e).isNotEqualTo(Entry.ofJson(new Revision(1), "/b.json", "{ \"foo\": \"bar\" }")); assertThat(e).isNotEqualTo(Entry.ofJson(new Revision(1), "/a.json", "null")); // JSON file vs. text file final Entry<String> e3 = Entry.ofText(new Revision(1), "/a.json", "{\"foo\":\"bar\"}"); assertThat(e).isNotEqualTo(e3); assertThat(e.hashCode()).isNotEqualTo(e3.hashCode()); // JSON file vs. directory final Entry<Void> e4 = Entry.ofDirectory(new Revision(1), "/foo"); assertThat(e).isNotEqualTo(e4); assertThat(e.hashCode()).isNotEqualTo(e4.hashCode()); }
@Test public void testWatchFileWithIdentityQuery() throws Exception { final Revision rev0 = rule.client() .normalizeRevision(rule.project(), rule.repo1(), Revision.HEAD) .join(); final CompletableFuture<Entry<JsonNode>> future = rule.client().watchFile( rule.project(), rule.repo1(), rev0, Query.ofJson("/test/test1.json"), 3000); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // An irrelevant change should not trigger a notification. final Change<JsonNode> change1 = Change.ofJsonUpsert("/test/test2.json", "[ 3, 2, 1 ]"); final PushResult res1 = rule.client().push( rule.project(), rule.repo1(), rev0, "Add test2.json", change1).join(); final Revision rev1 = res1.revision(); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // Make a relevant change now. final Change<JsonNode> change2 = Change.ofJsonUpsert("/test/test1.json", "[ -1, -2, -3 ]"); final PushResult res2 = rule.client().push( rule.project(), rule.repo1(), rev1, "Update test1.json", change2).join(); final Revision rev2 = res2.revision(); assertThat(rev2).isEqualTo(rev0.forward(2)); assertThat(future.get(3, TimeUnit.SECONDS)).isEqualTo( Entry.ofJson(rev2, "/test/test1.json", "[-1,-2,-3]")); }
@Test public void testWatchFile() throws Exception { final Revision rev0 = rule.client() .normalizeRevision(rule.project(), rule.repo1(), Revision.HEAD) .join(); final CompletableFuture<Entry<JsonNode>> future = rule.client().watchFile(rule.project(), rule.repo1(), rev0, Query.ofJsonPath("/test/test1.json", "$[0]"), 3000); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // An irrelevant change should not trigger a notification. final Change<JsonNode> change1 = Change.ofJsonUpsert("/test/test2.json", "[ 3, 2, 1 ]"); final PushResult res1 = rule.client().push( rule.project(), rule.repo1(), rev0, "Add test2.json", change1).join(); final Revision rev1 = res1.revision(); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // Make a relevant change now. final Change<JsonNode> change2 = Change.ofJsonUpsert("/test/test1.json", "[ -1, -2, -3 ]"); final PushResult res2 = rule.client().push( rule.project(), rule.repo1(), rev1, "Add test1.json", change2).join(); final Revision rev2 = res2.revision(); assertThat(rev2).isEqualTo(rev0.forward(2)); assertThat(future.get(3, TimeUnit.SECONDS)).isEqualTo( Entry.ofJson(rev2, "/test/test1.json", "-1")); }
final Entry<JsonNode> e3 = Entry.ofJson(new Revision(1), "/a.json", "{ \"foo\": \"bar\" }"); assertThat(e).isNotEqualTo(e3); assertThat(e.hashCode()).isNotEqualTo(e3.hashCode());
try { converted = unsafeCast( Entry.ofJson(normRev, query.path(), Jackson.readTree(r.getContent()))); } catch (IOException e) { throw new CompletionException(
try { converted = unsafeCast( Entry.ofJson(normRev, query.path(), Jackson.readTree(r.getContent()))); } catch (IOException e) { throw new CompletionException(
try { converted = unsafeCast( Entry.ofJson(normRev, query.path(), Jackson.readTree(r.getContent()))); } catch (IOException e) { throw new CompletionException(
case JSON: try { converted = unsafeCast(Entry.ofJson(revision, query.path(), r.getContent())); } catch (JsonParseException e) { throw new CompletionException("failed to parse the query result: " + query, e);
Entry.ofText(rev3, "/first/light.txt", "26-Aug-2014\n"), Entry.ofDirectory(rev3, "/second"), Entry.ofJson(rev3, "/second/son.json", "{\"release_date\": \"21-Mar-2014\"}"));
case JSON: try { converted = unsafeCast(Entry.ofJson(revision, query.path(), r.getContent())); } catch (JsonParseException e) { throw new CompletionException("failed to parse the query result: " + query, e);
case JSON: try { converted = unsafeCast(Entry.ofJson(revision, query.path(), r.getContent())); } catch (JsonParseException e) { throw new CompletionException("failed to parse the query result: " + query, e);