Merge "Code refactoring"
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
22  */
23
24 package org.onap.clamp.clds.it;
25
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;
30
31 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.security.Principal;
36 import java.util.Properties;
37
38 import javax.ws.rs.core.SecurityContext;
39
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;
57
58 /**
59  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
60  */
61 @RunWith(SpringJUnit4ClassRunner.class)
62 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
63 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
64 public class CldsServiceItCase extends AbstractItCase {
65
66     @Autowired
67     private CldsService cldsService;
68     private String bpmnText;
69     private String imageText;
70     private String bpmnPropText;
71     @Autowired
72     private CldsDao cldsDao;
73
74     /**
75      * Setup the variable before the tests execution.
76      * 
77      * @throws IOException
78      *             In case of issues when opening the files
79      */
80     @Before
81     public void setupBefore() throws IOException {
82         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
83         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
84         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
85     }
86
87     @Test
88     public void testCldsInfoNotAuthorized() {
89         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
90         Principal principal = Mockito.mock(Principal.class);
91         Mockito.when(principal.getName()).thenReturn("admin");
92         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
93         cldsService.setSecurityContext(securityContext);
94         CldsInfo cldsInfo = cldsService.getCldsInfo();
95         assertFalse(cldsInfo.isPermissionReadCl());
96         assertFalse(cldsInfo.isPermissionReadTemplate());
97         assertFalse(cldsInfo.isPermissionUpdateCl());
98         assertFalse(cldsInfo.isPermissionUpdateTemplate());
99     }
100
101     @Test
102     public void testCldsInfoAuthorized() throws Exception {
103         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
104         Principal principal = Mockito.mock(Principal.class);
105         Mockito.when(principal.getName()).thenReturn("admin");
106         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
107         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
108         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
109         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
110         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
111         cldsService.setSecurityContext(securityContext);
112         CldsInfo cldsInfo = cldsService.getCldsInfo();
113         assertTrue(cldsInfo.isPermissionReadCl());
114         assertTrue(cldsInfo.isPermissionReadTemplate());
115         assertTrue(cldsInfo.isPermissionUpdateCl());
116         assertTrue(cldsInfo.isPermissionUpdateTemplate());
117         Properties prop = new Properties();
118         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
119         prop.load(in);
120         in.close();
121         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
122         assertEquals(cldsInfo.getUserName(), "admin");
123     }
124
125     @Test
126     public void testGetHealthCheck() {
127         CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck();
128         assertNotNull(cldsHealthCheck);
129         assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
130         assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
131         assertEquals("OK", cldsHealthCheck.getDescription());
132     }
133
134     @Test
135     public void testPutModel() {
136         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
137         Principal principal = Mockito.mock(Principal.class);
138         Mockito.when(principal.getName()).thenReturn("admin");
139         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
140         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
141         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
142         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
143         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
144         cldsService.setSecurityContext(securityContext);
145         // Add the template first
146         CldsTemplate newTemplate = new CldsTemplate();
147         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
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());
157         // Save the model
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         // Test the PutModel method
168         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
169         cldsService.putModel(randomNameModel, newModel);
170         // Verify whether it has been added properly or not
171         assertNotNull(cldsDao.getModel(randomNameModel));
172     }
173 }