ObjectMapperFactory { public ObjectMapper forParsing() { return new ObjectMapper() .registerModules(jacksonModules) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } @Inject  ObjectMapperFactory(MailboxId.Factory mailboxIdFactory, MessageId.Factory messageIdFactory); ObjectMapper forParsing(); ObjectMapper forWriting();  }
@Test public void mailboxIdShouldBeDeserializable() throws Exception { String json = "{ \"mailboxId\": \"123\"}"; MailboxIdTestContainer expected = new MailboxIdTestContainer(InMemoryId.of(123)); MailboxIdTestContainer actual = testee.forParsing().readValue(json, MailboxIdTestContainer.class); assertThat(actual).isEqualToComparingFieldByField(expected); }
@Test public void mailboxIdShouldBeDeserializableWhenKey() throws Exception { String json = "{ \"map\": {\"123\": \"value\"}}"; MailboxIdKeyTestContainer expected = new MailboxIdKeyTestContainer(ImmutableMap.of(InMemoryId.of(123), "value")); MailboxIdKeyTestContainer actual = testee.forParsing().readValue(json, MailboxIdKeyTestContainer.class); assertThat(actual).isEqualToComparingFieldByField(expected); }
@Test public void readValueShouldParseRightObject() throws Exception { Rights.Right actual = testee.forParsing() .readValue("\"a\"", Rights.Right.class); assertThat(actual) .isEqualTo(Rights.Right.Administer); }
@Test public void readValueShouldParseUsernameObject() throws Exception { String username = "username"; Username actual = testee.forParsing() .readValue("\"" + username + "\"", Username.class); assertThat(actual) .isEqualTo(Username.of(username)); }
@Test public void readValueShouldParseActionModeWhenAutomatic() throws Exception { DispositionActionMode actual = testee.forParsing() .readValue("\"" + DispositionActionMode.Automatic.getValue() + "\"", DispositionActionMode.class); assertThat(actual) .isEqualTo(DispositionActionMode.Automatic); }
@Test public void readValueShouldParseActionModeWhenManual() throws Exception { DispositionActionMode actual = testee.forParsing() .readValue("\"" + DispositionActionMode.Manual.getValue() + "\"", DispositionActionMode.class); assertThat(actual) .isEqualTo(DispositionActionMode.Manual); }
@Test public void readValueShouldFailOnInvalidActionMode() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"illegal\"", DispositionActionMode.class)) .isInstanceOf(IllegalStateException.class) .hasMessage("Unrecognized MDN Disposition action mode illegal. Should be one of [manual-action, automatic-action]"); }
@Test public void readValueShouldParseSendingModeWhenAutomatic() throws Exception { DispositionSendingMode actual = testee.forParsing() .readValue("\"" + DispositionSendingMode.Automatic.getValue() + "\"", DispositionSendingMode.class); assertThat(actual) .isEqualTo(DispositionSendingMode.Automatic); }
@Test public void readValueShouldParseSendingModeWhenManual() throws Exception { DispositionSendingMode actual = testee.forParsing() .readValue("\"" + DispositionSendingMode.Manual.getValue() + "\"", DispositionSendingMode.class); assertThat(actual) .isEqualTo(DispositionSendingMode.Manual); }
@Test public void readValueShouldFailOnInvalidSendingMode() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"illegal\"", DispositionSendingMode.class)) .isInstanceOf(IllegalStateException.class) .hasMessage("Unrecognized MDN Disposition sending mode illegal. Should be one of [MDN-sent-manually, MDN-sent-automatically]"); }
@Test public void readValueShouldParseSendingModeWhenDeleted() throws Exception { DispositionType actual = testee.forParsing() .readValue("\"" + DispositionType.Deleted.getValue() + "\"", DispositionType.class); assertThat(actual) .isEqualTo(DispositionType.Deleted); }
@Test public void readValueShouldParseSendingModeWhenDispatched() throws Exception { DispositionType actual = testee.forParsing() .readValue("\"" + DispositionType.Dispatched.getValue() + "\"", DispositionType.class); assertThat(actual) .isEqualTo(DispositionType.Dispatched); }
@Test public void readValueShouldParseSendingModeWhenDisplayed() throws Exception { DispositionType actual = testee.forParsing() .readValue("\"" + DispositionType.Displayed.getValue() + "\"", DispositionType.class); assertThat(actual) .isEqualTo(DispositionType.Displayed); }
@Test public void readValueShouldParseSendingModeWhenProcessed() throws Exception { DispositionType actual = testee.forParsing() .readValue("\"" + DispositionType.Processed.getValue() + "\"", DispositionType.class); assertThat(actual) .isEqualTo(DispositionType.Processed); }
@Test public void readValueShouldFailOnInvalidDispositionType() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"illegal\"", DispositionType.class)) .isInstanceOf(IllegalStateException.class) .hasMessage("Unrecognized MDN Disposition type illegal. Should be one of [deleted, dispatched, displayed, processed]"); }
@Test public void readValueShouldParseRightsObject() throws Exception { String username = "username"; Rights actual = testee.forParsing() .readValue("{\"" + username + "\" : [\"a\", \"e\"]}", Rights.class); assertThat(actual) .isEqualTo(Rights.builder() .delegateTo(Username.of(username), Rights.Right.Administer, Rights.Right.Expunge) .build()); }
@Test public void readValueShouldRejectMultiCharacterRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"ae\"", Rights.Right.class)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void readValueShouldRejectUnsupportedRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"p\"", Rights.Right.class)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void readValueShouldRejectUnExistingRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"z\"", Rights.Right.class)) .isInstanceOf(IllegalArgumentException.class); }