-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDiscussionEventChannelJsonTest.java
More file actions
116 lines (95 loc) · 5.22 KB
/
Copy pathDiscussionEventChannelJsonTest.java
File metadata and controls
116 lines (95 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package edu.stanford.protege.webprotege.issues;
import com.google.common.collect.ImmutableList;
import edu.stanford.protege.webprotege.common.EventId;
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotege.common.UserId;
import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonContent;
import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Contract-guard tests that pin the on-the-wire {@code @JsonTypeName} discriminator for
* every event in the discussion namespace.
*
* <p>These events are routed across the JSON-RPC envelope by their {@code @JsonTypeName}
* value. If a discriminator drifts (for example the canonical plural {@code discussions}
* silently reverts to the singular {@code discussion}), the consumer treats the message as
* an unknown type and drops it — no exception is thrown on either side. See
* protegeproject/webprotege-backend-api#54.
*
* <p>The expected values below are hard-coded on purpose: asserting against each class's
* own {@code CHANNEL} constant would let a constant rename slip through undetected. The
* whole point of this test is to fail the build if any of these strings ever change.
*/
@JsonTest
class DiscussionEventChannelJsonTest {
private static final String PROJECT_ID = "00000000-1111-2222-3333-444444444444";
private static final String COMMENT_POSTED = "webprotege.events.discussions.CommentPosted";
private static final String COMMENT_UPDATED = "webprotege.events.discussions.CommentUpdated";
private static final String STATUS_CHANGED = "webprotege.events.discussions.DiscussionThreadStatusChanged";
private static final String THREAD_CREATED = "webprotege.events.discussions.DiscussionThreadCreated";
@Autowired
private JacksonTester<CommentPostedEvent> commentPostedTester;
@Autowired
private JacksonTester<CommentUpdatedEvent> commentUpdatedTester;
@Autowired
private JacksonTester<DiscussionThreadStatusChangedEvent> statusChangedTester;
@Autowired
private JacksonTester<DiscussionThreadCreatedEvent> threadCreatedTester;
private static EventId eventId() {
return new EventId("event-id");
}
private static ProjectId projectId() {
return new ProjectId(PROJECT_ID);
}
private static ThreadId threadId() {
return ThreadId.valueOf("thread-id");
}
private static Comment comment() {
return new Comment(CommentId.valueOf("comment-id"),
UserId.valueOf("the-user"),
0L,
Optional.empty(),
"body",
"renderedBody");
}
@Test
void commentPostedEventSerializesWithPluralDiscriminator() throws Exception {
var event = new CommentPostedEvent(eventId(), projectId(), threadId(), comment(),
Optional.empty(), 0, 0);
JsonContent<CommentPostedEvent> json = commentPostedTester.write(event);
assertThat(json).extractingJsonPathStringValue("$.['@type']").isEqualTo(COMMENT_POSTED);
assertThat(CommentPostedEvent.CHANNEL).isEqualTo(COMMENT_POSTED);
}
@Test
void commentUpdatedEventSerializesWithPluralDiscriminator() throws Exception {
var event = new CommentUpdatedEvent(eventId(), projectId(), threadId(), comment());
JsonContent<CommentUpdatedEvent> json = commentUpdatedTester.write(event);
assertThat(json).extractingJsonPathStringValue("$.['@type']").isEqualTo(COMMENT_UPDATED);
assertThat(CommentUpdatedEvent.CHANNEL).isEqualTo(COMMENT_UPDATED);
}
@Test
void discussionThreadStatusChangedEventSerializesWithPluralDiscriminator() throws Exception {
var event = new DiscussionThreadStatusChangedEvent(eventId(), projectId(), threadId(),
Optional.empty(), 0, Status.OPEN);
JsonContent<DiscussionThreadStatusChangedEvent> json = statusChangedTester.write(event);
assertThat(json).extractingJsonPathStringValue("$.['@type']").isEqualTo(STATUS_CHANGED);
assertThat(DiscussionThreadStatusChangedEvent.CHANNEL).isEqualTo(STATUS_CHANGED);
}
@Test
void discussionThreadCreatedEventSerializesWithPluralDiscriminator() throws Exception {
OWLClass entity = new OWLClassImpl(OWLRDFVocabulary.OWL_THING.getIRI());
var thread = new EntityDiscussionThread(threadId(), projectId(), entity, Status.OPEN,
ImmutableList.of());
var event = new DiscussionThreadCreatedEvent(eventId(), projectId(), thread);
JsonContent<DiscussionThreadCreatedEvent> json = threadCreatedTester.write(event);
assertThat(json).extractingJsonPathStringValue("$.['@type']").isEqualTo(THREAD_CREATED);
assertThat(DiscussionThreadCreatedEvent.CHANNEL).isEqualTo(THREAD_CREATED);
}
}