[DMAAP-MR] Get topics from kafka option
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / mr / cambria / beans / DMaaPKafkaMetaBrokerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.dmaap.mr.cambria.beans;
21
22 import static org.junit.Assert.assertTrue;
23
24 import com.att.nsa.configs.ConfigDb;
25 import com.att.nsa.configs.ConfigDbException;
26 import com.att.nsa.configs.ConfigPath;
27 import java.util.Properties;
28 import org.mockito.ArgumentMatchers;
29 import org.I0Itec.zkclient.ZkClient;
30 import org.apache.kafka.clients.admin.AdminClient;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.dmaap.dmf.mr.CambriaApiException;
38 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
39 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker.KafkaTopic;
40 import org.onap.dmaap.dmf.mr.metabroker.Broker1.TopicExistsException;
41 import org.onap.dmaap.dmf.mr.metabroker.Topic;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({ AdminClient.class })
50 public class DMaaPKafkaMetaBrokerTest {
51
52         @Mock
53         private DMaaPKafkaMetaBroker dMaaPKafkaMetaBroker;
54         @Mock
55         private ZkClient fZk;
56         @Mock
57         private AdminClient fKafkaAdminClient;
58         @Mock
59         private ConfigDb configDb;
60         @Mock
61         ConfigPath fBaseTopicData;
62         @Mock
63         private ZkClient zkClient;
64         @Mock
65         Topic mockTopic;
66
67         @Before
68         public void setUp() {
69                 MockitoAnnotations.initMocks(this);
70                 PowerMockito.mockStatic(AdminClient.class);
71                 PowerMockito.when(AdminClient.create(ArgumentMatchers.any(Properties.class))).thenReturn(fKafkaAdminClient);
72                 PowerMockito.when(configDb.parse("/topics")).thenReturn(fBaseTopicData);
73         }
74
75         @Test
76         public void testBrokercreate() {
77                 DMaaPKafkaMetaBroker broker = new DMaaPKafkaMetaBroker();
78         }
79
80         @Test
81         public void testcreateTopicEntry() {
82                 try {
83                         KafkaTopic kafkaTopic = new KafkaTopic("topics", configDb, fBaseTopicData);
84                         dMaaPKafkaMetaBroker.createTopicEntry("name", "desc", "owner", true);
85                 } catch (Exception e) {
86                         assertTrue(true);
87                 }
88         }
89
90         @Test
91         public void testGetAlltopics() {
92                 try {
93                         dMaaPKafkaMetaBroker.getAllTopics();
94                 } catch (ConfigDbException e) {
95                         // TODO Auto-generated catch block
96                         e.printStackTrace();
97                 }
98         }
99
100         @Test
101         public void testcreateTopic() {
102                 try {
103                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
104                 } catch (CambriaApiException e) {
105                         // TODO Auto-generated catch block
106                         e.printStackTrace();
107                 } catch (TopicExistsException e) {
108                         // TODO Auto-generated catch block
109                         e.printStackTrace();
110                 } catch (Exception e) {
111                         // TODO Auto-generatee.printStackTrace();
112                 }
113
114         }
115
116         @Test
117         public void testcreateTopic_wrongPartition() {
118                 try {
119
120                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 0, 1, true);
121                 } catch (CambriaApiException e) {
122                         assertTrue(true);
123                 } catch (TopicExistsException e) {
124                         // TODO Auto-generated catch block
125                         e.printStackTrace();
126                 } catch (Exception e) {
127                         // TODO Auto-generatee.printStackTrace();
128                 }
129
130         }
131
132         @Test
133         public void testcreateTopic_wrongReplica() {
134                 try {
135
136                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 0, true);
137                 } catch (CambriaApiException e) {
138                         assertTrue(true);
139                 } catch (TopicExistsException e) {
140                         // TODO Auto-generated catch block
141                         e.printStackTrace();
142                 } catch (Exception e) {
143                         // TODO Auto-generatee.printStackTrace();
144                 }
145
146         }
147
148         @Test
149         public void testcreateTopic_error1() {
150                 try {
151                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
152                 } catch (CambriaApiException e) {
153                         assertTrue(true);
154                 } catch (TopicExistsException e) {
155                         // TODO Auto-generated catch block
156                         e.printStackTrace();
157                 } catch (Exception e) {
158                         e.printStackTrace();
159                 }
160
161         }
162
163         @Test
164         public void testcreateTopic_error2() {
165                 try {
166                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
167                 } catch (CambriaApiException e) {
168                         assertTrue(true);
169                 } catch (TopicExistsException e) {
170                         // TODO Auto-generated catch block
171                         e.printStackTrace();
172                 } catch (Exception e) {
173                         e.printStackTrace();
174                 }
175         }
176
177         @Test
178         public void testcreateTopic_error3() {
179                 try {
180                         dMaaPKafkaMetaBroker.createTopic("testtopic", "testtopic", "admin", 1, 1, true);
181                 } catch (CambriaApiException e) {
182                         // TODO Auto-generated catch block
183                         e.printStackTrace();
184                 } catch (TopicExistsException e) {
185                         assertTrue(true);
186
187                 } catch (Exception e) {
188                         e.printStackTrace();
189                 }
190
191         }
192
193         @Test
194         public void testDeleteTopic() {
195                 try {
196                         dMaaPKafkaMetaBroker.deleteTopic("testtopic");
197                 } catch (CambriaApiException e) {
198                         // TODO Auto-generated catch block
199                         e.printStackTrace();
200                 } catch (TopicExistsException e) {
201                         // TODO Auto-generated catch block
202                         e.printStackTrace();
203                 } catch (Exception e) {
204                         e.printStackTrace();
205                 }
206                 assertTrue(true);
207
208         }
209
210         @Test
211         public void testDeleteTopic_error1() {
212                 try {
213                         dMaaPKafkaMetaBroker.deleteTopic("testtopic");
214                 } catch (CambriaApiException e) {
215                         assertTrue(true);
216                 } catch (TopicExistsException e) {
217                         // TODO Auto-generated catch block
218                         e.printStackTrace();
219                 } catch (Exception e) {
220                         e.printStackTrace();
221                 }
222
223         }
224
225         @Test
226         public void testDeleteTopic_error2() {
227                 try {
228                         dMaaPKafkaMetaBroker.deleteTopic("testtopic");
229                 } catch (CambriaApiException e) {
230                         assertTrue(true);
231                 } catch (TopicExistsException e) {
232                         // TODO Auto-generated catch block
233                         e.printStackTrace();
234                 } catch (Exception e) {
235                         e.printStackTrace();
236                 }
237
238         }
239
240         @Test
241         public void testDeleteTopic_error3() {
242                 try {
243                         dMaaPKafkaMetaBroker.deleteTopic("testtopic");
244                 } catch (CambriaApiException e) {
245                         // TODO Auto-generated catch block
246                         e.printStackTrace();
247                 } catch (TopicExistsException e) {
248                         assertTrue(true);
249                 } catch (Exception e) {
250                         e.printStackTrace();
251                 }
252
253         }
254
255 }