ef4d5a0030eb3d50c3d69c280c34248fcfbac748
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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 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;
46
47 public class BusConsumerTest extends BusTopicTestBase {
48
49     @Before
50     public void setUp() {
51         super.setUp();
52     }
53
54     @Test
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());
68     }
69
70     @Test
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);
75
76         CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
77         Whitebox.setInternalState(cons, "consumer", inner);
78
79         assertEquals(lst, IteratorUtils.toList(cons.fetch().iterator()));
80
81         // arrange to throw exception next time fetch is called
82         IOException ex = new IOException(EXPECTED);
83         when(inner.fetch()).thenThrow(ex);
84
85         cons.fetchTimeout = 10;
86
87         try {
88             cons.fetch();
89             fail("missing exception");
90
91         } catch (IOException | InterruptedException e) {
92             assertEquals(ex, e);
93         }
94     }
95
96     @Test
97     public void testCambriaConsumerWrapperClose() throws Exception {
98         CambriaConsumerWrapper cons = new CambriaConsumerWrapper(builder.build());
99
100         // set filter several times to cause different branches of close() to be executed
101         for (int count = 0; count < 3; ++count) {
102             cons.close();
103             cons.setFilter("close=" + count);
104         }
105     }
106
107     @Test
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);
113         }
114     }
115
116     @Test
117     public void testCambriaConsumerWrapperToString() {
118         assertNotNull(new CambriaConsumerWrapper(makeBuilder().build()).toString());
119     }
120
121     @Test
122     public void testDmaapConsumerWrapper() throws Exception {
123         // verify that different wrappers can be built
124         new DmaapAafConsumerWrapper(makeBuilder().build());
125     }
126
127     @Test(expected = IllegalArgumentException.class)
128     public void testDmaapConsumerWrapper_InvalidTopic() throws Exception {
129         new DmaapAafConsumerWrapper(makeBuilder().topic(null).build());
130     }
131
132     @Test
133     public void testDmaapConsumerWrapperFetch() throws Exception {
134         DmaapAafConsumerWrapper dmaap = new DmaapAafConsumerWrapper(makeBuilder().build());
135         MRConsumerImpl cons = mock(MRConsumerImpl.class);
136
137         dmaap.fetchTimeout = 5;
138         dmaap.consumer = cons;
139
140         // null return
141         when(cons.fetchWithReturnConsumerResponse()).thenReturn(null);
142         assertFalse(dmaap.fetch().iterator().hasNext());
143
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);
150
151         assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
152
153         // null messages
154         resp.setActualMessages(null);
155         when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
156
157         assertFalse(dmaap.fetch().iterator().hasNext());
158
159         // with messages, NOT 200
160         resp.setResponseCode("400");
161         resp.setActualMessages(lst);
162         when(cons.fetchWithReturnConsumerResponse()).thenReturn(resp);
163
164         assertEquals(lst, IteratorUtils.toList(dmaap.fetch().iterator()));
165     }
166
167     @Test
168     public void testDmaapConsumerWrapperClose() throws Exception {
169         new DmaapAafConsumerWrapper(makeBuilder().build()).close();
170     }
171
172     @Test
173     public void testDmaapConsumerWrapperToString() throws Exception {
174         assertNotNull(new DmaapConsumerWrapper(makeBuilder().build()) {}.toString());
175     }
176
177     @Test
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());
182     }
183
184     @Test(expected = IllegalArgumentException.class)
185     public void testDmaapAafConsumerWrapper_InvalidServers() throws Exception {
186         /*
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.
190          */
191         new DmaapAafConsumerWrapper(makeBuilder().servers(Collections.emptyList()).build());
192     }
193
194     @Test
195     public void testDmaapAafConsumerWrapperToString() throws Exception {
196         assertNotNull(new DmaapAafConsumerWrapper(makeBuilder().build()).toString());
197     }
198
199     @Test
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());
206
207         addProps.put(ROUTE_PROP, MY_ROUTE);
208         new DmaapDmeConsumerWrapper(makeBuilder().build());
209         new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
210     }
211
212     @Test(expected = IllegalArgumentException.class)
213     public void testDmaapDmeConsumerWrapper_InvalidEnvironment() throws Exception {
214         new DmaapDmeConsumerWrapper(makeBuilder().environment(null).build());
215     }
216
217     @Test(expected = IllegalArgumentException.class)
218     public void testDmaapDmeConsumerWrapper_InvalidAft() throws Exception {
219         new DmaapDmeConsumerWrapper(makeBuilder().aftEnvironment(null).build());
220     }
221
222     @Test(expected = IllegalArgumentException.class)
223     public void testDmaapDmeConsumerWrapper_InvalidLat() throws Exception {
224         new DmaapDmeConsumerWrapper(makeBuilder().latitude(null).build());
225     }
226
227     @Test(expected = IllegalArgumentException.class)
228     public void testDmaapDmeConsumerWrapper_InvalidLong() throws Exception {
229         new DmaapDmeConsumerWrapper(makeBuilder().longitude(null).build());
230     }
231
232     @Test(expected = IllegalArgumentException.class)
233     public void testDmaapDmeConsumerWrapper_InvalidPartner() throws Exception {
234         new DmaapDmeConsumerWrapper(makeBuilder().partner(null).build());
235     }
236 }