deleting namespace and permission implementation
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / aaf / AafServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.aaf;
22
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
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.Matchers.eq;
37 import static org.mockito.Mockito.verifyZeroInteractions;
38
39 @RunWith(JUnitParamsRunner.class)
40 public class AafServiceImplTest {
41
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;
47     @Mock
48     private AafConnection aafConnection;
49     private AafServiceImpl aafService;
50
51     @Before
52     public void setUp() throws Exception {
53         MockitoAnnotations.initMocks(this);
54         given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(CREATED);
55         given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(OK);
56         aafService = new AafServiceImpl(USE_AAF, AAF_URL, IDENTITY, aafConnection);
57     }
58
59     @Test
60     public void shouldReturnCorrectIdentity() {
61
62         assertEquals(IDENTITY, aafService.getIdentity());
63     }
64
65     @Test
66     public void shouldAddPermission() {
67         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
68
69         int status = aafService.addPerm(perm);
70
71         then(aafConnection).should().postAaf(perm, AAF_URL + "authz/perm");
72         assertEquals(CREATED, status);
73     }
74
75
76     @Test
77     public void shouldAddDmaapGrant() {
78         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
79
80         int status = aafService.addGrant(grant);
81
82         then(aafConnection).should().postAaf(grant, AAF_URL + "authz/role/perm");
83         assertEquals(CREATED, status);
84     }
85
86     @Test
87     public void shouldAddUserRole() {
88         AafUserRole userRole = new AafUserRole("ident", "role");
89
90         int status = aafService.addUserRole(userRole);
91
92         then(aafConnection).should().postAaf(userRole, AAF_URL + "authz/userRole");
93         assertEquals(CREATED, status);
94     }
95
96     @Test
97     public void shouldAddRole() {
98         AafRole role = new AafRole("ns", "role");
99
100         int status = aafService.addRole(role);
101
102         then(aafConnection).should().postAaf(role, AAF_URL + "authz/role");
103         assertEquals(CREATED, status);
104     }
105
106     @Test
107     public void shouldAddNamespace() {
108         AafNamespace ns = new AafNamespace("ns", "ident");
109
110         int status = aafService.addNamespace(ns);
111
112         then(aafConnection).should().postAaf(ns, AAF_URL + "authz/ns");
113         assertEquals(CREATED, status);
114     }
115
116     @Test
117     public void shouldDeleteDmaapGrant() {
118         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
119
120         int status = aafService.delGrant(grant);
121
122         then(aafConnection).should().delAaf(grant, AAF_URL + "authz/role/:" + grant.getRole() + "/perm");
123         assertEquals(OK, status);
124     }
125
126     @Test
127     public void shouldNotConnectToAafDuringCreate() {
128         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
129         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
130
131         int status = aafService.addPerm(perm);
132
133         verifyZeroInteractions(aafConnection);
134         assertEquals(CREATED, status);
135     }
136
137     @Test
138     public void shouldNotConnectToAafDuringDelete() {
139         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
140         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
141
142         int status = aafService.delGrant(grant);
143
144         verifyZeroInteractions(aafConnection);
145         assertEquals(OK, status);
146     }
147
148     @Test
149     @Parameters({"401", "403", "409", "200", "500"})
150     public void shouldHandleErrorDuringCreate(int aafServiceReturnedCode) {
151         given(aafConnection.postAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
152         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
153
154         int status = aafService.addPerm(perm);
155
156         assertEquals(aafServiceReturnedCode, status);
157     }
158
159     @Test
160     @Parameters({"401", "403", "404", "200", "500"})
161     public void shouldHandleErrorDuringDelete(int aafServiceReturnedCode) {
162         given(aafConnection.delAaf(any(AafObject.class), anyString())).willReturn(aafServiceReturnedCode);
163         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
164
165         int status = aafService.delGrant(grant);
166
167         assertEquals(aafServiceReturnedCode, status);
168     }
169
170     @Test
171     public void shouldDeletePermission() {
172         DmaapPerm perm = new DmaapPerm("permName", "type", "action");
173
174         int status = aafService.delPerm(perm, false);
175
176         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action"));
177         assertEquals(OK, status);
178     }
179
180     @Test
181     public void shouldDeletePermissionWithForce() {
182         DmaapPerm perm = new DmaapPerm("permName", "type", "action");
183
184         int status = aafService.delPerm(perm, true);
185
186         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action?force=true"));
187         assertEquals(OK, status);
188     }
189
190     @Test
191     public void shouldDeleteNamespace() {
192         AafNamespace ns = new AafNamespace("nsName", "ident");
193
194         int status = aafService.delNamespace(ns, false);
195
196         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName"));
197         assertEquals(OK, status);
198     }
199
200     @Test
201     public void shouldDeleteNamespaceWithForce() {
202         AafNamespace ns = new AafNamespace("nsName", "ident");
203
204         int status = aafService.delNamespace(ns, true);
205
206         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName?force=true"));
207         assertEquals(OK, status);
208     }
209 }