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;
39 import java.util.List;
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;
47 @RunWith(JUnitParamsRunner.class)
48 public class AafTopicSetupServiceTest {
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();
60 private DmaapService dmaapService;
61 private AafTopicSetupService aafTopicSetupService;
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);
74 @Parameters({"201", "409"})
75 public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
76 aafService.givenReturnCode(aafServiceReturnedCode);
78 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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"));
86 public void shouldReturnOkStatusWhenNoError() {
87 aafService.givenReturnCode(201);
89 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
91 assertOkStatus(apiError);
95 @Parameters({"201", "409"})
96 public void shouldAddNamespace(int aafServiceReturnedCode) {
97 aafService.givenReturnCode(aafServiceReturnedCode);
98 Topic topic = givenTopic(TOPIC_FQTN);
100 aafTopicSetupService.aafTopicSetup(topic);
102 AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
103 aafService.shouldAddNamespace(namespace);
107 @Parameters({"201", "409"})
108 public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
109 aafService.givenReturnCode(aafServiceReturnedCode);
110 Topic topic = givenTopic(TOPIC_FQTN);
112 aafTopicSetupService.aafTopicSetup(topic);
114 AafRole role = new AafRole(TOPIC_FQTN, "publisher");
115 aafService.shouldAddRole(role);
116 assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
120 @Parameters({"201", "409"})
121 public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
122 aafService.givenReturnCode(aafServiceReturnedCode);
123 Topic topic = givenTopic(TOPIC_FQTN);
125 aafTopicSetupService.aafTopicSetup(topic);
127 AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
128 aafService.shouldAddRole(role);
129 assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
133 @Parameters({"201", "409"})
134 public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
135 aafService.givenReturnCode(aafServiceReturnedCode);
137 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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()));
147 @Parameters({"201", "409"})
148 public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
149 aafService.givenReturnCode(aafServiceReturnedCode);
151 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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()));
161 public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
162 aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, false);
164 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
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();
173 public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
175 String topicFqtn = "sample_topic";
176 aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));
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();
185 public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
186 Dmaap dmaap = new Dmaap();
187 dmaap.setTopicNsRoot(null);
188 given(dmaapService.getDmaap()).willReturn(dmaap);
190 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
192 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
196 public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
197 aafService.givenAddPermStatus(NOT_FOUND);
199 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
201 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
205 public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
206 aafService.givenAddNamespaceStatus(NOT_FOUND);
208 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
210 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
214 public void shouldHandleExceptionWhenRoleCreationWasFailed() {
215 aafService.givenAddRoleStatus(NOT_FOUND);
217 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
219 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
223 public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
224 aafService.givenAddGrantStatus(NOT_FOUND);
226 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
228 assertErrorStatus(apiError, NOT_FOUND);
232 @Parameters({"200", "404"})
233 public void shouldremovePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
234 aafService.givenReturnCode(aafServiceReturnedCode);
236 aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
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"));
244 @Parameters({"200", "404"})
245 public void shouldRemoveNamespace(int aafServiceReturnedCode) {
246 aafService.givenReturnCode(aafServiceReturnedCode);
247 Topic topic = givenTopic(TOPIC_FQTN);
249 aafTopicSetupService.aafTopicCleanup(topic);
251 AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
252 aafService.shouldRemoveNamespace(namespace);
256 public void shouldRemoveOnlyPermissionsWhenCreateTopicRolesIsFalse() {
257 aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, false);
259 aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
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();
268 public void shouldRemoveOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
270 String topicFqtn = "sample_topic";
271 aafTopicSetupService.aafTopicCleanup(givenTopic(topicFqtn));
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();
280 public void shouldHandleExceptionWhenPermissionRemovalWasFailed() {
281 aafService.givenRemovePermStatus(INTERNAL_SERVER_ERROR);
283 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
285 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
289 public void shouldHandleExceptionWhenNamespaceRemovalWasFailed() {
290 aafService.givenRemoveNamespaceStatus(INTERNAL_SERVER_ERROR);
292 ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
294 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
297 private Topic givenTopic(String topicFqtn) {
298 Topic topic = new Topic();
299 topic.setFqtn(topicFqtn);
303 private void assertOkStatus(ApiError apiError) {
304 assertTrue(apiError.is2xx());
305 assertEquals("OK", apiError.getMessage());
308 private void assertErrorStatus(ApiError apiError, int code) {
309 assertEquals(code, apiError.getCode());
312 private class AafServiceStub implements AafService {
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;
328 public String getIdentity() {
333 public int addPerm(DmaapPerm perm) {
334 this.addedPerms.add(perm);
335 return addPermStatus;
339 public int delPerm(DmaapPerm perm) {
340 removedPerms.add(perm);
341 return removePermStatus;
345 public int addGrant(DmaapGrant grant) {
346 addedGrants.add(grant);
347 return addGrantStatus;
351 public int addUserRole(AafUserRole ur) {
352 throw new UnsupportedOperationException();
356 public int delGrant(DmaapGrant grant) {
357 throw new UnsupportedOperationException();
361 public int addRole(AafRole role) {
362 this.addedRoles.add(role);
363 return addRoleStatus;
367 public int addNamespace(AafNamespace namespace) {
368 this.addedNamespace = namespace;
369 return addNamespaceStatus;
373 public int delNamespace(AafNamespace namespace) {
374 this.removedNamespace = namespace;
375 return removeNamespaceStatus;
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;
387 void givenAddNamespaceStatus(int addNamespaceStatus) {
388 this.addNamespaceStatus = addNamespaceStatus;
391 void givenRemoveNamespaceStatus(int removeNamespaceStatus) {
392 this.removeNamespaceStatus = removeNamespaceStatus;
395 void givenAddGrantStatus(int addGrantStatus) {
396 this.addGrantStatus = addGrantStatus;
399 void givenAddRoleStatus(int addRoleStatus) {
400 this.addRoleStatus = addRoleStatus;
403 void givenAddPermStatus(int addPermStatus) {
404 this.addPermStatus = addPermStatus;
407 void givenRemovePermStatus(int removePermStatus) {
408 this.removePermStatus = removePermStatus;
411 void shouldAddPerm(DmaapPerm perm) {
412 assertTrue(addedPerms.contains(perm));
415 void shouldRemovePerm(DmaapPerm perm) {
416 assertTrue(removedPerms.contains(perm));
419 void shouldAddNamespace(AafNamespace namespace) {
420 assertEquals(namespace, this.addedNamespace);
423 void shouldRemoveNamespace(AafNamespace namespace) {
424 assertEquals(namespace, this.removedNamespace);
427 void shouldAddRole(AafRole role) {
428 assertTrue(addedRoles.contains(role));
431 void shouldAddGrant(DmaapGrant grant) {
432 assertTrue(addedGrants.contains(grant));
435 void shouldHaveNoNamespaceRolesAndGrantsAdded() {
436 assertNull(this.addedNamespace);
437 assertTrue(this.addedGrants.isEmpty());
438 assertTrue(this.addedRoles.isEmpty());
441 void shouldNotRemoveNamespace() {
442 assertNull(this.removedNamespace);