Mass add newline package (Style Warnings)
[aaf/authz.git] / auth / auth-locate / src / test / java / org / onap / aaf / auth / locate / JU_BasicAuthCodeTest.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 \r
22 package org.onap.aaf.auth.locate;\r
23 \r
24 import static org.junit.Assert.assertEquals;\r
25 import static org.mockito.Mockito.verify;\r
26 import static org.mockito.Mockito.when;\r
27 import static org.mockito.MockitoAnnotations.initMocks;\r
28 \r
29 import javax.servlet.http.HttpServletRequest;\r
30 import javax.servlet.http.HttpServletResponse;\r
31 \r
32 import org.eclipse.jetty.http.HttpStatus;\r
33 import org.junit.Before;\r
34 import org.junit.Test;\r
35 import org.mockito.Answers;\r
36 import org.mockito.Mock;\r
37 import org.onap.aaf.auth.env.AuthzTrans;\r
38 import org.onap.aaf.auth.locate.facade.LocateFacade;\r
39 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;\r
40 import org.onap.aaf.cadi.principal.BasicPrincipal;\r
41 import org.onap.aaf.cadi.principal.X509Principal;\r
42 import org.onap.aaf.misc.env.LogTarget;\r
43 \r
44 public class JU_BasicAuthCodeTest {\r
45 \r
46     @Mock\r
47     AAFAuthn authn;\r
48 \r
49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
50     AuthzTrans trans;\r
51 \r
52     @Mock\r
53     HttpServletRequest req;\r
54 \r
55     @Mock\r
56     HttpServletResponse resp;\r
57 \r
58     @Mock\r
59     LogTarget error;\r
60 \r
61     @Mock\r
62     LocateFacade facade;\r
63 \r
64     @Mock\r
65     BasicPrincipal basicPrincipal;\r
66     @Mock\r
67     X509Principal x509Principal;\r
68 \r
69     @Before\r
70     public void setUp() throws Exception {\r
71         initMocks(this);\r
72     }\r
73 \r
74     @Test\r
75     public void testWithNullUserPrincipal() throws Exception {\r
76         BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
77         LocateCode locateCode = basicAuthCode.clone(facade, false);\r
78 \r
79         assertEquals(locateCode.desc(), basicAuthCode.desc());\r
80 \r
81         when(trans.getUserPrincipal()).thenReturn(null);\r
82         when(trans.error()).thenReturn(error);\r
83 \r
84         basicAuthCode.handle(trans, req, resp);\r
85     }\r
86 \r
87     @Test\r
88     public void testWithBasicUserPrincipal() throws Exception {\r
89         BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
90         LocateCode locateCode = basicAuthCode.clone(facade, false);\r
91 \r
92         assertEquals(locateCode.desc(), basicAuthCode.desc());\r
93 \r
94         when(trans.getUserPrincipal()).thenReturn(basicPrincipal);\r
95 \r
96         basicAuthCode.handle(trans, req, resp);\r
97 \r
98         verify(resp).setStatus(HttpStatus.OK_200);\r
99     }\r
100 \r
101     @Test\r
102     public void testWithX509UserPrincipal() throws Exception {\r
103         BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
104         LocateCode locateCode = basicAuthCode.clone(facade, false);\r
105 \r
106         assertEquals(locateCode.desc(), basicAuthCode.desc());\r
107 \r
108         when(trans.getUserPrincipal()).thenReturn(x509Principal);\r
109         when(req.getHeader("Authorization")).thenReturn("Basic 76//76");\r
110 \r
111         basicAuthCode.handle(trans, req, resp);\r
112 \r
113         verify(resp).setStatus(HttpStatus.FORBIDDEN_403);\r
114     }\r
115 \r
116 }\r