Improve code coverage for auth locate
[aaf/authz.git] / auth / auth-locate / src / test / java / org / onap / aaf / auth / locate / JU_BasicAuthCodeTest.java
diff --git a/auth/auth-locate/src/test/java/org/onap/aaf/auth/locate/JU_BasicAuthCodeTest.java b/auth/auth-locate/src/test/java/org/onap/aaf/auth/locate/JU_BasicAuthCodeTest.java
new file mode 100644 (file)
index 0000000..eea60eb
--- /dev/null
@@ -0,0 +1,115 @@
+/**\r
+ * ============LICENSE_START====================================================\r
+ * org.onap.aaf\r
+ * ===========================================================================\r
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
+ * ===========================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END====================================================\r
+ *\r
+ */\r
+package org.onap.aaf.auth.locate;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.mockito.Mockito.verify;\r
+import static org.mockito.Mockito.when;\r
+import static org.mockito.MockitoAnnotations.initMocks;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.eclipse.jetty.http.HttpStatus;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.mockito.Answers;\r
+import org.mockito.Mock;\r
+import org.onap.aaf.auth.env.AuthzTrans;\r
+import org.onap.aaf.auth.locate.facade.LocateFacade;\r
+import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;\r
+import org.onap.aaf.cadi.principal.BasicPrincipal;\r
+import org.onap.aaf.cadi.principal.X509Principal;\r
+import org.onap.aaf.misc.env.LogTarget;\r
+\r
+public class JU_BasicAuthCodeTest {\r
+\r
+       @Mock\r
+       AAFAuthn authn;\r
+\r
+       @Mock(answer = Answers.RETURNS_DEEP_STUBS)\r
+       AuthzTrans trans;\r
+\r
+       @Mock\r
+       HttpServletRequest req;\r
+\r
+       @Mock\r
+       HttpServletResponse resp;\r
+\r
+       @Mock\r
+       LogTarget error;\r
+\r
+       @Mock\r
+       LocateFacade facade;\r
+\r
+       @Mock\r
+       BasicPrincipal basicPrincipal;\r
+       @Mock\r
+       X509Principal x509Principal;\r
+\r
+       @Before\r
+       public void setUp() throws Exception {\r
+               initMocks(this);\r
+       }\r
+\r
+       @Test\r
+       public void testWithNullUserPrincipal() throws Exception {\r
+               BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
+               LocateCode locateCode = basicAuthCode.clone(facade, false);\r
+\r
+               assertEquals(locateCode.desc(), basicAuthCode.desc());\r
+\r
+               when(trans.getUserPrincipal()).thenReturn(null);\r
+               when(trans.error()).thenReturn(error);\r
+\r
+               basicAuthCode.handle(trans, req, resp);\r
+       }\r
+\r
+       @Test\r
+       public void testWithBasicUserPrincipal() throws Exception {\r
+               BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
+               LocateCode locateCode = basicAuthCode.clone(facade, false);\r
+\r
+               assertEquals(locateCode.desc(), basicAuthCode.desc());\r
+\r
+               when(trans.getUserPrincipal()).thenReturn(basicPrincipal);\r
+\r
+               basicAuthCode.handle(trans, req, resp);\r
+\r
+               verify(resp).setStatus(HttpStatus.OK_200);\r
+       }\r
+\r
+       @Test\r
+       public void testWithX509UserPrincipal() throws Exception {\r
+               BasicAuthCode basicAuthCode = new BasicAuthCode(authn, facade);\r
+               LocateCode locateCode = basicAuthCode.clone(facade, false);\r
+\r
+               assertEquals(locateCode.desc(), basicAuthCode.desc());\r
+\r
+               when(trans.getUserPrincipal()).thenReturn(x509Principal);\r
+               when(req.getHeader("Authorization")).thenReturn("Basic 76//76");\r
+\r
+               basicAuthCode.handle(trans, req, resp);\r
+\r
+               verify(resp).setStatus(HttpStatus.FORBIDDEN_403);\r
+       }\r
+\r
+}\r