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