From bf4593cc3c4f3c433b2021af917123b93d9feb0e Mon Sep 17 00:00:00 2001
From: sawantmandar <ms5838@att.com>
Date: Wed, 13 Mar 2019 13:19:03 -0500
Subject: updating test cases for more coverage

Change-Id: Icd5457780d906ba633659557798da463dff9ce33
Signed-off-by: sawantmandar <ms5838@att.com>
Issue-ID: DMAAP-1094
---
 .../base/authorization/Cadi3AAFProviderTest.java   | 21 ++++-
 .../onap/dmaap/kafkaAuthorize/JUnitTestSuite.java  |  2 +-
 .../kafkaAuthorize/KafkaCustomAuthorizerTest.java  | 14 +++-
 .../kafkaAuthorize/PlainLoginModule1Test.java      | 64 +++++++++++++++
 .../dmaap/kafkaAuthorize/PlainSaslServer1Test.java | 95 +++++++++++++++++++++-
 5 files changed, 191 insertions(+), 5 deletions(-)
 create mode 100644 src/test/java/org/onap/dmaap/kafkaAuthorize/PlainLoginModule1Test.java

diff --git a/src/test/java/org/onap/dmaap/commonauth/kafka/base/authorization/Cadi3AAFProviderTest.java b/src/test/java/org/onap/dmaap/commonauth/kafka/base/authorization/Cadi3AAFProviderTest.java
index fbe1e59..70631dc 100644
--- a/src/test/java/org/onap/dmaap/commonauth/kafka/base/authorization/Cadi3AAFProviderTest.java
+++ b/src/test/java/org/onap/dmaap/commonauth/kafka/base/authorization/Cadi3AAFProviderTest.java
@@ -31,16 +31,28 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.onap.aaf.cadi.PropAccess;
+import org.onap.aaf.cadi.aaf.AAFPermission;
 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
+import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
+import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLur;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
 import org.powermock.modules.junit4.PowerMockRunner;
 
+
 @RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
 public class Cadi3AAFProviderTest {
 
 	public Cadi3AAFProvider cadi3AAFProvider;
 
 	@Mock
 	private static AAFAuthn<?> aafAuthn;
+	
+	@Mock
+	private static AAFConHttp aafCon;
+	
+	@Mock
+	private static AbsAAFLur<AAFPermission> aafLur;
 
 	@Mock
 	private static PropAccess access;
@@ -57,6 +69,13 @@ public class Cadi3AAFProviderTest {
 		assertFalse(cadi3AAFProvider.hasPermission("userID", "permission", "instance", "action"));
 	}
 
+	@Test
+	public void testHasAdminPermission() {
+		System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
+		cadi3AAFProvider = new Cadi3AAFProvider();
+		assertEquals(cadi3AAFProvider.hasPermission("admin", "permission", "instance", "action"), true);
+	}
+	
 	@Test(expected = NullPointerException.class)
 	public void tesAuthenticate() throws Exception {
 		System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
@@ -72,5 +91,5 @@ public class Cadi3AAFProviderTest {
 		when(aafAuthn.validate("admin", "password")).thenReturn("valid");
 		assertNull(cadi3AAFProvider.authenticate("admin", "password"));
 	}
-
+	
 }
diff --git a/src/test/java/org/onap/dmaap/kafkaAuthorize/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/kafkaAuthorize/JUnitTestSuite.java
index d52c8bc..9486662 100644
--- a/src/test/java/org/onap/dmaap/kafkaAuthorize/JUnitTestSuite.java
+++ b/src/test/java/org/onap/dmaap/kafkaAuthorize/JUnitTestSuite.java
@@ -27,7 +27,7 @@ import org.junit.runners.Suite.SuiteClasses;
 import org.apache.log4j.Logger;
 
 @RunWith(Suite.class)
-@SuiteClasses({ KafkaCustomAuthorizerTest.class, PlainSaslServer1Test.class })
+@SuiteClasses({ KafkaCustomAuthorizerTest.class, PlainSaslServer1Test.class, PlainLoginModule1Test.class })
 public class JUnitTestSuite {
 	private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class);
 
diff --git a/src/test/java/org/onap/dmaap/kafkaAuthorize/KafkaCustomAuthorizerTest.java b/src/test/java/org/onap/dmaap/kafkaAuthorize/KafkaCustomAuthorizerTest.java
index 3075327..4793acf 100644
--- a/src/test/java/org/onap/dmaap/kafkaAuthorize/KafkaCustomAuthorizerTest.java
+++ b/src/test/java/org/onap/dmaap/kafkaAuthorize/KafkaCustomAuthorizerTest.java
@@ -31,6 +31,7 @@ import org.mockito.MockitoAnnotations;
 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
 import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
@@ -40,6 +41,7 @@ import kafka.security.auth.Resource;
 import kafka.security.auth.ResourceType;
 
 @RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
 @PrepareForTest({ AuthorizationProviderFactory.class })
 public class KafkaCustomAuthorizerTest {
 	@Mock
@@ -57,7 +59,11 @@ public class KafkaCustomAuthorizerTest {
 	@Mock
 	AuthorizationProvider provider;
 
-	KafkaCustomAuthorizer authorizer = new KafkaCustomAuthorizer();
+	KafkaCustomAuthorizer authorizer;
+	
+	static {
+		System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
+	}
 
 	@Before
 	public void setUp() throws Exception {
@@ -77,17 +83,21 @@ public class KafkaCustomAuthorizerTest {
 
 	@Test
 	public void testAuthorizerSuccess() {
+
+
 		PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
 				.thenReturn(true);
+		authorizer = new KafkaCustomAuthorizer();
 		assertTrue(authorizer.authorize(arg0, arg1, arg2));
 
 	}
 
 	@Test
 	public void testAuthorizerFailure() {
-
+		System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
 		PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
 				.thenReturn(false);
+		authorizer = new KafkaCustomAuthorizer();
 		try {
 			authorizer.authorize(arg0, arg1, arg2);
 		} catch (Exception e) {
diff --git a/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainLoginModule1Test.java b/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainLoginModule1Test.java
new file mode 100644
index 0000000..fcb8b2e
--- /dev/null
+++ b/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainLoginModule1Test.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ *  ============LICENSE_START=======================================================
+ *  org.onap.dmaap
+ *  ================================================================================
+ *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *        http://www.apache.org/licenses/LICENSE-2.0
+*  
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *  ============LICENSE_END=========================================================
+ *  
+ *  
+ *******************************************************************************/
+package org.onap.dmaap.kafkaAuthorize;
+
+import javax.security.auth.login.LoginException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ PlainLoginModule1.class })
+public class PlainLoginModule1Test {
+
+	PlainLoginModule1 pLogin = new PlainLoginModule1();
+	
+	@Before
+	public void setUp() throws Exception {
+		MockitoAnnotations.initMocks(this);
+	}
+
+	@Test
+	public void testLogin() throws LoginException {
+		boolean b = pLogin.login();
+		
+		assert(b==true);
+	}
+	
+	@Test
+	public void testLogout() throws LoginException {
+		assert(pLogin.logout()==true);
+	}
+	
+	@Test
+	public void testCommit() throws LoginException {
+		assert(pLogin.commit()==true);
+	}
+	
+	@Test
+	public void testAbort() throws LoginException {
+		assert(pLogin.abort()==false);
+	}
+}
diff --git a/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainSaslServer1Test.java b/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainSaslServer1Test.java
index 8826f17..3e73062 100644
--- a/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainSaslServer1Test.java
+++ b/src/test/java/org/onap/dmaap/kafkaAuthorize/PlainSaslServer1Test.java
@@ -21,11 +21,11 @@
 package org.onap.dmaap.kafkaAuthorize;
 
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import javax.security.sasl.SaslException;
 
 import org.apache.kafka.common.security.JaasContext;
-import org.apache.kafka.common.security.plain.PlainSaslServer;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -65,4 +65,97 @@ public class PlainSaslServer1Test {
 		assertNotNull(sslServer.evaluateResponse(response.getBytes()));
 
 	}
+
+	@Test
+	public void testAuthenticationEmptyAuth() throws Exception {
+		String response = "\u0000username\u0000password";
+		PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
+		sslServer.evaluateResponse(response.getBytes());
+		assert(true);
+	}
+
+	@Test
+	public void testAuthenticationEmptyUser() throws Exception {
+		String response = "authorizationID\u0000\u0000password";
+		PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
+		
+		try {
+			sslServer.evaluateResponse(response.getBytes());
+		}
+		catch (SaslException e) {
+			assertTrue(e.getMessage().equalsIgnoreCase("Authentication failed: username not specified"));
+		}
+	}
+	@Test
+	public void testAuthenticationEmptyPassword() throws Exception {
+		String response = "authorizationID\u0000username\u0000";
+		PowerMockito.when(provider.authenticate("username", "password")).thenReturn(null);
+		try {
+			sslServer.evaluateResponse(response.getBytes());
+		}
+		catch (SaslException e) {
+			assertTrue(e.getMessage().equalsIgnoreCase("Invalid SASL/PLAIN response: expected 3 tokens, got 2"));
+		}
+	}
+	
+	@Test
+	public void testGetAuthorizationIdWithException() {
+		
+		try {
+		sslServer.getAuthorizationID();
+		}
+		catch (IllegalStateException ise) {
+			assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
+		}
+	}
+
+	@Test
+	public void testGetNegotiatedPropertyWithException() {
+		
+		try {
+		sslServer.getNegotiatedProperty("test");
+		}
+		catch (IllegalStateException ise) {
+			assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
+		}
+	}
+	
+	@Test
+	public void testIsComplete() {
+		
+		try {
+		sslServer.getNegotiatedProperty("test");
+		}
+		catch (IllegalStateException ise) {
+			assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
+		}
+		assert(true);
+	}	
+
+	
+	@Test
+	public void testUnwrap() {
+		try {
+		sslServer.unwrap(new byte[1], 0, 0);
+		}
+		catch (IllegalStateException ise) {
+			assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
+		} catch (SaslException e) {
+			e.printStackTrace();
+		}
+		assert(true);
+	}	
+	
+	@Test
+	public void testWrap() {
+		try {
+		sslServer.wrap(new byte[1], 0, 0);
+		}
+		catch (IllegalStateException ise) {
+			assertTrue(ise.getMessage().equalsIgnoreCase("Authentication exchange has not completed"));
+		} catch (SaslException e) {
+			e.printStackTrace();
+		}
+		assert(true);
+	}	
 }
-- 
cgit