2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
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.dao.CldsDao;
44 import org.onap.clamp.clds.model.CldsToscaModel;
45 import org.onap.clamp.clds.service.CldsToscaService;
46 import org.onap.clamp.clds.util.LoggingUtils;
47 import org.onap.clamp.clds.util.ResourceFileUtil;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
53 import org.springframework.security.core.Authentication;
54 import org.springframework.security.core.GrantedAuthority;
55 import org.springframework.security.core.authority.SimpleGrantedAuthority;
56 import org.springframework.security.core.context.SecurityContext;
57 import org.springframework.security.core.userdetails.User;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
61 * Test CLDS Tosca Service APIs.
63 @RunWith(SpringJUnit4ClassRunner.class)
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 public class CldsToscaServiceItCase {
67 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsToscaServiceItCase.class);
69 private CldsToscaService cldsToscaService;
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;
79 * Setup the variable before the tests execution.
82 * In case of issues when opening the files
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);
95 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
96 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
98 util = Mockito.mock(LoggingUtils.class);
99 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
100 cldsToscaService.setLoggingUtil(util);
102 cldsToscaService.setSecurityContext(securityContext);
104 toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml");
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);
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());
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());
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());