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.assertTrue;
44 import static org.mockito.BDDMockito.given;
46 @RunWith(JUnitParamsRunner.class)
47 public class AafTopicSetupServiceTest {
49 private static final int INTERNAL_SERVER_ERROR = 500;
50 private static final int NOT_FOUND = 404;
51 private static final int CREATED = 201;
52 private static final String TOPIC_NS_ROOT = "org.onap.dmaap.mr";
53 private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
54 private static final String TOPIC_FQTN = "org.onap.dmaap.mr.sample_topic";
55 private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
56 private AafServiceStub aafService = new AafServiceStub();
58 private DmaapService dmaapService;
59 private AafTopicSetupService aafTopicSetupService;
62 public void setUp() throws Exception {
63 MockitoAnnotations.initMocks(this);
64 Dmaap dmaap = new Dmaap();
65 dmaap.setTopicNsRoot(TOPIC_NS_ROOT);
66 given(dmaapService.getDmaap()).willReturn(dmaap);
67 given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
68 aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, true);
72 @Parameters({"201", "409"})
73 public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
74 aafService.givenReturnCode(aafServiceReturnedCode);
76 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
78 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
79 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
80 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
84 public void shouldReturnOkStatusWhenNoError() {
85 aafService.givenReturnCode(201);
87 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
89 assertOkStatus(apiError);
93 @Parameters({"201", "409"})
94 public void shouldAddNamespace(int aafServiceReturnedCode) {
95 aafService.givenReturnCode(aafServiceReturnedCode);
96 Topic topic = givenTopic(TOPIC_FQTN);
98 aafTopicSetupService.aafTopicSetup(topic);
100 AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
101 aafService.shouldAddNamespace(namespace);
105 @Parameters({"201", "409"})
106 public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
107 aafService.givenReturnCode(aafServiceReturnedCode);
108 Topic topic = givenTopic(TOPIC_FQTN);
110 aafTopicSetupService.aafTopicSetup(topic);
112 AafRole role = new AafRole(TOPIC_FQTN, "publisher");
113 aafService.shouldAddRole(role);
114 assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
118 @Parameters({"201", "409"})
119 public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
120 aafService.givenReturnCode(aafServiceReturnedCode);
121 Topic topic = givenTopic(TOPIC_FQTN);
123 aafTopicSetupService.aafTopicSetup(topic);
125 AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
126 aafService.shouldAddRole(role);
127 assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
131 @Parameters({"201", "409"})
132 public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
133 aafService.givenReturnCode(aafServiceReturnedCode);
135 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
137 AafRole role = new AafRole(TOPIC_FQTN, "publisher");
138 DmaapPerm pubPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub");
139 DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
140 aafService.shouldAddGrant(new DmaapGrant(pubPerm, role.getFullyQualifiedRole()));
141 aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
145 @Parameters({"201", "409"})
146 public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
147 aafService.givenReturnCode(aafServiceReturnedCode);
149 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
151 AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
152 DmaapPerm subPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub");
153 DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
154 aafService.shouldAddGrant(new DmaapGrant(subPerm, role.getFullyQualifiedRole()));
155 aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
159 public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
160 aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, false);
162 aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
164 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
165 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
166 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
167 aafService.shouldHaveNoRolesAndGrants();
171 public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
173 String topicFqtn = "sample_topic";
174 aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));
176 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
177 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
178 aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
179 aafService.shouldHaveNoRolesAndGrants();
183 public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
184 Dmaap dmaap = new Dmaap();
185 dmaap.setTopicNsRoot(null);
186 given(dmaapService.getDmaap()).willReturn(dmaap);
188 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
190 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
194 public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
195 aafService.givenAddPermStatus(NOT_FOUND);
197 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
199 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
203 public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
204 aafService.givenAddNamespaceStatus(NOT_FOUND);
206 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
208 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
212 public void shouldHandleExceptionWhenRoleCreationWasFailed() {
213 aafService.givenAddRoleStatus(NOT_FOUND);
215 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
217 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
221 public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
222 aafService.givenAddGrantStatus(NOT_FOUND);
224 ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
226 assertErrorStatus(apiError, NOT_FOUND);
229 private Topic givenTopic(String topicFqtn) {
230 Topic topic = new Topic();
231 topic.setFqtn(topicFqtn);
235 private void assertOkStatus(ApiError apiError) {
236 assertTrue(apiError.is2xx());
237 assertEquals("OK", apiError.getMessage());
240 private void assertErrorStatus(ApiError apiError, int code) {
241 assertEquals(code, apiError.getCode());
244 private class AafServiceStub implements AafService {
246 private AafNamespace namespace;
247 private List<DmaapPerm> perms = newArrayList();
248 private List<AafRole> roles = newArrayList();
249 private List<DmaapGrant> grants = newArrayList();
250 private int addNamespaceStatus = CREATED;
251 private int addGrantStatus = CREATED;
252 private int addRoleStatus = CREATED;
253 private int addPermStatus = CREATED;
256 public String getIdentity() {
261 public int addPerm(DmaapPerm perm) {
262 this.perms.add(perm);
263 return addPermStatus;
267 public int addGrant(DmaapGrant grant) {
269 return addGrantStatus;
273 public int addUserRole(AafUserRole ur) {
274 throw new UnsupportedOperationException();
278 public int delGrant(DmaapGrant grant) {
279 throw new UnsupportedOperationException();
283 public int addRole(AafRole role) {
284 this.roles.add(role);
285 return addRoleStatus;
289 public int addNamespace(AafNamespace namespace) {
290 this.namespace = namespace;
291 return addNamespaceStatus;
294 void givenReturnCode(int status) {
295 this.addNamespaceStatus = status;
296 this.addGrantStatus = status;
297 this.addRoleStatus = status;
298 this.addPermStatus = status;
301 void givenAddNamespaceStatus(int addNamespaceStatus) {
302 this.addNamespaceStatus = addNamespaceStatus;
305 void givenAddGrantStatus(int addGrantStatus) {
306 this.addGrantStatus = addGrantStatus;
309 void givenAddRoleStatus(int addRoleStatus) {
310 this.addRoleStatus = addRoleStatus;
313 void givenAddPermStatus(int addPermStatus) {
314 this.addPermStatus = addPermStatus;
317 void shouldAddPerm(DmaapPerm perm) {
318 assertTrue(perms.contains(perm));
321 void shouldAddNamespace(AafNamespace namespace) {
322 assertEquals(namespace, this.namespace);
325 void shouldAddRole(AafRole role) {
326 assertTrue(roles.contains(role));
329 void shouldAddGrant(DmaapGrant grant) {
330 assertTrue(grants.contains(grant));
333 void shouldHaveNoRolesAndGrants() {
334 assertTrue(this.grants.isEmpty());
335 assertTrue(this.roles.isEmpty());