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.parameters;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.amber.oauth2.common.OAuth;
28  import org.apache.amber.oauth2.common.message.OAuthMessage;
29  import org.apache.amber.oauth2.common.utils.DummyOAuthMessage;
30  import org.apache.amber.oauth2.common.utils.JSONUtils;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   *
36   *
37   *
38   */
39  public class JSONBodyParametersApplierTest {
40  
41      @Test
42      public void testApplyOAuthParameters() throws Exception {
43  
44          OAuthParametersApplier app = new JSONBodyParametersApplier();
45  
46          Map<String, Object> params = new HashMap<String, Object>();
47          params.put(OAuth.OAUTH_EXPIRES_IN, 3600l);
48          params.put(OAuth.OAUTH_ACCESS_TOKEN, "token_authz");
49          params.put(OAuth.OAUTH_CODE, "code_");
50          params.put(OAuth.OAUTH_SCOPE, "read");
51          params.put(OAuth.OAUTH_STATE, "state");
52          params.put("empty_param", "");
53          params.put("null_param", null);
54          params.put("", "some_value");
55          params.put(null, "some_value");
56  
57          OAuthMessage message = new DummyOAuthMessage("http://www.example.com/rd", 200);
58          app.applyOAuthParameters(message, params);
59  
60          String msgBody = message.getBody();
61          Map<String, Object> map = JSONUtils.parseJSON(msgBody);
62          Assert.assertEquals(3600, map.get(OAuth.OAUTH_EXPIRES_IN));
63          Assert.assertEquals("token_authz", map.get(OAuth.OAUTH_ACCESS_TOKEN));
64          Assert.assertEquals("code_", map.get(OAuth.OAUTH_CODE));
65          Assert.assertEquals("read", map.get(OAuth.OAUTH_SCOPE));
66          Assert.assertEquals("state", map.get(OAuth.OAUTH_STATE));
67          Assert.assertNull(map.get("empty_param"));
68          Assert.assertNull(map.get("null_param"));
69          Assert.assertNull(map.get(""));
70          Assert.assertNull(map.get(null));
71      }
72  }