2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.assertTrue;
31 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.security.Principal;
36 import java.util.Properties;
38 import javax.ws.rs.core.SecurityContext;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.AbstractItCase;
45 import org.onap.clamp.clds.dao.CldsDao;
46 import org.onap.clamp.clds.model.CldsHealthCheck;
47 import org.onap.clamp.clds.model.CldsInfo;
48 import org.onap.clamp.clds.model.CldsModel;
49 import org.onap.clamp.clds.model.CldsTemplate;
50 import org.onap.clamp.clds.service.CldsService;
51 import org.onap.clamp.clds.util.ResourceFileUtil;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
55 import org.springframework.test.context.TestPropertySource;
56 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
61 @RunWith(SpringJUnit4ClassRunner.class)
62 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
64 public class CldsServiceItCase extends AbstractItCase {
66 CldsService cldsService;
67 private String bpmnText;
68 private String imageText;
69 private String bpmnPropText;
71 public CldsDao cldsDao;
74 * Setup the variable before the tests execution.
77 * In case of issues when opening the files
80 public void setupBefore() throws IOException {
81 bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
82 imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
83 bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
87 public void testCldsInfoNotAuthorized() throws Exception {
88 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
89 Principal principal = Mockito.mock(Principal.class);
90 Mockito.when(principal.getName()).thenReturn("admin");
91 Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
92 cldsService.setSecurityContext(securityContext);
93 CldsInfo cldsInfo = cldsService.getCldsInfo();
94 assertFalse(cldsInfo.isPermissionReadCl());
95 assertFalse(cldsInfo.isPermissionReadTemplate());
96 assertFalse(cldsInfo.isPermissionUpdateCl());
97 assertFalse(cldsInfo.isPermissionUpdateTemplate());
101 public void testCldsInfoAuthorized() throws Exception {
102 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
103 Principal p = Mockito.mock(Principal.class);
104 Mockito.when(p.getName()).thenReturn("admin");
105 Mockito.when(securityContext.getUserPrincipal()).thenReturn(p);
106 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
107 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
108 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
109 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
110 cldsService.setSecurityContext(securityContext);
111 CldsInfo cldsInfo = cldsService.getCldsInfo();
112 assertTrue(cldsInfo.isPermissionReadCl());
113 assertTrue(cldsInfo.isPermissionReadTemplate());
114 assertTrue(cldsInfo.isPermissionUpdateCl());
115 assertTrue(cldsInfo.isPermissionUpdateTemplate());
116 Properties prop = new Properties();
117 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
120 assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
121 assertEquals(cldsInfo.getUserName(), "admin");
125 public void testGetHealthCheck() throws Exception {
126 CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck();
127 assertNotNull(cldsHealthCheck);
128 assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
129 assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
130 assertEquals("OK", cldsHealthCheck.getDescription());
134 public void testPutModel() throws Exception {
135 String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
136 String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
137 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
138 Principal p = Mockito.mock(Principal.class);
139 Mockito.when(p.getName()).thenReturn("admin");
140 Mockito.when(securityContext.getUserPrincipal()).thenReturn(p);
141 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
142 Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
143 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
144 Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
145 cldsService.setSecurityContext(securityContext);
146 // Add the template first
147 CldsTemplate newTemplate = new CldsTemplate();
148 newTemplate.setName(randomNameTemplate);
149 newTemplate.setBpmnText(bpmnText);
150 newTemplate.setImageText(imageText);
151 // Save the template in DB
152 cldsDao.setTemplate(newTemplate, "user");
153 // Test if it's well there
154 CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
155 assertEquals(bpmnText, newTemplateRead.getBpmnText());
156 assertEquals(imageText, newTemplateRead.getImageText());
158 CldsModel newModel = new CldsModel();
159 newModel.setName(randomNameTemplate);
160 newModel.setBpmnText(bpmnText);
161 newModel.setImageText(imageText);
162 newModel.setPropText(bpmnPropText);
163 newModel.setControlNamePrefix("ClosedLoop-");
164 newModel.setTemplateName("test-template");
165 newModel.setTemplateId(newTemplate.getId());
166 newModel.setDocText(newTemplate.getPropText());
167 newModel.setDocId(newTemplate.getPropId());
168 // Test the PutModel method
169 cldsService.putModel(randomNameModel, newModel);
170 // Verify whether it has been added properly or not
171 assertNotNull(cldsDao.getModel(randomNameModel));