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.Mockito.verifyZeroInteractions;
52 import static org.onap.dmaap.dbcapi.model.ReplicationType.REPLICATION_GLOBAL_TO_FQDN;
54 @RunWith(MockitoJUnitRunner.class)
55 public class TopicServiceTest {
57 private static final String TOPIC_FQTN = "topic_1";
58 private static final String GLOBAL_MR_HOST = "global.mr.host";
59 private TopicService topicService;
61 private MR_ClientService clientService;
63 private DmaapConfig dmaapConfig;
65 private MR_ClusterService clusters;
67 private DcaeLocationService locations;
69 private MirrorMakerService bridge;
72 public void setUp() throws Exception {
73 given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST);
78 public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
80 ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
81 topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge);
83 assertEquals(topics, topicService.getTopics());
87 public void getAllTopics_shouldReturnTopicsWithClients() {
89 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
90 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
92 List<Topic> allTopics = topicService.getAllTopics();
94 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
95 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
99 public void getAllTopicsWithoutClients_shouldReturnNoClients() {
101 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
103 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
104 assertNull(getOnlyElement(allTopics).getClients());
105 verifyZeroInteractions(clientService);
109 public void getAllTopics_shouldCacheClients() {
111 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
112 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
114 topicService.getAllTopics();
115 List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
117 assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
118 assertEquals(mrClients, getOnlyElement(allTopics).getClients());
122 public void getTopic_shouldReturnTopicByFqtn() {
124 ApiError apiError = new ApiError();
125 Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
127 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
128 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
132 public void getTopic_shouldReturnTopicWithMrClients() {
134 ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
135 given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
137 Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
139 assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
140 assertEquals(mrClients, topic.getClients());
144 public void getTopic_shouldReturnError() {
146 ApiError apiError = new ApiError();
147 Topic topic = topicService.getTopic("not_existing", apiError);
150 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), apiError.getCode());
154 public void addTopic_shouldAddNewTopic() {
155 Topic newTopic = createTopic("");
157 ApiError apiError = new ApiError();
158 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
160 assertSame(newTopic, addedTopic);
161 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
162 assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
166 public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
167 Topic newTopic = createTopic("");
169 ApiError apiError = new ApiError();
170 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
171 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
173 assertNull(secondAddedTopic);
174 assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
178 public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
179 Topic newTopic = createTopic("");
181 ApiError apiError = new ApiError();
182 Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
183 Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
185 assertSame(addedTopic, secondAddedTopic);
186 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
187 assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
192 public void addTopic_shouldSetGlobalMrURL() {
193 Topic newTopic = createTopic(TOPIC_FQTN);
194 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
196 ApiError apiError = new ApiError();
197 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
199 assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
200 assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
204 public void addTopic_shouldReturnErrorWhenGlobalMrURLIsInvalid() {
205 given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn("invalid@host");
206 createTopicService();
207 Topic newTopic = createTopic(TOPIC_FQTN);
208 newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
210 ApiError apiError = new ApiError();
211 Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
213 assertEquals(500, apiError.getCode());
214 assertNull(addedTopic);
217 private void createTopicService() {
218 Map<String, Topic> mrTopics = new HashMap<>();
219 mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN));
220 topicService = new TopicService(mrTopics, clientService, dmaapConfig, clusters, locations, bridge);
223 private Topic createTopic(String fqtn) {
224 return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
227 public static Matcher<Topic> hasCorrectFqtn(final String fqtn) {
228 return new BaseMatcher<Topic>() {
229 public boolean matches(Object o) {
230 return fqtn.equals(((Topic) o).getFqtn());
233 public void describeTo(Description description) {
234 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);