8745cc5d2472e2a2772af0a221454317452e103b
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
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.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Properties;
36
37 import javax.servlet.http.HttpServletRequest;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Matchers;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.model.CldsInfo;
45 import org.onap.clamp.clds.service.CldsService;
46 import org.onap.clamp.clds.util.LoggingUtils;
47 import org.onap.clamp.clds.util.ResourceFileUtil;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
52 import org.springframework.security.core.Authentication;
53 import org.springframework.security.core.GrantedAuthority;
54 import org.springframework.security.core.authority.SimpleGrantedAuthority;
55 import org.springframework.security.core.context.SecurityContext;
56 import org.springframework.security.core.userdetails.User;
57 import org.springframework.security.core.userdetails.UserDetails;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59
60 /**
61  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
62  */
63 @RunWith(SpringJUnit4ClassRunner.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 public class CldsServiceItCase {
66
67     @Autowired
68     private CldsService cldsService;
69     private String bpmnText;
70     private String imageText;
71     private String bpmnPropText;
72     private String docText;
73
74     private Authentication authentication;
75     private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
76     private LoggingUtils util;
77
78     /**
79      * Setup the variable before the tests execution.
80      *
81      * @throws IOException In case of issues when opening the files
82      */
83     @Before
84     public void setupBefore() throws IOException {
85         bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml");
86         imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
87         bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
88         docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");
89
90         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
91         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
92         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
93         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
94         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
95         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
96         authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
97         authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
98
99         util = Mockito.mock(LoggingUtils.class);
100         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
101         cldsService.setLoggingUtil(util);
102
103     }
104
105     @Test
106     public void testCldsInfoNotAuthorized() {
107         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
108         Authentication localAuth = Mockito.mock(Authentication.class);
109         UserDetails userDetails = Mockito.mock(UserDetails.class);
110         Mockito.when(userDetails.getUsername()).thenReturn("admin");
111         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
112         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
113
114         cldsService.setSecurityContext(securityContext);
115         CldsInfo cldsInfo = cldsService.getCldsInfo();
116         assertFalse(cldsInfo.isPermissionReadCl());
117         assertFalse(cldsInfo.isPermissionReadTemplate());
118         assertFalse(cldsInfo.isPermissionUpdateCl());
119         assertFalse(cldsInfo.isPermissionUpdateTemplate());
120     }
121
122     @Test
123     public void testCldsInfoAuthorized() throws Exception {
124         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
125         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
126
127         cldsService.setSecurityContext(securityContext);
128         CldsInfo cldsInfo = cldsService.getCldsInfo();
129         assertTrue(cldsInfo.isPermissionReadCl());
130         assertTrue(cldsInfo.isPermissionReadTemplate());
131         assertTrue(cldsInfo.isPermissionUpdateCl());
132         assertTrue(cldsInfo.isPermissionUpdateTemplate());
133         Properties prop = new Properties();
134         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
135         prop.load(in);
136         assertNotNull(in);
137         in.close();
138         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
139         assertEquals(cldsInfo.getUserName(), "admin");
140     }
141 }