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 org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.assertThat;
50 import static org.mockito.BDDMockito.given;
51 import static org.mockito.Matchers.any;
52 import static org.mockito.Mockito.verifyZeroInteractions;
53 import static org.onap.dmaap.dbcapi.model.ReplicationType.REPLICATION_GLOBAL_TO_FQDN;
55 @RunWith(MockitoJUnitRunner.class)
56 public class TopicServiceTest {
58 private static final String TOPIC_FQTN = "topic_1";
59 private static final String GLOBAL_MR_HOST = "global.mr.host";
60 private TopicService topicService;
62 private MR_ClientService clientService;
64 private DmaapConfig dmaapConfig;
66 private MR_ClusterService clusters;
68 private DcaeLocationService locations;
70 private MirrorMakerService bridge;
72 private AafTopicSetupService aafTopicSetupService;
75 public void setUp() throws Exception {
76 given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST);
77 given(aafTopicSetupService.aafTopicSetup(any(Topic.class))).willReturn(new ApiError(200, "OK"));
82 public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
84 ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
85 topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
87 assertEquals(topics, topicService.getTopics());
91 public void getAllTopics_shouldReturnTopicsWithClients() {
93 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
94 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
96 List<Topic> allTopics = topicService.getAllTopics();
98 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
99 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
103 public void getAllTopicsWithoutClients_shouldReturnNoClients() {
105 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
107 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
108 assertNull(getOnlyElement(allTopics).getClients());
109 verifyZeroInteractions(clientService);
113 public void getAllTopics_shouldCacheClients() {
115 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
116 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
118 topicService.getAllTopics();
119 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
121 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
122 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
126 public void getTopic_shouldReturnTopicByFqtn() {
128 ApiError apiError = new ApiError();
129 Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
131 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
132 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
136 public void getTopic_shouldReturnTopicWithMrClients() {
138 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
139 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
141 Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
143 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
144 assertEquals(mrClients, topic.getClients());
148 public void getTopic_shouldReturnError() {
150 ApiError apiError = new ApiError();
151 Topic topic = topicService.getTopic("not_existing", apiError);
154 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), apiError.getCode());
158 public void addTopic_shouldAddNewTopic() {
159 Topic newTopic = createTopic("");
161 ApiError apiError = new ApiError();
162 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
164 assertSame(newTopic, addedTopic);
165 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
166 assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
170 public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
171 Topic newTopic = createTopic("");
173 ApiError apiError = new ApiError();
174 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
175 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
177 assertNull(secondAddedTopic);
178 assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
182 public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
183 Topic newTopic = createTopic("");
185 ApiError apiError = new ApiError();
186 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
187 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
189 assertSame(addedTopic, secondAddedTopic);
190 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
191 assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
196 public void addTopic_shouldSetGlobalMrURL() {
197 Topic newTopic = createTopic(TOPIC_FQTN);
198 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
200 ApiError apiError = new ApiError();
201 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
203 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
204 assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
208 public void addTopic_shouldReturnErrorWhenGlobalMrURLIsInvalid() {
209 given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn("invalid@host");
210 createTopicService();
211 Topic newTopic = createTopic(TOPIC_FQTN);
212 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
214 ApiError apiError = new ApiError();
215 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
217 assertEquals(500, apiError.getCode());
218 assertNull(addedTopic);
221 private void createTopicService() {
222 Map<String, Topic> mrTopics = new HashMap<>();
223 mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN));
224 topicService = new TopicService(mrTopics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
227 private Topic createTopic(String fqtn) {
228 return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
231 public static Matcher<Topic> hasCorrectFqtn(final String fqtn) {
232 return new BaseMatcher<Topic>() {
233 public boolean matches(Object o) {
234 return fqtn.equals(((Topic) o).getFqtn());
237 public void describeTo(Description description) {
238 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);