1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.amber.oauth2.common.utils;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.xml.stream.XMLStreamReader;
28
29 import org.apache.amber.oauth2.common.error.OAuthError;
30 import org.codehaus.jettison.AbstractXMLStreamReader;
31 import org.codehaus.jettison.json.JSONObject;
32 import org.codehaus.jettison.mapped.MappedXMLStreamReader;
33 import org.junit.Assert;
34 import org.junit.Test;
35
36
37
38
39
40
41 public class JSONUtilsTest {
42
43 @Test
44 public void testBuildJSON() throws Exception {
45
46 Map<String, Object> params = new HashMap<String, Object>();
47 params.put(OAuthError.OAUTH_ERROR, OAuthError.TokenResponse.INVALID_REQUEST);
48
49 String json = JSONUtils.buildJSON(params);
50
51 JSONObject obj = new JSONObject(json);
52
53 AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj);
54
55 Assert.assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
56 Assert.assertEquals(OAuthError.OAUTH_ERROR, reader.getName().getLocalPart());
57
58 Assert.assertEquals(OAuthError.TokenResponse.INVALID_REQUEST, reader.getText());
59 Assert.assertEquals(XMLStreamReader.CHARACTERS, reader.next());
60 Assert.assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
61 Assert.assertEquals(XMLStreamReader.END_DOCUMENT, reader.next());
62
63 }
64
65 @Test
66 public void testParseJson() throws Exception {
67 Map<String, Object> jsonParams = new HashMap<String, Object>();
68 jsonParams.put("author", "John B. Smith");
69 jsonParams.put("year", "2000");
70
71 String s = JSONUtils.buildJSON(jsonParams);
72 Map<String, Object> map = JSONUtils.parseJSON(s);
73 Assert.assertEquals("John B. Smith", map.get("author"));
74 Assert.assertEquals("2000", map.get("year"));
75
76 }
77 }