static Revision extractRevision(String jsonString) { try { final JsonNode jsonNode = Jackson.readTree(jsonString); return new Revision(Jackson.textValue(jsonNode.get(FIELD_NAME_REVISION), "")); } catch (Exception e) { throw new StorageException("failed to extract revision from " + jsonString, e); } }
static Revision extractRevision(String jsonString) { try { final JsonNode jsonNode = Jackson.readTree(jsonString); return new Revision(Jackson.textValue(jsonNode.get(FIELD_NAME_REVISION), "")); } catch (Exception e) { throw new StorageException("failed to extract revision from " + jsonString, e); } }
/** * GET /projects/{projectName}/repos/{repoName}/revision/{revision} * * <p>Normalizes the revision into an absolute revision. */ @Get("/projects/{projectName}/repos/{repoName}/revision/{revision}") @RequiresReadPermission public Map<String, Integer> normalizeRevision(Repository repository, @Param("revision") String revision) { final Revision normalizedRevision = repository.normalizeNow(new Revision(revision)); return ImmutableMap.of("revision", normalizedRevision.major()); } }
@Test public void normalizeRevision() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<TRevision> callback = invocation.getArgument(3); callback.onComplete(new TRevision(3)); return null; }).when(iface).normalizeRevision(anyString(), anyString(), any(), any()); assertThat(client.normalizeRevision("project", "repo", new Revision(1)).get()) .isEqualTo(new Revision(3)); verify(iface).normalizeRevision(eq("project"), eq("repo"), any(), 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 watchRepository() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5); callback.onComplete(new WatchRepositoryResult().setRevision(new TRevision(42))); return null; }).when(iface).watchRepository(any(), any(), any(), anyString(), anyLong(), any()); assertThat(client.watchRepository("project", "repo", new Revision(1), "/a.txt", 100).get()) .isEqualTo(new Revision(42)); verify(iface).watchRepository(eq("project"), eq("repo"), any(), eq("/a.txt"), eq(100L), any()); }
@Test public void watchRepositoryTimedOut() throws Exception { doAnswer(invocation -> { AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5); callback.onComplete(new WatchRepositoryResult()); return null; }).when(iface).watchRepository(any(), any(), any(), anyString(), anyLong(), any()); assertThat(client.watchRepository("project", "repo", new Revision(1), "/a.txt", 100).get()) .isNull(); verify(iface).watchRepository(eq("project"), eq("repo"), any(), eq("/a.txt"), eq(100L), any()); }
@Test public void testEquals() { final Entry<Void> e = Entry.ofDirectory(new Revision(1), "/foo"); assertThat(e).isNotEqualTo(null); assertThat(e).isNotEqualTo(new Object()); assertThat(e).isEqualTo(e); }
@Test public void finaLatestRevisionHead() { final Repository repo = newCachingRepo(); final Revision actualHeadRev = new Revision(2); doReturn(new RevisionRange(actualHeadRev, actualHeadRev)) .when(delegateRepo).normalizeNow(HEAD, HEAD); assertThat(repo.findLatestRevision(HEAD, "/**").join()).isNull(); verify(delegateRepo, never()).findLatestRevision(any(), any()); verifyNoMoreInteractions(delegateRepo); }
/** * GET /projects/{projectName}/repos/{repoName}/revision/{revision} * * <p>Normalizes the revision into an absolute revision. */ @Get("/projects/{projectName}/repos/{repoName}/revision/{revision}") @RequiresReadPermission public Map<String, Integer> normalizeRevision(Repository repository, @Param("revision") String revision) { final Revision normalizedRevision = repository.normalizeNow(new Revision(revision)); return ImmutableMap.of("revision", normalizedRevision.major()); } }
/** * GET /projects/{projectName}/repos/{repoName}/revision/{revision} * * <p>Normalizes the revision into an absolute revision. */ @Get("/projects/{projectName}/repos/{repoName}/revision/{revision}") @Decorator(HasReadPermission.class) public Map<String, Integer> normalizeRevision(Repository repository, @Param("revision") String revision) { final Revision normalizedRevision = repository.normalizeNow(new Revision(revision)); return ImmutableMap.of("revision", normalizedRevision.major()); } }
/** * Ensure that the absolute major revision numbers are returned as-is. */ @Test public void testAbsoluteMajor() throws Exception { final Revision expected = new Revision(1); final Revision actual = rule.client().normalizeRevision(rule.project(), rule.repo1(), expected).join(); assertThat(actual).isEqualTo(expected); }
@Test public void testToString() { assertThat(Entry.ofText(new Revision(1), "/a.txt", "a").toString()).isNotEmpty(); } }
/** * Ensure that the out-of-range absolute major revision numbers are rejected. */ @Test public void testAbsoluteMajorOutOfRange() throws Exception { final Revision outOfRange = new Revision(Integer.MAX_VALUE); assertThatThrownByWithExpectedException(RevisionNotFoundException.class, "2147483647", () -> rule.client().normalizeRevision(rule.project(), rule.repo1(), outOfRange).join()) .isInstanceOf(CompletionException.class).hasCauseInstanceOf(RevisionNotFoundException.class); }
@Test public void testInvalidRevision() throws Exception { final Change<String> change = Change.ofTextUpsert("/a_new_text_file.txt", "text"); assertThatThrownByWithExpectedException(RevisionNotFoundException.class, "2147483647", () -> rule.client().getPreviewDiffs( rule.project(), rule.repo1(), new Revision(Integer.MAX_VALUE), change).join()) .isInstanceOf(CompletionException.class).hasCauseInstanceOf(RevisionNotFoundException.class); }
/** * GET /projects/{projectName}/repositories/{repoName}/revision/{revision} * Normalizes the revision into an absolute revision. */ @Get("/projects/{projectName}/repositories/{repoName}/revision/{revision}") public RevisionDto normalizeRevision(@Param("projectName") String projectName, @Param("repoName") String repoName, @Param("revision") String revision) { return DtoConverter.convert(projectManager().get(projectName).repos().get(repoName) .normalizeNow(new Revision(revision))); }
/** * GET /projects/{projectName}/repositories/{repoName}/revision/{revision} * Normalizes the revision into an absolute revision. */ @Get("/projects/{projectName}/repositories/{repoName}/revision/{revision}") public RevisionDto normalizeRevision(@Param("projectName") String projectName, @Param("repoName") String repoName, @Param("revision") String revision) { return DtoConverter.convert(projectManager().get(projectName).repos().get(repoName) .normalizeNow(new Revision(revision))); }
@Test public void serialization() throws Exception { assertThatJson(new Revision(9)).isEqualTo("9"); assertThatJson(Revision.HEAD).isEqualTo("-1"); }