aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/test/java/org/onap/dmaap/dbcapi/service/AafTopicSetupServiceTest.java
blob: aaac537a41a147caee7a7c31409fde4a5eba64d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dmaap.dbcapi.service;

import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;

import java.util.List;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.dmaap.dbcapi.aaf.AafNamespace;
import org.onap.dmaap.dbcapi.aaf.AafRole;
import org.onap.dmaap.dbcapi.aaf.AafService;
import org.onap.dmaap.dbcapi.aaf.AafUserRole;
import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.Dmaap;
import org.onap.dmaap.dbcapi.model.Topic;
import org.onap.dmaap.dbcapi.util.DmaapConfig;

@RunWith(JUnitParamsRunner.class)
public class AafTopicSetupServiceTest {

    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int NOT_FOUND = 404;
    private static final int CREATED = 201;
    private static final int OK = 200;
    private static final String TOPIC_NS_ROOT = "org.onap.dmaap.mr";
    private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
    private static final String TOPIC_FQTN = "org.onap.dmaap.mr.sample_topic";
    private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
    private AafServiceStub aafService = new AafServiceStub();
    @Mock
    private DmaapService dmaapService;
    @Mock
    private DmaapConfig dmaapConfig;
    private AafTopicSetupService aafTopicSetupService;

    @Before
    public void setUp() throws Exception {
        System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
        MockitoAnnotations.initMocks(this);
        Dmaap dmaap = new Dmaap();
        dmaap.setTopicNsRoot(TOPIC_NS_ROOT);
        given(dmaapService.getDmaap()).willReturn(dmaap);
        given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("true");
        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("2");
        aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, dmaapConfig);
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldCreatePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);

        aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
    }

    @Test
    public void shouldReturnOkStatusWhenNoError() {
        aafService.givenReturnCode(201);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertOkStatus(apiError);
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldAddNamespace(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);
        Topic topic = givenTopic(TOPIC_FQTN);

        aafTopicSetupService.aafTopicSetup(topic);

        AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
        aafService.shouldAddNamespace(namespace);
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldCretePublisherRoleAndSetItToTopic(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);
        Topic topic = givenTopic(TOPIC_FQTN);

        aafTopicSetupService.aafTopicSetup(topic);

        AafRole role = new AafRole(TOPIC_FQTN, "publisher");
        aafService.shouldAddRole(role);
        assertEquals(role.getFullyQualifiedRole(), topic.getPublisherRole());
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldCreteSubscriberRoleAndSetItToTopic(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);
        Topic topic = givenTopic(TOPIC_FQTN);

        aafTopicSetupService.aafTopicSetup(topic);

        AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
        aafService.shouldAddRole(role);
        assertEquals(role.getFullyQualifiedRole(), topic.getSubscriberRole());
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldGrantPubAndViewPermissionToPublisherRole(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);

        aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        AafRole role = new AafRole(TOPIC_FQTN, "publisher");
        DmaapPerm pubPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub");
        DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
        aafService.shouldAddGrant(new DmaapGrant(pubPerm, role.getFullyQualifiedRole()));
        aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
    }

    @Test
    @Parameters({"201", "409"})
    public void shouldGrantSubAndViewPermissionToSubscriberRole(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);

        aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        AafRole role = new AafRole(TOPIC_FQTN, "subscriber");
        DmaapPerm subPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub");
        DmaapPerm viewPerm = new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view");
        aafService.shouldAddGrant(new DmaapGrant(subPerm, role.getFullyQualifiedRole()));
        aafService.shouldAddGrant(new DmaapGrant(viewPerm, role.getFullyQualifiedRole()));
    }

    @Test
    public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");

        aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
        aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
    }

    @Test
    public void shouldCreateOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {

        String topicFqtn = "sample_topic";
        aafTopicSetupService.aafTopicSetup(givenTopic(topicFqtn));

        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
        aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
        aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
    }

    @Test
    public void shouldHandleExceptionWhenTopicSnRootIsNotDefined() {
        Dmaap dmaap = new Dmaap();
        dmaap.setTopicNsRoot(null);
        given(dmaapService.getDmaap()).willReturn(dmaap);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldHandleExceptionWhenPermissionCreationWasFailed() {
        aafService.givenAddPermStatus(NOT_FOUND);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldHandleExceptionWhenNamespaceCreationWasFailed() {
        aafService.givenAddNamespaceStatus(NOT_FOUND);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldHandleExceptionWhenRoleCreationWasFailed() {
        aafService.givenAddRoleStatus(NOT_FOUND);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldHandleExceptionWhenGrantPermToRoleWasFailed() {
        aafService.givenAddGrantStatus(NOT_FOUND);

        ApiError apiError = aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, NOT_FOUND);
    }

    @Test
    @Parameters({"200", "404"})
    public void shouldremovePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);

        aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
    }

    @Test
    @Parameters({"200", "404"})
    public void shouldRemoveNamespace(int aafServiceReturnedCode) {
        aafService.givenReturnCode(aafServiceReturnedCode);
        Topic topic = givenTopic(TOPIC_FQTN);

        aafTopicSetupService.aafTopicCleanup(topic);

        AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
        aafService.shouldRemoveNamespace(namespace);
    }

    @Test
    public void shouldRemoveOnlyPermissionsWhenCreateTopicRolesIsFalse() {
        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");

        aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
        aafService.shouldNotRemoveNamespace();
    }

    @Test
    public void shouldRemoveOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {

        String topicFqtn = "sample_topic";
        aafTopicSetupService.aafTopicCleanup(givenTopic(topicFqtn));

        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
        aafService.shouldNotRemoveNamespace();
    }

    @Test
    public void shouldHandleExceptionWhenPermissionRemovalWasFailed() {
        aafService.givenRemovePermStatus(INTERNAL_SERVER_ERROR);

        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldHandleExceptionWhenNamespaceRemovalWasFailed() {
        aafService.givenRemoveNamespaceStatus(INTERNAL_SERVER_ERROR);

        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
    }

    @Test
    public void shouldNotPerformCleanupWhenDeleteLevelIsLessThanTwo() {
        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("0");

        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        aafService.shouldNotPerformCleanup();
        assertOkStatus(apiError);
    }

    @Test
    public void shouldNotPerformCleanupWhenDeleteLevelIsNotNumericValue() {
        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("not number");

        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));

        aafService.shouldNotPerformCleanup();
        assertOkStatus(apiError);
    }

    private Topic givenTopic(String topicFqtn) {
        Topic topic = new Topic();
        topic.setFqtn(topicFqtn);
        return topic;
    }

    private void assertOkStatus(ApiError apiError) {
        assertTrue(apiError.is2xx());
        assertEquals("OK", apiError.getMessage());
    }

    private void assertErrorStatus(ApiError apiError, int code) {
        assertEquals(code, apiError.getCode());
    }

    private class AafServiceStub implements AafService {

        private AafNamespace addedNamespace;
        private AafNamespace removedNamespace;
        private List<DmaapPerm> addedPerms = newArrayList();
        private List<DmaapPerm> removedPerms = newArrayList();
        private List<AafRole> addedRoles = newArrayList();
        private List<DmaapGrant> addedGrants = newArrayList();
        private int addNamespaceStatus = CREATED;
        private int addGrantStatus = CREATED;
        private int addRoleStatus = CREATED;
        private int addPermStatus = CREATED;
        private int removePermStatus = OK;
        private int removeNamespaceStatus = OK;

        @Override
        public String getIdentity() {
            return IDENTITY;
        }

        @Override
        public int addPerm(DmaapPerm perm) {
            this.addedPerms.add(perm);
            return addPermStatus;
        }

        @Override
        public int delPerm(DmaapPerm perm, boolean force) {
            removedPerms.add(perm);
            return removePermStatus;
        }

        @Override
        public int addGrant(DmaapGrant grant) {
            addedGrants.add(grant);
            return addGrantStatus;
        }

        @Override
        public int addUserRole(AafUserRole ur) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int addRole(AafRole role) {
            this.addedRoles.add(role);
            return addRoleStatus;
        }

        @Override
        public int addNamespace(AafNamespace namespace) {
            this.addedNamespace = namespace;
            return addNamespaceStatus;
        }

        @Override
        public int delNamespace(AafNamespace namespace, boolean force) {
            this.removedNamespace = namespace;
            return removeNamespaceStatus;
        }

        void givenReturnCode(int status) {
            this.addNamespaceStatus = status;
            this.addGrantStatus = status;
            this.addRoleStatus = status;
            this.addPermStatus = status;
            this.removePermStatus = status;
            this.removeNamespaceStatus = status;
        }

        void givenAddNamespaceStatus(int addNamespaceStatus) {
            this.addNamespaceStatus = addNamespaceStatus;
        }

        void givenRemoveNamespaceStatus(int removeNamespaceStatus) {
            this.removeNamespaceStatus = removeNamespaceStatus;
        }

        void givenAddGrantStatus(int addGrantStatus) {
            this.addGrantStatus = addGrantStatus;
        }

        void givenAddRoleStatus(int addRoleStatus) {
            this.addRoleStatus = addRoleStatus;
        }

        void givenAddPermStatus(int addPermStatus) {
            this.addPermStatus = addPermStatus;
        }

        void givenRemovePermStatus(int removePermStatus) {
            this.removePermStatus = removePermStatus;
        }

        void shouldAddPerm(DmaapPerm perm) {
            assertTrue(addedPerms.contains(perm));
        }

        void shouldRemovePerm(DmaapPerm perm) {
            assertTrue(removedPerms.contains(perm));
        }

        void shouldAddNamespace(AafNamespace namespace) {
            assertEquals(namespace, this.addedNamespace);
        }

        void shouldRemoveNamespace(AafNamespace namespace) {
            assertEquals(namespace, this.removedNamespace);
        }

        void shouldAddRole(AafRole role) {
            assertTrue(addedRoles.contains(role));
        }

        void shouldAddGrant(DmaapGrant grant) {
            assertTrue(addedGrants.contains(grant));
        }

        void shouldHaveNoNamespaceRolesAndGrantsAdded() {
            assertNull(this.addedNamespace);
            assertTrue(this.addedGrants.isEmpty());
            assertTrue(this.addedRoles.isEmpty());
        }

        void shouldNotRemoveNamespace() {
            assertNull(this.removedNamespace);
        }

        void shouldNotPerformCleanup() {
            shouldNotRemoveNamespace();
            assertTrue(removedPerms.isEmpty());
        }
    }
}