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.security.Principal;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Properties;
42 import javax.servlet.http.HttpServletRequest;
44 import org.apache.commons.codec.DecoderException;
45 import org.json.JSONException;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Matchers;
50 import org.mockito.Mockito;
51 import org.onap.clamp.clds.dao.CldsDao;
52 import org.onap.clamp.clds.model.CldsHealthCheck;
53 import org.onap.clamp.clds.model.CldsInfo;
54 import org.onap.clamp.clds.model.CldsModel;
55 import org.onap.clamp.clds.model.CldsServiceData;
56 import org.onap.clamp.clds.model.CldsTemplate;
57 import org.onap.clamp.clds.service.CldsService;
58 import org.onap.clamp.clds.util.LoggingUtils;
59 import org.onap.clamp.clds.util.ResourceFileUtil;
60 import org.skyscreamer.jsonassert.JSONAssert;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.boot.test.context.SpringBootTest;
63 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
64 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
65 import org.springframework.security.core.authority.SimpleGrantedAuthority;
66 import org.springframework.security.core.Authentication;
67 import org.springframework.security.core.GrantedAuthority;
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;
86 private CldsDao cldsDao;
87 private Authentication authentication;
88 private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
89 private LoggingUtils util;
91 * Setup the variable before the tests execution.
94 * In case of issues when opening the files
97 public void setupBefore() throws IOException {
98 bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
99 imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
100 bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
102 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
103 authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
104 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
105 authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
106 authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
107 authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
109 util = Mockito.mock(LoggingUtils.class);
110 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
111 cldsService.setLoggingUtil(util);
115 public void testCldsInfoNotAuthorized() {
116 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
117 Authentication localAuth = Mockito.mock(Authentication.class);
118 UserDetails userDetails = Mockito.mock(UserDetails.class);
119 Mockito.when(userDetails.getUsername()).thenReturn("admin");
120 Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
121 Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
123 cldsService.setSecurityContext(securityContext);
124 CldsInfo cldsInfo = cldsService.getCldsInfo();
125 assertFalse(cldsInfo.isPermissionReadCl());
126 assertFalse(cldsInfo.isPermissionReadTemplate());
127 assertFalse(cldsInfo.isPermissionUpdateCl());
128 assertFalse(cldsInfo.isPermissionUpdateTemplate());
132 public void testCldsInfoAuthorized() throws Exception {
133 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
134 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
136 cldsService.setSecurityContext(securityContext);
137 CldsInfo cldsInfo = cldsService.getCldsInfo();
138 assertTrue(cldsInfo.isPermissionReadCl());
139 assertTrue(cldsInfo.isPermissionReadTemplate());
140 assertTrue(cldsInfo.isPermissionUpdateCl());
141 assertTrue(cldsInfo.isPermissionUpdateTemplate());
142 Properties prop = new Properties();
143 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
146 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
147 assertEquals(cldsInfo.getUserName(), "admin");
151 public void testPutModel() {
152 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
153 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
155 cldsService.setSecurityContext(securityContext);
156 // Add the template first
157 CldsTemplate newTemplate = new CldsTemplate();
158 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
159 newTemplate.setName(randomNameTemplate);
160 newTemplate.setBpmnText(bpmnText);
161 newTemplate.setImageText(imageText);
162 // Save the template in DB
163 cldsDao.setTemplate(newTemplate, "user");
164 // Test if it's well there
165 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
166 assertEquals(bpmnText, newTemplateRead.getBpmnText());
167 assertEquals(imageText, newTemplateRead.getImageText());
169 CldsModel newModel = new CldsModel();
170 newModel.setName(randomNameTemplate);
171 newModel.setBpmnText(bpmnText);
172 newModel.setImageText(imageText);
173 newModel.setPropText(bpmnPropText);
174 newModel.setControlNamePrefix("ClosedLoop-");
175 newModel.setTemplateName("test-template");
176 newModel.setTemplateId(newTemplate.getId());
177 newModel.setDocText(newTemplate.getPropText());
178 // Test the PutModel method
179 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
180 cldsService.putModel(randomNameModel, newModel);
181 // Verify whether it has been added properly or not
182 assertNotNull(cldsDao.getModel(randomNameModel));
186 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
187 String result = cldsService.getSdcServices();
188 JSONAssert.assertEquals(
189 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
194 public void testGetSdcPropertiesByServiceUuidForRefresh()
195 throws GeneralSecurityException, DecoderException, JSONException, IOException {
196 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
197 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
199 cldsService.setSecurityContext(securityContext);
200 // Test basic functionalities
201 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
203 JSONAssert.assertEquals(
204 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
206 // Now test the Cache effect
207 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
208 // Should not be there, so should be null
209 assertNull(cldsServiceDataCache);
210 cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
211 // Should be there now, so should NOT be null
212 cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
213 assertNotNull(cldsServiceDataCache);
214 cldsDao.clearServiceCache();