@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Change)) { return false; } final Change<?> that = (Change<?>) o; if (type != that.type()) { return false; } if (!path.equals(that.path())) { return false; } return Objects.equals(content, that.content()); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Change)) { return false; } final Change<?> that = (Change<?>) o; if (type != that.type()) { return false; } if (!path.equals(that.path())) { return false; } return Objects.equals(content, that.content()); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Change)) { return false; } final Change<?> that = (Change<?>) o; if (type != that.type()) { return false; } if (!path.equals(that.path())) { return false; } return Objects.equals(content, that.content()); }
public static <T> ChangeDto<T> convert(Change<T> change) { requireNonNull(change, "change"); return new ChangeDto<>(change.path(), change.type(), change.content()); }
public static <T> ChangeDto<T> convert(Change<T> change) { requireNonNull(change, "change"); return new ChangeDto<>(change.path(), change.type(), change.content()); }
public static <T> ChangeDto<T> convert(Change<T> change) { requireNonNull(change, "change"); return new ChangeDto<>(change.path(), change.type(), change.content()); }
@Override public void diffFile(String projectName, String repositoryName, Revision from, Revision to, Query query, AsyncMethodCallback resultHandler) { // FIXME(trustin): Remove the firstNonNull() on the change content once we make it optional. handle(projectManager.get(projectName).repos().get(repositoryName) .diff(convert(from), convert(to), convert(query)) .thenApply(change -> new DiffFileResult(convert(change.type()), firstNonNull(change.contentAsText(), ""))), resultHandler); }
static ChangeDto convert(Change<?> change) { final ChangeDto dto = new ChangeDto(); dto.setPath(change.path()); dto.setType(change.type().name()); dto.setContent(change.contentAsText()); return dto; }
static ChangeDto convert(Change<?> change) { final ChangeDto dto = new ChangeDto(); dto.setPath(change.path()); dto.setType(change.type().name()); dto.setContent(change.contentAsText()); return dto; }
static ChangeDto convert(Change<?> change) { final ChangeDto dto = new ChangeDto(); dto.setPath(change.path()); dto.setType(change.type().name()); dto.setContent(change.contentAsText()); return dto; }
@Test public void testApplyUpsertOnExistingPath() throws Exception { final String jsonPath = "/a_new_json_file.json"; rule.client().push(rule.project(), rule.repo1(), Revision.HEAD, "Add a new JSON file", Change.ofJsonUpsert(jsonPath, "{ \"a\": \"apple\" }")).join(); final Change<JsonNode> change = Change.ofJsonPatch(jsonPath, "{ \"a\": \"apple\" }", "{ \"a\": \"angle\" }"); final List<Change<?>> returnedList = rule.client().getPreviewDiffs(rule.project(), rule.repo1(), Revision.HEAD, change).join(); assertThat(returnedList).hasSize(1); assertThat(returnedList.get(0).type()).isEqualTo(ChangeType.APPLY_JSON_PATCH); } }
@Override public void diffFile(String projectName, String repositoryName, Revision from, Revision to, Query query, AsyncMethodCallback resultHandler) { // FIXME(trustin): Remove the firstNonNull() on the change content once we make it optional. handle(projectManager.get(projectName).repos().get(repositoryName) .diff(convert(from), convert(to), convert(query)) .thenApply(change -> new DiffFileResult(convert(change.type()), firstNonNull(change.contentAsText(), ""))), resultHandler); }
@Override public void diffFile(String projectName, String repositoryName, Revision from, Revision to, Query query, AsyncMethodCallback resultHandler) { // FIXME(trustin): Remove the firstNonNull() on the change content once we make it optional. handle(projectManager.get(projectName).repos().get(repositoryName) .diff(convert(from), convert(to), convert(query)) .thenApply(change -> new DiffFileResult(convert(change.type()), firstNonNull(change.contentAsText(), ""))), resultHandler); }
@Test public void testPreviewDiff() { final Map<String, Change<?>> changeMap = repo.previewDiff(HEAD, jsonUpserts[0]).join(); assertThat(changeMap).containsEntry(jsonPaths[0], jsonUpserts[0]); // Invalid patch assertThatThrownBy(() -> repo.previewDiff(HEAD, jsonPatches[1]).join()) .isInstanceOf(CompletionException.class) .hasCauseInstanceOf(ChangeConflictException.class); // Invalid removal assertThatThrownBy(() -> repo.previewDiff(HEAD, Change.ofRemoval(jsonPaths[0])).join()) .isInstanceOf(CompletionException.class) .hasCauseInstanceOf(ChangeConflictException.class); // Apply a series of changes final List<Change<?>> changes = Arrays.asList(jsonUpserts[0], jsonPatches[1], jsonPatches[2], Change.ofRename(jsonPaths[0], jsonPaths[1]), Change.ofRemoval(jsonPaths[1])); Map<String, Change<?>> returnedChangeMap = repo.previewDiff(HEAD, changes).join(); assertThat(returnedChangeMap).isEmpty(); assertThatThrownBy(() -> repo.previewDiff(new Revision(Integer.MAX_VALUE), changes).join()) .isInstanceOf(CompletionException.class) .hasCauseInstanceOf(RevisionNotFoundException.class); assertThat(repo.previewDiff(new Revision(-1), Collections.emptyList()).join()).isEmpty(); // Test upsert on an existing path repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, jsonPatches[0], jsonPatches[1]).join(); returnedChangeMap = repo.previewDiff(HEAD, jsonUpserts[0]).join(); assertThat(returnedChangeMap.get(jsonPaths[0]).type()).isEqualTo(ChangeType.APPLY_JSON_PATCH); }
@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")); }
@Override protected Change doForward(com.linecorp.centraldogma.common.Change<?> value) { final Change change = new Change(value.path(), convertChangeType(value.type())); switch (change.getType()) { case UPSERT_JSON: case APPLY_JSON_PATCH: try { change.setContent(Jackson.writeValueAsString(value.content())); } catch (JsonProcessingException e) { throw new ChangeFormatException("failed to read a JSON tree", e); } break; case UPSERT_TEXT: case APPLY_TEXT_PATCH: case RENAME: change.setContent((String) value.content()); break; case REMOVE: break; } return change; }
@Override protected Change doForward(com.linecorp.centraldogma.common.Change<?> value) { final Change change = new Change(value.path(), convertChangeType(value.type())); switch (change.getType()) { case UPSERT_JSON: case APPLY_JSON_PATCH: try { change.setContent(Jackson.writeValueAsString(value.content())); } catch (JsonProcessingException e) { throw new ChangeFormatException("failed to read a JSON tree", e); } break; case UPSERT_TEXT: case APPLY_TEXT_PATCH: case RENAME: change.setContent((String) value.content()); break; case REMOVE: break; } return change; }
: null; switch (change.type()) { case UPSERT_JSON: { final JsonNode oldJsonNode = oldContent != null ? Jackson.readTree(oldContent) : null;
@Test public void testQueryByRange() throws Exception { final String path = "/test_json_file.json"; for (int i = 0; i < 5; i++) { final Change<JsonNode> change = Change.ofJsonUpsert(path, String.format("{ \"key\" : \"%d\"}", i)); rule.client().push( rule.project(), rule.repo1(), HEAD, TestConstants.randomText(), change).join(); } final Change<JsonNode> res = rule.client().getDiff( rule.project(), rule.repo1(), new Revision(-4), new Revision(-1), Query.ofJsonPath(path, "$.key")).join(); assertThat(res.type()).isEqualTo(ChangeType.APPLY_JSON_PATCH); assertThatJson(res.content()).isEqualTo( "[{" + " \"op\": \"safeReplace\"," + " \"path\": \"\"," + " \"oldValue\": \"1\"," + " \"value\": \"4\"" + "}]"); }