Skip to content

Commit

Permalink
Events API: test JSON serde with views (#9645)
Browse files Browse the repository at this point in the history
  • Loading branch information
adutra authored Sep 26, 2024
1 parent 5f67e7a commit 209d07e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,56 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
import org.projectnessie.model.ContentKey;
import org.projectnessie.model.IcebergTable;
import org.projectnessie.model.ImmutableCommitMeta;
import org.projectnessie.model.Reference;
import org.projectnessie.model.ser.Views.V1;
import org.projectnessie.model.ser.Views.V2;

class TestJsonSerde {

private static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new Jdk8Module());
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};

private static final UUID ID = UUID.fromString("58bdb0a2-20c0-4a6f-b6fe-abd4ebce2e96");
private static final Reference BRANCH_1 = Branch.of("branch1", "cafebabe");
private static final Reference BRANCH_2 = Branch.of("branch2", "deadbeef");

@Test
void testCommit() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void testCommit(Class<?> view) throws IOException {
CommitMeta.Builder commitMeta =
ImmutableCommitMeta.builder()
.committer("committer")
.message("message")
.commitTime(Instant.parse("2024-09-26T11:11:11Z"))
.authorTime(Instant.parse("2024-09-26T22:22:22Z"))
.hash("hash2");
if (view == V1.class) {
commitMeta
.properties(Map.of("key1", "value1"))
.author("author1")
.signedOffBy("signedOffBy1")
.build();
} else {
commitMeta
.allProperties(Map.of("key1", List.of("value1", "value2")))
.addAllAuthors("author1", "author2")
.addAllSignedOffBy("signedOffBy1", "signedOffBy2")
.addAllParentCommitHashes(List.of("parent1", "parent2"))
.build();
}
CommitEvent event =
ImmutableCommitEvent.builder()
.id(ID)
Expand All @@ -51,22 +76,18 @@ void testCommit() throws IOException {
.hashAfter("hash2")
.eventCreationTimestamp(Instant.parse("2024-09-26T00:00:00Z"))
.eventInitiator("Alice")
.commitMeta(
ImmutableCommitMeta.builder()
.committer("committer")
.message("message")
.commitTime(Instant.parse("2024-09-26T11:11:11Z"))
.authorTime(Instant.parse("2024-09-26T22:22:22Z"))
.build())
.commitMeta(commitMeta.build())
.putProperty("key1", "value1")
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.COMMIT);
testSerde(event, "commit.json", CommitEvent.class);
testSerde(
event, view == V1.class ? "commit-v1.json" : "commit-v2.json", CommitEvent.class, view);
}

@Test
void merge() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void merge(Class<?> view) throws IOException {
MergeEvent event =
ImmutableMergeEvent.builder()
.id(ID)
Expand All @@ -84,11 +105,12 @@ void merge() throws IOException {
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.MERGE);
testSerde(event, "merge.json", MergeEvent.class);
testSerde(event, "merge.json", MergeEvent.class, view);
}

@Test
void transplant() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void transplant(Class<?> view) throws IOException {
TransplantEvent event =
ImmutableTransplantEvent.builder()
.id(ID)
Expand All @@ -103,46 +125,49 @@ void transplant() throws IOException {
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.TRANSPLANT);
testSerde(event, "transplant.json", TransplantEvent.class);
testSerde(event, "transplant.json", TransplantEvent.class, view);
}

@Test
void referenceCreated() throws IOException {
ReferenceCreatedEvent event =
ImmutableReferenceCreatedEvent.builder()
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void referenceUpdated(Class<?> view) throws IOException {
ReferenceUpdatedEvent event =
ImmutableReferenceUpdatedEvent.builder()
.id(ID)
.repositoryId("repo1")
.reference(BRANCH_1)
.eventCreationTimestamp(Instant.parse("2024-09-26T00:00:00Z"))
.eventInitiator("Alice")
.hashBefore("hash1")
.hashAfter("hash2")
.putProperty("key1", "value1")
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.REFERENCE_CREATED);
testSerde(event, "reference-created.json", ReferenceCreatedEvent.class);
assertThat(event.getType()).isEqualTo(EventType.REFERENCE_UPDATED);
testSerde(event, "reference-updated.json", ReferenceUpdatedEvent.class, view);
}

@Test
void referenceUpdated() throws IOException {
ReferenceUpdatedEvent event =
ImmutableReferenceUpdatedEvent.builder()
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void referenceCreated(Class<?> view) throws IOException {
ReferenceCreatedEvent event =
ImmutableReferenceCreatedEvent.builder()
.id(ID)
.repositoryId("repo1")
.reference(BRANCH_1)
.eventCreationTimestamp(Instant.parse("2024-09-26T00:00:00Z"))
.eventInitiator("Alice")
.hashBefore("hash1")
.hashAfter("hash2")
.putProperty("key1", "value1")
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.REFERENCE_UPDATED);
testSerde(event, "reference-updated.json", ReferenceUpdatedEvent.class);
assertThat(event.getType()).isEqualTo(EventType.REFERENCE_CREATED);
testSerde(event, "reference-created.json", ReferenceCreatedEvent.class, view);
}

@Test
void referenceDeleted() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void referenceDeleted(Class<?> view) throws IOException {
ReferenceDeletedEvent event =
ImmutableReferenceDeletedEvent.builder()
.id(ID)
Expand All @@ -155,11 +180,12 @@ void referenceDeleted() throws IOException {
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.REFERENCE_DELETED);
testSerde(event, "reference-deleted.json", ReferenceDeletedEvent.class);
testSerde(event, "reference-deleted.json", ReferenceDeletedEvent.class, view);
}

@Test
void contentStored() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void contentStored(Class<?> view) throws IOException {
ContentStoredEvent event =
ImmutableContentStoredEvent.builder()
.id(ID)
Expand All @@ -175,11 +201,12 @@ void contentStored() throws IOException {
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.CONTENT_STORED);
testSerde(event, "content-stored.json", ContentStoredEvent.class);
testSerde(event, "content-stored.json", ContentStoredEvent.class, view);
}

@Test
void contentRemoved() throws IOException {
@ParameterizedTest
@ValueSource(classes = {V1.class, V2.class})
void contentRemoved(Class<?> view) throws IOException {
ContentRemovedEvent event =
ImmutableContentRemovedEvent.builder()
.id(ID)
Expand All @@ -194,15 +221,18 @@ void contentRemoved() throws IOException {
.putProperty("key2", "value2")
.build();
assertThat(event.getType()).isEqualTo(EventType.CONTENT_REMOVED);
testSerde(event, "content-removed.json", ContentRemovedEvent.class);
testSerde(event, "content-removed.json", ContentRemovedEvent.class, view);
}

private void testSerde(Event event, String resource, Class<? extends Event> eventClass)
private void testSerde(
Event event, String resource, Class<? extends Event> eventClass, Class<?> view)
throws IOException {
String serialized = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(event);
assertThat(MAPPER.readValue(serialized, MAP_TYPE))
.isEqualTo(MAPPER.readValue(getClass().getResourceAsStream(resource), MAP_TYPE));
Event deserialized = MAPPER.readValue(serialized, eventClass);
String serialized =
MAPPER.writerWithDefaultPrettyPrinter().withView(view).writeValueAsString(event);
System.out.println(serialized);
assertThat(MAPPER.readerWithView(view).readTree(serialized))
.isEqualTo(MAPPER.readerWithView(view).readTree(getClass().getResourceAsStream(resource)));
Event deserialized = MAPPER.readerWithView(view).readValue(serialized, eventClass);
assertThat(deserialized).isEqualTo(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"hashBefore" : "hash1",
"hashAfter" : "hash2",
"commitMeta" : {
"properties" : { },
"hash" : null,
"properties" : {
"key1" : "value1"
},
"hash" : "hash2",
"committer" : "committer",
"authors" : [ ],
"allSignedOffBy" : [ ],
"message" : "message",
"commitTime" : "2024-09-26T11:11:11Z",
"authorTime" : "2024-09-26T22:22:22Z",
"author" : null,
"signedOffBy" : null
"author" : "author1",
"signedOffBy" : "signedOffBy1"
},
"key1" : "value1",
"key2" : "value2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type" : "COMMIT",
"id" : "58bdb0a2-20c0-4a6f-b6fe-abd4ebce2e96",
"repositoryId" : "repo1",
"eventCreationTimestamp" : "2024-09-26T00:00:00Z",
"eventInitiator" : "Alice",
"reference" : {
"type" : "BRANCH",
"name" : "branch1",
"hash" : "cafebabe"
},
"hashBefore" : "hash1",
"hashAfter" : "hash2",
"commitMeta" : {
"hash" : "hash2",
"committer" : "committer",
"authors" : [ "author1", "author2" ],
"allSignedOffBy" : [ "signedOffBy1", "signedOffBy2" ],
"message" : "message",
"commitTime" : "2024-09-26T11:11:11Z",
"authorTime" : "2024-09-26T22:22:22Z",
"allProperties" : {
"key1" : [ "value1", "value2" ]
},
"parentCommitHashes" : [ "parent1", "parent2" ]
},
"key1" : "value1",
"key2" : "value2"
}

0 comments on commit 209d07e

Please sign in to comment.