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=========================================================
20 package org.onap.dmaap.dbcapi.service;
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;
40 import java.util.List;
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;
48 @RunWith(JUnitParamsRunner.class)
49 public class AafTopicSetupServiceTest {
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();
61 private DmaapService dmaapService;
63 private DmaapConfig dmaapConfig;
64 private AafTopicSetupService aafTopicSetupService;
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);
79 @Parameters({"201", "409"})
80 public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
81 aafService.givenReturnCode(aafServiceReturnedCode);
83 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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"));
91 public void shouldReturnOkStatusWhenNoError() {
92 aafService.givenReturnCode(201);
94 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
96 assertOkStatus(apiError);
100 @Parameters({"201", "409"})
101 public void shouldAddNamespace(int aafServiceReturnedCode) {
102 aafService.givenReturnCode(aafServiceReturnedCode);
103 Topic topic = givenTopic(TOPIC_FQTN);
105 aafTopicSetupService.aafTopicSetup(topic);
107 AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
108 aafService.shouldAddNamespace(namespace);
112 @Parameters({"201", "409"})
113 public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
114 aafService.givenReturnCode(aafServiceReturnedCode);
115 Topic topic = givenTopic(TOPIC_FQTN);
117 aafTopicSetupService.aafTopicSetup(topic);
119 AafRole role = new AafRole(TOPIC_FQTN, "publisher");
120 aafService.shouldAddRole(role);
121 assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
125 @Parameters({"201", "409"})
126 public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
127 aafService.givenReturnCode(aafServiceReturnedCode);
128 Topic topic = givenTopic(TOPIC_FQTN);
130 aafTopicSetupService.aafTopicSetup(topic);
132 AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
133 aafService.shouldAddRole(role);
134 assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
138 @Parameters({"201", "409"})
139 public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
140 aafService.givenReturnCode(aafServiceReturnedCode);
142 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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()));
152 @Parameters({"201", "409"})
153 public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
154 aafService.givenReturnCode(aafServiceReturnedCode);
156 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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()));
166 public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
167 given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
169 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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();
178 public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
180 String topicFqtn = "sample_topic";
181 aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));
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();
190 public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
191 Dmaap dmaap = new Dmaap();
192 dmaap.setTopicNsRoot(null);
193 given(dmaapService.getDmaap()).willReturn(dmaap);
195 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
197 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
201 public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
202 aafService.givenAddPermStatus(NOT_FOUND);
204 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
206 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
210 public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
211 aafService.givenAddNamespaceStatus(NOT_FOUND);
213 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
215 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
219 public void shouldHandleExceptionWhenRoleCreationWasFailed() {
220 aafService.givenAddRoleStatus(NOT_FOUND);
222 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
224 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
228 public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
229 aafService.givenAddGrantStatus(NOT_FOUND);
231 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
233 assertErrorStatus(apiError, NOT_FOUND);
237 @Parameters({"200", "404"})
238 public void shouldremovePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
239 aafService.givenReturnCode(aafServiceReturnedCode);
241 aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
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"));
249 @Parameters({"200", "404"})
250 public void shouldRemoveNamespace(int aafServiceReturnedCode) {
251 aafService.givenReturnCode(aafServiceReturnedCode);
252 Topic topic = givenTopic(TOPIC_FQTN);
254 aafTopicSetupService.aafTopicCleanup(topic);
256 AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
257 aafService.shouldRemoveNamespace(namespace);
261 public void shouldRemoveOnlyPermissionsWhenCreateTopicRolesIsFalse() {
262 given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
264 aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
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();
273 public void shouldRemoveOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
275 String topicFqtn = "sample_topic";
276 aafTopicSetupService.aafTopicCleanup(givenTopic(topicFqtn));
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();
285 public void shouldHandleExceptionWhenPermissionRemovalWasFailed() {
286 aafService.givenRemovePermStatus(INTERNAL_SERVER_ERROR);
288 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
290 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
294 public void shouldHandleExceptionWhenNamespaceRemovalWasFailed() {
295 aafService.givenRemoveNamespaceStatus(INTERNAL_SERVER_ERROR);
297 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
299 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
303 public void shouldNotPerformCleanupWhenDeleteLevelIsLessThanTwo() {
304 given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("0");
306 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
308 aafService.shouldNotPerformCleanup();
309 assertOkStatus(apiError);
313 public void shouldNotPerformCleanupWhenDeleteLevelIsNotNumericValue() {
314 given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("not number");
316 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
318 aafService.shouldNotPerformCleanup();
319 assertOkStatus(apiError);
322 private Topic givenTopic(String topicFqtn) {
323 Topic topic = new Topic();
324 topic.setFqtn(topicFqtn);
328 private void assertOkStatus(ApiError apiError) {
329 assertTrue(apiError.is2xx());
330 assertEquals("OK", apiError.getMessage());
333 private void assertErrorStatus(ApiError apiError, int code) {
334 assertEquals(code, apiError.getCode());
337 private class AafServiceStub implements AafService {
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;
353 public String getIdentity() {
358 public int addPerm(DmaapPerm perm) {
359 this.addedPerms.add(perm);
360 return addPermStatus;
364 public int delPerm(DmaapPerm perm, boolean force) {
365 removedPerms.add(perm);
366 return removePermStatus;
370 public int addGrant(DmaapGrant grant) {
371 addedGrants.add(grant);
372 return addGrantStatus;
376 public int addUserRole(AafUserRole ur) {
377 throw new UnsupportedOperationException();
381 public int addRole(AafRole role) {
382 this.addedRoles.add(role);
383 return addRoleStatus;
387 public int addNamespace(AafNamespace namespace) {
388 this.addedNamespace = namespace;
389 return addNamespaceStatus;
393 public int delNamespace(AafNamespace namespace, boolean force) {
394 this.removedNamespace = namespace;
395 return removeNamespaceStatus;
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;
407 void givenAddNamespaceStatus(int addNamespaceStatus) {
408 this.addNamespaceStatus = addNamespaceStatus;
411 void givenRemoveNamespaceStatus(int removeNamespaceStatus) {
412 this.removeNamespaceStatus = removeNamespaceStatus;
415 void givenAddGrantStatus(int addGrantStatus) {
416 this.addGrantStatus = addGrantStatus;
419 void givenAddRoleStatus(int addRoleStatus) {
420 this.addRoleStatus = addRoleStatus;
423 void givenAddPermStatus(int addPermStatus) {
424 this.addPermStatus = addPermStatus;
427 void givenRemovePermStatus(int removePermStatus) {
428 this.removePermStatus = removePermStatus;
431 void shouldAddPerm(DmaapPerm perm) {
432 assertTrue(addedPerms.contains(perm));
435 void shouldRemovePerm(DmaapPerm perm) {
436 assertTrue(removedPerms.contains(perm));
439 void shouldAddNamespace(AafNamespace namespace) {
440 assertEquals(namespace, this.addedNamespace);
443 void shouldRemoveNamespace(AafNamespace namespace) {
444 assertEquals(namespace, this.removedNamespace);
447 void shouldAddRole(AafRole role) {
448 assertTrue(addedRoles.contains(role));
451 void shouldAddGrant(DmaapGrant grant) {
452 assertTrue(addedGrants.contains(grant));
455 void shouldHaveNoNamespaceRolesAndGrantsAdded() {
456 assertNull(this.addedNamespace);
457 assertTrue(this.addedGrants.isEmpty());
458 assertTrue(this.addedRoles.isEmpty());
461 void shouldNotRemoveNamespace() {
462 assertNull(this.removedNamespace);
465 void shouldNotPerformCleanup() {
466 shouldNotRemoveNamespace();
467 assertTrue(removedPerms.isEmpty());