-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestNessObjectMapperProvider.java
More file actions
143 lines (124 loc) · 4.92 KB
/
Copy pathTestNessObjectMapperProvider.java
File metadata and controls
143 lines (124 loc) · 4.92 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* Licensed 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 com.nesscomputing.jackson;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.Stage;
import com.google.inject.util.Modules;
import com.nesscomputing.config.ConfigModule;
import org.junit.Assert;
import org.junit.Test;
public class TestNessObjectMapperProvider
{
private ObjectMapper getObjectMapper(final Module module)
{
return getObjectMapper(module, new AbstractModule() { @Override protected void configure() {} });
}
private ObjectMapper getObjectMapper(final Module module, final Module overrides)
{
final Injector injector = Guice.createInjector(Stage.PRODUCTION,
Modules.override(
ConfigModule.forTesting(),
new NessJacksonModule(),
new AbstractModule() {
@Override
public void configure() {
if (module != null) {
install(module);
}
}
}).with(overrides));
return injector.getInstance(ObjectMapper.class);
}
@Test
public void testSimple()
{
final ObjectMapper mapper = getObjectMapper(null);
Assert.assertNotNull(mapper);
}
// This test ensures that the GuavaModule is correctly installed
@Test
public void testMultisetDeserialization() throws Exception {
ObjectMapper mapper = getObjectMapper(new AbstractModule() {
@Override
protected void configure() {
NessObjectMapperBinder.bindJacksonModule(binder()).to(GuavaModule.class).in(Scopes.SINGLETON);
}
});
Multiset<String> set = mapper.readValue("[\"a\",\"a\"]", new TypeReference<HashMultiset<String>>() {});
Assert.assertEquals(ImmutableMultiset.of("a", "a"), set);
Multimap<String, String> map = mapper.readValue("{\"a\":[\"b\",\"c\"]}", new TypeReference<ImmutableMultimap<String, String>>() {});
Assert.assertEquals(ImmutableMultimap.of("a", "b", "a", "c"), map);
}
public static class DummyBean
{
private final String dummyValue;
@JsonCreator
public DummyBean(@JsonProperty final String dummyValue) {
this.dummyValue = dummyValue;
}
public String getDummyValue()
{
return dummyValue;
}
}
public static final class ExtendedDummyBean extends DummyBean
{
@JsonCreator
public ExtendedDummyBean(@JsonProperty final String dummyValue) {
super(dummyValue);
}
}
public static final class DummySerializer extends JsonSerializer<DummyBean>
{
@Override
public void serialize(DummyBean value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException
{
jgen.writeStartObject();
jgen.writeStringField("dummyValue", value.getDummyValue() + "world");
jgen.writeEndObject();
}
}
public static final class DummyDeserializer extends JsonDeserializer<DummyBean>
{
@Override
public DummyBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
{
return new DummyBean("hello, world");
}
}
}