[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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 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;
30
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;
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 shouldNotConnectToAafDuringCreate() {
118         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
119         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
120
121         int status = aafService.addPerm(perm);
122
123         verifyZeroInteractions(aafConnection);
124         assertEquals(CREATED, status);
125     }
126
127     @Test
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");
132
133         int status = aafService.addPerm(perm);
134
135         assertEquals(aafServiceReturnedCode, status);
136     }
137
138     @Test
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");
143
144         int status = aafService.delPerm(perm, false);
145
146         assertEquals(aafServiceReturnedCode, status);
147     }
148
149     @Test
150     public void shouldDeletePermission() {
151         DmaapPerm perm = new DmaapPerm("permName", "type", "action");
152
153         int status = aafService.delPerm(perm, false);
154
155         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action"));
156         assertEquals(OK, status);
157     }
158
159     @Test
160     public void shouldDeletePermissionWithForce() {
161         DmaapPerm perm = new DmaapPerm("permName", "type", "action");
162
163         int status = aafService.delPerm(perm, true);
164
165         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/perm/permName/type/action?force=true"));
166         assertEquals(OK, status);
167     }
168
169     @Test
170     public void shouldDeleteNamespace() {
171         AafNamespace ns = new AafNamespace("nsName", "ident");
172
173         int status = aafService.delNamespace(ns, false);
174
175         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName"));
176         assertEquals(OK, status);
177     }
178
179     @Test
180     public void shouldDeleteNamespaceWithForce() {
181         AafNamespace ns = new AafNamespace("nsName", "ident");
182
183         int status = aafService.delNamespace(ns, true);
184
185         then(aafConnection).should().delAaf(any(AafEmpty.class), eq(AAF_URL + "authz/ns/nsName?force=true"));
186         assertEquals(OK, status);
187     }
188
189     @Test
190     public void shouldReturnExpectedCodeDuringPostWhenUseAffIsSetToFalse() {
191         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
192         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
193
194         int status = aafService.addPerm(perm);
195
196         assertEquals(CREATED, status);
197     }
198
199     @Test
200     public void shouldReturnExpectedCodeDuringDeleteWhenUseAffIsSetToFalse() {
201         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
202         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
203
204         int status = aafService.delPerm(perm, false);
205
206         assertEquals(OK, status);
207     }
208 }