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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import junitparams.JUnitParamsRunner;
24 import junitparams.Parameters;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.onap.dmaap.dbcapi.aaf.AafService;
31 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
32 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
33 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.MR_Client;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.BDDMockito.given;
40 import static org.mockito.BDDMockito.then;
41 import static org.mockito.Mockito.verifyZeroInteractions;
42 import static org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status.INVALID;
43 import static org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status.VALID;
45 @RunWith(JUnitParamsRunner.class)
46 public class AafPermissionServiceTest {
48 private static final String ROLE = "dmaap.mr.demoTopic.publisher";
49 private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
50 private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
51 private static final String FQTN = "org.onap.dmaap.mr.demoTopic";
52 private static final String PUB_ACTION = "pub";
53 private static final int INTERNAL_SERVER_ERROR = 500;
55 private AafService aafService;
57 private DmaapService dmaapService;
59 private MR_Client mrClient;
60 private AafPermissionService aafPermissionService;
63 public void setUp() throws Exception {
64 MockitoAnnotations.initMocks(this);
65 aafPermissionService = new AafPermissionService(aafService, dmaapService);
66 given(mrClient.getClientIdentity()).willReturn(IDENTITY);
67 given(mrClient.getFqtn()).willReturn(FQTN);
68 given(mrClient.getAction()).willReturn(new String[]{PUB_ACTION});
69 given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
73 @Parameters({"201", "409"})
74 public void shouldAssignClientToRole(int aafServiceReturnedCode) {
75 AafUserRole userRole = new AafUserRole(IDENTITY, ROLE);
76 given(aafService.addUserRole(userRole)).willReturn(aafServiceReturnedCode);
78 ApiError apiError = aafPermissionService.assignClientToRole(mrClient, ROLE);
80 then(aafService).should().addUserRole(userRole);
81 then(mrClient).should().setStatus(VALID);
82 assertOkStatus(apiError);
86 public void shouldReturnErrorStatusWhenClientWasNotAssignedToRole() {
87 AafUserRole userRole = new AafUserRole(IDENTITY, ROLE);
88 given(aafService.addUserRole(userRole)).willReturn(INTERNAL_SERVER_ERROR);
90 ApiError apiError = aafPermissionService.assignClientToRole(mrClient, ROLE);
92 then(mrClient).should().setStatus(INVALID);
93 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
97 @Parameters({"201", "409"})
98 public void shouldGrantActionPermissionForClientRole(int aafServiceReturnedCode) {
99 DmaapGrant grant = new DmaapGrant(new DmaapPerm(TOPIC_PERM, ":topic." + FQTN, PUB_ACTION), ROLE);
100 given(mrClient.getClientRole()).willReturn(ROLE);
101 given(aafService.addGrant(grant)).willReturn(aafServiceReturnedCode);
103 ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
105 then(aafService).should().addGrant(grant);
106 then(mrClient).should().setStatus(VALID);
107 assertOkStatus(apiError);
111 public void shouldReturnErrorStatusWhenPermissionWasNotGrantToRole() {
112 DmaapGrant grant = new DmaapGrant(new DmaapPerm(TOPIC_PERM, ":topic." + FQTN, PUB_ACTION), ROLE);
113 given(mrClient.getClientRole()).willReturn(ROLE);
114 given(aafService.addGrant(grant)).willReturn(INTERNAL_SERVER_ERROR);
116 ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
118 then(mrClient).should().setStatus(INVALID);
119 assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
123 public void shouldReturnOkStatusWhenClientRoleIsNull() {
124 given(mrClient.getClientRole()).willReturn(null);
126 ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
128 verifyZeroInteractions(aafService);
129 then(mrClient).should().setStatus(VALID);
130 assertOkStatus(apiError);
133 private void assertErrorStatus(ApiError apiError, int code) {
134 assertEquals(code, apiError.getCode());
137 private void assertOkStatus(ApiError apiError) {
138 assertTrue(apiError.is2xx());
139 assertEquals("OK", apiError.getMessage());