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 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;
31 import static org.junit.Assert.assertEquals;
32 import static org.mockito.BDDMockito.given;
33 import static org.mockito.BDDMockito.then;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Mockito.verifyZeroInteractions;
38 @RunWith(JUnitParamsRunner.class)
39 public class AafServiceImplTest {
41 private static final String AAF_URL = "https://aaf.url/";
42 private static final String IDENTITY = "dmaap-bc@onap.org";
43 private static final boolean USE_AAF = true;
44 private static final int CREATED = 201;
45 private static final int OK = 200;
47 private AafConnection aafConnection;
48 private AafServiceImpl aafService;
51 public void setUp() throws Exception {
52 MockitoAnnotations.initMocks(this);
53 given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(CREATED);
54 given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(OK);
55 aafService = new AafServiceImpl(USE_AAF, AAF_URL, IDENTITY, aafConnection);
59 public void shouldReturnCorrectIdentity() {
61 assertEquals(IDENTITY, aafService.getIdentity());
65 public void shouldAddPermission() {
66 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
68 int status = aafService.addPerm(perm);
70 then(aafConnection).should().postAaf(perm, AAF_URL + "authz/perm");
71 assertEquals(CREATED, status);
76 public void shouldAddDmaapGrant() {
77 DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
79 int status = aafService.addGrant(grant);
81 then(aafConnection).should().postAaf(grant, AAF_URL + "authz/role/perm");
82 assertEquals(CREATED, status);
86 public void shouldAddUserRole() {
87 AafUserRole userRole = new AafUserRole("ident", "role");
89 int status = aafService.addUserRole(userRole);
91 then(aafConnection).should().postAaf(userRole, AAF_URL + "authz/userRole");
92 assertEquals(CREATED, status);
96 public void shouldAddRole() {
97 AafRole role = new AafRole("ns", "role");
99 int status = aafService.addRole(role);
101 then(aafConnection).should().postAaf(role, AAF_URL + "authz/role");
102 assertEquals(CREATED, status);
106 public void shouldAddNamespace() {
107 AafNamespace ns = new AafNamespace("ns", "ident");
109 int status = aafService.addNamespace(ns);
111 then(aafConnection).should().postAaf(ns, AAF_URL + "authz/ns");
112 assertEquals(CREATED, status);
116 public void shouldDeleteDmaapGrant() {
117 DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
119 int status = aafService.delGrant(grant);
121 then(aafConnection).should().delAaf(grant, AAF_URL + "authz/role/:" + grant.getRole() + "/perm");
122 assertEquals(OK, status);
126 public void shouldNotConnectToAafDuringCreate() {
127 aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
128 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
130 int status = aafService.addPerm(perm);
132 verifyZeroInteractions(aafConnection);
133 assertEquals(CREATED, status);
137 public void shouldNotConnectToAafDuringDelete() {
138 aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
139 DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
141 int status = aafService.delGrant(grant);
143 verifyZeroInteractions(aafConnection);
144 assertEquals(OK, status);
148 @Parameters({"401", "403", "409", "200", "500"})
149 public void shouldHandleErrorDuringCreate(int aafServiceReturnedCode) {
150 given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
151 DmaapPerm perm = new DmaapPerm("perm", "type", "action");
153 int status = aafService.addPerm(perm);
155 assertEquals(aafServiceReturnedCode, status);
159 @Parameters({"401", "403", "404", "200", "500"})
160 public void shouldHandleErrorDuringDelete(int aafServiceReturnedCode) {
161 given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
162 DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
164 int status = aafService.delGrant(grant);
166 assertEquals(aafServiceReturnedCode, status);