a250c901f131030c6f5a38c11bd3f4f6b87cb6b6
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / service / AafTopicSetupServiceTest.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 package org.onap.dmaap.dbcapi.service;
21
22 import junitparams.JUnitParamsRunner;
23 import junitparams.Parameters;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.dmaap.dbcapi.aaf.AafNamespace;
30 import org.onap.dmaap.dbcapi.aaf.AafRole;
31 import org.onap.dmaap.dbcapi.aaf.AafService;
32 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
33 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
34 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
35 import org.onap.dmaap.dbcapi.model.ApiError;
36 import org.onap.dmaap.dbcapi.model.Dmaap;
37 import org.onap.dmaap.dbcapi.model.Topic;
38
39 import java.util.List;
40
41 import static com.google.common.collect.Lists.newArrayList;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.BDDMockito.given;
45
46 @RunWith(JUnitParamsRunner.class)
47 public class AafTopicSetupServiceTest {
48
49     private static final int INTERNAL_SERVER_ERROR = 500;
50     private static final int NOT_FOUND = 404;
51     private static final int CREATED = 201;
52     private static final String TOPIC_NS_ROOT = "org.onap.dmaap.mr";
53     private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
54     private static final String TOPIC_FQTN = "org.onap.dmaap.mr.sample_topic";
55     private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
56     private AafServiceStub aafService = new AafServiceStub();
57     @Mock
58     private DmaapService dmaapService;
59     private AafTopicSetupService aafTopicSetupService;
60
61     @Before
62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64         Dmaap dmaap = new Dmaap();
65         dmaap.setTopicNsRoot(TOPIC_NS_ROOT);
66         given(dmaapService.getDmaap()).willReturn(dmaap);
67         given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
68         aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, true);
69     }
70
71     @Test
72     @Parameters({"201", "409"})
73     public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
74         aafService.givenReturnCode(aafServiceReturnedCode);
75
76         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
77
78         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
79         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
80         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
81     }
82
83     @Test
84     public void shouldReturnOkStatusWhenNoError() {
85         aafService.givenReturnCode(201);
86
87         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
88
89         assertOkStatus(apiError);
90     }
91
92     @Test
93     @Parameters({"201", "409"})
94     public void shouldAddNamespace(int aafServiceReturnedCode) {
95         aafService.givenReturnCode(aafServiceReturnedCode);
96         Topic topic = givenTopic(TOPIC_FQTN);
97
98         aafTopicSetupService.aafTopicSetup(topic);
99
100         AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
101         aafService.shouldAddNamespace(namespace);
102     }
103
104     @Test
105     @Parameters({"201", "409"})
106     public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
107         aafService.givenReturnCode(aafServiceReturnedCode);
108         Topic topic = givenTopic(TOPIC_FQTN);
109
110         aafTopicSetupService.aafTopicSetup(topic);
111
112         AafRole role = new AafRole(TOPIC_FQTN, "publisher");
113         aafService.shouldAddRole(role);
114         assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
115     }
116
117     @Test
118     @Parameters({"201", "409"})
119     public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
120         aafService.givenReturnCode(aafServiceReturnedCode);
121         Topic topic = givenTopic(TOPIC_FQTN);
122
123         aafTopicSetupService.aafTopicSetup(topic);
124
125         AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
126         aafService.shouldAddRole(role);
127         assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
128     }
129
130     @Test
131     @Parameters({"201", "409"})
132     public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
133         aafService.givenReturnCode(aafServiceReturnedCode);
134
135         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
136
137         AafRole role = new AafRole(TOPIC_FQTN, "publisher");
138         DmaapPerm pubPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub");
139         DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
140         aafService.shouldAddGrant(new DmaapGrant(pubPerm, role.getFullyQualifiedRole()));
141         aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
142     }
143
144     @Test
145     @Parameters({"201", "409"})
146     public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
147         aafService.givenReturnCode(aafServiceReturnedCode);
148
149         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
150
151         AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
152         DmaapPerm subPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub");
153         DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
154         aafService.shouldAddGrant(new DmaapGrant(subPerm, role.getFullyQualifiedRole()));
155         aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
156     }
157
158     @Test
159     public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
160         aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, false);
161
162         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
163
164         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
165         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
166         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
167         aafService.shouldHaveNoRolesAndGrants();
168     }
169
170     @Test
171     public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
172
173         String topicFqtn = "sample_topic";
174         aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));
175
176         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
177         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
178         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
179         aafService.shouldHaveNoRolesAndGrants();
180     }
181
182     @Test
183     public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
184         Dmaap dmaap = new Dmaap();
185         dmaap.setTopicNsRoot(null);
186         given(dmaapService.getDmaap()).willReturn(dmaap);
187
188         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
189
190         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
191     }
192
193     @Test
194     public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
195         aafService.givenAddPermStatus(NOT_FOUND);
196
197         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
198
199         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
200     }
201
202     @Test
203     public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
204         aafService.givenAddNamespaceStatus(NOT_FOUND);
205
206         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
207
208         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
209     }
210
211     @Test
212     public void shouldHandleExceptionWhenRoleCreationWasFailed() {
213         aafService.givenAddRoleStatus(NOT_FOUND);
214
215         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
216
217         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
218     }
219
220     @Test
221     public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
222         aafService.givenAddGrantStatus(NOT_FOUND);
223
224         ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
225
226         assertErrorStatus(apiError, NOT_FOUND);
227     }
228
229     private Topic givenTopic(String topicFqtn) {
230         Topic topic = new Topic();
231         topic.setFqtn(topicFqtn);
232         return topic;
233     }
234
235     private void assertOkStatus(ApiError apiError) {
236         assertTrue(apiError.is2xx());
237         assertEquals("OK", apiError.getMessage());
238     }
239
240     private void assertErrorStatus(ApiError apiError, int code) {
241         assertEquals(code, apiError.getCode());
242     }
243
244     private class AafServiceStub implements AafService {
245
246         private AafNamespace namespace;
247         private List<DmaapPerm> perms = newArrayList();
248         private List<AafRole> roles = newArrayList();
249         private List<DmaapGrant> grants = newArrayList();
250         private int addNamespaceStatus = CREATED;
251         private int addGrantStatus = CREATED;
252         private int addRoleStatus = CREATED;
253         private int addPermStatus = CREATED;
254
255         @Override
256         public String getIdentity() {
257             return IDENTITY;
258         }
259
260         @Override
261         public int addPerm(DmaapPerm perm) {
262             this.perms.add(perm);
263             return addPermStatus;
264         }
265
266         @Override
267         public int addGrant(DmaapGrant grant) {
268             grants.add(grant);
269             return addGrantStatus;
270         }
271
272         @Override
273         public int addUserRole(AafUserRole ur) {
274             throw new UnsupportedOperationException();
275         }
276
277         @Override
278         public int delGrant(DmaapGrant grant) {
279             throw new UnsupportedOperationException();
280         }
281
282         @Override
283         public int addRole(AafRole role) {
284             this.roles.add(role);
285             return addRoleStatus;
286         }
287
288         @Override
289         public int addNamespace(AafNamespace namespace) {
290             this.namespace = namespace;
291             return addNamespaceStatus;
292         }
293
294         void givenReturnCode(int status) {
295             this.addNamespaceStatus = status;
296             this.addGrantStatus = status;
297             this.addRoleStatus = status;
298             this.addPermStatus = status;
299         }
300
301         void givenAddNamespaceStatus(int addNamespaceStatus) {
302             this.addNamespaceStatus = addNamespaceStatus;
303         }
304
305         void givenAddGrantStatus(int addGrantStatus) {
306             this.addGrantStatus = addGrantStatus;
307         }
308
309         void givenAddRoleStatus(int addRoleStatus) {
310             this.addRoleStatus = addRoleStatus;
311         }
312
313         void givenAddPermStatus(int addPermStatus) {
314             this.addPermStatus = addPermStatus;
315         }
316
317         void shouldAddPerm(DmaapPerm perm) {
318             assertTrue(perms.contains(perm));
319         }
320
321         void shouldAddNamespace(AafNamespace namespace) {
322             assertEquals(namespace, this.namespace);
323         }
324
325         void shouldAddRole(AafRole role) {
326             assertTrue(roles.contains(role));
327         }
328
329         void shouldAddGrant(DmaapGrant grant) {
330             assertTrue(grants.contains(grant));
331         }
332
333         void shouldHaveNoRolesAndGrants() {
334             assertTrue(this.grants.isEmpty());
335             assertTrue(this.roles.isEmpty());
336         }
337     }
338 }