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.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
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;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.NotFoundException;
42 import javax.xml.transform.TransformerException;
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;
79 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
81 @RunWith(SpringJUnit4ClassRunner.class)
82 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
83 public class CldsServiceItCase {
86 private CldsService cldsService;
87 private String bpmnText;
88 private String imageText;
89 private String bpmnPropText;
90 private String docText;
93 private CldsDao cldsDao;
94 private Authentication authentication;
95 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
96 private LoggingUtils util;
99 * Setup the variable before the tests execution.
101 * @throws IOException
102 * In case of issues when opening the files
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");
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);
120 util = Mockito.mock(LoggingUtils.class);
121 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
122 cldsService.setLoggingUtil(util);
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);
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());
143 public void testCldsInfoAuthorized() throws Exception {
144 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
145 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
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");
157 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
158 assertEquals(cldsInfo.getUserName(), "admin");
162 public void testCompleteFlow() throws TransformerException, ParseException {
163 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
164 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
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());
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
192 cldsService.putModel(randomNameModel, newModel);
193 // Verify whether it has been added properly or not
194 assertNotNull(cldsDao.getModel(randomNameModel));
196 CldsModel model = cldsService.getModel(randomNameModel);
197 // Verify with GetModel
198 assertEquals(model.getTemplateName(), randomNameTemplate);
199 assertEquals(model.getName(), randomNameModel);
201 assertTrue(cldsService.getModelNames().size() >= 1);
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()));
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()));
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()));
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()));
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()));
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()));
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());
252 cldsService.getModel(randomNameModel);
253 fail("Should have raised an NotFoundException exception");
254 } catch (NotFoundException ne) {
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");
272 public void testGetSdcProperties() throws IOException {
273 JSONAssert.assertEquals(
274 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
275 cldsService.getSdcProperties(), true);
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);
286 public void testGetSdcPropertiesByServiceUuidForRefresh()
287 throws GeneralSecurityException, DecoderException, JSONException, IOException {
288 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
289 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
291 cldsService.setSecurityContext(securityContext);
292 // Test basic functionalities
293 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
295 JSONAssert.assertEquals(
296 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
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();