[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / service / AafPermissionServiceTest.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.service;
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 import org.onap.dmaap.dbcapi.aaf.AafService;
31 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
32 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
33 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.MR_Client;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.BDDMockito.given;
40 import static org.mockito.BDDMockito.then;
41 import static org.mockito.Mockito.verifyZeroInteractions;
42 import static org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status.INVALID;
43 import static org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status.VALID;
44
45 @RunWith(JUnitParamsRunner.class)
46 public class AafPermissionServiceTest {
47
48     private static final String ROLE = "dmaap.mr.demoTopic.publisher";
49     private static final String IDENTITY = "dmaap-bc@dmaap-bc.onap.org";
50     private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
51     private static final String FQTN = "org.onap.dmaap.mr.demoTopic";
52     private static final String PUB_ACTION = "pub";
53     private static final int INTERNAL_SERVER_ERROR = 500;
54     @Mock
55     private AafService aafService;
56     @Mock
57     private DmaapService dmaapService;
58     @Mock
59     private MR_Client mrClient;
60     private AafPermissionService aafPermissionService;
61
62     @Before
63     public void setUp() throws Exception {
64         MockitoAnnotations.initMocks(this);
65         aafPermissionService = new AafPermissionService(aafService, dmaapService);
66         given(mrClient.getClientIdentity()).willReturn(IDENTITY);
67         given(mrClient.getFqtn()).willReturn(FQTN);
68         given(mrClient.getAction()).willReturn(new String[]{PUB_ACTION});
69         given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
70     }
71
72     @Test
73     @Parameters({"201", "409"})
74     public void shouldAssignClientToRole(int aafServiceReturnedCode) {
75         AafUserRole userRole = new AafUserRole(IDENTITY, ROLE);
76         given(aafService.addUserRole(userRole)).willReturn(aafServiceReturnedCode);
77
78         ApiError apiError = aafPermissionService.assignClientToRole(mrClient, ROLE);
79
80         then(aafService).should().addUserRole(userRole);
81         then(mrClient).should().setStatus(VALID);
82         assertOkStatus(apiError);
83     }
84
85     @Test
86     public void shouldReturnErrorStatusWhenClientWasNotAssignedToRole() {
87         AafUserRole userRole = new AafUserRole(IDENTITY, ROLE);
88         given(aafService.addUserRole(userRole)).willReturn(INTERNAL_SERVER_ERROR);
89
90         ApiError apiError = aafPermissionService.assignClientToRole(mrClient, ROLE);
91
92         then(mrClient).should().setStatus(INVALID);
93         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
94     }
95
96     @Test
97     @Parameters({"201", "409"})
98     public void shouldGrantActionPermissionForClientRole(int aafServiceReturnedCode) {
99         DmaapGrant grant = new DmaapGrant(new DmaapPerm(TOPIC_PERM, ":topic." + FQTN, PUB_ACTION), ROLE);
100         given(mrClient.getClientRole()).willReturn(ROLE);
101         given(aafService.addGrant(grant)).willReturn(aafServiceReturnedCode);
102
103         ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
104
105         then(aafService).should().addGrant(grant);
106         then(mrClient).should().setStatus(VALID);
107         assertOkStatus(apiError);
108     }
109
110     @Test
111     public void shouldReturnErrorStatusWhenPermissionWasNotGrantToRole() {
112         DmaapGrant grant = new DmaapGrant(new DmaapPerm(TOPIC_PERM, ":topic." + FQTN, PUB_ACTION), ROLE);
113         given(mrClient.getClientRole()).willReturn(ROLE);
114         given(aafService.addGrant(grant)).willReturn(INTERNAL_SERVER_ERROR);
115
116         ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
117
118         then(mrClient).should().setStatus(INVALID);
119         assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
120     }
121
122     @Test
123     public void shouldReturnOkStatusWhenClientRoleIsNull() {
124         given(mrClient.getClientRole()).willReturn(null);
125
126         ApiError apiError = aafPermissionService.grantClientRolePerms(mrClient);
127
128         verifyZeroInteractions(aafService);
129         then(mrClient).should().setStatus(VALID);
130         assertOkStatus(apiError);
131     }
132
133     private void assertErrorStatus(ApiError apiError, int code) {
134         assertEquals(code, apiError.getCode());
135     }
136
137     private void assertOkStatus(ApiError apiError) {
138         assertTrue(apiError.is2xx());
139         assertEquals("OK", apiError.getMessage());
140     }
141 }