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.aaf;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.BDDMockito.given;
25 import static org.mockito.BDDMockito.then;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.verifyZeroInteractions;
31 import junitparams.JUnitParamsRunner;
32 import junitparams.Parameters;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
39 @RunWith(JUnitParamsRunner.class)
40 public class AafServiceImplTest {
42 private static final String AAF_URL = "https://aaf.url/";
43 private static final String IDENTITY = "dmaap-bc@onap.org";
44 private static final boolean USE_AAF = true;
45 private static final int CREATED = 201;
46 private static final int OK = 200;
48 private AafConnection aafConnection;
49 private AafServiceImpl aafService;
52 public void setUp() throws Exception {
53 System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
54 MockitoAnnotations.initMocks(this);
55 given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(CREATED);
56 given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(OK);
57 aafService = new AafServiceImpl(USE_AAF, AAF_URL, IDENTITY, aafConnection);
61 public void shouldReturnCorrectIdentity() {
62 assertEquals(IDENTITY, aafService.getIdentity());
66 public void shouldAddPermission() {
67 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
69 int status = aafService.addPerm(perm);
71 then(aafConnection).should().postAaf(perm, AAF_URL + "authz/perm");
72 assertEquals(CREATED, status);
77 public void shouldAddDmaapGrant() {
78 DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
80 int status = aafService.addGrant(grant);
82 then(aafConnection).should().postAaf(grant, AAF_URL + "authz/role/perm");
83 assertEquals(CREATED, status);
87 public void shouldAddUserRole() {
88 AafUserRole userRole = new AafUserRole("ident", "role");
90 int status = aafService.addUserRole(userRole);
92 then(aafConnection).should().postAaf(userRole, AAF_URL + "authz/userRole");
93 assertEquals(CREATED, status);
97 public void shouldAddRole() {
98 AafRole role = new AafRole("ns", "role");
100 int status = aafService.addRole(role);
102 then(aafConnection).should().postAaf(role, AAF_URL + "authz/role");
103 assertEquals(CREATED, status);
107 public void shouldAddNamespace() {
108 AafNamespace ns = new AafNamespace("ns", "ident");
110 int status = aafService.addNamespace(ns);
112 then(aafConnection).should().postAaf(ns, AAF_URL + "authz/ns");
113 assertEquals(CREATED, status);
117 public void shouldNotConnectToAafDuringCreate() {
118 aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
119 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
121 int status = aafService.addPerm(perm);
123 verifyZeroInteractions(aafConnection);
124 assertEquals(CREATED, status);
128 @Parameters({"401", "403", "409", "200", "500"})
129 public void shouldHandleErrorDuringCreate(int aafServiceReturnedCode) {
130 given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
131 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
133 int status = aafService.addPerm(perm);
135 assertEquals(aafServiceReturnedCode, status);
139 @Parameters({"401", "403", "404", "200", "500"})
140 public void shouldHandleErrorDuringDelete(int aafServiceReturnedCode) {
141 given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
142 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
144 int status = aafService.delPerm(perm, false);
146 assertEquals(aafServiceReturnedCode, status);
150 public void shouldDeletePermission() {
151 DmaapPerm perm = new DmaapPerm("permName", "type", "action");
153 int status = aafService.delPerm(perm, false);
155 then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action"));
156 assertEquals(OK, status);
160 public void shouldDeletePermissionWithForce() {
161 DmaapPerm perm = new DmaapPerm("permName", "type", "action");
163 int status = aafService.delPerm(perm, true);
165 then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action?force=true"));
166 assertEquals(OK, status);
170 public void shouldDeleteNamespace() {
171 AafNamespace ns = new AafNamespace("nsName", "ident");
173 int status = aafService.delNamespace(ns, false);
175 then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName"));
176 assertEquals(OK, status);
180 public void shouldDeleteNamespaceWithForce() {
181 AafNamespace ns = new AafNamespace("nsName", "ident");
183 int status = aafService.delNamespace(ns, true);
185 then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName?force=true"));
186 assertEquals(OK, status);
190 public void shouldReturnExpectedCodeDuringPostWhenUseAffIsSetToFalse() {
191 aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
192 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
194 int status = aafService.addPerm(perm);
196 assertEquals(CREATED, status);
200 public void shouldReturnExpectedCodeDuringDeleteWhenUseAffIsSetToFalse() {
201 aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
202 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
204 int status = aafService.delPerm(perm, false);
206 assertEquals(OK, status);