Add tests
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsToscaServiceItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 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.assertNotNull;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
35
36 import javax.servlet.http.HttpServletRequest;
37
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Matchers;
42 import org.mockito.Mockito;
43 import org.onap.clamp.clds.model.CldsToscaModel;
44 import org.onap.clamp.clds.service.CldsToscaService;
45 import org.onap.clamp.clds.util.LoggingUtils;
46 import org.onap.clamp.clds.util.ResourceFileUtil;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
52 import org.springframework.security.core.Authentication;
53 import org.springframework.security.core.GrantedAuthority;
54 import org.springframework.security.core.authority.SimpleGrantedAuthority;
55 import org.springframework.security.core.context.SecurityContext;
56 import org.springframework.security.core.userdetails.User;
57 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
58
59 /**
60  * Test CLDS Tosca Service APIs.
61  */
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 public class CldsToscaServiceItCase {
65
66     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsToscaServiceItCase.class);
67     @Autowired
68     private CldsToscaService cldsToscaService;
69     private String toscaModelYaml;
70     private Authentication authentication;
71     private CldsToscaModel cldsToscaModel;
72     private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
73     private LoggingUtils util;
74
75     /**
76      * Setup the variable before the tests execution.
77      *
78      * @throws IOException
79      *         In case of issues when opening the files
80      */
81     @Before
82     public void setupBefore() throws IOException {
83         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
84         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
85         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
86         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
87         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
88         authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|read"));
89         authList.add(new SimpleGrantedAuthority("permission-type-tosca|dev|update"));
90         authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
91
92         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
94
95         util = Mockito.mock(LoggingUtils.class);
96         Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
97         cldsToscaService.setLoggingUtil(util);
98
99         cldsToscaService.setSecurityContext(securityContext);
100
101         toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml");
102
103         cldsToscaModel = new CldsToscaModel();
104         cldsToscaModel.setToscaModelName("tca-policy-test");
105         cldsToscaModel.setToscaModelYaml(toscaModelYaml);
106         cldsToscaModel.setUserId("admin");
107         cldsToscaModel.setPolicyType("tca");
108         cldsToscaService.parseToscaModelAndSave("tca-policy-test", cldsToscaModel);
109         logger.info("Initial Tosca Model uploaded in DB:" + cldsToscaModel);
110     }
111
112     @Test
113     public void testParseToscaModelAndSave() throws Exception {
114         ResponseEntity responseEntity = cldsToscaService.parseToscaModelAndSave("tca-policy-test", cldsToscaModel);
115         CldsToscaModel savedModel = (CldsToscaModel) responseEntity.getBody();
116         assertNotNull(savedModel);
117         logger.info("Parsed Tosca Model is:" + savedModel);
118         assertEquals("tca-policy-test", savedModel.getToscaModelName());
119     }
120
121     @Test
122     public void testGetToscaModel() throws Exception {
123         ResponseEntity<CldsToscaModel> responseEntity = cldsToscaService.getToscaModel("tca-policy-test");
124         CldsToscaModel savedModel = responseEntity.getBody();
125         assertNotNull(savedModel);
126         assertEquals("tca-policy-test", savedModel.getToscaModelName());
127     }
128
129     @Test
130     public void testGetToscaModelsByPolicyType() throws Exception {
131         ResponseEntity<CldsToscaModel> responseEntity = cldsToscaService.getToscaModelsByPolicyType("tca");
132         CldsToscaModel savedModel = responseEntity.getBody();
133         assertNotNull(savedModel);
134         assertEquals("tca-policy-test", savedModel.getToscaModelName());
135         assertEquals("tca", savedModel.getPolicyType());
136     }
137
138 }