1 package com.atlassian.marketplace.client.impl;
2
3 import java.net.URI;
4
5 import com.atlassian.fugue.Option;
6 import com.atlassian.marketplace.client.MpacException;
7 import com.atlassian.marketplace.client.api.EnumWithKey;
8 import com.atlassian.marketplace.client.encoding.InvalidFieldValue;
9 import com.atlassian.marketplace.client.encoding.MissingRequiredField;
10 import com.atlassian.marketplace.client.model.HtmlString;
11 import com.atlassian.marketplace.client.model.ReadOnly;
12
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15
16 import org.junit.Test;
17
18 import static com.atlassian.fugue.Option.none;
19 import static com.atlassian.fugue.Option.some;
20 import static com.atlassian.marketplace.client.model.HtmlString.html;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.equalTo;
23 import static org.junit.Assert.fail;
24
25 public class BasicTypesJsonTest extends BaseJsonTests
26 {
27 @Test
28 public void requiredPropertyIsDecoded() throws Exception
29 {
30 assertThat(decode("{\"x\":\"y\"}", HasRequiredProperty.class).x, equalTo("y"));
31 }
32
33 @Test
34 public void errorIfRequiredPropertyIsOmitted() throws Exception
35 {
36 try
37 {
38 decode("{\"xxx\":1}", HasRequiredProperty.class);
39 fail("expected exception");
40 }
41 catch (MpacException.InvalidResponseError e)
42 {
43 causeShouldBe(e, MissingRequiredField.class, some("missing required property \"x\" in HasRequiredProperty"));
44 }
45 }
46
47 @Test
48 public void requiredPropertyIsEncoded() throws Exception
49 {
50 HasRequiredProperty o = new HasRequiredProperty();
51 o.x = "y";
52 assertThat(encode(o), equalTo("{\"x\":\"y\"}"));
53 }
54
55 @Test
56 public void optionalPropertyIsDecoded() throws Exception
57 {
58 assertThat(decode("{\"x\":\"y\"}", HasOptionalProperty.class).x, equalTo(some("y")));
59 }
60
61 @Test
62 public void optionalPropertyCanBeOmitted() throws Exception
63 {
64 assertThat(decode("{\"xxx\":1}", HasOptionalProperty.class).x, equalTo(none(String.class)));
65 }
66
67 @Test
68 public void optionalPropertyIsEncodedIfSome() throws Exception
69 {
70 HasOptionalProperty o = new HasOptionalProperty();
71 o.x = some("y");
72 assertThat(encode(o), equalTo("{\"x\":\"y\"}"));
73 }
74
75 @Test
76 public void optionalPropertyIsNotEncodedIfNone() throws Exception
77 {
78 HasOptionalProperty o = new HasOptionalProperty();
79 o.x = none();
80 assertThat(encode(o), equalTo("{}"));
81 }
82
83 @Test
84 public void listPropertyIsDecoded() throws Exception
85 {
86 assertThat(decode("{\"xs\":[\"y\",\"z\"]}", HasListProperty.class).xs, equalTo(ImmutableList.of("y", "z")));
87 }
88
89 @Test
90 public void listPropertyIsEncoded() throws Exception
91 {
92 HasListProperty o = new HasListProperty();
93 o.xs = ImmutableList.of("y", "z");
94 assertThat(encode(o), equalTo("{\"xs\":[\"y\",\"z\"]}"));
95 }
96
97 @Test
98 public void uriPropertyIsDecoded() throws Exception
99 {
100 assertThat(decode("{\"x\":\"http://foo/bar\"}", HasUriProperty.class).x,
101 equalTo(URI.create("http://foo/bar")));
102 }
103
104 @Test
105 public void errorIfUriIsMalformed() throws Exception
106 {
107 try
108 {
109 decode("{\"x\":\"::\"}", HasUriProperty.class);
110 fail("expected error");
111 }
112 catch (MpacException.InvalidResponseError e)
113 {
114 causeShouldBe(e, InvalidFieldValue.class, some("\"::\" is not a valid value for URI"));
115 }
116 }
117
118 @Test
119 public void uriPropertyIsEncoded() throws Exception
120 {
121 HasUriProperty o = new HasUriProperty();
122 o.x = URI.create("http://foo/bar");
123 assertThat(encode(o), equalTo("{\"x\":\"http://foo/bar\"}"));
124 }
125
126 @Test
127 public void enumPropertyIsDecoded() throws Exception
128 {
129 assertThat(decode("{\"color\":\"blu\"}", HasEnumProperty.class).color, equalTo(MyEnum.BLUE));
130 }
131
132 @Test
133 public void errorIfEnumPropertyIsUnknownValue() throws Exception
134 {
135 try
136 {
137 assertThat(decode("{\"color\":\"blue\"}", HasEnumProperty.class).color, equalTo(MyEnum.BLUE));
138 fail("expected error");
139 }
140 catch (MpacException.InvalidResponseError e)
141 {
142 causeShouldBe(e, InvalidFieldValue.class, some("\"blue\" is not a valid value for MyEnum"));
143 }
144 }
145
146 @Test
147 public void enumPropertyIsEncoded() throws Exception
148 {
149 HasEnumProperty o = new HasEnumProperty();
150 o.color = MyEnum.GREEN;
151 assertThat(encode(o), equalTo("{\"color\":\"gryn\"}"));
152 }
153
154 @Test
155 public void htmlStringPropertyIsDecoded() throws Exception
156 {
157 assertThat(decode("{\"hs\":\"<b>hi</b>\"}", HasHtmlStringProperty.class).hs, equalTo(html("<b>hi</b>")));
158 }
159
160 @Test
161 public void htmlStringPropertyIsEncoded() throws Exception
162 {
163 HasHtmlStringProperty o = new HasHtmlStringProperty();
164 o.hs = html("<b>hi</b>");
165 assertThat(encode(o), equalTo("{\"hs\":\"<b>hi</b>\"}"));
166 }
167
168 @Test
169 public void uriMapPropertyIsDecoded() throws Exception
170 {
171 assertThat(decode("{\"uris\":{\"foo\":\"/url1\",\"bar\":\"/url2\"}}", HasUriMapProperty.class).uris,
172 equalTo(ImmutableMap.of("foo", URI.create("/url1"), "bar", URI.create("/url2"))));
173 }
174
175 @Test
176 public void uriMapPropertyIsEncoded() throws Exception
177 {
178 HasUriMapProperty o = new HasUriMapProperty();
179 o.uris = ImmutableMap.of("foo", URI.create("/url1"), "bar", URI.create("/url2"));
180 assertThat(encode(o), equalTo("{\"uris\":{\"foo\":\"/url1\",\"bar\":\"/url2\"}}"));
181 }
182
183 @Test
184 public void readOnlyPropertyIsDecoded() throws Exception
185 {
186 assertThat(decode("{\"n\":1,\"s\":\"x\"}", HasReadOnlyProperty.class).s, equalTo("x"));
187 }
188
189 @Test
190 public void readOnlyPropertyCanBeEncoded() throws Exception
191 {
192 HasReadOnlyProperty o = new HasReadOnlyProperty();
193 o.n = 1;
194 o.s = "x";
195 assertThat(encode(o, true), equalTo("{\"n\":1,\"s\":\"x\"}"));
196 }
197
198 @Test
199 public void readOnlyPropertyCanBeOmitted() throws Exception
200 {
201 HasReadOnlyProperty o = new HasReadOnlyProperty();
202 o.n = 1;
203 o.s = "x";
204 assertThat(encode(o, false), equalTo("{\"n\":1}"));
205 }
206
207 static final class HasRequiredProperty
208 {
209 String x;
210 }
211
212 static final class HasOptionalProperty
213 {
214 Option<String> x;
215 }
216
217 static final class HasListProperty
218 {
219 ImmutableList<String> xs;
220 }
221
222 static final class HasUriProperty
223 {
224 URI x;
225 }
226
227 static final class HasHtmlStringProperty
228 {
229 HtmlString hs;
230 }
231
232 public static enum MyEnum implements EnumWithKey
233 {
234 BLUE("blu"),
235 GREEN("gryn");
236
237 private final String key;
238
239 private MyEnum(String key)
240 {
241 this.key = key;
242 }
243
244 public String getKey()
245 {
246 return key;
247 }
248 }
249
250 static final class HasEnumProperty
251 {
252 MyEnum color;
253 }
254
255 static final class HasUriMapProperty
256 {
257 ImmutableMap<String, URI> uris;
258 }
259
260 static final class HasReadOnlyProperty
261 {
262 int n;
263 @ReadOnly String s;
264 }
265 }