Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-oauth / src / test / java / org / onap / aaf / auth / oauth / JU_DirectOAuthTAF.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.oauth;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
29
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.TreeMap;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.layer.Result;
42 import org.onap.aaf.auth.oauth.facade.DirectIntrospect;
43 import org.onap.aaf.auth.rserv.TransFilter;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.PropAccess;
46 import org.onap.aaf.cadi.taf.TafResp;
47 import org.onap.aaf.misc.env.APIException;
48
49 import aafoauth.v2_0.Introspect;
50
51 public class JU_DirectOAuthTAF {
52
53     @Mock
54     private AuthzEnv env;
55
56     @Mock
57     private PropAccess access;
58
59     private Properties props = new Properties();
60
61     @Mock
62     private HttpServletRequest req;
63
64     private Map<String, String[]> parameterMap;
65     @Mock
66     private DirectIntrospect<Introspect> facade;
67     @Mock
68     private AuthzTrans trans;
69     @Mock
70     private Result<Introspect> ri;
71
72     @Before
73     public void setup() {
74         initMocks(this);
75         parameterMap = new TreeMap<String, String[]>();
76
77     }
78
79     @Test
80     public void testValidateWithoutSecret() throws APIException, CadiException {
81         parameterMap.put("client_id", new String[] { "Client1" });
82         // parameterMap.put("client_secret", new String[] { "Secret1" });
83         parameterMap.put("username", new String[] { "User1" });
84         parameterMap.put("password", new String[] { "Pass1" });
85         parameterMap.put("token", new String[] { "token1" });
86         when(env.access()).thenReturn(access);
87         when(access.getProperties()).thenReturn(props);
88         when(req.getContentType()).thenReturn("application/x-www-form-urlencoded");
89         when(req.getParameterMap()).thenReturn(parameterMap);
90
91         DirectOAuthTAF oAuthTaf = new DirectOAuthTAF(env, null, null);
92
93         TafResp validate = oAuthTaf.validate(null, req, null);
94
95         assertNotNull(validate);
96         assertEquals(validate.getAccess(), access);
97         assertEquals(validate.desc(), "client_id and client_secret required");
98     }
99
100     @Test
101     public void testValidateWithSecret() throws APIException, CadiException {
102         parameterMap.put("client_id", new String[] { "Client1" });
103         parameterMap.put("client_secret", new String[] { "Secret1" });
104         parameterMap.put("username", new String[] { "User1" });
105         parameterMap.put("password", new String[] { "Pass1" });
106         parameterMap.put("token", new String[] { "token1" });
107
108         when(env.access()).thenReturn(access);
109         when(access.getProperties()).thenReturn(props);
110         when(req.getContentType()).thenReturn("application/x-www-form-urlencoded");
111         when(req.getParameterMap()).thenReturn(parameterMap);
112         when(req.getAttribute(TransFilter.TRANS_TAG)).thenReturn(trans);
113         when(facade.mappedIntrospect(trans, "token1")).thenReturn(ri);
114
115         DirectOAuthTAF oAuthTaf = new DirectOAuthTAF(env, null, facade);
116
117         TafResp validate = oAuthTaf.validate(null, req, null);
118
119         assertNotNull(validate);
120         assertEquals(validate.getAccess(), access);
121         assertEquals(validate.desc(), ri.errorString());
122
123         assertNull(oAuthTaf.revalidate(null, null));
124         assertNotNull(oAuthTaf.directUserPass());
125     }
126
127 }