Add delete test
[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 com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
34
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.security.GeneralSecurityException;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Properties;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.NotFoundException;
44 import javax.xml.transform.TransformerException;
45
46 import org.apache.commons.codec.DecoderException;
47 import org.json.JSONException;
48 import org.json.simple.parser.ParseException;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Matchers;
53 import org.mockito.Mockito;
54 import org.onap.clamp.clds.dao.CldsDao;
55 import org.onap.clamp.clds.model.CldsEvent;
56 import org.onap.clamp.clds.model.CldsInfo;
57 import org.onap.clamp.clds.model.CldsModel;
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     @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 testCompleteFlow() throws TransformerException, ParseException {
164         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
165         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
166
167         cldsService.setSecurityContext(securityContext);
168         // Add the template first
169         CldsTemplate newTemplate = new CldsTemplate();
170         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
171         newTemplate.setName(randomNameTemplate);
172         newTemplate.setBpmnText(bpmnText);
173         newTemplate.setImageText(imageText);
174         // Save the template in DB
175         cldsDao.setTemplate(newTemplate, "user");
176         // Test if it's well there
177         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
178         assertEquals(bpmnText, newTemplateRead.getBpmnText());
179         assertEquals(imageText, newTemplateRead.getImageText());
180         // Save the model
181         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
182         CldsModel newModel = new CldsModel();
183         newModel.setName(randomNameModel);
184         newModel.setBpmnText(bpmnText);
185         newModel.setImageText(imageText);
186         newModel.setPropText(bpmnPropText);
187         newModel.setControlNamePrefix("ClosedLoop-");
188         newModel.setTemplateName(randomNameTemplate);
189         newModel.setTemplateId(newTemplate.getId());
190         newModel.setDocText(docText);
191         // Test the PutModel method
192
193         cldsService.putModel(randomNameModel, newModel);
194         // Verify whether it has been added properly or not
195         assertNotNull(cldsDao.getModel(randomNameModel));
196
197         CldsModel model = cldsService.getModel(randomNameModel);
198         // Verify with GetModel
199         assertEquals(model.getTemplateName(), randomNameTemplate);
200         assertEquals(model.getName(), randomNameModel);
201
202         assertTrue(cldsService.getModelNames().size() >= 1);
203
204         // Should fail
205         ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
206             randomNameModel, "false", cldsService.getModel(randomNameModel));
207         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
208         assertNotNull(responseEntity.getBody());
209         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
210         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
211
212         responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
213         assertNotNull(responseEntity);
214         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
215         assertNotNull(responseEntity.getBody());
216         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
217         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
218
219         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP,
220             randomNameModel, "false", cldsService.getModel(randomNameModel));
221         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
222         assertNotNull(responseEntity.getBody());
223         assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
224         assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
225
226         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART,
227             randomNameModel, "false", cldsService.getModel(randomNameModel));
228         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
229         assertNotNull(responseEntity.getBody());
230         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
231         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
232
233         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE,
234             randomNameModel, "false", cldsService.getModel(randomNameModel));
235         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
236         assertNotNull(responseEntity.getBody());
237         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
238         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
239
240         responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
241         assertNotNull(responseEntity);
242         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
243         assertNotNull(responseEntity.getBody());
244         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
245         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
246
247         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE,
248             randomNameModel, "false", cldsService.getModel(randomNameModel));
249         assertNotNull(responseEntity);
250         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
251         assertNotNull(responseEntity.getBody());
252         try {
253             cldsService.getModel(randomNameModel);
254             fail("Should have raised an NotFoundException exception");
255         } catch(NotFoundException ne) {
256
257         }
258
259     }
260
261     @Test
262     public void testDcaePost() {
263         DcaeEvent dcaeEvent = new DcaeEvent();
264         dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
265         dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
266         dcaeEvent.setResourceUUID("1");
267         dcaeEvent.setServiceUUID("2");
268         assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
269             "event=created serviceUUID=2 resourceUUID=1 artifactName=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
270     }
271
272     @Test
273     public void testGetSdcProperties() throws IOException {
274         JSONAssert.assertEquals(
275             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
276             cldsService.getSdcProperties(), true);
277     }
278
279     @Test
280     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
281         String result = cldsService.getSdcServices();
282         JSONAssert.assertEquals(
283             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
284     }
285
286     @Test
287     public void testGetSdcPropertiesByServiceUuidForRefresh()
288         throws GeneralSecurityException, DecoderException, JSONException, IOException {
289         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
290         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
291
292         cldsService.setSecurityContext(securityContext);
293         // Test basic functionalities
294         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
295             false);
296         JSONAssert.assertEquals(
297             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
298             true);
299         // Now test the Cache effect
300         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
301         // Should not be there, so should be null
302         assertNull(cldsServiceDataCache);
303         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
304         // Should be there now, so should NOT be null
305         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
306         assertNotNull(cldsServiceDataCache);
307         cldsDao.clearServiceCache();
308     }
309 }