DMAAP-MR - Sonar bug fixes
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / mr / cambria / backends / kafka / KafkaConsumerCacheTest.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
21  package org.onap.dmaap.mr.cambria.backends.kafka;
22
23 import static org.junit.Assert.assertTrue;
24
25 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
26 import java.util.Collection;
27 import java.util.concurrent.ConcurrentHashMap;
28 import org.apache.curator.framework.CuratorFramework;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.dmaap.dmf.mr.backends.Consumer;
37 import org.onap.dmaap.dmf.mr.backends.MetricsSet;
38 import org.onap.dmaap.dmf.mr.backends.kafka.Kafka011Consumer;
39 import org.onap.dmaap.dmf.mr.backends.kafka.KafkaConsumerCache;
40 import org.onap.dmaap.dmf.mr.backends.kafka.KafkaConsumerCache.KafkaConsumerCacheException;
41
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class KafkaConsumerCacheTest {
45
46         @Mock
47         private ConcurrentHashMap<String, Kafka011Consumer> fConsumers;
48         @Mock
49         private MetricsSet fMetrics;
50         @Mock
51         private AJSCPropertiesMap ajscPropertiesMap;
52         @InjectMocks
53         private KafkaConsumerCache kafkaConsumerCache;
54
55         @Before
56         public void setUp() throws Exception {
57         }
58
59         @After
60         public void tearDown() throws Exception {
61         }
62
63         @Test
64         public void testSweep() {
65                 kafkaConsumerCache.sweep();
66                 Collection<? extends Consumer> cachedConsumers =
67                         kafkaConsumerCache.getConsumers();
68                 assertTrue(cachedConsumers.isEmpty());
69         }
70
71
72         // DOES NOT WORK
73         @Test
74         public void testStartCache() {
75
76                 /*
77                  * KafkaConsumerCache kafka = null;
78                  *
79                  * try { kafka = new KafkaConsumerCache("123", null);
80                  *
81                  * } catch (NoClassDefFoundError e) { try { kafka.startCache("DMAAP",
82                  * null); } catch (NullPointerException e1) { // TODO Auto-generated
83                  * catch block assertTrue(true); } catch (KafkaConsumerCacheException
84                  * e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
85                  */
86
87
88                 new CuratorFrameworkImpl();
89                 new MetricsSetImpl();
90                 KafkaConsumerCache kafka=null;
91                 try {
92                         kafka = new KafkaConsumerCache();
93                         kafka.setfApiId("1");
94                         kafka.startCache("DMAAP", null);
95                 } catch (NoClassDefFoundError e) {
96
97                 } catch (KafkaConsumerCacheException e) {
98                         // TODO Auto-generated catch block
99                         e.printStackTrace();
100                 }
101
102         }
103
104         @Test
105         public void testGetCuratorFramework() {
106
107                 CuratorFramework curator = new CuratorFrameworkImpl();
108                 new MetricsSetImpl();
109                 try {
110
111                 } catch (NoClassDefFoundError e) {
112
113                         KafkaConsumerCache.getCuratorFramework(curator);
114                 }
115
116         }
117
118         /*
119          * @Test public void testStopCache() {
120          *
121          * KafkaConsumerCache kafka = null; new CuratorFrameworkImpl(); new
122          * MetricsSetImpl(); try { kafka = new KafkaConsumerCache("123", null);
123          * kafka.stopCache(); } catch (NoClassDefFoundError e) {
124          *
125          * }
126          *
127          * }
128          */
129
130         @Test(expected= KafkaConsumerCacheException.class)
131         public void testGetConsumerThrowNoConnectionToCache() throws KafkaConsumerCacheException{
132                 KafkaConsumerCache kafka = new KafkaConsumerCache();
133                 kafka.getConsumerFor("testTopic", "CG1", "23");
134         }
135
136         @Test
137         public void testGetConsumerFor() {
138
139                 KafkaConsumerCache kafka = null;
140
141                 try {
142                         kafka = new KafkaConsumerCache();
143                         kafka.getConsumerFor("testTopic", "CG1", "23");
144                 } catch (NoClassDefFoundError e) {
145
146                 } catch (KafkaConsumerCacheException e) {
147                         // TODO Auto-generated catch block
148                         e.printStackTrace();
149                 }
150
151         }
152
153         @Test
154         public void testPutConsumerFor() {
155
156                 Kafka011Consumer consumer = null;
157                 KafkaConsumerCache kafka = null;
158
159                 try {
160                         kafka = new KafkaConsumerCache();
161
162                 } catch (NoClassDefFoundError e) {
163                         try {
164                                 kafka.putConsumerFor("testTopic", "CG1", "23", consumer);
165                         } catch (NullPointerException e1) {
166                                 // TODO Auto-generated catch block
167                                 assertTrue(true);
168                         } catch (KafkaConsumerCacheException e1) {
169                                 // TODO Auto-generated catch block
170                                 e1.printStackTrace();
171                         }
172                 }
173
174         }
175
176         @Test
177         public void testGetConsumers() {
178
179                 KafkaConsumerCache kafka = null;
180
181                 try {
182                         kafka = new KafkaConsumerCache();
183
184                 } catch (NoClassDefFoundError e) {
185                         try {
186                                 kafka.getConsumers();
187                         } catch (NullPointerException e1) {
188                                 // TODO Auto-generated catch block
189                                 assertTrue(true);
190                         }
191                 }
192
193         }
194
195         @Test
196         public void testDropAllConsumers() {
197
198                 KafkaConsumerCache kafka = null;
199                 try {
200                         kafka = new KafkaConsumerCache();
201
202                 } catch (NoClassDefFoundError e) {
203                         try {
204                                 kafka.dropAllConsumers();
205                         } catch (NullPointerException e1) {
206                                 // TODO Auto-generated catch block
207                                 assertTrue(true);
208                         }
209                 }
210
211         }
212
213         @Test
214         public void testSignalOwnership() {
215
216                 KafkaConsumerCache kafka = null;
217
218                 try {
219                         kafka = new KafkaConsumerCache();
220                  try {
221                         kafka.signalOwnership("testTopic", "CG1", "23");
222                 } catch (KafkaConsumerCacheException e) {
223                         assertTrue(true);
224                 }
225                 } catch (NoClassDefFoundError e) {}
226
227                 //
228         }
229
230         @Test
231         public void testDropConsumer() {
232
233                 KafkaConsumerCache kafka = null;
234
235                 try {
236                         kafka = new KafkaConsumerCache();
237                         // kafka.dropConsumer("testTopic", "CG1", "23");
238                 } catch (NoClassDefFoundError e) {
239                         try {
240                                 kafka.dropConsumer("testTopic", "CG1", "23");
241                         } catch (NullPointerException e1) {
242                                 // TODO Auto-generated catch block
243                                 assertTrue(true);
244                         }
245                 }
246
247         }
248
249
250 }