Fix the security issue
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsTemplateServiceItCase.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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32
33 import java.io.IOException;
34 import java.security.Principal;
35 import java.util.LinkedList;
36 import java.util.List;
37
38 import javax.servlet.http.HttpServletRequest;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Matchers;
44 import org.mockito.Mockito;
45 import org.onap.clamp.clds.dao.CldsDao;
46 import org.onap.clamp.clds.model.CldsTemplate;
47 import org.onap.clamp.clds.model.ValueItem;
48 import org.onap.clamp.clds.service.CldsTemplateService;
49 import org.onap.clamp.clds.util.LoggingUtils;
50 import org.onap.clamp.clds.util.ResourceFileUtil;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
55 import org.springframework.security.core.Authentication;
56 import org.springframework.security.core.GrantedAuthority;
57 import org.springframework.security.core.authority.SimpleGrantedAuthority;
58 import org.springframework.security.core.context.SecurityContext;
59 import org.springframework.security.core.context.SecurityContextHolder;
60 import org.springframework.security.core.userdetails.User;
61 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
62
63 /**
64  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
65  */
66 @RunWith(SpringJUnit4ClassRunner.class)
67 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
68 public class CldsTemplateServiceItCase {
69
70     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsTemplateServiceItCase.class);
71     @Autowired
72     private CldsTemplateService cldsTemplateService;
73     @Autowired
74     private CldsDao cldsDao;
75     private String bpmnText;
76     private String imageText;
77     private String bpmnPropText;
78     private CldsTemplate cldsTemplate;
79     private Authentication authentication;
80     private List<GrantedAuthority> authList =  new LinkedList<GrantedAuthority>();
81     private LoggingUtils util;
82
83     /**
84      * Setup the variable before the tests execution.
85      * 
86      * @throws IOException
87      *             In case of issues when opening the files
88      */
89     @Before
90     public void setupBefore() throws IOException {
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         authentication =  new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
97
98         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
99         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
100
101         util = Mockito.mock(LoggingUtils.class);
102         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
103         cldsTemplateService.setLoggingUtil(util);
104
105         cldsTemplateService.setSecurityContext(securityContext);
106         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
107         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
108         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
109         cldsTemplate = new CldsTemplate();
110         cldsTemplate.setName("testModel");
111         cldsTemplate.setBpmnText(bpmnText);
112         cldsTemplate.setImageText(imageText);
113         cldsTemplate.setPropText(bpmnPropText);
114         cldsTemplateService.putTemplate("testModel", cldsTemplate);
115     }
116
117     @Test
118     public void testPutTemplate() throws Exception {
119         CldsTemplate savedTemplate = CldsTemplate.retrieve(cldsDao, "testModel", false);
120         assertNotNull(savedTemplate);
121         logger.info("saved template bpmn text is:" + savedTemplate.getBpmnText());
122         assertEquals(bpmnText, savedTemplate.getBpmnText());
123         assertEquals(imageText, savedTemplate.getImageText());
124         assertEquals(bpmnPropText, savedTemplate.getPropText());
125         assertEquals("testModel", savedTemplate.getName());
126     }
127
128     @Test
129     public void testGetTemplate() throws Exception {
130         CldsTemplate getTemplate = cldsTemplateService.getTemplate("testModel");
131         assertNotNull(getTemplate);
132         assertEquals(bpmnText, getTemplate.getBpmnText());
133         assertEquals(imageText, getTemplate.getImageText());
134         assertEquals(bpmnPropText, getTemplate.getPropText());
135         assertEquals("testModel", getTemplate.getName());
136     }
137
138     @Test
139     public void testGetImageXml() throws Exception {
140         String imageXml = cldsTemplateService.getImageXml("testModel");
141         assertEquals(imageText, imageXml);
142     }
143
144     @Test
145     public void testGetBpmnTemplate() throws Exception {
146         String bpmnTemplate = cldsTemplateService.getBpmnTemplate("testModel");
147         assertEquals(bpmnText, bpmnTemplate);
148     }
149
150     @Test
151     public void testGetTemplateNames() throws Exception {
152         CldsTemplate cldsTemplateNew = new CldsTemplate();
153         cldsTemplateNew.setName("testModelNew");
154         cldsTemplateNew.setBpmnText(bpmnText);
155         cldsTemplateNew.setImageText(imageText);
156         cldsTemplateNew.setPropText(bpmnPropText);
157         cldsTemplateService.putTemplate("testModelNew", cldsTemplateNew);
158         List<ValueItem> templateNames = cldsTemplateService.getTemplateNames();
159         boolean testModel = false;
160         boolean testModelNew = false;
161         for (ValueItem item : templateNames) {
162             if (item.getValue().equals("testModel")) {
163                 testModel = true;
164             }
165             if (item.getValue().equals("testModelNew")) {
166                 testModelNew = true;
167             }
168         }
169         assertTrue(testModel || testModelNew);
170     }
171 }