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.Properties;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.SecurityContext;
43 import org.apache.commons.codec.DecoderException;
44 import org.json.JSONException;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mockito;
49 import org.onap.clamp.clds.dao.CldsDao;
50 import org.onap.clamp.clds.model.CldsHealthCheck;
51 import org.onap.clamp.clds.model.CldsInfo;
52 import org.onap.clamp.clds.model.CldsModel;
53 import org.onap.clamp.clds.model.CldsServiceData;
54 import org.onap.clamp.clds.model.CldsTemplate;
55 import org.onap.clamp.clds.service.CldsService;
56 import org.onap.clamp.clds.util.ResourceFileUtil;
57 import org.skyscreamer.jsonassert.JSONAssert;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
61 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
64 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
66 @RunWith(SpringJUnit4ClassRunner.class)
67 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
68 public class CldsServiceItCase {
71 private CldsService cldsService;
72 private String bpmnText;
73 private String imageText;
74 private String bpmnPropText;
76 private CldsDao cldsDao;
79 * Setup the variable before the tests execution.
82 * In case of issues when opening the files
85 public void setupBefore() throws IOException {
86 bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
87 imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
88 bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
92 public void testCldsInfoNotAuthorized() {
93 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
94 Principal principal = Mockito.mock(Principal.class);
95 Mockito.when(principal.getName()).thenReturn("admin");
96 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
97 cldsService.setSecurityContext(securityContext);
98 CldsInfo cldsInfo = cldsService.getCldsInfo();
99 assertFalse(cldsInfo.isPermissionReadCl());
100 assertFalse(cldsInfo.isPermissionReadTemplate());
101 assertFalse(cldsInfo.isPermissionUpdateCl());
102 assertFalse(cldsInfo.isPermissionUpdateTemplate());
106 public void testCldsInfoAuthorized() throws Exception {
107 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
108 Principal principal = Mockito.mock(Principal.class);
109 Mockito.when(principal.getName()).thenReturn("admin");
110 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
111 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
112 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
113 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
114 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
115 cldsService.setSecurityContext(securityContext);
116 CldsInfo cldsInfo = cldsService.getCldsInfo();
117 assertTrue(cldsInfo.isPermissionReadCl());
118 assertTrue(cldsInfo.isPermissionReadTemplate());
119 assertTrue(cldsInfo.isPermissionUpdateCl());
120 assertTrue(cldsInfo.isPermissionUpdateTemplate());
121 Properties prop = new Properties();
122 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
125 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
126 assertEquals(cldsInfo.getUserName(), "admin");
130 public void testGetHealthCheck() {
131 Response response = cldsService.gethealthcheck();
132 CldsHealthCheck cldsHealthCheck = (CldsHealthCheck) response.getEntity();
133 assertNotNull(cldsHealthCheck);
134 assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
135 assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
136 assertEquals("OK", cldsHealthCheck.getDescription());
140 public void testPutModel() {
141 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
142 Principal principal = Mockito.mock(Principal.class);
143 Mockito.when(principal.getName()).thenReturn("admin");
144 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
145 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
146 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
147 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
148 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
149 cldsService.setSecurityContext(securityContext);
150 // Add the template first
151 CldsTemplate newTemplate = new CldsTemplate();
152 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
153 newTemplate.setName(randomNameTemplate);
154 newTemplate.setBpmnText(bpmnText);
155 newTemplate.setImageText(imageText);
156 // Save the template in DB
157 cldsDao.setTemplate(newTemplate, "user");
158 // Test if it's well there
159 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
160 assertEquals(bpmnText, newTemplateRead.getBpmnText());
161 assertEquals(imageText, newTemplateRead.getImageText());
163 CldsModel newModel = new CldsModel();
164 newModel.setName(randomNameTemplate);
165 newModel.setBpmnText(bpmnText);
166 newModel.setImageText(imageText);
167 newModel.setPropText(bpmnPropText);
168 newModel.setControlNamePrefix("ClosedLoop-");
169 newModel.setTemplateName("test-template");
170 newModel.setTemplateId(newTemplate.getId());
171 newModel.setDocText(newTemplate.getPropText());
172 // Test the PutModel method
173 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
174 cldsService.putModel(randomNameModel, newModel);
175 // Verify whether it has been added properly or not
176 assertNotNull(cldsDao.getModel(randomNameModel));
180 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
181 String result = cldsService.getSdcServices();
182 JSONAssert.assertEquals(
183 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
188 public void testGetSdcPropertiesByServiceUuidForRefresh()
189 throws GeneralSecurityException, DecoderException, JSONException, IOException {
190 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
191 Principal principal = Mockito.mock(Principal.class);
192 Mockito.when(principal.getName()).thenReturn("admin");
193 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
194 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
195 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
196 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
197 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
198 Mockito.when(securityContext.isUserInRole("permission-type-filter-vf|dev|*")).thenReturn(true);
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();