[DMAAP-BC] Fix failing jenkins
[dmaap/buscontroller.git] / dmaap-bc / 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.assertEquals;
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.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.dmaap.dbcapi.model.Dmaap;
35 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
36 import org.onap.dmaap.dbcapi.service.DmaapService;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class PermissionBuilderTest {
40
41     private static final String DMAAP_NAME = "mr";
42     private PermissionBuilder permissionBuilder;
43     @Mock
44     private DmaapConfig dmaapConfig;
45     @Mock
46     private DmaapService dmaapService;
47     @Mock
48     private HttpServletRequest request;
49
50     @BeforeClass
51     public static void setUpClass() throws Exception {
52         System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
53     }
54
55     @Test
56     public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsNotInitialized() {
57         //given
58         doReturn(null).when(dmaapService).getDmaap();
59         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
60
61         //when
62         permissionBuilder.updateDmaapInstance();
63
64         //then
65         assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
66     }
67
68     @Test
69     public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsInitializedWithDefaultInstance() {
70         //given
71         doReturn(provideDefaultInstance()).when(dmaapService).getDmaap();
72         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
73
74         //when
75         permissionBuilder.updateDmaapInstance();
76
77         //then
78         assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
79     }
80
81     @Test
82     public void updateDmaapInstance_shouldSetRealInstance_whenDmaapServiceProvidesOne() {
83         //given
84         when(dmaapService.getDmaap()).thenReturn(provideDefaultInstance(), provideRealInstance(DMAAP_NAME));
85         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
86
87         //when
88         permissionBuilder.updateDmaapInstance();
89
90         //then
91         assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
92     }
93
94     @Test
95     public void updateDmaapInstance_shouldNotUpdateDmaapInstance_whenAlreadyInitializedWithRealInstance() {
96         //given
97         when(dmaapService.getDmaap()).thenReturn(provideRealInstance(DMAAP_NAME), provideRealInstance("newName"));
98         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
99
100         //when
101         permissionBuilder.updateDmaapInstance();
102
103         //then
104         assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
105         verify(dmaapService, atMost(1)).getDmaap();
106     }
107
108     @Test
109     public void buildPermission_shouldBuildPermissionWithBootInstance() {
110         //given
111         String path = "/dmaap";
112         String method = "GET";
113         initPermissionBuilder(path, method, provideDefaultInstance());
114
115         //when
116         String permission = permissionBuilder.buildPermission(request);
117
118         //then
119         assertEquals("org.onap.dmaap-bc.api.dmaap|boot|GET", permission);
120     }
121
122     @Test
123     public void buildPermission_shouldBuildPermissionWithRealInstance() {
124         //given
125         String path = "/dmaap";
126         String method = "GET";
127         initPermissionBuilder(path, method, provideRealInstance(DMAAP_NAME));
128
129         //when
130         String permission = permissionBuilder.buildPermission(request);
131
132         //then
133         assertEquals("org.onap.dmaap-bc.api.dmaap|mr|GET", permission);
134     }
135
136     @Test
137     public void buildPermission_shouldBuildPermissionWhenUrlContainsId() {
138         //given
139         String path = "/topics/topic_id_123";
140         String method = "GET";
141         initPermissionBuilder(path, method, provideRealInstance(DMAAP_NAME));
142
143         //when
144         String permission = permissionBuilder.buildPermission(request);
145
146         //then
147         assertEquals("org.onap.dmaap-bc.api.topics|mr|GET", permission);
148     }
149
150     private void initPermissionBuilder(String path, String method, Dmaap dmaapInstance) {
151         when(dmaapConfig.getProperty(PermissionBuilder.API_NS_PROP, PermissionBuilder.DEFAULT_API_NS))
152             .thenReturn(PermissionBuilder.DEFAULT_API_NS);
153         when(dmaapService.getDmaap()).thenReturn(dmaapInstance);
154         permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
155
156         when(request.getPathInfo()).thenReturn(path);
157         when(request.getMethod()).thenReturn(method);
158     }
159
160     private Dmaap provideDefaultInstance() {
161         return new Dmaap.DmaapBuilder().setVer("0").setTnr("").setDn("").setDpu("").setLu("").setBat("").setNk("").setAko("").createDmaap();
162     }
163
164     private Dmaap provideRealInstance(String dmaapName) {
165         Dmaap dmaap = new Dmaap.DmaapBuilder().setVer("1").setTnr("org.onap.dmaap").setDn(dmaapName).setDpu("https://dmaap-dr-prov:8443").setLu("").setBat("DCAE_MM_AGENT").setNk("").setAko("").createDmaap();
166         dmaap.setStatus(DmaapObject_Status.VALID);
167         return dmaap;
168     }
169
170 }