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