added testcases for code coverage
Issue-ID: DMAAP-271
Change-Id: I6b84ffcb3ce402e0df2cd3f3a4e60f11dffd4a47
Signed-off-by: Sunil Unnava <su622b@att.com>
diff --git a/src/main/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBroker.java b/src/main/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBroker.java
index 9d53ef2..e7d777e 100644
--- a/src/main/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBroker.java
+++ b/src/main/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBroker.java
@@ -170,9 +170,7 @@
// make Kafka aware of the
// topic creation. (Otherwise, the topic is only partially
// created in ZK.)
- zkClient = new ZkClient(ConfigurationReader.getMainZookeeperConnectionString(), 10000, 10000,
- ZKStringSerializer$.MODULE$);
-
+ zkClient = ZkClientFactory.createZkClient();
log.info("Zookeeper client loaded successfully. Creating topic.");
AdminUtils.createTopic(zkClient, topic, partitions, replicas, new Properties());
} catch (kafka.common.TopicExistsException e) {
@@ -217,8 +215,7 @@
// Kafka aware of the
// topic creation. (Otherwise, the topic is only partially created
// in ZK.)
- zkClient = new ZkClient(ConfigurationReader.getMainZookeeperConnectionString(), 10000, 10000,
- ZKStringSerializer$.MODULE$);
+ zkClient = ZkClientFactory.createZkClient();
log.info("Zookeeper client loaded successfully. Deleting topic.");
AdminUtils.deleteTopic(zkClient, topic);
@@ -245,6 +242,8 @@
// throw new UnsupportedOperationException ( "We can't programmatically
// delete Kafka topics yet." );
}
+
+
//private final rrNvReadable fSettings;
private final ZkClient fZk;
diff --git a/src/main/java/com/att/nsa/cambria/beans/ZkClientFactory.java b/src/main/java/com/att/nsa/cambria/beans/ZkClientFactory.java
new file mode 100644
index 0000000..2aedb95
--- /dev/null
+++ b/src/main/java/com/att/nsa/cambria/beans/ZkClientFactory.java
@@ -0,0 +1,36 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 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 com.att.nsa.cambria.beans;
+
+import org.I0Itec.zkclient.ZkClient;
+
+import com.att.nsa.cambria.utils.ConfigurationReader;
+
+import kafka.utils.ZKStringSerializer$;
+
+public class ZkClientFactory {
+
+ public static ZkClient createZkClient(){
+ return new ZkClient(ConfigurationReader.getMainZookeeperConnectionString(), 10000, 10000,
+ ZKStringSerializer$.MODULE$);
+
+ }
+
+}
diff --git a/src/test/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBrokerTest.java b/src/test/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBrokerTest.java
new file mode 100644
index 0000000..eb28f67
--- /dev/null
+++ b/src/test/java/com/att/nsa/cambria/beans/DMaaPKafkaMetaBrokerTest.java
@@ -0,0 +1,247 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 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 com.att.nsa.cambria.beans;
+
+import static org.junit.Assert.assertTrue;
+
+import org.I0Itec.zkclient.ZkClient;
+import org.I0Itec.zkclient.exception.ZkNoNodeException;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import com.att.nsa.cambria.CambriaApiException;
+import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
+import com.att.nsa.configs.ConfigDb;
+import com.att.nsa.configs.ConfigDbException;
+import com.att.nsa.configs.ConfigPath;
+
+import kafka.admin.AdminUtils;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ AdminUtils.class, ZkClientFactory.class })
+public class DMaaPKafkaMetaBrokerTest {
+
+ @InjectMocks
+ private DMaaPKafkaMetaBroker dMaaPKafkaMetaBroker;
+
+ @Mock
+ private ZkClient zk;
+ @Mock
+ private ConfigDb configDb;
+ @Mock
+ ConfigPath fBaseTopicData;
+ @Mock
+ private ZkClient zkClient;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ PowerMockito.mockStatic(AdminUtils.class);
+ PowerMockito.mockStatic(ZkClientFactory.class);
+ PowerMockito.when(configDb.parse("/topics")).thenReturn(fBaseTopicData);
+
+ }
+
+ @Test
+ public void testGetAlltopics() {
+ try {
+ dMaaPKafkaMetaBroker.getAllTopics();
+ } catch (ConfigDbException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testcreateTopic() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenReturn(zkClient);
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
+ } catch (CambriaApiException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generatee.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testcreateTopic_wrongPartition() {
+ try {
+
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenReturn(zkClient);
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 0, 1, true);
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generatee.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testcreateTopic_wrongReplica() {
+ try {
+
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenReturn(zkClient);
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 0, true);
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generatee.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testcreateTopic_error1() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenThrow(new ZkNoNodeException());
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testcreateTopic_error2() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient())
+ .thenThrow(new kafka.admin.AdminOperationException("error"));
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testcreateTopic_error3() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenThrow(new kafka.common.TopicExistsException());
+ dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
+ } catch (CambriaApiException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (TopicExistsException e) {
+ assertTrue(true);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testDeleteTopic() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenReturn(zkClient);
+ dMaaPKafkaMetaBroker.deleteTopic("testtopic");
+ } catch (CambriaApiException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ assertTrue(true);
+
+ }
+
+ @Test
+ public void testDeleteTopic_error1() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenThrow(new ZkNoNodeException());
+ dMaaPKafkaMetaBroker.deleteTopic("testtopic");
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testDeleteTopic_error2() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient())
+ .thenThrow(new kafka.admin.AdminOperationException("error"));
+ dMaaPKafkaMetaBroker.deleteTopic("testtopic");
+ } catch (CambriaApiException e) {
+ assertTrue(true);
+ } catch (TopicExistsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testDeleteTopic_error3() {
+ try {
+ PowerMockito.when(ZkClientFactory.createZkClient()).thenThrow(new kafka.common.TopicExistsException());
+ dMaaPKafkaMetaBroker.deleteTopic("testtopic");
+ } catch (CambriaApiException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (TopicExistsException e) {
+ assertTrue(true);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/com/att/nsa/cambria/beans/JUnitTestSuite.java b/src/test/java/com/att/nsa/cambria/beans/JUnitTestSuite.java
index 8e00156..6c900a4 100644
--- a/src/test/java/com/att/nsa/cambria/beans/JUnitTestSuite.java
+++ b/src/test/java/com/att/nsa/cambria/beans/JUnitTestSuite.java
@@ -28,7 +28,7 @@
import org.apache.log4j.Logger;
@RunWith(Suite.class)
-@SuiteClasses({ ApiKeyBeanTest.class, ApiKeyBeanTest2.class, ApiKeyBeanTest3.class, ApiKeyBeanTest4.class, ApiKeyBeanTest5.class, ApiKeyBeanTest6.class,
+@SuiteClasses({ DMaaPKafkaMetaBrokerTest.class, ApiKeyBeanTest.class, ApiKeyBeanTest2.class, ApiKeyBeanTest3.class, ApiKeyBeanTest4.class, ApiKeyBeanTest5.class, ApiKeyBeanTest6.class,
DMaaPCambriaLimiterTest.class, DMaaPContextTest.class, DMaaPContextTest2.class,
DMaaPContextTest3.class,DMaaPContextTest4.class,DMaaPContextTest5.class,DMaaPContextTest6.class,
LogDetailsTest.class, LogDetailsTest2.class,LogDetailsTest3.class,LogDetailsTest4.class,LogDetailsTest5.class,LogDetailsTest6.class,