2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
31 import com.att.nsa.cambria.client.CambriaConsumer;
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import org.apache.commons.collections4.IteratorUtils;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
40 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
41 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.CambriaConsumerWrapper;
43 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapAafConsumerWrapper;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapConsumerWrapper;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapDmeConsumerWrapper;
46 import org.powermock.reflect.Whitebox;
48 public class BusConsumerTest extends TopicTestBase {
57 public void testCambriaConsumerWrapper() {
58 // verify that different wrappers can be built
59 new CambriaConsumerWrapper(makeBuilder().build());
60 new CambriaConsumerWrapper(makeBuilder().useHttps(false).build());
61 new CambriaConsumerWrapper(makeBuilder().useHttps(true).build());
62 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
63 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
64 new CambriaConsumerWrapper(makeBuilder().apiKey(null).build());
65 new CambriaConsumerWrapper(makeBuilder().apiSecret(null).build());
66 new CambriaConsumerWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
67 new CambriaConsumerWrapper(makeBuilder().userName(null).build());
68 new CambriaConsumerWrapper(makeBuilder().password(null).build());
70 assertThatCode(() -> new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build()))
71 .doesNotThrowAnyException();
75 public void testCambriaConsumerWrapperFetch() throws Exception {
76 CambriaConsumer inner = mock(CambriaConsumer.class);
77 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
78 when(inner.fetch()).thenReturn(lst);
80 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
81 Whitebox.setInternalState(cons, "consumer", inner);
83 assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));
85 // arrange to throw exception next time fetch is called
86 IOException ex = new IOException(EXPECTED);
87 when(inner.fetch()).thenThrow(ex);
89 cons.fetchTimeout = 10;
93 fail("missing exception");
95 } catch (IOException e) {
101 public void testCambriaConsumerWrapperClose() {
102 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
104 // set filter several times to cause different branches of close() to be executed
105 for (int count = 0; count < 3; ++count) {
107 final int count2 = count;
108 assertThatCode(() -> cons.setFilter("close=" + count2)).doesNotThrowAnyException();
113 public void testCambriaConsumerWrapperSetFilter() {
114 // set filter several times to cause different branches to be executed
115 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
116 for (int count = 0; count < 3; ++count) {
117 final int count2 = count;
118 assertThatCode(() -> cons.setFilter("set-filter=" + count2)).doesNotThrowAnyException();
123 public void testCambriaConsumerWrapperToString() {
124 assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
128 public void testDmaapConsumerWrapper() throws Exception {
129 // verify that different wrappers can be built
130 assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build())).doesNotThrowAnyException();
133 @Test(expected = IllegalArgumentException.class)
134 public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
135 new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
139 public void testDmaapConsumerWrapperFetch() throws Exception {
140 DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
141 MRConsumerImpl cons = mock(MRConsumerImpl.class);
143 dmaap.fetchTimeout = 5;
144 dmaap.consumer = cons;
147 when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
148 assertFalse(dmaap.fetch().iterator().hasNext());
150 // with messages, 200
151 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
152 MRConsumerResponse resp = new MRConsumerResponse();
153 resp.setResponseCode("200");
154 resp.setActualMessages(lst);
155 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
157 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
160 resp.setActualMessages(null);
161 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
163 assertFalse(dmaap.fetch().iterator().hasNext());
165 // with messages, NOT 200
166 resp.setResponseCode("400");
167 resp.setActualMessages(lst);
168 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
170 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
174 public void testDmaapConsumerWrapperClose() throws Exception {
175 assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().build()).close()).doesNotThrowAnyException();
179 public void testDmaapConsumerWrapperToString() throws Exception {
180 assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
184 public void testDmaapAafConsumerWrapper() throws Exception {
185 // verify that different wrappers can be built
186 new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build());
187 assertThatCode(() -> new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build()))
188 .doesNotThrowAnyException();
191 @Test(expected = IllegalArgumentException.class)
192 public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
194 * Unfortunately, the MR code intercepts this and throws an exception before the
195 * wrapper gets a chance to check it, thus this test does not improve the coverage
196 * for the constructor.
198 new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
202 public void testDmaapAafConsumerWrapperToString() throws Exception {
203 assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
207 public void testDmaapDmeConsumerWrapper() throws Exception {
208 // verify that different wrappers can be built
209 new DmaapDmeConsumerWrapper(makeBuilder().build());
210 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(true).build());
211 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(false).build());
212 new DmaapDmeConsumerWrapper(makeBuilder().additionalProps(null).build());
214 addProps.put(ROUTE_PROP, MY_ROUTE);
215 new DmaapDmeConsumerWrapper(makeBuilder().build());
216 assertThatCode(() -> new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build()))
217 .doesNotThrowAnyException();
220 @Test(expected = IllegalArgumentException.class)
221 public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
222 new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
225 @Test(expected = IllegalArgumentException.class)
226 public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
227 new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
230 @Test(expected = IllegalArgumentException.class)
231 public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
232 new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
235 @Test(expected = IllegalArgumentException.class)
236 public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
237 new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
240 @Test(expected = IllegalArgumentException.class)
241 public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
242 new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());