2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import com.att.nsa.cambria.client.CambriaConsumer;
31 import java.io.IOException;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import org.apache.commons.collections4.IteratorUtils;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
39 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
40 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
41 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.CambriaConsumerWrapper;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapAafConsumerWrapper;
43 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapConsumerWrapper;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.DmaapDmeConsumerWrapper;
45 import org.powermock.reflect.Whitebox;
47 public class BusConsumerTest extends TopicTestBase {
56 public void testCambriaConsumerWrapper() {
57 // verify that different wrappers can be built
58 new CambriaConsumerWrapper(makeBuilder().build());
59 new CambriaConsumerWrapper(makeBuilder().useHttps(false).build());
60 new CambriaConsumerWrapper(makeBuilder().useHttps(true).build());
61 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
62 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
63 new CambriaConsumerWrapper(makeBuilder().apiKey(null).build());
64 new CambriaConsumerWrapper(makeBuilder().apiSecret(null).build());
65 new CambriaConsumerWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
66 new CambriaConsumerWrapper(makeBuilder().userName(null).build());
67 new CambriaConsumerWrapper(makeBuilder().password(null).build());
68 new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build());
72 public void testCambriaConsumerWrapperFetch() throws Exception {
73 CambriaConsumer inner = mock(CambriaConsumer.class);
74 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
75 when(inner.fetch()).thenReturn(lst);
77 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
78 Whitebox.setInternalState(cons, "consumer", inner);
80 assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));
82 // arrange to throw exception next time fetch is called
83 IOException ex = new IOException(EXPECTED);
84 when(inner.fetch()).thenThrow(ex);
86 cons.fetchTimeout = 10;
90 fail("missing exception");
92 } catch (IOException e) {
98 public void testCambriaConsumerWrapperClose() {
99 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
101 // set filter several times to cause different branches of close() to be executed
102 for (int count = 0; count < 3; ++count) {
104 cons.setFilter("close=" + count);
109 public void testCambriaConsumerWrapperSetFilter() {
110 // set filter several times to cause different branches to be executed
111 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
112 for (int count = 0; count < 3; ++count) {
113 cons.setFilter("set-filter=" + count);
118 public void testCambriaConsumerWrapperToString() {
119 assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
123 public void testDmaapConsumerWrapper() throws Exception {
124 // verify that different wrappers can be built
125 new DmaapAafConsumerWrapper(makeBuilder().build());
128 @Test(expected = IllegalArgumentException.class)
129 public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
130 new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
134 public void testDmaapConsumerWrapperFetch() throws Exception {
135 DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
136 MRConsumerImpl cons = mock(MRConsumerImpl.class);
138 dmaap.fetchTimeout = 5;
139 dmaap.consumer = cons;
142 when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
143 assertFalse(dmaap.fetch().iterator().hasNext());
145 // with messages, 200
146 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
147 MRConsumerResponse resp = new MRConsumerResponse();
148 resp.setResponseCode("200");
149 resp.setActualMessages(lst);
150 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
152 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
155 resp.setActualMessages(null);
156 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
158 assertFalse(dmaap.fetch().iterator().hasNext());
160 // with messages, NOT 200
161 resp.setResponseCode("400");
162 resp.setActualMessages(lst);
163 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
165 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
169 public void testDmaapConsumerWrapperClose() throws Exception {
170 new DmaapAafConsumerWrapper(makeBuilder().build()).close();
174 public void testDmaapConsumerWrapperToString() throws Exception {
175 assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
179 public void testDmaapAafConsumerWrapper() throws Exception {
180 // verify that different wrappers can be built
181 new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build());
182 new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build());
185 @Test(expected = IllegalArgumentException.class)
186 public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
188 * Unfortunately, the MR code intercepts this and throws an exception before the
189 * wrapper gets a chance to check it, thus this test does not improve the coverage
190 * for the constructor.
192 new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
196 public void testDmaapAafConsumerWrapperToString() throws Exception {
197 assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
201 public void testDmaapDmeConsumerWrapper() throws Exception {
202 // verify that different wrappers can be built
203 new DmaapDmeConsumerWrapper(makeBuilder().build());
204 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(true).build());
205 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(false).build());
206 new DmaapDmeConsumerWrapper(makeBuilder().additionalProps(null).build());
208 addProps.put(ROUTE_PROP, MY_ROUTE);
209 new DmaapDmeConsumerWrapper(makeBuilder().build());
210 new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
213 @Test(expected = IllegalArgumentException.class)
214 public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
215 new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
218 @Test(expected = IllegalArgumentException.class)
219 public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
220 new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
223 @Test(expected = IllegalArgumentException.class)
224 public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
225 new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
228 @Test(expected = IllegalArgumentException.class)
229 public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
230 new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
233 @Test(expected = IllegalArgumentException.class)
234 public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
235 new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());