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