fd70a16e05fa1be13fdc4a3c147021dfca049c47
[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.Mockito.verifyZeroInteractions;
37
38 @RunWith(JUnitParamsRunner.class)
39 public class AafServiceImplTest {
40
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;
46     @Mock
47     private AafConnection aafConnection;
48     private AafServiceImpl aafService;
49
50     @Before
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);
56     }
57
58     @Test
59     public void shouldReturnCorrectIdentity() {
60
61         assertEquals(IDENTITY, aafService.getIdentity());
62     }
63
64     @Test
65     public void shouldAddPermission() {
66         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
67
68         int status = aafService.addPerm(perm);
69
70         then(aafConnection).should().postAaf(perm, AAF_URL + "authz/perm");
71         assertEquals(CREATED, status);
72     }
73
74
75     @Test
76     public void shouldAddDmaapGrant() {
77         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
78
79         int status = aafService.addGrant(grant);
80
81         then(aafConnection).should().postAaf(grant, AAF_URL + "authz/role/perm");
82         assertEquals(CREATED, status);
83     }
84
85     @Test
86     public void shouldAddUserRole() {
87         AafUserRole userRole = new AafUserRole("ident", "role");
88
89         int status = aafService.addUserRole(userRole);
90
91         then(aafConnection).should().postAaf(userRole, AAF_URL + "authz/userRole");
92         assertEquals(CREATED, status);
93     }
94
95     @Test
96     public void shouldAddRole() {
97         AafRole role = new AafRole("ns", "role");
98
99         int status = aafService.addRole(role);
100
101         then(aafConnection).should().postAaf(role, AAF_URL + "authz/role");
102         assertEquals(CREATED, status);
103     }
104
105     @Test
106     public void shouldAddNamespace() {
107         AafNamespace ns = new AafNamespace("ns", "ident");
108
109         int status = aafService.addNamespace(ns);
110
111         then(aafConnection).should().postAaf(ns, AAF_URL + "authz/ns");
112         assertEquals(CREATED, status);
113     }
114
115     @Test
116     public void shouldDeleteDmaapGrant() {
117         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
118
119         int status = aafService.delGrant(grant);
120
121         then(aafConnection).should().delAaf(grant, AAF_URL + "authz/role/:" + grant.getRole() + "/perm");
122         assertEquals(OK, status);
123     }
124
125     @Test
126     public void shouldNotConnectToAafDuringCreate() {
127         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
128         DmaapPerm perm = new DmaapPerm("perm", "type", "action");
129
130         int status = aafService.addPerm(perm);
131
132         verifyZeroInteractions(aafConnection);
133         assertEquals(CREATED, status);
134     }
135
136     @Test
137     public void shouldNotConnectToAafDuringDelete() {
138         aafService = new AafServiceImpl(false, AAF_URL, IDENTITY, aafConnection);
139         DmaapGrant grant = new DmaapGrant(new DmaapPerm("perm", "type", "action"), "roles");
140
141         int status = aafService.delGrant(grant);
142
143         verifyZeroInteractions(aafConnection);
144         assertEquals(OK, status);
145     }
146
147     @Test
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");
152
153         int status = aafService.addPerm(perm);
154
155         assertEquals(aafServiceReturnedCode, status);
156     }
157
158     @Test
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");
163
164         int status = aafService.delGrant(grant);
165
166         assertEquals(aafServiceReturnedCode, status);
167     }
168 }