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