2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
30 import java.io.InputStream;
31 import java.security.Principal;
32 import java.util.Properties;
34 import javax.ws.rs.core.SecurityContext;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.clamp.clds.AbstractIT;
40 import org.onap.clamp.clds.model.CldsInfo;
41 import org.onap.clamp.clds.service.CldsService;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
51 @RunWith(SpringJUnit4ClassRunner.class)
52 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
53 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
54 public class CldsServiceIT extends AbstractIT {
57 CldsService cldsService;
60 public void testCldsInfoNotAuthorized() throws Exception {
61 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
62 Principal p = Mockito.mock(Principal.class);
63 Mockito.when(p.getName()).thenReturn("admin");
65 Mockito.when(securityContext.getUserPrincipal()).thenReturn(p);
66 cldsService.setSecurityContext(securityContext);
68 CldsInfo cldsInfo = cldsService.getCldsInfo();
69 assertFalse(cldsInfo.isPermissionReadCl());
70 assertFalse(cldsInfo.isPermissionReadTemplate());
71 assertFalse(cldsInfo.isPermissionUpdateCl());
72 assertFalse(cldsInfo.isPermissionUpdateTemplate());
76 public void testCldsInfoAuthorized() throws Exception {
78 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
79 Principal p = Mockito.mock(Principal.class);
80 Mockito.when(p.getName()).thenReturn("admin");
82 Mockito.when(securityContext.getUserPrincipal()).thenReturn(p);
83 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
84 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
85 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
86 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
88 cldsService.setSecurityContext(securityContext);
90 CldsInfo cldsInfo = cldsService.getCldsInfo();
91 assertTrue(cldsInfo.isPermissionReadCl());
92 assertTrue(cldsInfo.isPermissionReadTemplate());
93 assertTrue(cldsInfo.isPermissionUpdateCl());
94 assertTrue(cldsInfo.isPermissionUpdateTemplate());
96 Properties prop = new Properties();
98 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
102 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
103 assertEquals(cldsInfo.getUserName(), "admin");