2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.aft.dme2.internal.apache.commons.collections.IteratorUtils;
31 import com.att.nsa.cambria.client.CambriaConsumer;
32 import com.att.nsa.mr.client.impl.MRConsumerImpl;
33 import com.att.nsa.mr.client.response.MRConsumerResponse;
34 import java.io.IOException;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicTestBase;
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 BusTopicTestBase {
55 public void testCambriaConsumerWrapper() {
56 // verify that different wrappers can be built
57 new CambriaConsumerWrapper(makeBuilder().build());
58 new CambriaConsumerWrapper(makeBuilder().useHttps(false).build());
59 new CambriaConsumerWrapper(makeBuilder().useHttps(true).build());
60 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
61 new CambriaConsumerWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
62 new CambriaConsumerWrapper(makeBuilder().apiKey(null).build());
63 new CambriaConsumerWrapper(makeBuilder().apiSecret(null).build());
64 new CambriaConsumerWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
65 new CambriaConsumerWrapper(makeBuilder().userName(null).build());
66 new CambriaConsumerWrapper(makeBuilder().password(null).build());
67 new CambriaConsumerWrapper(makeBuilder().userName(null).password(null).build());
71 public void testCambriaConsumerWrapperFetch() throws Exception {
72 CambriaConsumer inner = mock(CambriaConsumer.class);
73 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
74 when(inner.fetch()).thenReturn(lst);
76 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
77 Whitebox.setInternalState(cons, "consumer", inner);
79 assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));
81 // arrange to throw exception next time fetch is called
82 IOException ex = new IOException(EXPECTED);
83 when(inner.fetch()).thenThrow(ex);
85 cons.fetchTimeout = 10;
89 fail("missing exception");
91 } catch (IOException | InterruptedException e) {
97 public void testCambriaConsumerWrapperClose() throws Exception {
98 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
100 // set filter several times to cause different branches of close() to be executed
101 for (int count = 0; count < 3; ++count) {
103 cons.setFilter("close=" + count);
108 public void testCambriaConsumerWrapperSetFilter() {
109 // set filter several times to cause different branches to be executed
110 CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
111 for (int count = 0; count < 3; ++count) {
112 cons.setFilter("set-filter=" + count);
117 public void testCambriaConsumerWrapperToString() {
118 assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
122 public void testDmaapConsumerWrapper() throws Exception {
123 // verify that different wrappers can be built
124 new DmaapAafConsumerWrapper(makeBuilder().build());
127 @Test(expected = IllegalArgumentException.class)
128 public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
129 new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
133 public void testDmaapConsumerWrapperFetch() throws Exception {
134 DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
135 MRConsumerImpl cons = mock(MRConsumerImpl.class);
137 dmaap.fetchTimeout = 5;
138 dmaap.consumer = cons;
141 when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
142 assertFalse(dmaap.fetch().iterator().hasNext());
144 // with messages, 200
145 List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
146 MRConsumerResponse resp = new MRConsumerResponse();
147 resp.setResponseCode("200");
148 resp.setActualMessages(lst);
149 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
151 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
154 resp.setActualMessages(null);
155 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
157 assertFalse(dmaap.fetch().iterator().hasNext());
159 // with messages, NOT 200
160 resp.setResponseCode("400");
161 resp.setActualMessages(lst);
162 when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
164 assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
168 public void testDmaapConsumerWrapperClose() throws Exception {
169 new DmaapAafConsumerWrapper(makeBuilder().build()).close();
173 public void testDmaapConsumerWrapperToString() throws Exception {
174 assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
178 public void testDmaapAafConsumerWrapper() throws Exception {
179 // verify that different wrappers can be built
180 new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build());
181 new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build());
184 @Test(expected = IllegalArgumentException.class)
185 public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
187 * Unfortunately, the MR code intercepts this and throws an exception before the
188 * wrapper gets a chance to check it, thus this test does not improve the coverage
189 * for the constructor.
191 new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
195 public void testDmaapAafConsumerWrapperToString() throws Exception {
196 assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
200 public void testDmaapDmeConsumerWrapper() throws Exception {
201 // verify that different wrappers can be built
202 new DmaapDmeConsumerWrapper(makeBuilder().build());
203 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(true).build());
204 new DmaapDmeConsumerWrapper(makeBuilder().useHttps(false).build());
205 new DmaapDmeConsumerWrapper(makeBuilder().additionalProps(null).build());
207 addProps.put(ROUTE_PROP, MY_ROUTE);
208 new DmaapDmeConsumerWrapper(makeBuilder().build());
209 new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
212 @Test(expected = IllegalArgumentException.class)
213 public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
214 new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
217 @Test(expected = IllegalArgumentException.class)
218 public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
219 new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
222 @Test(expected = IllegalArgumentException.class)
223 public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
224 new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
227 @Test(expected = IllegalArgumentException.class)
228 public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
229 new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
232 @Test(expected = IllegalArgumentException.class)
233 public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
234 new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());