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