aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusConsumerTest.java
blob: a95e773d7dd20dfc4f286a695aea82d3213af737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * ============LICENSE_START=======================================================
 * policy-endpoints
 * ================================================================================
 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
 * ================================================================================
 * 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.policy.common.endpoints.event.comm.bus.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.att.nsa.cambria.client.CambriaConsumer;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
import org.onap.dmaap.mr.client.response.MRConsumerResponse;
import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.CambriaConsumerWrapper;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapAafConsumerWrapper;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapConsumerWrapper;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapDmeConsumerWrapper;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.FetchingBusConsumer;
import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.KafkaConsumerWrapper;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.springframework.test.util.ReflectionTestUtils;

public class BusConsumerTest extends TopicTestBase {

    private static final int SHORT_TIMEOUT_MILLIS = 10;
    private static final int LONG_TIMEOUT_MILLIS = 3000;

    @Mock
    KafkaConsumer<String, String> mockedKafkaConsumer;

    @Before
    @Override
    public void setUp() {
        super.setUp();
        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void testFetchingBusConsumer() {
        // should not be negative
        var cons = new FetchingBusConsumerImpl(makeBuilder().fetchTimeout(-1).build());
        assertThat(cons.getSleepTime()).isEqualTo(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH);

        // should not be zero
        cons = new FetchingBusConsumerImpl(makeBuilder().fetchTimeout(0).build());
        assertThat(cons.getSleepTime()).isEqualTo(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH);

        // should not be too large
        cons = new FetchingBusConsumerImpl(
                        makeBuilder().fetchTimeout(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH + 100).build());
        assertThat(cons.getSleepTime()).isEqualTo(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH);

        // should not be what was specified
        cons = new FetchingBusConsumerImpl(makeBuilder().fetchTimeout(100).build());
        assertThat(cons.getSleepTime()).isEqualTo(100);
    }

    @Test
    public void testFetchingBusConsumerSleepAfterFetchFailure() throws InterruptedException {

        var cons = new FetchingBusConsumerImpl(makeBuilder().fetchTimeout(SHORT_TIMEOUT_MILLIS).build()) {

            private CountDownLatch started = new CountDownLatch(1);

            @Override
            protected void sleepAfterFetchFailure() {
                started.countDown();
                super.sleepAfterFetchFailure();
            }
        };

        // full sleep
        long tstart = System.currentTimeMillis();
        cons.sleepAfterFetchFailure();
        assertThat(System.currentTimeMillis() - tstart).isGreaterThanOrEqualTo(SHORT_TIMEOUT_MILLIS);

        // close while sleeping - sleep should halt prematurely
        cons.fetchTimeout = LONG_TIMEOUT_MILLIS;
        cons.started = new CountDownLatch(1);
        Thread thread = new Thread(cons::sleepAfterFetchFailure);
        tstart = System.currentTimeMillis();
        thread.start();
        cons.started.await();
        cons.close();
        thread.join();
        assertThat(System.currentTimeMillis() - tstart).isLessThan(LONG_TIMEOUT_MILLIS);

        // interrupt while sleeping - sleep should halt prematurely
        cons.fetchTimeout = LONG_TIMEOUT_MILLIS;
        cons.started = new CountDownLatch(1);
        thread = new Thread(cons::sleepAfterFetchFailure);
        tstart = System.currentTimeMillis();
        thread.start();
        cons.started.await();
        thread.interrupt();
        thread.join();
        assertThat(System.currentTimeMillis() - tstart).isLessThan(LONG_TIMEOUT_MILLIS);
    }

    @Test
    public void testCambriaConsumerWrapper() {
        // verify that different wrappers can be built
        new CambriaConsumerWrapper(makeBuilder().build());
        new CambriaConsumerWrapper(makeBuilder().useHttps(false).build());
        new CambriaConsumerWrapper(makeBuilder().useHttps(true).build());
        new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
        new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
        new CambriaConsumerWrapper(makeBuilder().apiKey(null).build());
        new CambriaConsumerWrapper(makeBuilder().apiSecret(null).build());
        new CambriaConsumerWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
        new CambriaConsumerWrapper(makeBuilder().userName(null).build());
        new CambriaConsumerWrapper(makeBuilder().password(null).build());

        assertThatCode(() -> new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build()))
                        .doesNotThrowAnyException();
    }

    @Test
    public void testCambriaConsumerWrapperFetch() throws Exception {
        CambriaConsumer inner = mock(CambriaConsumer.class);
        List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
        when(inner.fetch()).thenReturn(lst);

        CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
        ReflectionTestUtils.setField(cons, "consumer", inner);

        assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));

        // arrange to throw exception next time fetch is called
        IOException ex = new IOException(EXPECTED);
        when(inner.fetch()).thenThrow(ex);

        cons.fetchTimeout = 10;

        try {
            cons.fetch();
            fail("missing exception");

        } catch (IOException e) {
            assertEquals(ex, e);
        }
    }

    @Test
    public void testCambriaConsumerWrapperClose() {
        CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
        assertThatCode(cons::close).doesNotThrowAnyException();
    }

    @Test
    public void testCambriaConsumerWrapperToString() {
        assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
    }

    @Test
    public void testDmaapConsumerWrapper() {
        // verify that different wrappers can be built
        assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build())).doesNotThrowAnyException();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
        new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
    }

    @Test
    public void testDmaapConsumerWrapperFetch() throws Exception {
        DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
        MRConsumerImpl cons = mock(MRConsumerImpl.class);

        dmaap.fetchTimeout = 5;
        dmaap.consumer = cons;

        // null return
        when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
        assertFalse(dmaap.fetch().iterator().hasNext());

        // with messages, 200
        List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
        MRConsumerResponse resp = new MRConsumerResponse();
        resp.setResponseCode("200");
        resp.setActualMessages(lst);
        when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);

        assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));

        // null messages
        resp.setActualMessages(null);
        when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);

        assertFalse(dmaap.fetch().iterator().hasNext());

        // with messages, NOT 200
        resp.setResponseCode("400");
        resp.setActualMessages(lst);
        when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);

        assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
    }

    @Test
    public void testDmaapConsumerWrapperClose() {
        assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build()).close()).doesNotThrowAnyException();
    }

    @Test
    public void testDmaapConsumerWrapperToString() throws Exception {
        assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
    }

    @Test
    public void testDmaapAafConsumerWrapper() throws Exception {
        // verify that different wrappers can be built
        new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build());
        assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build()))
                        .doesNotThrowAnyException();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
        /*
         * Unfortunately, the MR code intercepts this and throws an exception before the
         * wrapper gets a chance to check it, thus this test does not improve the coverage
         * for the constructor.
         */
        new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
    }

    @Test
    public void testDmaapAafConsumerWrapperToString() throws Exception {
        assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
    }

    @Test
    public void testDmaapDmeConsumerWrapper() throws Exception {
        // verify that different wrappers can be built
        new DmaapDmeConsumerWrapper(makeBuilder().build());
        new DmaapDmeConsumerWrapper(makeBuilder().useHttps(true).build());
        new DmaapDmeConsumerWrapper(makeBuilder().useHttps(false).build());
        new DmaapDmeConsumerWrapper(makeBuilder().additionalProps(null).build());

        addProps.put(ROUTE_PROP, MY_ROUTE);
        new DmaapDmeConsumerWrapper(makeBuilder().build());
        assertThatCode(() -> new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build()))
                        .doesNotThrowAnyException();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
        new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
        new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
        new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
        new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
        new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
    }

    @Test
    public void testKafkaConsumerWrapper() {
        // verify that different wrappers can be built
        assertThatCode(() -> new KafkaConsumerWrapper(makeKafkaBuilder().build())).doesNotThrowAnyException();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testKafkaConsumerWrapper_InvalidTopic() {
        new KafkaConsumerWrapper(makeBuilder().topic(null).build());
    }

    @Test
    public void testKafkaConsumerWrapperFetch() {

        //Setup Properties for consumer
        Properties kafkaProps = new Properties();
        kafkaProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        kafkaProps.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test");
        kafkaProps.setProperty("enable.auto.commit", "true");
        kafkaProps.setProperty("auto.commit.interval.ms", "1000");
        kafkaProps.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
            "org.apache.kafka.common.serialization.StringDeserializer");
        kafkaProps.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
            "org.apache.kafka.common.serialization.StringDeserializer");
        kafkaProps.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        kafkaProps.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");

        KafkaConsumerWrapper kafka = new KafkaConsumerWrapper(makeKafkaBuilder().build());
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(kafkaProps);
        kafka.consumer = consumer;

        assertThrows(java.lang.IllegalStateException.class, () -> kafka.fetch().iterator().hasNext());
        consumer.close();
    }

    @Test
    public void testFetchNoMessages() throws IOException {
        KafkaConsumerWrapper kafkaConsumerWrapper = new KafkaConsumerWrapper(makeKafkaBuilder().build());
        kafkaConsumerWrapper.consumer = mockedKafkaConsumer;

        when(mockedKafkaConsumer.poll(any())).thenReturn(new ConsumerRecords<>(Collections.emptyMap()));

        Iterable<String> result = kafkaConsumerWrapper.fetch();

        verify(mockedKafkaConsumer, times(1)).poll(any());

        assertThat(result != null);

        assertThat(!result.iterator().hasNext());

        mockedKafkaConsumer.close();
    }

    @Test
    public void testFetchWithMessages() {
        // Setup
        KafkaConsumerWrapper kafkaConsumerWrapper = new KafkaConsumerWrapper(makeKafkaBuilder().build());
        kafkaConsumerWrapper.consumer = mockedKafkaConsumer;

        ConsumerRecord<String, String> record = new ConsumerRecord<>("my-effective-topic", 0, 0, "key", "value");
        Map<TopicPartition, List<ConsumerRecord<String, String>>> recordsMap = new HashMap<>();
        recordsMap.put(new TopicPartition("my-effective-topic", 0), Collections.singletonList(record));
        ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(recordsMap);

        when(mockedKafkaConsumer.poll(any())).thenReturn(consumerRecords);

        Iterable<String> result = kafkaConsumerWrapper.fetch();

        verify(mockedKafkaConsumer, times(1)).poll(any());

        verify(mockedKafkaConsumer, times(1)).commitSync(any(Map.class));

        assertThat(result != null);

        assertThat(result.iterator().hasNext());

        assertThat(result.iterator().next().equals("value"));

        mockedKafkaConsumer.close();
    }

    @Test
    public void testFetchWithMessagesAndTraceparent() {
        // Setup
        KafkaConsumerWrapper kafkaConsumerWrapper = new KafkaConsumerWrapper(makeKafkaBuilder().build());
        kafkaConsumerWrapper.consumer = mockedKafkaConsumer;

        ConsumerRecord<String, String> record = new ConsumerRecord<>("my-effective-topic", 0, 0, "key", "value");
        record.headers().add(
                "traceparent",
                "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".getBytes(StandardCharsets.UTF_8)
        );

        Map<TopicPartition, List<ConsumerRecord<String, String>>> recordsMap = new HashMap<>();
        recordsMap.put(new TopicPartition("my-effective-topic", 0), Collections.singletonList(record));
        ConsumerRecords<String, String> consumerRecords = new ConsumerRecords<>(recordsMap);

        when(mockedKafkaConsumer.poll(any())).thenReturn(consumerRecords);

        Iterable<String> result = kafkaConsumerWrapper.fetch();

        verify(mockedKafkaConsumer, times(1)).poll(any());

        verify(mockedKafkaConsumer, times(1)).commitSync(any(Map.class));

        assertThat(result != null);

        assertThat(result.iterator().hasNext());

        assertThat(result.iterator().next().equals("value"));

        mockedKafkaConsumer.close();
    }


    @Test
    public void testKafkaConsumerWrapperClose() {
        assertThatCode(() -> new KafkaConsumerWrapper(makeKafkaBuilder().build()).close()).doesNotThrowAnyException();
    }

    @Test
    public void testKafkaConsumerWrapperToString() {
        assertNotNull(new KafkaConsumerWrapper(makeKafkaBuilder().build()) {}.toString());
    }

    private static class FetchingBusConsumerImpl extends FetchingBusConsumer {

        protected FetchingBusConsumerImpl(BusTopicParams busTopicParams) {
            super(busTopicParams);
        }

        @Override
        public Iterable<String> fetch() {
            return null;
        }
    }
}