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