Upgrade spring/camel versions
[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.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.security.GeneralSecurityException;
37 import java.security.Principal;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Properties;
41
42 import org.apache.commons.codec.DecoderException;
43 import org.json.JSONException;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.onap.clamp.clds.dao.CldsDao;
49 import org.onap.clamp.clds.model.CldsHealthCheck;
50 import org.onap.clamp.clds.model.CldsInfo;
51 import org.onap.clamp.clds.model.CldsModel;
52 import org.onap.clamp.clds.model.CldsServiceData;
53 import org.onap.clamp.clds.model.CldsTemplate;
54 import org.onap.clamp.clds.service.CldsService;
55 import org.onap.clamp.clds.util.ResourceFileUtil;
56 import org.skyscreamer.jsonassert.JSONAssert;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
60 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
61 import org.springframework.security.core.authority.SimpleGrantedAuthority;
62 import org.springframework.security.core.Authentication;
63 import org.springframework.security.core.GrantedAuthority;
64 import org.springframework.security.core.context.SecurityContext;
65 import org.springframework.security.core.context.SecurityContextHolder;
66 import org.springframework.security.core.userdetails.User;
67 import org.springframework.security.core.userdetails.UserDetails;
68 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
69
70 /**
71  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
72  */
73 @RunWith(SpringJUnit4ClassRunner.class)
74 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
75 public class CldsServiceItCase {
76
77     @Autowired
78     private CldsService cldsService;
79     private String bpmnText;
80     private String imageText;
81     private String bpmnPropText;
82     @Autowired
83     private CldsDao cldsDao;
84     private Authentication authentication;
85     private List<GrantedAuthority> authList =  new LinkedList<GrantedAuthority>();
86
87     /**
88      * Setup the variable before the tests execution.
89      * 
90      * @throws IOException
91      *             In case of issues when opening the files
92      */
93     @Before
94     public void setupBefore() throws IOException {
95         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
96         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
97         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
98
99         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
100         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
101         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
102         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
103         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
104         authentication =  new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
105     }
106
107     @Test
108     public void testCldsInfoNotAuthorized() {
109         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
110         Authentication localAuth = Mockito.mock(Authentication.class);
111         UserDetails userDetails = Mockito.mock(UserDetails.class);
112         Mockito.when(userDetails.getUsername()).thenReturn("admin");
113         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
114         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
115
116         cldsService.setSecurityContext(securityContext);
117         CldsInfo cldsInfo = cldsService.getCldsInfo();
118         assertFalse(cldsInfo.isPermissionReadCl());
119         assertFalse(cldsInfo.isPermissionReadTemplate());
120         assertFalse(cldsInfo.isPermissionUpdateCl());
121         assertFalse(cldsInfo.isPermissionUpdateTemplate());
122     }
123
124     @Test
125     public void testCldsInfoAuthorized() throws Exception {
126         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
127         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
128
129         cldsService.setSecurityContext(securityContext);
130         CldsInfo cldsInfo = cldsService.getCldsInfo();
131         assertTrue(cldsInfo.isPermissionReadCl());
132         assertTrue(cldsInfo.isPermissionReadTemplate());
133         assertTrue(cldsInfo.isPermissionUpdateCl());
134         assertTrue(cldsInfo.isPermissionUpdateTemplate());
135         Properties prop = new Properties();
136         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
137         prop.load(in);
138         in.close();
139         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
140         assertEquals(cldsInfo.getUserName(), "admin");
141     }
142
143     @Test
144     public void testPutModel() {
145         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
146         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
147         cldsService.setSecurityContext(securityContext);
148         // Add the template first
149         CldsTemplate newTemplate = new CldsTemplate();
150         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
151         newTemplate.setName(randomNameTemplate);
152         newTemplate.setBpmnText(bpmnText);
153         newTemplate.setImageText(imageText);
154         // Save the template in DB
155         cldsDao.setTemplate(newTemplate, "user");
156         // Test if it's well there
157         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
158         assertEquals(bpmnText, newTemplateRead.getBpmnText());
159         assertEquals(imageText, newTemplateRead.getImageText());
160         // Save the model
161         CldsModel newModel = new CldsModel();
162         newModel.setName(randomNameTemplate);
163         newModel.setBpmnText(bpmnText);
164         newModel.setImageText(imageText);
165         newModel.setPropText(bpmnPropText);
166         newModel.setControlNamePrefix("ClosedLoop-");
167         newModel.setTemplateName("test-template");
168         newModel.setTemplateId(newTemplate.getId());
169         newModel.setDocText(newTemplate.getPropText());
170         // Test the PutModel method
171         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
172         cldsService.putModel(randomNameModel, newModel);
173         // Verify whether it has been added properly or not
174         assertNotNull(cldsDao.getModel(randomNameModel));
175     }
176
177     @Test
178     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
179         String result = cldsService.getSdcServices();
180         JSONAssert.assertEquals(
181                 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
182                 true);
183     }
184
185     @Test
186     public void testGetSdcPropertiesByServiceUuidForRefresh()
187             throws GeneralSecurityException, DecoderException, JSONException, IOException {
188         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
189         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
190
191         cldsService.setSecurityContext(securityContext);
192         // Test basic functionalities
193         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
194                 false);
195         JSONAssert.assertEquals(
196                 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
197                 result, true);
198         // Now test the Cache effect
199         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
200         // Should not be there, so should be null
201         assertNull(cldsServiceDataCache);
202         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
203         // Should be there now, so should NOT be null
204         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
205         assertNotNull(cldsServiceDataCache);
206         cldsDao.clearServiceCache();
207     }
208 }