View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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  }