[DMAAP-BC] Fix failing jenkins
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / service / TopicServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.dmaap.dbcapi.service;
22
23 import static com.google.common.collect.Iterables.getOnlyElement;
24 import static com.google.common.collect.Lists.newArrayList;
25 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
26 import static javax.ws.rs.core.Response.Status.OK;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertThat;
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.BDDMockito.given;
34 import static org.mockito.BDDMockito.then;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Mockito.verifyZeroInteractions;
37 import static org.onap.dmaap.dbcapi.model.ReplicationType.REPLICATION_GLOBAL_TO_FQDN;
38
39 import com.google.common.collect.ImmutableMap;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import javax.ws.rs.core.Response;
45 import org.hamcrest.BaseMatcher;
46 import org.hamcrest.Description;
47 import org.hamcrest.Matcher;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.runners.MockitoJUnitRunner;
54 import org.onap.dmaap.dbcapi.model.ApiError;
55 import org.onap.dmaap.dbcapi.model.MR_Client;
56 import org.onap.dmaap.dbcapi.model.Topic;
57 import org.onap.dmaap.dbcapi.util.DmaapConfig;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class TopicServiceTest {
61
62     private static final String TOPIC_FQTN = "topic_1";
63     private static final String GLOBAL_MR_HOST = "global.mr.host";
64     private TopicService topicService;
65     @Mock
66     private MR_ClientService clientService;
67     @Mock
68     private DmaapConfig dmaapConfig;
69     @Mock
70     private MR_ClusterService clusters;
71     @Mock
72     private DcaeLocationService locations;
73     @Mock
74     private MirrorMakerService bridge;
75     @Mock
76     private AafTopicSetupService aafTopicSetupService;
77
78     @BeforeClass
79     public static void setUpClass(){
80         System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
81
82     }
83
84     @Before
85     public void setUp() throws Exception {
86         given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST);
87         given(aafTopicSetupService.aafTopicSetup(any(Topic.class))).willReturn(new ApiError(200, "OK"));
88         given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(200, "OK"));
89         createTopicService();
90     }
91
92     @Test
93     public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
94
95         ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
96         topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
97
98         assertEquals(topics, topicService.getTopics());
99     }
100
101     @Test
102     public void getAllTopics_shouldReturnTopicsWithClients() {
103
104         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
105         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
106
107         List<Topic> allTopics = topicService.getAllTopics();
108
109         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
110         assertEquals(mrClients, getOnlyElement(allTopics).getClients());
111     }
112
113     @Test
114     public void getAllTopicsWithoutClients_shouldReturnNoClients() {
115
116         List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
117
118         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
119         assertNull(getOnlyElement(allTopics).getClients());
120         verifyZeroInteractions(clientService);
121     }
122
123     @Test
124     public void getAllTopics_shouldCacheClients() {
125
126         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
127         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
128
129         topicService.getAllTopics();
130         List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
131
132         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
133         assertEquals(mrClients, getOnlyElement(allTopics).getClients());
134     }
135
136     @Test
137     public void getTopic_shouldReturnTopicByFqtn() {
138
139         ApiError apiError = new ApiError();
140         Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
141
142         assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
143         assertEquals(OK.getStatusCode(), apiError.getCode());
144     }
145
146     @Test
147     public void getTopic_shouldReturnTopicWithMrClients() {
148
149         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
150         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
151
152         Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
153
154         assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
155         assertEquals(mrClients, topic.getClients());
156     }
157
158     @Test
159     public void getTopic_shouldReturnError() {
160
161         ApiError apiError = new ApiError();
162         Topic topic = topicService.getTopic("not_existing", apiError);
163
164         assertNull(topic);
165         assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
166     }
167
168     @Test
169     public void addTopic_shouldAddNewTopic() {
170         Topic newTopic = createTopic("");
171
172         ApiError apiError = new ApiError();
173         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
174
175         assertSame(newTopic, addedTopic);
176         assertEquals(OK.getStatusCode(), apiError.getCode());
177         assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
178     }
179
180     @Test
181     public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
182         Topic newTopic = createTopic("");
183
184         ApiError apiError = new ApiError();
185         Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
186         Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
187
188         assertNull(secondAddedTopic);
189         assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
190     }
191
192     @Test
193     public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
194         Topic newTopic = createTopic("");
195
196         ApiError apiError = new ApiError();
197         Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
198         Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
199
200         assertSame(addedTopic, secondAddedTopic);
201         assertEquals(OK.getStatusCode(), apiError.getCode());
202         assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
203     }
204
205
206     @Test
207     public void addTopic_shouldSetGlobalMrURL() {
208         Topic newTopic = createTopic(TOPIC_FQTN);
209         newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
210
211         ApiError apiError = new ApiError();
212         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
213
214         assertEquals(OK.getStatusCode(), apiError.getCode());
215         assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
216     }
217
218     @Test
219     public void addTopic_shouldReturnErrorWhenGlobalMrURLIsInvalid() {
220         given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn("invalid@host");
221         createTopicService();
222         Topic newTopic = createTopic(TOPIC_FQTN);
223         newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
224
225         ApiError apiError = new ApiError();
226         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
227
228         assertEquals(500, apiError.getCode());
229         assertNull(addedTopic);
230     }
231
232     @Test
233     public void removeTopic_shouldFailIfTopicDoesNotExist() {
234         ApiError apiError = new ApiError();
235
236         Topic removedTopic = topicService.removeTopic("not_existing_fqtn", apiError);
237
238         assertNull(removedTopic);
239         assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
240         assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
241     }
242
243     @Test
244     public void removeTopic_shouldExecuteAafCleanup() {
245         ApiError apiError = new ApiError();
246
247         Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
248
249         then(aafTopicSetupService).should().aafTopicCleanup(removedTopic);
250         assertEquals(OK.getStatusCode(), apiError.getCode());
251     }
252
253     @Test
254     public void removeTopic_shouldRemoveEachMrClientAssignedToTopic() {
255         ApiError apiError = new ApiError();
256         MR_Client mrClient = new MR_Client();
257         mrClient.setMrClientId("mrClientId");
258
259         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(newArrayList(mrClient));
260
261         topicService.removeTopic(TOPIC_FQTN, apiError);
262
263         then(clientService).should().removeMr_Client(mrClient.getMrClientId(), false, apiError);
264         assertEquals(OK.getStatusCode(), apiError.getCode());
265     }
266
267     @Test
268     public void removeTopic_shouldRemoveTopicFromCache() {
269         ApiError apiError = new ApiError();
270
271         topicService.removeTopic(TOPIC_FQTN, apiError);
272
273         assertTrue(topicService.getTopics().isEmpty());
274         assertEquals(OK.getStatusCode(), apiError.getCode());
275     }
276
277     @Test
278     public void removeTopic_shouldFailIfAafCleanupWasFailed() {
279         ApiError apiError = new ApiError();
280         given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(404, "sth went wrong"));
281
282         Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
283
284         assertNull(removedTopic);
285         assertEquals(404, apiError.getCode());
286         assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
287     }
288
289     private void createTopicService() {
290         Map<String, Topic> mrTopics = new HashMap<>();
291         mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN));
292         topicService = new TopicService(mrTopics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
293     }
294
295     private Topic createTopic(String fqtn) {
296         return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
297     }
298
299     public static Matcher<Topic> hasCorrectFqtn(final String fqtn) {
300         return new BaseMatcher<Topic>() {
301             public boolean matches(Object o) {
302                 return fqtn.equals(((Topic) o).getFqtn());
303             }
304
305             public void describeTo(Description description) {
306                 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);
307             }
308         };
309     }
310
311 }