@Test public void watchFileTimedOut() throws Exception { doAnswer(invocation -> { AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5); callback.onComplete(new WatchFileResult()); return null; }).when(iface).watchFile(any(), any(), any(), any(), anyLong(), any()); assertThat(client.watchFile("project", "repo", new Revision(1), Query.ofText("/a.txt"), 100).get()).isNull(); verify(iface).watchFile(eq("project"), eq("repo"), any(), any(), eq(100L), any()); } }
@Test public void diffFile() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<DiffFileResult> callback = invocation.getArgument(5); callback.onComplete(new DiffFileResult(ChangeType.UPSERT_TEXT, "some_text")); return null; }).when(iface).diffFile(any(), any(), any(), any(), any(), any()); assertThat(client.getDiff("project", "repo", new Revision(1), new Revision(3), Query.ofText("/a.txt")).get()) .isEqualTo(Change.ofTextUpsert("/a.txt", "some_text")); verify(iface).diffFile(eq("project"), eq("repo"), any(), any(), any(), any()); }
@Test public void getFile() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback = invocation.getArgument(3); callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0)); return null; }).when(iface).normalizeRevision(any(), any(), any(), any()); doAnswer(invocation -> { final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4); callback.onComplete(new GetFileResult(TEntryType.TEXT, "content")); return null; }).when(iface).getFile(any(), any(), any(), any(), any()); assertThat(client.getFile("project", "repo", new Revision(1), Query.ofText("/a.txt")).get()) .isEqualTo(Entry.ofText(new Revision(1), "/a.txt", "content")); verify(iface).getFile(eq("project"), eq("repo"), any(), any(), any()); }
@Test(timeout = 10000) public void testWatchWithIdentityQuery() throws Exception { final Revision rev1 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, textUpserts[0]).join(); final CompletableFuture<Entry<String>> f = repo.watch(rev1, Query.ofText(textPaths[0])); final Revision rev2 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, textPatches[1]).join(); final Entry<String> res = f.get(3, TimeUnit.SECONDS); assertThat(res.revision()).isEqualTo(rev2); assertThat(res.type()).isEqualTo(EntryType.TEXT); // Text must be sanitized so that the last line ends with \n. assertThat(res.content()).isEqualTo(textUpserts[1].content() + '\n'); }
@Test public void getFile_path() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback = invocation.getArgument(3); callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0)); return null; }).when(iface).normalizeRevision(any(), any(), any(), any()); doAnswer(invocation -> { final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4); callback.onComplete(new GetFileResult(TEntryType.TEXT, "content")); return null; }).when(iface).getFile(any(), any(), any(), any(), any()); assertThat(client.getFile("project", "repo", new Revision(1), Query.ofText("/a.txt")).get()) .isEqualTo(Entry.ofText(new Revision(1), "/a.txt", "content")); verify(iface).getFile(eq("project"), eq("repo"), any(), any(), any()); }
@Test public void watchFile() throws Exception { doAnswer(invocation -> { AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5); callback.onComplete(new WatchFileResult().setRevision(new TRevision(42)) .setType(TEntryType.TEXT) .setContent("foo")); return null; }).when(iface).watchFile(any(), any(), any(), any(), anyLong(), any()); assertThat(client.watchFile("project", "repo", new Revision(1), Query.ofText("/a.txt"), 100).get()) .isEqualTo(Entry.ofText(new Revision(42), "/a.txt", "foo")); verify(iface).watchFile(eq("project"), eq("repo"), any(), any(), eq(100L), any()); }
@Test @SuppressWarnings("unchecked") public void identityQuery() { final Repository repo = setMockNames(newCachingRepo()); final Query<String> query = Query.ofText("/baz.txt"); final Entry<String> result = Entry.ofText(new Revision(10), "/baz.txt", "qux"); final Map<String, Entry<?>> entries = ImmutableMap.of("/baz.txt", result); 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(entries)); assertThat(repo.get(HEAD, query).join()).isEqualTo(result); verify(delegateRepo).find(new Revision(10), "/baz.txt", FIND_ONE_WITH_CONTENT); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.get(HEAD, query).join()).isEqualTo(result); assertThat(repo.get(new Revision(10), query).join()).isEqualTo(result); verify(delegateRepo, never()).find(any(), any(), any()); verifyNoMoreInteractions(delegateRepo); }
@Test @SuppressWarnings("unchecked") public void identityQueryMissingEntry() { final Repository repo = setMockNames(newCachingRepo()); final Query<String> query = Query.ofText("/baz.txt"); 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())); assertThat(repo.getOrNull(HEAD, query).join()).isNull(); verify(delegateRepo).find(new Revision(10), "/baz.txt", FIND_ONE_WITH_CONTENT); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.getOrNull(HEAD, query).join()).isNull(); assertThat(repo.getOrNull(new Revision(10), query).join()).isNull(); verify(delegateRepo, never()).find(any(), any(), any()); verifyNoMoreInteractions(delegateRepo); }
@Test public void backwardCompatibility() throws Exception { final SaveNamedQueryCommand c = (SaveNamedQueryCommand) Jackson.readValue( '{' + " \"type\": \"SAVE_NAMED_QUERY\"," + " \"projectName\": \"foo\"," + " \"queryName\": \"bar\"," + " \"enabled\": true," + " \"repositoryName\": \"qux\"," + " \"query\": {" + " \"type\": \"IDENTITY\"," + " \"path\": \"/first.txt\"" + " }," + " \"comment\": \"plaintext comment\"," + " \"markup\": \"PLAINTEXT\"" + '}', Command.class); assertThat(c.author()).isEqualTo(Author.SYSTEM); assertThat(c.timestamp()).isNotZero(); assertThat(c.projectName()).isEqualTo("foo"); assertThat(c.queryName()).isEqualTo("bar"); assertThat(c.isEnabled()).isTrue(); assertThat(c.repositoryName()).isEqualTo("qux"); assertThat(c.query()).isEqualTo(Query.ofText("/first.txt")); assertThat(c.comment()).isEqualTo("plaintext comment"); assertThat(c.markup()).isSameAs(Markup.PLAINTEXT); } }
@Test public void singleDiff() { final Repository repo = setMockNames(newCachingRepo()); final Query<String> query = Query.ofText("/foo.txt"); final Change<String> change = Change.ofTextUpsert(query.path(), "bar"); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(HEAD, INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(-1), new Revision(-10)); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), new Revision(-10)); // Uncached when(delegateRepo.diff(any(), any(), any(Query.class))).thenReturn(completedFuture(change)); assertThat(repo.diff(HEAD, INIT, query).join()).isEqualTo(change); verify(delegateRepo).diff(INIT, new Revision(10), query); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.diff(HEAD, new Revision(-10), query).join()).isEqualTo(change); assertThat(repo.diff(HEAD, INIT, query).join()).isEqualTo(change); assertThat(repo.diff(new Revision(10), new Revision(-10), query).join()).isEqualTo(change); assertThat(repo.diff(new Revision(10), INIT, query).join()).isEqualTo(change); verify(delegateRepo, never()).diff(any(), any(), any(Query.class)); verifyNoMoreInteractions(delegateRepo); }
final Query<String> query = Query.ofText("/foo.txt"); final Entry<?> entry = client.getFile(project, REPO_FOO, HEAD, query).join(); final CacheStats stats3 = cacheStatsSupplier.get();
@Test public void testDiff_remove() throws Exception { final CentralDogma client = rule.client(); final Revision rev1 = client.push(rule.project(), rule.repo1(), HEAD, "summary1", Change.ofTextUpsert("/foo.txt", "hello")).join().revision(); final Revision rev2 = client.push(rule.project(), rule.repo1(), HEAD, "summary2", Change.ofRemoval("/foo.txt")).join().revision(); assertThat(rev1.forward(1)).isEqualTo(rev2); assertThat(client.getDiff(rule.project(), rule.repo1(), rev1, rev1, Query.ofText("/foo.txt")).join().type()) .isEqualTo(ChangeType.APPLY_TEXT_PATCH); assertThat(client.getDiff(rule.project(), rule.repo1(), rev1, rev2, Query.ofText("/foo.txt")).join()) .isEqualTo(Change.ofRemoval("/foo.txt")); }
@Test public void testDiff_rename() throws Exception { final CentralDogma client = rule.client(); final Revision rev1 = client.push(rule.project(), rule.repo1(), HEAD, "summary1", Change.ofTextUpsert("/bar.txt", "hello")).join().revision(); final Revision rev2 = client.push(rule.project(), rule.repo1(), HEAD, "summary2", Change.ofRename("/bar.txt", "/baz.txt")).join().revision(); assertThat(rev1.forward(1)).isEqualTo(rev2); assertThat(client.getDiff(rule.project(), rule.repo1(), rev1, rev2, Query.ofText("/bar.txt")).join()) .isEqualTo(Change.ofRemoval("/bar.txt")); } }
@Test public void testJsonConversion() { TestUtil.assertJsonConversion(Query.ofText("/foo.txt"), Query.class, '{' + " \"type\": \"IDENTITY\"," + " \"path\": \"/foo.txt\"" + '}'); TestUtil.assertJsonConversion(Query.ofJsonPath("/bar.json", "$..author", "$..name"), Query.class, '{' + " \"type\": \"JSON_PATH\"," + " \"path\": \"/bar.json\"," + " \"expressions\": [ \"$..author\", \"$..name\" ]" + '}'); } }
@Test(timeout = 10000) public void text() throws Exception { try (Watcher<String> watcher = dogma.client().fileWatcher("directory", "my-service", Query.ofText("/endpoints.txt"))) { final CountDownLatch latch = new CountDownLatch(2); watcher.watch(unused -> latch.countDown()); final CentralDogmaEndpointGroup<String> endpointGroup = CentralDogmaEndpointGroup.ofWatcher( watcher, EndpointListDecoder.TEXT); endpointGroup.awaitInitialEndpoints(); assertThat(endpointGroup.endpoints()).isEqualTo(ENDPOINT_LIST); assertThat(latch.getCount()).isOne(); dogma.client().push("directory", "my-service", Revision.HEAD, "commit", Change.ofTextUpsert("/endpoints.txt", "foo.bar:1234")) .join(); await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> assertThat(latch.getCount()).isZero()); assertThat(endpointGroup.endpoints()).containsExactly(Endpoint.of("foo.bar", 1234)); } } }
@Test public void testJsonConversion() { assertJsonConversion(new SaveNamedQueryCommand(1234L, Author.SYSTEM, "foo", "bar", true, "qux", Query.ofText("/first.txt"), "plaintext comment", Markup.PLAINTEXT), Command.class,