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;
32 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.security.GeneralSecurityException;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Properties;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.xml.transform.TransformerException;
44 import org.apache.commons.codec.DecoderException;
45 import org.json.JSONException;
46 import org.json.simple.parser.ParseException;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Matchers;
51 import org.mockito.Mockito;
52 import org.onap.clamp.clds.dao.CldsDao;
53 import org.onap.clamp.clds.model.CldsEvent;
54 import org.onap.clamp.clds.model.CldsInfo;
55 import org.onap.clamp.clds.model.CldsModel;
56 import org.onap.clamp.clds.model.CldsServiceData;
57 import org.onap.clamp.clds.model.CldsTemplate;
58 import org.onap.clamp.clds.model.DcaeEvent;
59 import org.onap.clamp.clds.service.CldsService;
60 import org.onap.clamp.clds.util.LoggingUtils;
61 import org.onap.clamp.clds.util.ResourceFileUtil;
62 import org.skyscreamer.jsonassert.JSONAssert;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.boot.test.context.SpringBootTest;
65 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
66 import org.springframework.http.HttpStatus;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
69 import org.springframework.security.core.Authentication;
70 import org.springframework.security.core.GrantedAuthority;
71 import org.springframework.security.core.authority.SimpleGrantedAuthority;
72 import org.springframework.security.core.context.SecurityContext;
73 import org.springframework.security.core.userdetails.User;
74 import org.springframework.security.core.userdetails.UserDetails;
75 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
78 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
80 @RunWith(SpringJUnit4ClassRunner.class)
81 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
82 public class CldsServiceItCase {
85 private CldsService cldsService;
86 private String bpmnText;
87 private String imageText;
88 private String bpmnPropText;
89 private String docText;
92 private CldsDao cldsDao;
93 private Authentication authentication;
94 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
95 private LoggingUtils util;
98 * Setup the variable before the tests execution.
100 * @throws IOException
101 * In case of issues when opening the files
104 public void setupBefore() throws IOException {
105 bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml");
106 imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
107 bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
108 docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");
110 authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
111 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
112 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
113 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
114 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
115 authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
116 authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
117 authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
119 util = Mockito.mock(LoggingUtils.class);
120 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
121 cldsService.setLoggingUtil(util);
125 public void testCldsInfoNotAuthorized() {
126 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
127 Authentication localAuth = Mockito.mock(Authentication.class);
128 UserDetails userDetails = Mockito.mock(UserDetails.class);
129 Mockito.when(userDetails.getUsername()).thenReturn("admin");
130 Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
131 Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
133 cldsService.setSecurityContext(securityContext);
134 CldsInfo cldsInfo = cldsService.getCldsInfo();
135 assertFalse(cldsInfo.isPermissionReadCl());
136 assertFalse(cldsInfo.isPermissionReadTemplate());
137 assertFalse(cldsInfo.isPermissionUpdateCl());
138 assertFalse(cldsInfo.isPermissionUpdateTemplate());
142 public void testCldsInfoAuthorized() throws Exception {
143 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
144 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
146 cldsService.setSecurityContext(securityContext);
147 CldsInfo cldsInfo = cldsService.getCldsInfo();
148 assertTrue(cldsInfo.isPermissionReadCl());
149 assertTrue(cldsInfo.isPermissionReadTemplate());
150 assertTrue(cldsInfo.isPermissionUpdateCl());
151 assertTrue(cldsInfo.isPermissionUpdateTemplate());
152 Properties prop = new Properties();
153 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
156 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
157 assertEquals(cldsInfo.getUserName(), "admin");
161 public void testCompleteFlow() throws TransformerException, ParseException {
162 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
163 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
165 cldsService.setSecurityContext(securityContext);
166 // Add the template first
167 CldsTemplate newTemplate = new CldsTemplate();
168 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
169 newTemplate.setName(randomNameTemplate);
170 newTemplate.setBpmnText(bpmnText);
171 newTemplate.setImageText(imageText);
172 // Save the template in DB
173 cldsDao.setTemplate(newTemplate, "user");
174 // Test if it's well there
175 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
176 assertEquals(bpmnText, newTemplateRead.getBpmnText());
177 assertEquals(imageText, newTemplateRead.getImageText());
179 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
180 CldsModel newModel = new CldsModel();
181 newModel.setName(randomNameModel);
182 newModel.setBpmnText(bpmnText);
183 newModel.setImageText(imageText);
184 newModel.setPropText(bpmnPropText);
185 newModel.setControlNamePrefix("ClosedLoop-");
186 newModel.setTemplateName(randomNameTemplate);
187 newModel.setTemplateId(newTemplate.getId());
188 newModel.setDocText(docText);
189 // Test the PutModel method
191 cldsService.putModel(randomNameModel, newModel);
192 // Verify whether it has been added properly or not
193 assertNotNull(cldsDao.getModel(randomNameModel));
195 CldsModel model = cldsService.getModel(randomNameModel);
196 // Verify with GetModel
197 assertEquals(model.getTemplateName(), randomNameTemplate);
198 assertEquals(model.getName(), randomNameModel);
200 assertTrue(cldsService.getModelNames().size() >= 1);
203 ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
204 randomNameModel, "false", cldsService.getModel(randomNameModel));
205 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
206 assertNotNull(responseEntity.getBody());
207 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
208 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
210 responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
211 assertNotNull(responseEntity);
212 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
213 assertNotNull(responseEntity.getBody());
214 assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
215 assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
217 responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
218 assertNotNull(responseEntity);
219 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
220 assertNotNull(responseEntity.getBody());
221 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
222 assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
224 DcaeEvent dcaeEvent = new DcaeEvent();
225 dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
226 dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
227 dcaeEvent.setResourceUUID("1");
228 dcaeEvent.setServiceUUID("2");
229 assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
230 "event=created serviceUUID=2 resourceUUID=1 artifactName=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
234 public void testGetSdcProperties() throws IOException {
235 JSONAssert.assertEquals(
236 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
237 cldsService.getSdcProperties(), true);
241 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
242 String result = cldsService.getSdcServices();
243 JSONAssert.assertEquals(
244 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
248 public void testGetSdcPropertiesByServiceUuidForRefresh()
249 throws GeneralSecurityException, DecoderException, JSONException, IOException {
250 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
251 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
253 cldsService.setSecurityContext(securityContext);
254 // Test basic functionalities
255 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
257 JSONAssert.assertEquals(
258 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
260 // Now test the Cache effect
261 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
262 // Should not be there, so should be null
263 assertNull(cldsServiceDataCache);
264 cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
265 // Should be there now, so should NOT be null
266 cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
267 assertNotNull(cldsServiceDataCache);
268 cldsDao.clearServiceCache();