Add tests
[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.CldsEvent;
54 import org.onap.clamp.clds.model.CldsInfo;
55 import org.onap.clamp.clds.model.CldsModel;
56 import org.onap.clamp.clds.model.CldsServiceData;
57 import org.onap.clamp.clds.model.CldsTemplate;
58 import org.onap.clamp.clds.service.CldsService;
59 import org.onap.clamp.clds.util.LoggingUtils;
60 import org.onap.clamp.clds.util.ResourceFileUtil;
61 import org.skyscreamer.jsonassert.JSONAssert;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.boot.test.context.SpringBootTest;
64 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
68 import org.springframework.security.core.Authentication;
69 import org.springframework.security.core.GrantedAuthority;
70 import org.springframework.security.core.authority.SimpleGrantedAuthority;
71 import org.springframework.security.core.context.SecurityContext;
72 import org.springframework.security.core.userdetails.User;
73 import org.springframework.security.core.userdetails.UserDetails;
74 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
75
76 /**
77  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
78  */
79 @RunWith(SpringJUnit4ClassRunner.class)
80 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
81 public class CldsServiceItCase {
82
83     @Autowired
84     private CldsService cldsService;
85     private String bpmnText;
86     private String imageText;
87     private String bpmnPropText;
88     @Autowired
89     private CldsDao cldsDao;
90     private Authentication authentication;
91     private List<GrantedAuthority> authList =  new LinkedList<GrantedAuthority>();
92     private LoggingUtils util;
93     /**
94      * Setup the variable before the tests execution.
95      *
96      * @throws IOException
97      *             In case of issues when opening the files
98      */
99     @Before
100     public void setupBefore() throws IOException {
101         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
102         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
103         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
104
105         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
106         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
107         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
108         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
109         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
110         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
111         authentication =  new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
112
113         util = Mockito.mock(LoggingUtils.class);
114         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
115         cldsService.setLoggingUtil(util);
116     }
117
118     @Test
119     public void testCldsInfoNotAuthorized() {
120         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
121         Authentication localAuth = Mockito.mock(Authentication.class);
122         UserDetails userDetails = Mockito.mock(UserDetails.class);
123         Mockito.when(userDetails.getUsername()).thenReturn("admin");
124         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
125         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
126
127         cldsService.setSecurityContext(securityContext);
128         CldsInfo cldsInfo = cldsService.getCldsInfo();
129         assertFalse(cldsInfo.isPermissionReadCl());
130         assertFalse(cldsInfo.isPermissionReadTemplate());
131         assertFalse(cldsInfo.isPermissionUpdateCl());
132         assertFalse(cldsInfo.isPermissionUpdateTemplate());
133     }
134
135     @Test
136     public void testCldsInfoAuthorized() throws Exception {
137         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
138         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
139
140         cldsService.setSecurityContext(securityContext);
141         CldsInfo cldsInfo = cldsService.getCldsInfo();
142         assertTrue(cldsInfo.isPermissionReadCl());
143         assertTrue(cldsInfo.isPermissionReadTemplate());
144         assertTrue(cldsInfo.isPermissionUpdateCl());
145         assertTrue(cldsInfo.isPermissionUpdateTemplate());
146         Properties prop = new Properties();
147         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
148         prop.load(in);
149         in.close();
150         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
151         assertEquals(cldsInfo.getUserName(), "admin");
152     }
153
154     @Test
155     public void testCompleteFlow() throws TransformerException, ParseException {
156         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
157         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
158
159         cldsService.setSecurityContext(securityContext);
160         // Add the template first
161         CldsTemplate newTemplate = new CldsTemplate();
162         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
163         newTemplate.setName(randomNameTemplate);
164         newTemplate.setBpmnText(bpmnText);
165         newTemplate.setImageText(imageText);
166         // Save the template in DB
167         cldsDao.setTemplate(newTemplate, "user");
168         // Test if it's well there
169         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
170         assertEquals(bpmnText, newTemplateRead.getBpmnText());
171         assertEquals(imageText, newTemplateRead.getImageText());
172         // Save the model
173         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
174         CldsModel newModel = new CldsModel();
175         newModel.setName(randomNameModel);
176         newModel.setBpmnText(bpmnText);
177         newModel.setImageText(imageText);
178         newModel.setPropText(bpmnPropText);
179         newModel.setControlNamePrefix("ClosedLoop-");
180         newModel.setTemplateName(randomNameTemplate);
181         newModel.setTemplateId(newTemplate.getId());
182         newModel.setDocText(newTemplate.getPropText());
183         // Test the PutModel method
184
185         cldsService.putModel(randomNameModel, newModel);
186         // Verify whether it has been added properly or not
187         assertNotNull(cldsDao.getModel(randomNameModel));
188
189         CldsModel model= cldsService.getModel(randomNameModel);
190         // Verify with GetModel
191         assertEquals(model.getTemplateName(),randomNameTemplate);
192         assertEquals(model.getName(),randomNameModel);
193
194         assertTrue(cldsService.getModelNames().size() >= 1);
195
196         // Should fail
197         ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT, randomNameModel, "true", model);
198         assertTrue(responseEntity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
199         model=(CldsModel)responseEntity.getBody();
200         assertNull(model);
201
202         responseEntity=cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
203         assertNotNull(responseEntity);
204         assertNotNull(responseEntity.getStatusCode());
205         model=(CldsModel)responseEntity.getBody();
206         assertNotNull(model);
207
208         responseEntity=cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
209         assertNotNull(responseEntity);
210         assertNotNull(responseEntity.getStatusCode());
211         model=(CldsModel)responseEntity.getBody();
212         assertNotNull(model);
213     }
214
215     @Test
216     public void testGetSdcProperties() throws IOException {
217         JSONAssert.assertEquals(
218             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"), cldsService.getSdcProperties(),
219             true);
220     }
221
222     @Test
223     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
224         String result = cldsService.getSdcServices();
225         JSONAssert.assertEquals(
226             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
227             true);
228     }
229
230     @Test
231     public void testGetSdcPropertiesByServiceUuidForRefresh()
232         throws GeneralSecurityException, DecoderException, JSONException, IOException {
233         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
234         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
235
236         cldsService.setSecurityContext(securityContext);
237         // Test basic functionalities
238         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
239             false);
240         JSONAssert.assertEquals(
241             ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
242             result, true);
243         // Now test the Cache effect
244         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
245         // Should not be there, so should be null
246         assertNull(cldsServiceDataCache);
247         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
248         // Should be there now, so should NOT be null
249         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
250         assertNotNull(cldsServiceDataCache);
251         cldsDao.clearServiceCache();
252     }
253 }