96131197b6f839c218d3405aa18830bf6de7e3fc
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.InputStream;
31 import java.security.Principal;
32 import java.util.Properties;
33
34 import javax.ws.rs.core.SecurityContext;
35
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;
47
48 /**
49  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
50  */
51 @RunWith(SpringJUnit4ClassRunner.class)
52 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
53 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
54 public class CldsServiceIT extends AbstractIT {
55
56     @Autowired
57     CldsService cldsService;
58
59     @Test
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");
64
65         Mockito.when(securityContext.getUserPrincipal()).thenReturn(p);
66         cldsService.setSecurityContext(securityContext);
67
68         CldsInfo cldsInfo = cldsService.getCldsInfo();
69         assertFalse(cldsInfo.isPermissionReadCl());
70         assertFalse(cldsInfo.isPermissionReadTemplate());
71         assertFalse(cldsInfo.isPermissionUpdateCl());
72         assertFalse(cldsInfo.isPermissionUpdateTemplate());
73     }
74
75     @Test
76     public void testCldsInfoAuthorized() throws Exception {
77
78         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
79         Principal p = Mockito.mock(Principal.class);
80         Mockito.when(p.getName()).thenReturn("admin");
81
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);
87
88         cldsService.setSecurityContext(securityContext);
89
90         CldsInfo cldsInfo = cldsService.getCldsInfo();
91         assertTrue(cldsInfo.isPermissionReadCl());
92         assertTrue(cldsInfo.isPermissionReadTemplate());
93         assertTrue(cldsInfo.isPermissionUpdateCl());
94         assertTrue(cldsInfo.isPermissionUpdateTemplate());
95
96         Properties prop = new Properties();
97         // InputStream in =
98         // CldsInfo.class.getResourceAsStream("clds-version.properties");
99         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
100         prop.load(in);
101         in.close();
102
103         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
104         assertEquals(cldsInfo.getUserName(), "admin");
105     }
106 }