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