[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / service / AafTopicSetupServiceTest.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 package org.onap.dmaap.dbcapi.service;
21
22 import junitparams.JUnitParamsRunner;
23 import junitparams.Parameters;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.dmaap.dbcapi.aaf.AafNamespace;
30 import org.onap.dmaap.dbcapi.aaf.AafRole;
31 import org.onap.dmaap.dbcapi.aaf.AafService;
32 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
33 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
34 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
35 import org.onap.dmaap.dbcapi.model.ApiError;
36 import org.onap.dmaap.dbcapi.model.Dmaap;
37 import org.onap.dmaap.dbcapi.model.Topic;
38 import org.onap.dmaap.dbcapi.util.DmaapConfig;
39
40 import java.util.List;
41
42 import static com.google.common.collect.Lists.newArrayList;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNull;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.BDDMockito.given;
47
48 @RunWith(JUnitParamsRunner.class)
49 public class AafTopicSetupServiceTest {
50
51     private static final int INTERNAL_SERVER_ERROR = 500;
52     private static final int NOT_FOUND = 404;
53     private static final int CREATED = 201;
54     private static final int OK = 200;
55     private static final String TOPIC_NS_ROOT = "org.onap.dmaap.mr";
56     private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
57     private static final String TOPIC_FQTN = "org.onap.dmaap.mr.sample_topic";
58     private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
59     private AafServiceStub aafService = new AafServiceStub();
60     @Mock
61     private DmaapService dmaapService;
62     @Mock
63     private DmaapConfig dmaapConfig;
64     private AafTopicSetupService aafTopicSetupService;
65
66     @Before
67     public void setUp() throws Exception {
68         MockitoAnnotations.initMocks(this);
69         Dmaap dmaap = new Dmaap();
70         dmaap.setTopicNsRoot(TOPIC_NS_ROOT);
71         given(dmaapService.getDmaap()).willReturn(dmaap);
72         given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
73         given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("true");
74         given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("2");
75         aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, dmaapConfig);
76     }
77
78     @Test
79     @Parameters({"201", "409"})
80     public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
81         aafService.givenReturnCode(aafServiceReturnedCode);
82
83         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
84
85         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
86         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
87         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
88     }
89
90     @Test
91     public void shouldReturnOkStatusWhenNoError() {
92         aafService.givenReturnCode(201);
93
94         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
95
96         assertOkStatus(apiError);
97     }
98
99     @Test
100     @Parameters({"201", "409"})
101     public void shouldAddNamespace(int aafServiceReturnedCode) {
102         aafService.givenReturnCode(aafServiceReturnedCode);
103         Topic topic = givenTopic(TOPIC_FQTN);
104
105         aafTopicSetupService.aafTopicSetup(topic);
106
107         AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
108         aafService.shouldAddNamespace(namespace);
109     }
110
111     @Test
112     @Parameters({"201", "409"})
113     public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
114         aafService.givenReturnCode(aafServiceReturnedCode);
115         Topic topic = givenTopic(TOPIC_FQTN);
116
117         aafTopicSetupService.aafTopicSetup(topic);
118
119         AafRole role = new AafRole(TOPIC_FQTN, "publisher");
120         aafService.shouldAddRole(role);
121         assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
122     }
123
124     @Test
125     @Parameters({"201", "409"})
126     public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
127         aafService.givenReturnCode(aafServiceReturnedCode);
128         Topic topic = givenTopic(TOPIC_FQTN);
129
130         aafTopicSetupService.aafTopicSetup(topic);
131
132         AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
133         aafService.shouldAddRole(role);
134         assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
135     }
136
137     @Test
138     @Parameters({"201", "409"})
139     public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
140         aafService.givenReturnCode(aafServiceReturnedCode);
141
142         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
143
144         AafRole role = new AafRole(TOPIC_FQTN, "publisher");
145         DmaapPerm pubPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub");
146         DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
147         aafService.shouldAddGrant(new DmaapGrant(pubPerm, role.getFullyQualifiedRole()));
148         aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
149     }
150
151     @Test
152     @Parameters({"201", "409"})
153     public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
154         aafService.givenReturnCode(aafServiceReturnedCode);
155
156         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
157
158         AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
159         DmaapPerm subPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub");
160         DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
161         aafService.shouldAddGrant(new DmaapGrant(subPerm, role.getFullyQualifiedRole()));
162         aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
163     }
164
165     @Test
166     public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
167         given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
168
169         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
170
171         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
172         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
173         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
174         aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
175     }
176
177     @Test
178     public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
179
180         String topicFqtn = "sample_topic";
181         aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));
182
183         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
184         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
185         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
186         aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
187     }
188
189     @Test
190     public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
191         Dmaap dmaap = new Dmaap();
192         dmaap.setTopicNsRoot(null);
193         given(dmaapService.getDmaap()).willReturn(dmaap);
194
195         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
196
197         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
198     }
199
200     @Test
201     public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
202         aafService.givenAddPermStatus(NOT_FOUND);
203
204         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
205
206         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
207     }
208
209     @Test
210     public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
211         aafService.givenAddNamespaceStatus(NOT_FOUND);
212
213         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
214
215         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
216     }
217
218     @Test
219     public void shouldHandleExceptionWhenRoleCreationWasFailed() {
220         aafService.givenAddRoleStatus(NOT_FOUND);
221
222         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
223
224         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
225     }
226
227     @Test
228     public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
229         aafService.givenAddGrantStatus(NOT_FOUND);
230
231         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
232
233         assertErrorStatus(apiError, NOT_FOUND);
234     }
235
236     @Test
237     @Parameters({"200", "404"})
238     public void shouldremovePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
239         aafService.givenReturnCode(aafServiceReturnedCode);
240
241         aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
242
243         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
244         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
245         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
246     }
247
248     @Test
249     @Parameters({"200", "404"})
250     public void shouldRemoveNamespace(int aafServiceReturnedCode) {
251         aafService.givenReturnCode(aafServiceReturnedCode);
252         Topic topic = givenTopic(TOPIC_FQTN);
253
254         aafTopicSetupService.aafTopicCleanup(topic);
255
256         AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
257         aafService.shouldRemoveNamespace(namespace);
258     }
259
260     @Test
261     public void shouldRemoveOnlyPermissionsWhenCreateTopicRolesIsFalse() {
262         given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
263
264         aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
265
266         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
267         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
268         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
269         aafService.shouldNotRemoveNamespace();
270     }
271
272     @Test
273     public void shouldRemoveOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
274
275         String topicFqtn = "sample_topic";
276         aafTopicSetupService.aafTopicCleanup(givenTopic(topicFqtn));
277
278         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
279         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
280         aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
281         aafService.shouldNotRemoveNamespace();
282     }
283
284     @Test
285     public void shouldHandleExceptionWhenPermissionRemovalWasFailed() {
286         aafService.givenRemovePermStatus(INTERNAL_SERVER_ERROR);
287
288         ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
289
290         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
291     }
292
293     @Test
294     public void shouldHandleExceptionWhenNamespaceRemovalWasFailed() {
295         aafService.givenRemoveNamespaceStatus(INTERNAL_SERVER_ERROR);
296
297         ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
298
299         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
300     }
301
302     @Test
303     public void shouldNotPerformCleanupWhenDeleteLevelIsLessThanTwo() {
304         given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("0");
305
306         ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
307
308         aafService.shouldNotPerformCleanup();
309         assertOkStatus(apiError);
310     }
311
312     @Test
313     public void shouldNotPerformCleanupWhenDeleteLevelIsNotNumericValue() {
314         given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("not number");
315
316         ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
317
318         aafService.shouldNotPerformCleanup();
319         assertOkStatus(apiError);
320     }
321
322     private Topic givenTopic(String topicFqtn) {
323         Topic topic = new Topic();
324         topic.setFqtn(topicFqtn);
325         return topic;
326     }
327
328     private void assertOkStatus(ApiError apiError) {
329         assertTrue(apiError.is2xx());
330         assertEquals("OK", apiError.getMessage());
331     }
332
333     private void assertErrorStatus(ApiError apiError, int code) {
334         assertEquals(code, apiError.getCode());
335     }
336
337     private class AafServiceStub implements AafService {
338
339         private AafNamespace addedNamespace;
340         private AafNamespace removedNamespace;
341         private List<DmaapPerm> addedPerms = newArrayList();
342         private List<DmaapPerm> removedPerms = newArrayList();
343         private List<AafRole> addedRoles = newArrayList();
344         private List<DmaapGrant> addedGrants = newArrayList();
345         private int addNamespaceStatus = CREATED;
346         private int addGrantStatus = CREATED;
347         private int addRoleStatus = CREATED;
348         private int addPermStatus = CREATED;
349         private int removePermStatus = OK;
350         private int removeNamespaceStatus = OK;
351
352         @Override
353         public String getIdentity() {
354             return IDENTITY;
355         }
356
357         @Override
358         public int addPerm(DmaapPerm perm) {
359             this.addedPerms.add(perm);
360             return addPermStatus;
361         }
362
363         @Override
364         public int delPerm(DmaapPerm perm, boolean force) {
365             removedPerms.add(perm);
366             return removePermStatus;
367         }
368
369         @Override
370         public int addGrant(DmaapGrant grant) {
371             addedGrants.add(grant);
372             return addGrantStatus;
373         }
374
375         @Override
376         public int addUserRole(AafUserRole ur) {
377             throw new UnsupportedOperationException();
378         }
379
380         @Override
381         public int addRole(AafRole role) {
382             this.addedRoles.add(role);
383             return addRoleStatus;
384         }
385
386         @Override
387         public int addNamespace(AafNamespace namespace) {
388             this.addedNamespace = namespace;
389             return addNamespaceStatus;
390         }
391
392         @Override
393         public int delNamespace(AafNamespace namespace, boolean force) {
394             this.removedNamespace = namespace;
395             return removeNamespaceStatus;
396         }
397
398         void givenReturnCode(int status) {
399             this.addNamespaceStatus = status;
400             this.addGrantStatus = status;
401             this.addRoleStatus = status;
402             this.addPermStatus = status;
403             this.removePermStatus = status;
404             this.removeNamespaceStatus = status;
405         }
406
407         void givenAddNamespaceStatus(int addNamespaceStatus) {
408             this.addNamespaceStatus = addNamespaceStatus;
409         }
410
411         void givenRemoveNamespaceStatus(int removeNamespaceStatus) {
412             this.removeNamespaceStatus = removeNamespaceStatus;
413         }
414
415         void givenAddGrantStatus(int addGrantStatus) {
416             this.addGrantStatus = addGrantStatus;
417         }
418
419         void givenAddRoleStatus(int addRoleStatus) {
420             this.addRoleStatus = addRoleStatus;
421         }
422
423         void givenAddPermStatus(int addPermStatus) {
424             this.addPermStatus = addPermStatus;
425         }
426
427         void givenRemovePermStatus(int removePermStatus) {
428             this.removePermStatus = removePermStatus;
429         }
430
431         void shouldAddPerm(DmaapPerm perm) {
432             assertTrue(addedPerms.contains(perm));
433         }
434
435         void shouldRemovePerm(DmaapPerm perm) {
436             assertTrue(removedPerms.contains(perm));
437         }
438
439         void shouldAddNamespace(AafNamespace namespace) {
440             assertEquals(namespace, this.addedNamespace);
441         }
442
443         void shouldRemoveNamespace(AafNamespace namespace) {
444             assertEquals(namespace, this.removedNamespace);
445         }
446
447         void shouldAddRole(AafRole role) {
448             assertTrue(addedRoles.contains(role));
449         }
450
451         void shouldAddGrant(DmaapGrant grant) {
452             assertTrue(addedGrants.contains(grant));
453         }
454
455         void shouldHaveNoNamespaceRolesAndGrantsAdded() {
456             assertNull(this.addedNamespace);
457             assertTrue(this.addedGrants.isEmpty());
458             assertTrue(this.addedRoles.isEmpty());
459         }
460
461         void shouldNotRemoveNamespace() {
462             assertNull(this.removedNamespace);
463         }
464
465         void shouldNotPerformCleanup() {
466             shouldNotRemoveNamespace();
467             assertTrue(removedPerms.isEmpty());
468         }
469     }
470 }