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