Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1818 Introduce mailboxId serializer/deserializer
- Loading branch information
Raphael Ouazana
committed
Sep 5, 2016
1 parent
4a25501
commit 460b650
Showing
7 changed files
with
215 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
package org.apache.james.jmap.json; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.james.mailbox.inmemory.InMemoryId; | ||
import org.apache.james.mailbox.model.MailboxId; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
public class ObjectMapperFactoryTest { | ||
|
||
private ObjectMapperFactory testee; | ||
|
||
@Before | ||
public void setup() { | ||
testee = new ObjectMapperFactory(new InMemoryId.Factory()); | ||
} | ||
|
||
@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 mailboxIdShouldBeSerializable() throws Exception { | ||
MailboxIdTestContainer container = new MailboxIdTestContainer(InMemoryId.of(123)); | ||
String expectedJson = "{\"mailboxId\":\"123\"}"; | ||
String actual = testee.forWriting().writeValueAsString(container); | ||
assertThat(actual).isEqualTo(expectedJson); | ||
} | ||
|
||
@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 mailboxIdShouldBeSerializableWhenKeyWithoutToString() throws Exception { | ||
ObjectMapperFactory testeeWithoutToString = new ObjectMapperFactory(new KeyWithoutToString.Factory()); | ||
MailboxIdKeyTestContainer container = new MailboxIdKeyTestContainer(ImmutableMap.of(new KeyWithoutToString("key"), "value")); | ||
String expectedJson = "{\"map\":{\"key\":\"value\"}}"; | ||
String actual = testeeWithoutToString.forWriting().writeValueAsString(container); | ||
assertThat(actual).isEqualTo(expectedJson); | ||
} | ||
|
||
public static class MailboxIdTestContainer { | ||
public MailboxId mailboxId; | ||
|
||
public MailboxIdTestContainer() { | ||
} | ||
|
||
public MailboxIdTestContainer(MailboxId mailboxId) { | ||
this.mailboxId = mailboxId; | ||
} | ||
} | ||
|
||
public static class MailboxIdKeyTestContainer { | ||
public Map<MailboxId, String> map; | ||
|
||
public MailboxIdKeyTestContainer() { | ||
} | ||
|
||
public MailboxIdKeyTestContainer(Map<MailboxId, String> map) { | ||
this.map = map; | ||
} | ||
} | ||
|
||
public static class KeyWithoutToString implements MailboxId { | ||
private String value; | ||
|
||
public KeyWithoutToString(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String serialize() { | ||
return value; | ||
} | ||
|
||
public static class Factory implements MailboxId.Factory { | ||
|
||
@Override | ||
public MailboxId fromString(String serialized) { | ||
return new KeyWithoutToString(serialized); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.