-
Notifications
You must be signed in to change notification settings - Fork 169
/
PyishObjectMapperTest.java
189 lines (170 loc) · 6.02 KB
/
PyishObjectMapperTest.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.hubspot.jinjava.objects.serialization;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.OutputTooBigException;
import com.hubspot.jinjava.objects.collections.SizeLimitingPyMap;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
public class PyishObjectMapperTest {
@Test
public void itSerializesMapWithNullKeysAsEmptyString() {
Map<String, Object> map = new SizeLimitingPyMap(new HashMap<>(), 10);
map.put("foo", "bar");
map.put(null, "null");
assertThat(PyishObjectMapper.getAsPyishString(map))
.isEqualTo("{'': 'null', 'foo': 'bar'} ");
}
@Test
public void itSerializesMapEntrySet() {
SizeLimitingPyMap map = new SizeLimitingPyMap(new HashMap<>(), 10);
map.put("foo", "bar");
map.put("bar", ImmutableMap.of("foobar", new ArrayList<>()));
String result = PyishObjectMapper.getAsPyishString(map.items());
assertThat(result)
.isEqualTo("[fn:map_entry('bar', {'foobar': []} ), fn:map_entry('foo', 'bar')]");
}
@Test
public void itSerializesMapEntrySetWithLimit() {
SizeLimitingPyMap map = new SizeLimitingPyMap(new HashMap<>(), 10);
map.put("foo", "bar");
map.put("bar", ImmutableMap.of("foobar", new ArrayList<>()));
Jinjava jinjava = new Jinjava(
JinjavaConfig.newBuilder().withMaxOutputSize(10000).build()
);
try {
JinjavaInterpreter.pushCurrent(jinjava.newInterpreter());
String result = PyishObjectMapper.getAsPyishString(map.items());
assertThat(result)
.isEqualTo("[fn:map_entry('bar', {'foobar': []} ), fn:map_entry('foo', 'bar')]");
} finally {
JinjavaInterpreter.popCurrent();
}
}
@Test
public void itSerializesMapWithNullValues() {
Map<String, Object> map = new SizeLimitingPyMap(new HashMap<>(), 10);
map.put("foo", "bar");
map.put("foobar", null);
assertThat(PyishObjectMapper.getAsPyishString(map))
.isEqualTo("{'foobar': null, 'foo': 'bar'} ");
}
@Test
public void itLimitsDepth() {
final List<Object> original = new ArrayList<>();
List<Object> list = original;
for (int i = 0; i < 20; i++) {
List<Object> temp = new ArrayList<>();
list.add("abcdefghijklmnopqrstuvwxyz");
list.add(temp);
list = temp;
}
list.add("a");
list.add(original);
try {
Jinjava jinjava = new Jinjava(
JinjavaConfig.newBuilder().withMaxOutputSize(10000).build()
);
JinjavaInterpreter.pushCurrent(jinjava.newInterpreter());
assertThatThrownBy(() -> PyishObjectMapper.getAsPyishStringOrThrow(original))
.as("The string to be serialized is larger than the max output size")
.isInstanceOf(JsonMappingException.class)
.hasCauseInstanceOf(LengthLimitingJsonProcessingException.class)
.hasMessageContaining("Max length of 10000 chars reached");
assertThatThrownBy(() -> PyishObjectMapper.getAsPyishString(original))
.isInstanceOf(OutputTooBigException.class);
} finally {
JinjavaInterpreter.popCurrent();
}
}
@Test
public void itLimitsOutputSize() {
String input = RandomStringUtils.random(10002);
try {
Jinjava jinjava = new Jinjava(
JinjavaConfig.newBuilder().withMaxOutputSize(10000).build()
);
JinjavaInterpreter.pushCurrent(jinjava.newInterpreter());
assertThatThrownBy(() -> PyishObjectMapper.getAsPyishString(input))
.isInstanceOf(OutputTooBigException.class)
.hasMessageContaining("over limit of 10000 bytes");
} finally {
JinjavaInterpreter.popCurrent();
}
}
@Test
public void itSerializesToSnakeCaseAccessibleMap() {
assertThat(PyishObjectMapper.getAsPyishString(new Foo("bar")))
.isEqualTo("{'fooBar': 'bar'} |allow_snake_case");
}
@Test
public void itSerializesToSnakeCaseAccessibleMapWhenInMapEntry() {
assertThat(
PyishObjectMapper.getAsPyishString(
new AbstractMap.SimpleImmutableEntry<>("foo", new Foo("bar"))
)
)
.isEqualTo("fn:map_entry('foo', {'fooBar': 'bar'} |allow_snake_case)");
}
@Test
public void itDoesNotConvertToSnakeCaseMapWhenResultIsForOutput() {
Jinjava jinjava = new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
)
.build()
);
JinjavaInterpreter interpreter = jinjava.newInterpreter();
interpreter.getContext().put("foo", new Foo("bar"));
assertThat(interpreter.render("{{ foo }}")).isEqualTo("{'fooBar': 'bar'}");
}
@Test
public void itSerializesToSnakeCaseWhenLegacyOverrideIsSet() {
Jinjava jinjava = new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUsePyishObjectMapper(true)
.withUseSnakeCasePropertyNaming(true)
.build()
)
.build()
);
JinjavaInterpreter interpreter = jinjava.newInterpreter();
try {
JinjavaInterpreter.pushCurrent(interpreter);
interpreter.getContext().put("foo", new Foo("bar"));
assertThat(interpreter.render("{{ foo }}")).isEqualTo("{'foo_bar': 'bar'}");
} finally {
JinjavaInterpreter.popCurrent();
}
}
@Test
public void itSerializesOptional() {
assertThat(PyishObjectMapper.getAsPyishString(Optional.of("foo"))).isEqualTo("'foo'");
}
static class Foo {
private final String bar;
public Foo(String bar) {
this.bar = bar;
}
public String getFooBar() {
return bar;
}
}
}