f73c297c2214b1d92a2941bc0f4069dbc1922ebb
[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 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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class TopicServiceTest {
57
58     private static final String TOPIC_FQTN = "topic_1";
59     private static final String GLOBAL_MR_HOST = "global.mr.host";
60     private TopicService topicService;
61     @Mock
62     private MR_ClientService clientService;
63     @Mock
64     private DmaapConfig dmaapConfig;
65     @Mock
66     private MR_ClusterService clusters;
67     @Mock
68     private DcaeLocationService locations;
69     @Mock
70     private MirrorMakerService bridge;
71     @Mock
72     private AafTopicSetupService aafTopicSetupService;
73
74     @Before
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"));
78         createTopicService();
79     }
80
81     @Test
82     public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
83
84         ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
85         topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService);
86
87         assertEquals(topics, topicService.getTopics());
88     }
89
90     @Test
91     public void getAllTopics_shouldReturnTopicsWithClients() {
92
93         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
94         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
95
96         List<Topic> allTopics = topicService.getAllTopics();
97
98         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
99         assertEquals(mrClients, getOnlyElement(allTopics).getClients());
100     }
101
102     @Test
103     public void getAllTopicsWithoutClients_shouldReturnNoClients() {
104
105         List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
106
107         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
108         assertNull(getOnlyElement(allTopics).getClients());
109         verifyZeroInteractions(clientService);
110     }
111
112     @Test
113     public void getAllTopics_shouldCacheClients() {
114
115         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
116         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
117
118         topicService.getAllTopics();
119         List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
120
121         assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
122         assertEquals(mrClients, getOnlyElement(allTopics).getClients());
123     }
124
125     @Test
126     public void getTopic_shouldReturnTopicByFqtn() {
127
128         ApiError apiError = new ApiError();
129         Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
130
131         assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
132         assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
133     }
134
135     @Test
136     public void getTopic_shouldReturnTopicWithMrClients() {
137
138         ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
139         given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
140
141         Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError());
142
143         assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
144         assertEquals(mrClients, topic.getClients());
145     }
146
147     @Test
148     public void getTopic_shouldReturnError() {
149
150         ApiError apiError = new ApiError();
151         Topic topic = topicService.getTopic("not_existing", apiError);
152
153         assertNull(topic);
154         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), apiError.getCode());
155     }
156
157     @Test
158     public void addTopic_shouldAddNewTopic() {
159         Topic newTopic = createTopic("");
160
161         ApiError apiError = new ApiError();
162         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
163
164         assertSame(newTopic, addedTopic);
165         assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
166         assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
167     }
168
169     @Test
170     public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() {
171         Topic newTopic = createTopic("");
172
173         ApiError apiError = new ApiError();
174         Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
175         Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false);
176
177         assertNull(secondAddedTopic);
178         assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode());
179     }
180
181     @Test
182     public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() {
183         Topic newTopic = createTopic("");
184
185         ApiError apiError = new ApiError();
186         Topic addedTopic = topicService.addTopic(newTopic, apiError, false);
187         Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
188
189         assertSame(addedTopic, secondAddedTopic);
190         assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
191         assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
192     }
193
194
195     @Test
196     public void addTopic_shouldSetGlobalMrURL() {
197         Topic newTopic = createTopic(TOPIC_FQTN);
198         newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN);
199
200         ApiError apiError = new ApiError();
201         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
202
203         assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
204         assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
205     }
206
207     @Test
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);
213
214         ApiError apiError = new ApiError();
215         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
216
217         assertEquals(500, apiError.getCode());
218         assertNull(addedTopic);
219     }
220
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);
225     }
226
227     private Topic createTopic(String fqtn) {
228         return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
229     }
230
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());
235             }
236
237             public void describeTo(Description description) {
238                 description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);
239             }
240         };
241     }
242
243 }