Remove bad import
[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.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     @Test
126     public void testCldsInfoNotAuthorized() {
127         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
128         Authentication localAuth = Mockito.mock(Authentication.class);
129         UserDetails userDetails = Mockito.mock(UserDetails.class);
130         Mockito.when(userDetails.getUsername()).thenReturn("admin");
131         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
132         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
133
134         cldsService.setSecurityContext(securityContext);
135         CldsInfo cldsInfo = cldsService.getCldsInfo();
136         assertFalse(cldsInfo.isPermissionReadCl());
137         assertFalse(cldsInfo.isPermissionReadTemplate());
138         assertFalse(cldsInfo.isPermissionUpdateCl());
139         assertFalse(cldsInfo.isPermissionUpdateTemplate());
140     }
141
142     @Test
143     public void testCldsInfoAuthorized() throws Exception {
144         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
145         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
146
147         cldsService.setSecurityContext(securityContext);
148         CldsInfo cldsInfo = cldsService.getCldsInfo();
149         assertTrue(cldsInfo.isPermissionReadCl());
150         assertTrue(cldsInfo.isPermissionReadTemplate());
151         assertTrue(cldsInfo.isPermissionUpdateCl());
152         assertTrue(cldsInfo.isPermissionUpdateTemplate());
153         Properties prop = new Properties();
154         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
155         prop.load(in);
156         in.close();
157         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
158         assertEquals(cldsInfo.getUserName(), "admin");
159     }
160
161     @Test
162     public void testCompleteFlow() throws TransformerException, ParseException {
163         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
164         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
165
166         cldsService.setSecurityContext(securityContext);
167         // Add the template first
168         CldsTemplate newTemplate = new CldsTemplate();
169         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
170         newTemplate.setName(randomNameTemplate);
171         newTemplate.setBpmnText(bpmnText);
172         newTemplate.setImageText(imageText);
173         // Save the template in DB
174         cldsDao.setTemplate(newTemplate, "user");
175         // Test if it's well there
176         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
177         assertEquals(bpmnText, newTemplateRead.getBpmnText());
178         assertEquals(imageText, newTemplateRead.getImageText());
179         // Save the model
180         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
181         CldsModel newModel = new CldsModel();
182         newModel.setName(randomNameModel);
183         newModel.setBpmnText(bpmnText);
184         newModel.setImageText(imageText);
185         newModel.setPropText(bpmnPropText);
186         newModel.setControlNamePrefix("ClosedLoop-");
187         newModel.setTemplateName(randomNameTemplate);
188         newModel.setTemplateId(newTemplate.getId());
189         newModel.setDocText(docText);
190         // Test the PutModel method
191
192         cldsService.putModel(randomNameModel, newModel);
193         // Verify whether it has been added properly or not
194         assertNotNull(cldsDao.getModel(randomNameModel));
195
196         CldsModel model = cldsService.getModel(randomNameModel);
197         // Verify with GetModel
198         assertEquals(model.getTemplateName(), randomNameTemplate);
199         assertEquals(model.getName(), randomNameModel);
200
201         assertTrue(cldsService.getModelNames().size() >= 1);
202
203         // Should fail
204         ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
205             randomNameModel, "false", cldsService.getModel(randomNameModel));
206         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
207         assertNotNull(responseEntity.getBody());
208         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
209         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
210
211         responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
212         assertNotNull(responseEntity);
213         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
214         assertNotNull(responseEntity.getBody());
215         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
216         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
217
218         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP, randomNameModel, "false",
219             cldsService.getModel(randomNameModel));
220         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
221         assertNotNull(responseEntity.getBody());
222         assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
223         assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
224
225         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART, randomNameModel, "false",
226             cldsService.getModel(randomNameModel));
227         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
228         assertNotNull(responseEntity.getBody());
229         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
230         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
231
232         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE, randomNameModel, "false",
233             cldsService.getModel(randomNameModel));
234         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
235         assertNotNull(responseEntity.getBody());
236         assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
237         assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
238
239         responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
240         assertNotNull(responseEntity);
241         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
242         assertNotNull(responseEntity.getBody());
243         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
244         assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
245
246         responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE, randomNameModel, "false",
247             cldsService.getModel(randomNameModel));
248         assertNotNull(responseEntity);
249         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
250         assertNotNull(responseEntity.getBody());
251         try {
252             cldsService.getModel(randomNameModel);
253             fail("Should have raised an NotFoundException exception");
254         } catch (NotFoundException ne) {
255
256         }
257
258     }
259
260     @Test
261     public void testDcaePost() {
262         DcaeEvent dcaeEvent = new DcaeEvent();
263         dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
264         dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
265         dcaeEvent.setResourceUUID("1");
266         dcaeEvent.setServiceUUID("2");
267         assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
268             "event=created serviceUUID=2 resourceUUID=1 artifactName=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
269     }
270
271     @Test
272     public void testGetSdcProperties() throws IOException {
273         JSONAssert.assertEquals(
274             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
275             cldsService.getSdcProperties(), true);
276     }
277
278     @Test
279     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
280         String result = cldsService.getSdcServices();
281         JSONAssert.assertEquals(
282             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
283     }
284
285     @Test
286     public void testGetSdcPropertiesByServiceUuidForRefresh()
287         throws GeneralSecurityException, DecoderException, JSONException, IOException {
288         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
289         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
290
291         cldsService.setSecurityContext(securityContext);
292         // Test basic functionalities
293         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
294             false);
295         JSONAssert.assertEquals(
296             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
297             true);
298         // Now test the Cache effect
299         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
300         // Should not be there, so should be null
301         assertNull(cldsServiceDataCache);
302         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
303         // Should be there now, so should NOT be null
304         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
305         assertNotNull(cldsServiceDataCache);
306         cldsDao.clearServiceCache();
307     }
308 }