Guard policy Backend
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
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.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.security.GeneralSecurityException;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Properties;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.xml.transform.TransformerException;
43
44 import org.apache.commons.codec.DecoderException;
45 import org.json.JSONException;
46 import org.json.simple.parser.ParseException;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Matchers;
51 import org.mockito.Mockito;
52 import org.onap.clamp.clds.dao.CldsDao;
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.Authentication;
66 import org.springframework.security.core.GrantedAuthority;
67 import org.springframework.security.core.authority.SimpleGrantedAuthority;
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;
72
73 /**
74  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
75  */
76 @RunWith(SpringJUnit4ClassRunner.class)
77 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
78 public class CldsServiceItCase {
79
80     @Autowired
81     private CldsService cldsService;
82     private String bpmnText;
83     private String imageText;
84     private String bpmnPropText;
85     @Autowired
86     private CldsDao cldsDao;
87     private Authentication authentication;
88     private List<GrantedAuthority> authList =  new LinkedList<GrantedAuthority>();
89     private LoggingUtils util;
90     /**
91      * Setup the variable before the tests execution.
92      *
93      * @throws IOException
94      *             In case of issues when opening the files
95      */
96     @Before
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");
101
102         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
103         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
104         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
105         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
106         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
107         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
108         authentication =  new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
109
110         util = Mockito.mock(LoggingUtils.class);
111         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
112         cldsService.setLoggingUtil(util);
113     }
114
115     @Test
116     public void testCldsInfoNotAuthorized() {
117         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
118         Authentication localAuth = Mockito.mock(Authentication.class);
119         UserDetails userDetails = Mockito.mock(UserDetails.class);
120         Mockito.when(userDetails.getUsername()).thenReturn("admin");
121         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
122         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
123
124         cldsService.setSecurityContext(securityContext);
125         CldsInfo cldsInfo = cldsService.getCldsInfo();
126         assertFalse(cldsInfo.isPermissionReadCl());
127         assertFalse(cldsInfo.isPermissionReadTemplate());
128         assertFalse(cldsInfo.isPermissionUpdateCl());
129         assertFalse(cldsInfo.isPermissionUpdateTemplate());
130     }
131
132     @Test
133     public void testCldsInfoAuthorized() throws Exception {
134         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
135         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
136
137         cldsService.setSecurityContext(securityContext);
138         CldsInfo cldsInfo = cldsService.getCldsInfo();
139         assertTrue(cldsInfo.isPermissionReadCl());
140         assertTrue(cldsInfo.isPermissionReadTemplate());
141         assertTrue(cldsInfo.isPermissionUpdateCl());
142         assertTrue(cldsInfo.isPermissionUpdateTemplate());
143         Properties prop = new Properties();
144         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
145         prop.load(in);
146         in.close();
147         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
148         assertEquals(cldsInfo.getUserName(), "admin");
149     }
150
151     @Test
152     public void testCompleteFlow() throws TransformerException, ParseException {
153         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
154         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
155
156         cldsService.setSecurityContext(securityContext);
157         // Add the template first
158         CldsTemplate newTemplate = new CldsTemplate();
159         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
160         newTemplate.setName(randomNameTemplate);
161         newTemplate.setBpmnText(bpmnText);
162         newTemplate.setImageText(imageText);
163         // Save the template in DB
164         cldsDao.setTemplate(newTemplate, "user");
165         // Test if it's well there
166         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
167         assertEquals(bpmnText, newTemplateRead.getBpmnText());
168         assertEquals(imageText, newTemplateRead.getImageText());
169         // Save the model
170         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
171         CldsModel newModel = new CldsModel();
172         newModel.setName(randomNameModel);
173         newModel.setBpmnText(bpmnText);
174         newModel.setImageText(imageText);
175         newModel.setPropText(bpmnPropText);
176         newModel.setControlNamePrefix("ClosedLoop-");
177         newModel.setTemplateName(randomNameTemplate);
178         newModel.setTemplateId(newTemplate.getId());
179         newModel.setDocText(newTemplate.getPropText());
180         // Test the PutModel method
181
182         cldsService.putModel(randomNameModel, newModel);
183         // Verify whether it has been added properly or not
184         assertNotNull(cldsDao.getModel(randomNameModel));
185
186         // Verify with GetModel
187         assertEquals(cldsService.getModel(randomNameModel).getTemplateName(),randomNameTemplate);
188         assertEquals(cldsService.getModel(randomNameModel).getName(),randomNameModel);
189
190         assertTrue(cldsService.getModelNames().size() >= 1);
191     }
192
193     @Test
194     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
195         String result = cldsService.getSdcServices();
196         JSONAssert.assertEquals(
197             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
198             true);
199     }
200
201     @Test
202     public void testGetSdcPropertiesByServiceUuidForRefresh()
203         throws GeneralSecurityException, DecoderException, JSONException, IOException {
204         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
205         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
206
207         cldsService.setSecurityContext(securityContext);
208         // Test basic functionalities
209         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
210             false);
211         JSONAssert.assertEquals(
212             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
213             result, true);
214         // Now test the Cache effect
215         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
216         // Should not be there, so should be null
217         assertNull(cldsServiceDataCache);
218         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
219         // Should be there now, so should NOT be null
220         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
221         assertNotNull(cldsServiceDataCache);
222         cldsDao.clearServiceCache();
223     }
224 }