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 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;
39 import com.google.common.collect.ImmutableMap;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
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;
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 static void setUpClass(){
80 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
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"));
93 public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
95 ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
96 topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
98 assertEquals(topics, topicService.getTopics());
102 public void getAllTopics_shouldReturnTopicsWithClients() {
104 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
105 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
107 List<Topic> allTopics = topicService.getAllTopics();
109 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
110 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
114 public void getAllTopicsWithoutClients_shouldReturnNoClients() {
116 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
118 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
119 assertNull(getOnlyElement(allTopics).getClients());
120 verifyZeroInteractions(clientService);
124 public void getAllTopics_shouldCacheClients() {
126 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
127 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
129 topicService.getAllTopics();
130 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
132 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
133 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
137 public void getTopic_shouldReturnTopicByFqtn() {
139 ApiError apiError = new ApiError();
140 Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
142 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
143 assertEquals(OK.getStatusCode(), apiError.getCode());
147 public void getTopic_shouldReturnTopicWithMrClients() {
149 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
150 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
152 Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
154 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
155 assertEquals(mrClients, topic.getClients());
159 public void getTopic_shouldReturnError() {
161 ApiError apiError = new ApiError();
162 Topic topic = topicService.getTopic("not_existing", apiError);
165 assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
169 public void addTopic_shouldAddNewTopic() {
170 Topic newTopic = createTopic("");
172 ApiError apiError = new ApiError();
173 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
175 assertSame(newTopic, addedTopic);
176 assertEquals(OK.getStatusCode(), apiError.getCode());
177 assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
181 public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
182 Topic newTopic = createTopic("");
184 ApiError apiError = new ApiError();
185 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
186 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
188 assertNull(secondAddedTopic);
189 assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
193 public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
194 Topic newTopic = createTopic("");
196 ApiError apiError = new ApiError();
197 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
198 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
200 assertSame(addedTopic, secondAddedTopic);
201 assertEquals(OK.getStatusCode(), apiError.getCode());
202 assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
207 public void addTopic_shouldSetGlobalMrURL() {
208 Topic newTopic = createTopic(TOPIC_FQTN);
209 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
211 ApiError apiError = new ApiError();
212 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
214 assertEquals(OK.getStatusCode(), apiError.getCode());
215 assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
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);
225 ApiError apiError = new ApiError();
226 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
228 assertEquals(500, apiError.getCode());
229 assertNull(addedTopic);
233 public void removeTopic_shouldFailIfTopicDoesNotExist() {
234 ApiError apiError = new ApiError();
236 Topic removedTopic = topicService.removeTopic("not_existing_fqtn", apiError);
238 assertNull(removedTopic);
239 assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
240 assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
244 public void removeTopic_shouldExecuteAafCleanup() {
245 ApiError apiError = new ApiError();
247 Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
249 then(aafTopicSetupService).should().aafTopicCleanup(removedTopic);
250 assertEquals(OK.getStatusCode(), apiError.getCode());
254 public void removeTopic_shouldRemoveEachMrClientAssignedToTopic() {
255 ApiError apiError = new ApiError();
256 MR_Client mrClient = new MR_Client();
257 mrClient.setMrClientId("mrClientId");
259 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(newArrayList(mrClient));
261 topicService.removeTopic(TOPIC_FQTN, apiError);
263 then(clientService).should().removeMr_Client(mrClient.getMrClientId(), false, apiError);
264 assertEquals(OK.getStatusCode(), apiError.getCode());
268 public void removeTopic_shouldRemoveTopicFromCache() {
269 ApiError apiError = new ApiError();
271 topicService.removeTopic(TOPIC_FQTN, apiError);
273 assertTrue(topicService.getTopics().isEmpty());
274 assertEquals(OK.getStatusCode(), apiError.getCode());
278 public void removeTopic_shouldFailIfAafCleanupWasFailed() {
279 ApiError apiError = new ApiError();
280 given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(404, "sth went wrong"));
282 Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
284 assertNull(removedTopic);
285 assertEquals(404, apiError.getCode());
286 assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
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);
295 private Topic createTopic(String fqtn) {
296 return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
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());
305 public void describeTo(Description description) {
306 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);