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