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.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  }