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