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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.SecurityContext;
42 import org.apache.commons.codec.DecoderException;
43 import org.json.JSONException;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.onap.clamp.clds.AbstractItCase;
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 extends AbstractItCase {
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 CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck();
132 assertNotNull(cldsHealthCheck);
133 assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
134 assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
135 assertEquals("OK", cldsHealthCheck.getDescription());
139 public void testPutModel() {
140 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
141 Principal principal = Mockito.mock(Principal.class);
142 Mockito.when(principal.getName()).thenReturn("admin");
143 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
144 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
145 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
146 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
147 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
148 cldsService.setSecurityContext(securityContext);
149 // Add the template first
150 CldsTemplate newTemplate = new CldsTemplate();
151 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
152 newTemplate.setName(randomNameTemplate);
153 newTemplate.setBpmnText(bpmnText);
154 newTemplate.setImageText(imageText);
155 // Save the template in DB
156 cldsDao.setTemplate(newTemplate, "user");
157 // Test if it's well there
158 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
159 assertEquals(bpmnText, newTemplateRead.getBpmnText());
160 assertEquals(imageText, newTemplateRead.getImageText());
162 CldsModel newModel = new CldsModel();
163 newModel.setName(randomNameTemplate);
164 newModel.setBpmnText(bpmnText);
165 newModel.setImageText(imageText);
166 newModel.setPropText(bpmnPropText);
167 newModel.setControlNamePrefix("ClosedLoop-");
168 newModel.setTemplateName("test-template");
169 newModel.setTemplateId(newTemplate.getId());
170 newModel.setDocText(newTemplate.getPropText());
171 // Test the PutModel method
172 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
173 cldsService.putModel(randomNameModel, newModel);
174 // Verify whether it has been added properly or not
175 assertNotNull(cldsDao.getModel(randomNameModel));
179 public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
180 String result = cldsService.getSdcServices();
181 JSONAssert.assertEquals(
182 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
187 public void testGetSdcPropertiesByServiceUUIDForRefresh()
188 throws GeneralSecurityException, DecoderException, JSONException, IOException {
189 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
190 Principal principal = Mockito.mock(Principal.class);
191 Mockito.when(principal.getName()).thenReturn("admin");
192 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
193 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
194 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
195 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
196 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
197 Mockito.when(securityContext.isUserInRole("permission-type-filter-vf|dev|*")).thenReturn(true);
198 cldsService.setSecurityContext(securityContext);
199 // Test basic functionalities
200 String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
202 JSONAssert.assertEquals(
203 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
205 // Now test the Cache effect
206 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
207 // Should not be there, so should be null
208 assertNull(cldsServiceDataCache);
209 cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
210 // Should be there now, so should NOT be null
211 cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
212 assertNotNull(cldsServiceDataCache);
213 cldsDao.clearServiceCache();