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");
153 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
154 assertEquals(cldsInfo.getUserName(), "admin");
158 public void testGetCldsDetails() throws IOException {
159 List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails();
160 assertNotNull(cldsMonitoringDetailsList);
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);
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());
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
194 cldsService.putModel(randomNameModel, newModel);
196 assertEquals(bpmnText, cldsService.getBpmnXml(randomNameModel));
197 assertEquals(imageText, cldsService.getImageXml(randomNameModel));
199 // Verify whether it has been added properly or not
200 assertNotNull(cldsDao.getModel(randomNameModel));
202 CldsModel model = cldsService.getModel(randomNameModel);
203 // Verify with GetModel
204 assertEquals(model.getTemplateName(), randomNameTemplate);
205 assertEquals(model.getName(), randomNameModel);
207 assertTrue(cldsService.getModelNames().size() >= 1);
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()));
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()));
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()));
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()));
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()));
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()));
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);
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");
274 public void testGetSdcProperties() throws IOException {
275 JSONAssert.assertEquals(
276 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
277 cldsService.getSdcProperties(), true);