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;
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.apache.amber.oauth2.common.utils.JSONUtils;
31 import org.apache.amber.oauth2.common.utils.OAuthUtils;
32 import org.codehaus.jettison.AbstractXMLStreamReader;
33 import org.codehaus.jettison.json.JSONObject;
34 import org.codehaus.jettison.mapped.MappedXMLStreamReader;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38
39
40
41
42
43 public class OAuthUtilsTest extends Assert {
44
45 @Test
46 public void testBuildJSON() throws Exception {
47 Map<String, Object> params = new HashMap<String, Object>();
48 params.put(OAuthError.OAUTH_ERROR, OAuthError.TokenResponse.INVALID_REQUEST);
49
50 String json = JSONUtils.buildJSON(params);
51
52 JSONObject obj = new JSONObject(json);
53
54 AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj);
55
56 assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
57 assertEquals(OAuthError.OAUTH_ERROR, reader.getName().getLocalPart());
58
59 assertEquals(OAuthError.TokenResponse.INVALID_REQUEST, reader.getText());
60 assertEquals(XMLStreamReader.CHARACTERS, reader.next());
61 assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
62 assertEquals(XMLStreamReader.END_DOCUMENT, reader.next());
63
64 }
65
66 @Test
67 public void testEncodeOAuthHeader() throws Exception {
68
69 Map<String, Object> entries = new HashMap<String, Object>();
70 entries.put("realm", "Some Example Realm");
71 entries.put("error", "invalid_token");
72
73 String header = OAuthUtils.encodeOAuthHeader(entries);
74 assertEquals("Bearer error=\"invalid_token\",realm=\"Some Example Realm\"", header);
75
76 }
77 }