23df19c628091ae64648f90fb5d387b91786e11b
[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.security.Principal;
38 import java.util.Properties;
39
40 import javax.ws.rs.core.SecurityContext;
41
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.dao.CldsDao;
49 import org.onap.clamp.clds.model.CldsHealthCheck;
50 import org.onap.clamp.clds.model.CldsInfo;
51 import org.onap.clamp.clds.model.CldsModel;
52 import org.onap.clamp.clds.model.CldsServiceData;
53 import org.onap.clamp.clds.model.CldsTemplate;
54 import org.onap.clamp.clds.service.CldsService;
55 import org.onap.clamp.clds.util.ResourceFileUtil;
56 import org.skyscreamer.jsonassert.JSONAssert;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
60 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
61
62 /**
63  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
64  */
65 @RunWith(SpringJUnit4ClassRunner.class)
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
67 public class CldsServiceItCase {
68
69     @Autowired
70     private CldsService cldsService;
71     private String bpmnText;
72     private String imageText;
73     private String bpmnPropText;
74     @Autowired
75     private CldsDao cldsDao;
76
77     /**
78      * Setup the variable before the tests execution.
79      * 
80      * @throws IOException
81      *             In case of issues when opening the files
82      */
83     @Before
84     public void setupBefore() throws IOException {
85         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
86         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
87         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
88     }
89
90     @Test
91     public void testCldsInfoNotAuthorized() {
92         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93         Principal principal = Mockito.mock(Principal.class);
94         Mockito.when(principal.getName()).thenReturn("admin");
95         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
96         cldsService.setSecurityContext(securityContext);
97         CldsInfo cldsInfo = cldsService.getCldsInfo();
98         assertFalse(cldsInfo.isPermissionReadCl());
99         assertFalse(cldsInfo.isPermissionReadTemplate());
100         assertFalse(cldsInfo.isPermissionUpdateCl());
101         assertFalse(cldsInfo.isPermissionUpdateTemplate());
102     }
103
104     @Test
105     public void testCldsInfoAuthorized() throws Exception {
106         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
107         Principal principal = Mockito.mock(Principal.class);
108         Mockito.when(principal.getName()).thenReturn("admin");
109         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
110         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
111         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
112         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
113         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
114         cldsService.setSecurityContext(securityContext);
115         CldsInfo cldsInfo = cldsService.getCldsInfo();
116         assertTrue(cldsInfo.isPermissionReadCl());
117         assertTrue(cldsInfo.isPermissionReadTemplate());
118         assertTrue(cldsInfo.isPermissionUpdateCl());
119         assertTrue(cldsInfo.isPermissionUpdateTemplate());
120         Properties prop = new Properties();
121         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
122         prop.load(in);
123         in.close();
124         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
125         assertEquals(cldsInfo.getUserName(), "admin");
126     }
127
128     @Test
129     public void testGetHealthCheck() {
130         CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck();
131         assertNotNull(cldsHealthCheck);
132         assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
133         assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
134         assertEquals("OK", cldsHealthCheck.getDescription());
135     }
136
137     @Test
138     public void testPutModel() {
139         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
140         Principal principal = Mockito.mock(Principal.class);
141         Mockito.when(principal.getName()).thenReturn("admin");
142         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
143         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
144         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
145         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
146         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
147         cldsService.setSecurityContext(securityContext);
148         // Add the template first
149         CldsTemplate newTemplate = new CldsTemplate();
150         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
151         newTemplate.setName(randomNameTemplate);
152         newTemplate.setBpmnText(bpmnText);
153         newTemplate.setImageText(imageText);
154         // Save the template in DB
155         cldsDao.setTemplate(newTemplate, "user");
156         // Test if it's well there
157         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
158         assertEquals(bpmnText, newTemplateRead.getBpmnText());
159         assertEquals(imageText, newTemplateRead.getImageText());
160         // Save the model
161         CldsModel newModel = new CldsModel();
162         newModel.setName(randomNameTemplate);
163         newModel.setBpmnText(bpmnText);
164         newModel.setImageText(imageText);
165         newModel.setPropText(bpmnPropText);
166         newModel.setControlNamePrefix("ClosedLoop-");
167         newModel.setTemplateName("test-template");
168         newModel.setTemplateId(newTemplate.getId());
169         newModel.setDocText(newTemplate.getPropText());
170         // Test the PutModel method
171         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
172         cldsService.putModel(randomNameModel, newModel);
173         // Verify whether it has been added properly or not
174         assertNotNull(cldsDao.getModel(randomNameModel));
175     }
176
177     @Test
178     public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
179         String result = cldsService.getSdcServices();
180         JSONAssert.assertEquals(
181                 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
182                 true);
183     }
184
185     @Test
186     public void testGetSdcPropertiesByServiceUuidForRefresh()
187             throws GeneralSecurityException, DecoderException, JSONException, IOException {
188         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
189         Principal principal = Mockito.mock(Principal.class);
190         Mockito.when(principal.getName()).thenReturn("admin");
191         Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
192         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
193         Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
194         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
195         Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
196         Mockito.when(securityContext.isUserInRole("permission-type-filter-vf|dev|*")).thenReturn(true);
197         cldsService.setSecurityContext(securityContext);
198         // Test basic functionalities
199         String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
200                 false);
201         JSONAssert.assertEquals(
202                 ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
203                 result, true);
204         // Now test the Cache effect
205         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
206         // Should not be there, so should be null
207         assertNull(cldsServiceDataCache);
208         cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
209         // Should be there now, so should NOT be null
210         cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
211         assertNotNull(cldsServiceDataCache);
212         cldsDao.clearServiceCache();
213     }
214 }