2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.it;
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;
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;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.NotFoundException;
39 import javax.xml.transform.TransformerException;
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;
74 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
76 @RunWith(SpringJUnit4ClassRunner.class)
77 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
78 public class CldsServiceItCase {
81 private CldsService cldsService;
82 private String bpmnText;
83 private String imageText;
84 private String bpmnPropText;
85 private String docText;
88 private CldsDao cldsDao;
89 private Authentication authentication;
90 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
91 private LoggingUtils util;
94 * Setup the variable before the tests execution.
97 * In case of issues when opening the files
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");
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);
115 util = Mockito.mock(LoggingUtils.class);
116 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
117 cldsService.setLoggingUtil(util);
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);
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());
139 public void testCldsInfoAuthorized() throws Exception {
140 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
141 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
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");
154 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
155 assertEquals(cldsInfo.getUserName(), "admin");
159 public void testGetCldsDetails() throws IOException {
160 List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails();
161 assertNotNull(cldsMonitoringDetailsList);
164 @Test(expected = NotFoundException.class)
165 public void testCompleteFlow() throws TransformerException, ParseException {
166 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
167 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
169 cldsService.setSecurityContext(securityContext);
170 // Add the template first
171 CldsTemplate newTemplate = new CldsTemplate();
172 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
173 newTemplate.setName(randomNameTemplate);
174 newTemplate.setBpmnText(bpmnText);
175 newTemplate.setImageText(imageText);
176 // Save the template in DB
177 cldsDao.setTemplate(newTemplate, "user");
178 // Test if it's well there
179 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
180 assertEquals(bpmnText, newTemplateRead.getBpmnText());
181 assertEquals(imageText, newTemplateRead.getImageText());
183 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
184 CldsModel newModel = new CldsModel();
185 newModel.setName(randomNameModel);
186 newModel.setBpmnText(bpmnText);
187 newModel.setImageText(imageText);
188 newModel.setPropText(bpmnPropText);
189 newModel.setControlNamePrefix("ClosedLoop-");
190 newModel.setTemplateName(randomNameTemplate);
191 newModel.setTemplateId(newTemplate.getId());
192 newModel.setDocText(docText);
193 // Test the PutModel method
195 cldsService.putModel(randomNameModel, newModel);
197 assertEquals(bpmnText, cldsService.getBpmnXml(randomNameModel));
198 assertEquals(imageText, cldsService.getImageXml(randomNameModel));
200 // Verify whether it has been added properly or not
201 assertNotNull(cldsDao.getModel(randomNameModel));
203 CldsModel model = cldsService.getModel(randomNameModel);
204 // Verify with GetModel
205 assertEquals(model.getTemplateName(), randomNameTemplate);
206 assertEquals(model.getName(), randomNameModel);
208 assertTrue(cldsService.getModelNames().size() >= 1);
211 ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
212 randomNameModel, "false", cldsService.getModel(randomNameModel));
213 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
214 assertNotNull(responseEntity.getBody());
215 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
216 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
218 responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
219 assertNotNull(responseEntity);
220 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
221 assertNotNull(responseEntity.getBody());
222 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
223 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
225 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP, randomNameModel, "false",
226 cldsService.getModel(randomNameModel));
227 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
228 assertNotNull(responseEntity.getBody());
229 assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
230 assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
232 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART, 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()));
239 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE, randomNameModel, "false",
240 cldsService.getModel(randomNameModel));
241 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
242 assertNotNull(responseEntity.getBody());
243 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
244 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
246 responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
247 assertNotNull(responseEntity);
248 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
249 assertNotNull(responseEntity.getBody());
250 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
251 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
253 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE, randomNameModel, "false",
254 cldsService.getModel(randomNameModel));
255 assertNotNull(responseEntity);
256 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
257 assertNotNull(responseEntity.getBody());
258 // This will raise an exception
259 cldsService.getModel(randomNameModel);
263 public void testDcaePost() {
264 DcaeEvent dcaeEvent = new DcaeEvent();
265 dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
266 dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
267 dcaeEvent.setResourceUUID("1");
268 dcaeEvent.setServiceUUID("2");
269 assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
270 "event=created serviceUUID=2 resourceUUID=1 artifactName="
271 + "ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
275 public void testGetSdcProperties() throws IOException {
276 JSONAssert.assertEquals(
277 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
278 cldsService.getSdcProperties(), true);