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