2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
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;
37 import javax.ws.rs.core.Response;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
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;
59 @RunWith(MockitoJUnitRunner.class)
60 public class TopicServiceTest {
62 private static final String TOPIC_FQTN = "topic_1";
63 private static final String GLOBAL_MR_HOST = "global.mr.host";
64 private TopicService topicService;
66 private MR_ClientService clientService;
68 private DmaapConfig dmaapConfig;
70 private MR_ClusterService clusters;
72 private DcaeLocationService locations;
74 private MirrorMakerService bridge;
76 private AafTopicSetupService aafTopicSetupService;
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"));
87 public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
89 ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
90 topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
92 assertEquals(topics, topicService.getTopics());
96 public void getAllTopics_shouldReturnTopicsWithClients() {
98 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
99 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
101 List<Topic> allTopics = topicService.getAllTopics();
103 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
104 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
108 public void getAllTopicsWithoutClients_shouldReturnNoClients() {
110 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
112 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
113 assertNull(getOnlyElement(allTopics).getClients());
114 verifyZeroInteractions(clientService);
118 public void getAllTopics_shouldCacheClients() {
120 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
121 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
123 topicService.getAllTopics();
124 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
126 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
127 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
131 public void getTopic_shouldReturnTopicByFqtn() {
133 ApiError apiError = new ApiError();
134 Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
136 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
137 assertEquals(OK.getStatusCode(), apiError.getCode());
141 public void getTopic_shouldReturnTopicWithMrClients() {
143 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
144 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
146 Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
148 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
149 assertEquals(mrClients, topic.getClients());
153 public void getTopic_shouldReturnError() {
155 ApiError apiError = new ApiError();
156 Topic topic = topicService.getTopic("not_existing", apiError);
159 assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
163 public void addTopic_shouldAddNewTopic() {
164 Topic newTopic = createTopic("");
166 ApiError apiError = new ApiError();
167 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
169 assertSame(newTopic, addedTopic);
170 assertEquals(OK.getStatusCode(), apiError.getCode());
171 assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
175 public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
176 Topic newTopic = createTopic("");
178 ApiError apiError = new ApiError();
179 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
180 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
182 assertNull(secondAddedTopic);
183 assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
187 public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
188 Topic newTopic = createTopic("");
190 ApiError apiError = new ApiError();
191 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
192 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
194 assertSame(addedTopic, secondAddedTopic);
195 assertEquals(OK.getStatusCode(), apiError.getCode());
196 assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
201 public void addTopic_shouldSetGlobalMrURL() {
202 Topic newTopic = createTopic(TOPIC_FQTN);
203 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
205 ApiError apiError = new ApiError();
206 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
208 assertEquals(OK.getStatusCode(), apiError.getCode());
209 assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
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);
219 ApiError apiError = new ApiError();
220 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
222 assertEquals(500, apiError.getCode());
223 assertNull(addedTopic);
227 public void removeTopic_shouldFailIfTopicDoesNotExist() {
228 ApiError apiError = new ApiError();
230 Topic removedTopic = topicService.removeTopic("not_existing_fqtn", apiError);
232 assertNull(removedTopic);
233 assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
234 assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
238 public void removeTopic_shouldExecuteAafCleanup() {
239 ApiError apiError = new ApiError();
241 Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
243 then(aafTopicSetupService).should().aafTopicCleanup(removedTopic);
244 assertEquals(OK.getStatusCode(), apiError.getCode());
248 public void removeTopic_shouldRemoveEachMrClientAssignedToTopic() {
249 ApiError apiError = new ApiError();
250 MR_Client mrClient = new MR_Client();
251 mrClient.setMrClientId("mrClientId");
253 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(newArrayList(mrClient));
255 topicService.removeTopic(TOPIC_FQTN, apiError);
257 then(clientService).should().removeMr_Client(mrClient.getMrClientId(), false, apiError);
258 assertEquals(OK.getStatusCode(), apiError.getCode());
262 public void removeTopic_shouldRemoveTopicFromCache() {
263 ApiError apiError = new ApiError();
265 topicService.removeTopic(TOPIC_FQTN, apiError);
267 assertTrue(topicService.getTopics().isEmpty());
268 assertEquals(OK.getStatusCode(), apiError.getCode());
272 public void removeTopic_shouldFailIfAafCleanupWasFailed() {
273 ApiError apiError = new ApiError();
274 given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(404, "sth went wrong"));
276 Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
278 assertNull(removedTopic);
279 assertEquals(404, apiError.getCode());
280 assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
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);
289 private Topic createTopic(String fqtn) {
290 return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
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());
299 public void describeTo(Description description) {
300 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);