ae07798d9d64fd88b65bc9934928f2b6455b2f34
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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.policy.common.endpoints.event.comm.bus.internal;
22
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;
29
30 import com.att.aft.dme2.internal.apache.commons.collections.IteratorUtils;
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.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;
46
47 public class BusConsumerTest extends TopicTestBase {
48
49     @Before
50     @Override
51     public void setUp() {
52         super.setUp();
53     }
54
55     @Test
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());
69     }
70
71     @Test
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);
76
77         CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
78         Whitebox.setInternalState(cons, "consumer", inner);
79
80         assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));
81
82         // arrange to throw exception next time fetch is called
83         IOException ex = new IOException(EXPECTED);
84         when(inner.fetch()).thenThrow(ex);
85
86         cons.fetchTimeout = 10;
87
88         try {
89             cons.fetch();
90             fail("missing exception");
91
92         } catch (IOException e) {
93             assertEquals(ex, e);
94
95         } catch (InterruptedException e) {
96             Thread.currentThread().interrupt();
97             assertEquals(ex, e);
98         }
99     }
100
101     @Test
102     public void testCambriaConsumerWrapperClose() {
103         CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
104
105         // set filter several times to cause different branches of close() to be executed
106         for (int count = 0; count < 3; ++count) {
107             cons.close();
108             cons.setFilter("close=" + count);
109         }
110     }
111
112     @Test
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             cons.setFilter("set-filter=" + count);
118         }
119     }
120
121     @Test
122     public void testCambriaConsumerWrapperToString() {
123         assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
124     }
125
126     @Test
127     public void testDmaapConsumerWrapper() throws Exception {
128         // verify that different wrappers can be built
129         new DmaapAafConsumerWrapper(makeBuilder().build());
130     }
131
132     @Test(expected = IllegalArgumentException.class)
133     public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
134         new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
135     }
136
137     @Test
138     public void testDmaapConsumerWrapperFetch() throws Exception {
139         DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
140         MRConsumerImpl cons = mock(MRConsumerImpl.class);
141
142         dmaap.fetchTimeout = 5;
143         dmaap.consumer = cons;
144
145         // null return
146         when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
147         assertFalse(dmaap.fetch().iterator().hasNext());
148
149         // with messages, 200
150         List<String> lst = Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
151         MRConsumerResponse resp = new MRConsumerResponse();
152         resp.setResponseCode("200");
153         resp.setActualMessages(lst);
154         when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
155
156         assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
157
158         // null messages
159         resp.setActualMessages(null);
160         when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
161
162         assertFalse(dmaap.fetch().iterator().hasNext());
163
164         // with messages, NOT 200
165         resp.setResponseCode("400");
166         resp.setActualMessages(lst);
167         when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
168
169         assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
170     }
171
172     @Test
173     public void testDmaapConsumerWrapperClose() throws Exception {
174         new DmaapAafConsumerWrapper(makeBuilder().build()).close();
175     }
176
177     @Test
178     public void testDmaapConsumerWrapperToString() throws Exception {
179         assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
180     }
181
182     @Test
183     public void testDmaapAafConsumerWrapper() throws Exception {
184         // verify that different wrappers can be built
185         new DmaapAafConsumerWrapper(makeBuilder().useHttps(true).build());
186         new DmaapAafConsumerWrapper(makeBuilder().useHttps(false).build());
187     }
188
189     @Test(expected = IllegalArgumentException.class)
190     public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
191         /*
192          * Unfortunately, the MR code intercepts this and throws an exception before the
193          * wrapper gets a chance to check it, thus this test does not improve the coverage
194          * for the constructor.
195          */
196         new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
197     }
198
199     @Test
200     public void testDmaapAafConsumerWrapperToString() throws Exception {
201         assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
202     }
203
204     @Test
205     public void testDmaapDmeConsumerWrapper() throws Exception {
206         // verify that different wrappers can be built
207         new DmaapDmeConsumerWrapper(makeBuilder().build());
208         new DmaapDmeConsumerWrapper(makeBuilder().useHttps(true).build());
209         new DmaapDmeConsumerWrapper(makeBuilder().useHttps(false).build());
210         new DmaapDmeConsumerWrapper(makeBuilder().additionalProps(null).build());
211
212         addProps.put(ROUTE_PROP, MY_ROUTE);
213         new DmaapDmeConsumerWrapper(makeBuilder().build());
214         new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
215     }
216
217     @Test(expected = IllegalArgumentException.class)
218     public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
219         new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
220     }
221
222     @Test(expected = IllegalArgumentException.class)
223     public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
224         new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
225     }
226
227     @Test(expected = IllegalArgumentException.class)
228     public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
229         new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
230     }
231
232     @Test(expected = IllegalArgumentException.class)
233     public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
234         new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
235     }
236
237     @Test(expected = IllegalArgumentException.class)
238     public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
239         new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
240     }
241 }