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 com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.security.GeneralSecurityException;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Properties;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.NotFoundException;
43 import javax.xml.transform.TransformerException;
45 import org.apache.commons.codec.DecoderException;
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.authority.SimpleGrantedAuthority;
72 import org.springframework.security.core.authority.SimpleGrantedAuthority;
73 import org.springframework.security.core.context.SecurityContext;
74 import org.springframework.security.core.GrantedAuthority;
75 import org.springframework.security.core.userdetails.User;
76 import org.springframework.security.core.userdetails.UserDetails;
77 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
80 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
82 @RunWith(SpringJUnit4ClassRunner.class)
83 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
84 public class CldsServiceItCase {
87 private CldsService cldsService;
88 private String bpmnText;
89 private String imageText;
90 private String bpmnPropText;
91 private String docText;
94 private CldsDao cldsDao;
95 private Authentication authentication;
96 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
97 private LoggingUtils util;
100 * Setup the variable before the tests execution.
102 * @throws IOException
103 * In case of issues when opening the files
106 public void setupBefore() throws IOException {
107 bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml");
108 imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
109 bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
110 docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");
112 authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
113 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
114 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
115 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
116 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
117 authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
118 authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
119 authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
121 util = Mockito.mock(LoggingUtils.class);
122 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
123 cldsService.setLoggingUtil(util);
127 public void testCldsInfoNotAuthorized() {
128 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
129 Authentication localAuth = Mockito.mock(Authentication.class);
130 UserDetails userDetails = Mockito.mock(UserDetails.class);
131 Mockito.when(userDetails.getUsername()).thenReturn("admin");
132 Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
133 Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
135 cldsService.setSecurityContext(securityContext);
136 CldsInfo cldsInfo = cldsService.getCldsInfo();
137 assertFalse(cldsInfo.isPermissionReadCl());
138 assertFalse(cldsInfo.isPermissionReadTemplate());
139 assertFalse(cldsInfo.isPermissionUpdateCl());
140 assertFalse(cldsInfo.isPermissionUpdateTemplate());
144 public void testCldsInfoAuthorized() throws Exception {
145 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
146 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
148 cldsService.setSecurityContext(securityContext);
149 CldsInfo cldsInfo = cldsService.getCldsInfo();
150 assertTrue(cldsInfo.isPermissionReadCl());
151 assertTrue(cldsInfo.isPermissionReadTemplate());
152 assertTrue(cldsInfo.isPermissionUpdateCl());
153 assertTrue(cldsInfo.isPermissionUpdateTemplate());
154 Properties prop = new Properties();
155 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
158 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
159 assertEquals(cldsInfo.getUserName(), "admin");
163 public void testCompleteFlow() throws TransformerException, ParseException {
164 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
165 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
167 cldsService.setSecurityContext(securityContext);
168 // Add the template first
169 CldsTemplate newTemplate = new CldsTemplate();
170 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
171 newTemplate.setName(randomNameTemplate);
172 newTemplate.setBpmnText(bpmnText);
173 newTemplate.setImageText(imageText);
174 // Save the template in DB
175 cldsDao.setTemplate(newTemplate, "user");
176 // Test if it's well there
177 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
178 assertEquals(bpmnText, newTemplateRead.getBpmnText());
179 assertEquals(imageText, newTemplateRead.getImageText());
181 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
182 CldsModel newModel = new CldsModel();
183 newModel.setName(randomNameModel);
184 newModel.setBpmnText(bpmnText);
185 newModel.setImageText(imageText);
186 newModel.setPropText(bpmnPropText);
187 newModel.setControlNamePrefix("ClosedLoop-");
188 newModel.setTemplateName(randomNameTemplate);
189 newModel.setTemplateId(newTemplate.getId());
190 newModel.setDocText(docText);
191 // Test the PutModel method
193 cldsService.putModel(randomNameModel, newModel);
194 // Verify whether it has been added properly or not
195 assertNotNull(cldsDao.getModel(randomNameModel));
197 CldsModel model = cldsService.getModel(randomNameModel);
198 // Verify with GetModel
199 assertEquals(model.getTemplateName(), randomNameTemplate);
200 assertEquals(model.getName(), randomNameModel);
202 assertTrue(cldsService.getModelNames().size() >= 1);
205 ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
206 randomNameModel, "false", cldsService.getModel(randomNameModel));
207 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
208 assertNotNull(responseEntity.getBody());
209 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
210 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
212 responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
213 assertNotNull(responseEntity);
214 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
215 assertNotNull(responseEntity.getBody());
216 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
217 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
219 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP,
220 randomNameModel, "false", cldsService.getModel(randomNameModel));
221 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
222 assertNotNull(responseEntity.getBody());
223 assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
224 assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
226 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART,
227 randomNameModel, "false", cldsService.getModel(randomNameModel));
228 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
229 assertNotNull(responseEntity.getBody());
230 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
231 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
233 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE,
234 randomNameModel, "false", cldsService.getModel(randomNameModel));
235 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
236 assertNotNull(responseEntity.getBody());
237 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
238 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
240 responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
241 assertNotNull(responseEntity);
242 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
243 assertNotNull(responseEntity.getBody());
244 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
245 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
247 responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE,
248 randomNameModel, "false", cldsService.getModel(randomNameModel));
249 assertNotNull(responseEntity);
250 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
251 assertNotNull(responseEntity.getBody());
253 cldsService.getModel(randomNameModel);
254 fail("Should have raised an NotFoundException exception");
255 } catch(NotFoundException ne) {
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=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
273 public void testGetSdcProperties() throws IOException {
274 JSONAssert.assertEquals(
275 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
276 cldsService.getSdcProperties(), true);
280 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
281 String result = cldsService.getSdcServices();
282 JSONAssert.assertEquals(
283 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
287 public void testGetSdcPropertiesByServiceUuidForRefresh()
288 throws GeneralSecurityException, DecoderException, JSONException, IOException {
289 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
290 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
292 cldsService.setSecurityContext(securityContext);
293 // Test basic functionalities
294 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
296 JSONAssert.assertEquals(
297 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
299 // Now test the Cache effect
300 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
301 // Should not be there, so should be null
302 assertNull(cldsServiceDataCache);
303 cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
304 // Should be there now, so should NOT be null
305 cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
306 assertNotNull(cldsServiceDataCache);
307 cldsDao.clearServiceCache();