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.service.CldsService;
59 import org.onap.clamp.clds.util.LoggingUtils;
60 import org.onap.clamp.clds.util.ResourceFileUtil;
61 import org.skyscreamer.jsonassert.JSONAssert;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.boot.test.context.SpringBootTest;
64 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
68 import org.springframework.security.core.Authentication;
69 import org.springframework.security.core.GrantedAuthority;
70 import org.springframework.security.core.authority.SimpleGrantedAuthority;
71 import org.springframework.security.core.context.SecurityContext;
72 import org.springframework.security.core.userdetails.User;
73 import org.springframework.security.core.userdetails.UserDetails;
74 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
77 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
79 @RunWith(SpringJUnit4ClassRunner.class)
80 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
81 public class CldsServiceItCase {
84 private CldsService cldsService;
85 private String bpmnText;
86 private String imageText;
87 private String bpmnPropText;
89 private CldsDao cldsDao;
90 private Authentication authentication;
91 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
92 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/dao/bpmn-template.xml");
102 imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
103 bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
105 authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
106 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
107 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
108 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
109 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
110 authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
111 authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
113 util = Mockito.mock(LoggingUtils.class);
114 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
115 cldsService.setLoggingUtil(util);
119 public void testCldsInfoNotAuthorized() {
120 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
121 Authentication localAuth = Mockito.mock(Authentication.class);
122 UserDetails userDetails = Mockito.mock(UserDetails.class);
123 Mockito.when(userDetails.getUsername()).thenReturn("admin");
124 Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
125 Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
127 cldsService.setSecurityContext(securityContext);
128 CldsInfo cldsInfo = cldsService.getCldsInfo();
129 assertFalse(cldsInfo.isPermissionReadCl());
130 assertFalse(cldsInfo.isPermissionReadTemplate());
131 assertFalse(cldsInfo.isPermissionUpdateCl());
132 assertFalse(cldsInfo.isPermissionUpdateTemplate());
136 public void testCldsInfoAuthorized() throws Exception {
137 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
138 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
140 cldsService.setSecurityContext(securityContext);
141 CldsInfo cldsInfo = cldsService.getCldsInfo();
142 assertTrue(cldsInfo.isPermissionReadCl());
143 assertTrue(cldsInfo.isPermissionReadTemplate());
144 assertTrue(cldsInfo.isPermissionUpdateCl());
145 assertTrue(cldsInfo.isPermissionUpdateTemplate());
146 Properties prop = new Properties();
147 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
150 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
151 assertEquals(cldsInfo.getUserName(), "admin");
155 public void testCompleteFlow() throws TransformerException, ParseException {
156 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
157 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
159 cldsService.setSecurityContext(securityContext);
160 // Add the template first
161 CldsTemplate newTemplate = new CldsTemplate();
162 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
163 newTemplate.setName(randomNameTemplate);
164 newTemplate.setBpmnText(bpmnText);
165 newTemplate.setImageText(imageText);
166 // Save the template in DB
167 cldsDao.setTemplate(newTemplate, "user");
168 // Test if it's well there
169 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
170 assertEquals(bpmnText, newTemplateRead.getBpmnText());
171 assertEquals(imageText, newTemplateRead.getImageText());
173 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
174 CldsModel newModel = new CldsModel();
175 newModel.setName(randomNameModel);
176 newModel.setBpmnText(bpmnText);
177 newModel.setImageText(imageText);
178 newModel.setPropText(bpmnPropText);
179 newModel.setControlNamePrefix("ClosedLoop-");
180 newModel.setTemplateName(randomNameTemplate);
181 newModel.setTemplateId(newTemplate.getId());
182 newModel.setDocText(newTemplate.getPropText());
183 // Test the PutModel method
185 cldsService.putModel(randomNameModel, newModel);
186 // Verify whether it has been added properly or not
187 assertNotNull(cldsDao.getModel(randomNameModel));
189 CldsModel model= cldsService.getModel(randomNameModel);
190 // Verify with GetModel
191 assertEquals(model.getTemplateName(),randomNameTemplate);
192 assertEquals(model.getName(),randomNameModel);
194 assertTrue(cldsService.getModelNames().size() >= 1);
197 ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT, randomNameModel, "true", model);
198 assertTrue(responseEntity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
199 model=(CldsModel)responseEntity.getBody();
202 responseEntity=cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
203 assertNotNull(responseEntity);
204 assertNotNull(responseEntity.getStatusCode());
205 model=(CldsModel)responseEntity.getBody();
206 assertNotNull(model);
208 responseEntity=cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
209 assertNotNull(responseEntity);
210 assertNotNull(responseEntity.getStatusCode());
211 model=(CldsModel)responseEntity.getBody();
212 assertNotNull(model);
216 public void testGetSdcProperties() throws IOException {
217 JSONAssert.assertEquals(
218 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"), cldsService.getSdcProperties(),
223 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
224 String result = cldsService.getSdcServices();
225 JSONAssert.assertEquals(
226 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
231 public void testGetSdcPropertiesByServiceUuidForRefresh()
232 throws GeneralSecurityException, DecoderException, JSONException, IOException {
233 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
234 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
236 cldsService.setSecurityContext(securityContext);
237 // Test basic functionalities
238 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
240 JSONAssert.assertEquals(
241 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
243 // Now test the Cache effect
244 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
245 // Should not be there, so should be null
246 assertNull(cldsServiceDataCache);
247 cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
248 // Should be there now, so should NOT be null
249 cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
250 assertNotNull(cldsServiceDataCache);
251 cldsDao.clearServiceCache();