2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.util;
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;
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;
37 @RunWith(MockitoJUnitRunner.class)
38 public class PermissionBuilderTest {
40 private static final String DMAAP_NAME = "mr";
41 private PermissionBuilder permissionBuilder;
43 private DmaapConfig dmaapConfig;
45 private DmaapService dmaapService;
47 private HttpServletRequest request;
51 public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsNotInitialized() {
53 doReturn(null).when(dmaapService).getDmaap();
54 permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
57 permissionBuilder.updateDmaapInstance();
60 assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
64 public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsInitializedWithDefaultInstance() {
66 doReturn(provideDefaultInstance()).when(dmaapService).getDmaap();
67 permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
70 permissionBuilder.updateDmaapInstance();
73 assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
77 public void updateDmaapInstance_shouldSetRealInstance_whenDmaapServiceProvidesOne() {
79 when(dmaapService.getDmaap()).thenReturn(provideDefaultInstance(), provideRealInstance(DMAAP_NAME));
80 permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
83 permissionBuilder.updateDmaapInstance();
86 assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
90 public void updateDmaapInstance_shouldNotUpdateDmaapInstance_whenAlreadyInitializedWithRealInstance() {
92 when(dmaapService.getDmaap()).thenReturn(provideRealInstance(DMAAP_NAME), provideRealInstance("newName"));
93 permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
96 permissionBuilder.updateDmaapInstance();
99 assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
100 verify(dmaapService, atMost(1)).getDmaap();
104 public void buildPermission_shouldBuildPermissionWithBootInstance() {
106 String path = "/dmaap";
107 String method = "GET";
108 initPermissionBuilder(path, method, provideDefaultInstance());
111 String permission = permissionBuilder.buildPermission(request);
114 assertEquals("org.onap.dmaap-bc.api.dmaap|boot|GET", permission);
118 public void buildPermission_shouldBuildPermissionWithRealInstance() {
120 String path = "/subpath/topics/";
121 String method = "GET";
122 initPermissionBuilder(path, method, provideRealInstance(DMAAP_NAME));
125 String permission = permissionBuilder.buildPermission(request);
128 assertEquals("org.onap.dmaap-bc.api.topics|mr|GET", permission);
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);
137 when(request.getPathInfo()).thenReturn(path);
138 when(request.getMethod()).thenReturn(method);
141 private Dmaap provideDefaultInstance() {
142 return new Dmaap("0", "", "", "", "", "", "", "");
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);