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