Additional code for Tosca
[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 import static org.junit.Assert.fail;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.security.GeneralSecurityException;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Properties;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.NotFoundException;
42 import javax.xml.transform.TransformerException;
43
44 import org.apache.commons.codec.DecoderException;
45 import org.apache.commons.lang3.RandomStringUtils;
46 import org.json.JSONException;
47 import org.json.simple.parser.ParseException;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.Matchers;
52 import org.mockito.Mockito;
53 import org.onap.clamp.clds.dao.CldsDao;
54 import org.onap.clamp.clds.model.CldsEvent;
55 import org.onap.clamp.clds.model.CldsInfo;
56 import org.onap.clamp.clds.model.CldsModel;
57 import org.onap.clamp.clds.model.CldsMonitoringDetails;
58 import org.onap.clamp.clds.model.CldsServiceData;
59 import org.onap.clamp.clds.model.CldsTemplate;
60 import org.onap.clamp.clds.model.DcaeEvent;
61 import org.onap.clamp.clds.service.CldsService;
62 import org.onap.clamp.clds.util.LoggingUtils;
63 import org.onap.clamp.clds.util.ResourceFileUtil;
64 import org.skyscreamer.jsonassert.JSONAssert;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
68 import org.springframework.http.HttpStatus;
69 import org.springframework.http.ResponseEntity;
70 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
71 import org.springframework.security.core.Authentication;
72 import org.springframework.security.core.GrantedAuthority;
73 import org.springframework.security.core.authority.SimpleGrantedAuthority;
74 import org.springframework.security.core.context.SecurityContext;
75 import org.springframework.security.core.userdetails.User;
76 import org.springframework.security.core.userdetails.UserDetails;
77 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
78
79 /**
80  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
81  */
82 @RunWith(SpringJUnit4ClassRunner.class)
83 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
84 public class CldsServiceItCase {
85
86     @Autowired
87     private CldsService cldsService;
88     private String bpmnText;
89     private String imageText;
90     private String bpmnPropText;
91     private String docText;
92
93     @Autowired
94     private CldsDao cldsDao;
95     private Authentication authentication;
96     private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
97     private LoggingUtils util;
98
99     /**
100      * Setup the variable before the tests execution.
101      *
102      * @throws IOException
103      *         In case of issues when opening the files
104      */
105     @Before
106     public void setupBefore() throws IOException {
107         bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml");
108         imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
109         bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
110         docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");
111
112         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
113         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
114         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
115         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
116         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
117         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
118         authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
119         authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
120
121         util = Mockito.mock(LoggingUtils.class);
122         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
123         cldsService.setLoggingUtil(util);
124
125     }
126
127     @Test
128     public void testCldsInfoNotAuthorized() {
129         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
130         Authentication localAuth = Mockito.mock(Authentication.class);
131         UserDetails userDetails = Mockito.mock(UserDetails.class);
132         Mockito.when(userDetails.getUsername()).thenReturn("admin");
133         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
134         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
135
136         cldsService.setSecurityContext(securityContext);
137         CldsInfo cldsInfo = cldsService.getCldsInfo();
138         assertFalse(cldsInfo.isPermissionReadCl());
139         assertFalse(cldsInfo.isPermissionReadTemplate());
140         assertFalse(cldsInfo.isPermissionUpdateCl());
141         assertFalse(cldsInfo.isPermissionUpdateTemplate());
142     }
143
144     @Test
145     public void testCldsInfoAuthorized() throws Exception {
146         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
147         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
148
149         cldsService.setSecurityContext(securityContext);
150         CldsInfo cldsInfo = cldsService.getCldsInfo();
151         assertTrue(cldsInfo.isPermissionReadCl());
152         assertTrue(cldsInfo.isPermissionReadTemplate());
153         assertTrue(cldsInfo.isPermissionUpdateCl());
154         assertTrue(cldsInfo.isPermissionUpdateTemplate());
155         Properties prop = new Properties();
156         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
157         prop.load(in);
158         in.close();
159         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
160         assertEquals(cldsInfo.getUserName(), "admin");
161     }
162
163     @Test
164     public void testGetCLDSDetails() throws IOException {
165         List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCLDSDetails();
166         assertNotNull(cldsMonitoringDetailsList);
167     }
168
169     @Test
170     public void testCompleteFlow() throws TransformerException, ParseException {
171         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
172         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
173
174         cldsService.setSecurityContext(securityContext);
175         // Add the template first
176         CldsTemplate newTemplate = new CldsTemplate();
177         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
178         newTemplate.setName(randomNameTemplate);
179         newTemplate.setBpmnText(bpmnText);
180         newTemplate.setImageText(imageText);
181         // Save the template in DB
182         cldsDao.setTemplate(newTemplate, "user");
183         // Test if it's well there
184         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
185         assertEquals(bpmnText, newTemplateRead.getBpmnText());
186         assertEquals(imageText, newTemplateRead.getImageText());
187         // Save the model
188         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
189         CldsModel newModel = new CldsModel();
190         newModel.setName(randomNameModel);
191         newModel.setBpmnText(bpmnText);
192         newModel.setImageText(imageText);
193         newModel.setPropText(bpmnPropText);
194         newModel.setControlNamePrefix("ClosedLoop-");
195         newModel.setTemplateName(randomNameTemplate);
196         newModel.setTemplateId(newTemplate.getId());
197         newModel.setDocText(docText);
198         // Test the PutModel method
199
200         cldsService.putModel(randomNameModel, newModel);
201
202         assertEquals(bpmnText, cldsService.getBpmnXml(randomNameModel));
203         assertEquals(imageText, cldsService.getImageXml(randomNameModel));
204
205         // Verify whether it has been added properly or not
206         assertNotNull(cldsDao.getModel(randomNameModel));
207
208         CldsModel model = cldsService.getModel(randomNameModel);
209         // Verify with GetModel
210         assertEquals(model.getTemplateName(), randomNameTemplate);
211         assertEquals(model.getName(), randomNameModel);
212
213         assertTrue(cldsService.getModelNames().size() >= 1);
214
215         // Should fail
216         ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
217             randomNameModel, "false", cldsService.getModel(randomNameModel));
218         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
219         assertNotNull(responseEntity.getBody());
220         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
221         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
222
223         responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
224         assertNotNull(responseEntity);
225         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
226         assertNotNull(responseEntity.getBody());
227         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
228         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
229
230         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP, randomNameModel, "false",
231             cldsService.getModel(randomNameModel));
232         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
233         assertNotNull(responseEntity.getBody());
234         assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
235         assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
236
237         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART, randomNameModel, "false",
238             cldsService.getModel(randomNameModel));
239         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
240         assertNotNull(responseEntity.getBody());
241         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
242         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
243
244         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE, randomNameModel, "false",
245             cldsService.getModel(randomNameModel));
246         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
247         assertNotNull(responseEntity.getBody());
248         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
249         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
250
251         responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
252         assertNotNull(responseEntity);
253         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
254         assertNotNull(responseEntity.getBody());
255         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
256         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
257
258         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE, randomNameModel, "false",
259             cldsService.getModel(randomNameModel));
260         assertNotNull(responseEntity);
261         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
262         assertNotNull(responseEntity.getBody());
263         try {
264             cldsService.getModel(randomNameModel);
265             fail("Should have raised an NotFoundException exception");
266         } catch (NotFoundException ne) {
267
268         }
269
270     }
271
272     @Test
273     public void testDcaePost() {
274         DcaeEvent dcaeEvent = new DcaeEvent();
275         dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
276         dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
277         dcaeEvent.setResourceUUID("1");
278         dcaeEvent.setServiceUUID("2");
279         assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
280             "event=created serviceUUID=2 resourceUUID=1 artifactName=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
281     }
282
283     @Test
284     public void testGetSdcProperties() throws IOException {
285         JSONAssert.assertEquals(
286             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
287             cldsService.getSdcProperties(), true);
288     }
289
290     @Test
291     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
292         String result = cldsService.getSdcServices();
293         JSONAssert.assertEquals(
294             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
295     }
296
297     @Test
298     public void testGetSdcPropertiesByServiceUuidForRefresh()
299         throws GeneralSecurityException, DecoderException, JSONException, IOException {
300         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
301         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
302
303         cldsService.setSecurityContext(securityContext);
304         // Test basic functionalities
305         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
306             false);
307         JSONAssert.assertEquals(
308             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
309             true);
310         // Now test the Cache effect
311         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
312         // Should not be there, so should be null
313         assertNull(cldsServiceDataCache);
314         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
315         // Should be there now, so should NOT be null
316         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
317         assertNotNull(cldsServiceDataCache);
318         cldsDao.clearServiceCache();
319     }
320 }