CADI authentication and authorization filters
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / util / PermissionBuilderTest.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.util;
21
22 import static org.junit.Assert.*;
23 import static org.mockito.Mockito.atMost;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import javax.servlet.http.HttpServletRequest;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.onap.dmaap.dbcapi.model.Dmaap;
34 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
35 import org.onap.dmaap.dbcapi.service.DmaapService;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class PermissionBuilderTest {
39
40     private static final String DMAAP_NAME = "mr";
41     private PermissionBuilder permissionBuilder;
42     @Mock
43     private DmaapConfig dmaapConfig;
44     @Mock
45     private DmaapService dmaapService;
46     @Mock
47     private HttpServletRequest request;
48
49
50     @Test
51     public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsNotInitialized() {
52         //given
53         doReturn(null).when(dmaapService).getDmaap();
54         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
55
56         //when
57         permissionBuilder.updateDmaapInstance();
58
59         //then
60         assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
61     }
62
63     @Test
64     public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsInitializedWithDefaultInstance() {
65         //given
66         doReturn(provideDefaultInstance()).when(dmaapService).getDmaap();
67         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
68
69         //when
70         permissionBuilder.updateDmaapInstance();
71
72         //then
73         assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
74     }
75
76     @Test
77     public void updateDmaapInstance_shouldSetRealInstance_whenDmaapServiceProvidesOne() {
78         //given
79         when(dmaapService.getDmaap()).thenReturn(provideDefaultInstance(), provideRealInstance(DMAAP_NAME));
80         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
81
82         //when
83         permissionBuilder.updateDmaapInstance();
84
85         //then
86         assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
87     }
88
89     @Test
90     public void updateDmaapInstance_shouldNotUpdateDmaapInstance_whenAlreadyInitializedWithRealInstance() {
91         //given
92         when(dmaapService.getDmaap()).thenReturn(provideRealInstance(DMAAP_NAME), provideRealInstance("newName"));
93         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
94
95         //when
96         permissionBuilder.updateDmaapInstance();
97
98         //then
99         assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
100         verify(dmaapService, atMost(1)).getDmaap();
101     }
102
103     @Test
104     public void buildPermission_shouldBuildPermissionWithBootInstance() {
105         //given
106         String path = "/dmaap";
107         String method = "GET";
108         initPermissionBuilder(path, method, provideDefaultInstance());
109
110         //when
111         String permission = permissionBuilder.buildPermission(request);
112
113         //then
114         assertEquals("org.onap.dmaap-bc.api.dmaap|boot|GET", permission);
115     }
116
117     @Test
118     public void buildPermission_shouldBuildPermissionWithRealInstance() {
119         //given
120         String path = "/subpath/topics/";
121         String method = "GET";
122         initPermissionBuilder(path, method, provideRealInstance(DMAAP_NAME));
123
124         //when
125         String permission = permissionBuilder.buildPermission(request);
126
127         //then
128         assertEquals("org.onap.dmaap-bc.api.topics|mr|GET", permission);
129     }
130
131     private void initPermissionBuilder(String path, String method, Dmaap dmaapInstance) {
132         when(dmaapConfig.getProperty(PermissionBuilder.API_NS_PROP, PermissionBuilder.DEFAULT_API_NS))
133             .thenReturn(PermissionBuilder.DEFAULT_API_NS);
134         when(dmaapService.getDmaap()).thenReturn(dmaapInstance);
135         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
136
137         when(request.getPathInfo()).thenReturn(path);
138         when(request.getMethod()).thenReturn(method);
139     }
140
141     private Dmaap provideDefaultInstance() {
142         return new  Dmaap("0", "", "", "", "", "", "", "");
143     }
144
145     private Dmaap provideRealInstance(String dmaapName) {
146         Dmaap dmaap = new Dmaap("1", "org.onap.dmaap", dmaapName, "https://dmaap-dr-prov:8443", "", "DCAE_MM_AGENT", "", "");
147         dmaap.setStatus(DmaapObject_Status.VALID);
148         return dmaap;
149     }
150
151 }