Skip to content

Commit

Permalink
'Support' table
Browse files Browse the repository at this point in the history
  • Loading branch information
stevie400 committed Jun 8, 2018
1 parent 007d800 commit 87878f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import com.hubspot.algebra.ResultModule.Case;

public class ResultSerializer extends StdSerializer<Result<?, ?>> {
Expand Down Expand Up @@ -58,6 +59,8 @@ private static Object flattenValue(Object value) {
return new MapFlattener((Map<?, ?>) value);
} else if (value instanceof Multimap) {
return new MapFlattener(((Multimap<?, ?>) value).asMap());
} else if (value instanceof Table) {
return new MapFlattener(((Table<?, ?, ?>) value).rowMap());
} else {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,56 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;

public class ResultModuleTest {

private static final Result<TestBean, TestError> BEAN_OK = Result.ok(new TestBean("test"));
private static final String BEAN_OK_JSON = "{\"value\":\"test\",\"@result\":\"OK\"}";
private static final Result<TestBean, TestBean> BEAN_ERR = Result.err(new TestBean("ERROR"));
private static final String BEAN_ERR_JSON = "{\"value\":\"ERROR\",\"@result\":\"ERR\"}";

private static final Result<TestBean, TestError> CUSTOM_ENUM_ERR = Result.err(TestError.ERROR);
private static final String CUSTOM_ENUM_ERR_JSON = "{\"name\":\"ERROR\",\"@result\":\"ERR\"}";
private static final Result<TestBean, RawError> RAW_ENUM_ERR = Result.err(RawError.ERROR);
private static final String RAW_ENUM_ERR_JSON = "{\"@error\":\"ERROR\",\"@result\":\"ERR\"}";

private static final Result<String, String> STRING_OK = Result.ok("test");
private static final String STRING_OK_JSON = "{\"@ok\":\"test\",\"@result\":\"OK\"}";
private static final Result<String, String> STRING_ERR = Result.err("ERROR");
private static final String STRING_ERR_JSON = "{\"@error\":\"ERROR\",\"@result\":\"ERR\"}";

private static final Result<List<String>, List<String>> LIST_OK = Result.ok(Arrays.asList("val0", "val1"));
private static final String LIST_OK_JSON = "{\"@ok\":[\"val0\",\"val1\"],\"@result\":\"OK\"}";
private static final Result<List<String>, List<String>> LIST_ERR = Result.err(Arrays.asList("err0", "err1"));
private static final String LIST_ERR_JSON = "{\"@error\":[\"err0\",\"err1\"],\"@result\":\"ERR\"}";

private static final Result<Map<String, String>, Map<String, String>> MAP_OK = Result.ok(Collections.singletonMap("key", "value"));
private static final String MAP_OK_JSON = "{\"key\":\"value\",\"@result\":\"OK\"}";
private static final Result<Map<String, String>, Map<String, String>> MAP_ERR = Result.err(Collections.singletonMap("key", "value"));
private static final String MAP_ERR_JSON = "{\"key\":\"value\",\"@result\":\"ERR\"}";

private static final Result<Multimap<String, String>, Multimap<String, String>> MULTIMAP_OK = Result.ok(
ImmutableMultimap.<String, String> builder().putAll("key", "val0", "val1").build());
private static final String MULTIMAP_OK_JSON = "{\"key\":[\"val0\",\"val1\"],\"@result\":\"OK\"}";
private static final Result<Multimap<String, String>, Multimap<String, String>> MULTIMAP_ERR = Result.err(
ImmutableMultimap.<String, String> builder().putAll("key", "err0", "err1").build());
private static final String MULTIMAP_ERR_JSON = "{\"key\":[\"err0\",\"err1\"],\"@result\":\"ERR\"}";

private static final Result<Table<String, String, String>, Table<String, String, String>> TABLE_OK = Result.ok(
ImmutableTable.<String, String, String> builder().put("row", "column", "value").build());
private static final String TABLE_OK_JSON = "{\"row\":{\"column\":\"value\"},\"@result\":\"OK\"}";
private static final Result<Table<String, String, String>, Table<String, String, String>> TABLE_ERR = Result.err(
ImmutableTable.<String, String, String> builder().put("row", "column", "value").build());
private static final String TABLE_ERR_JSON = "{\"row\":{\"column\":\"value\"},\"@result\":\"ERR\"}";

private static ObjectMapper objectMapper;

@BeforeClass
Expand Down Expand Up @@ -120,6 +135,16 @@ public void itSerializesMultimapErr() throws Exception {
itSerializes(MULTIMAP_ERR, MULTIMAP_ERR_JSON);
}

@Test
public void itSerializesTableOk() throws Exception {
itSerializes(TABLE_OK, TABLE_OK_JSON);
}

@Test
public void itSerializesTableErr() throws Exception {
itSerializes(TABLE_ERR, TABLE_ERR_JSON);
}

@Test
public void itDeserializesBeanOk() throws Exception {
itDeserializes(
Expand Down Expand Up @@ -229,6 +254,24 @@ public void itDeserializesMultimapErr() throws Exception {
);
}

@Test(expected = JsonMappingException.class)
public void itDeserializesTableOk() throws Exception {
itDeserializes(
TABLE_OK_JSON,
new TypeReference<Result<Table<String, String, String>, Table<String, String, String>>>(){},
TABLE_OK
);
}

@Test(expected = JsonMappingException.class)
public void itDeserializesTableErr() throws Exception {
itDeserializes(
TABLE_ERR_JSON,
new TypeReference<Result<Table<String, String, String>, Table<String, String, String>>>(){},
TABLE_ERR
);
}

private void itSerializes(Result<?, ?> result, String expectedJson) throws JsonProcessingException {
assertThat(objectMapper.writeValueAsString(result)).isEqualTo(expectedJson);
}
Expand Down

0 comments on commit 87878f5

Please sign in to comment.