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.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;
60 * Test CLDS Tosca Service APIs.
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
64 public class CldsToscaServiceItCase {
66 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsToscaServiceItCase.class);
68 private CldsToscaService cldsToscaService;
69 private String toscaModelYaml;
70 private Authentication authentication;
71 private CldsToscaModel cldsToscaModel;
72 private List<GrantedAuthority> authList = new LinkedList<>();
73 private LoggingUtils util;
76 * Setup the variable before the tests execution.
79 * In case of issues when opening the files
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);
92 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
93 Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
95 util = Mockito.mock(LoggingUtils.class);
96 Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
97 cldsToscaService.setLoggingUtil(util);
99 cldsToscaService.setSecurityContext(securityContext);
101 toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml");
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);
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());
122 public void testGetToscaModel() throws Exception {
123 CldsToscaModel savedModel = cldsToscaService.getToscaModel("tca-policy-test");
124 assertNotNull(savedModel);
125 assertEquals("tca-policy-test", savedModel.getToscaModelName());
129 public void testGetToscaModelsByPolicyType() throws Exception {
130 CldsToscaModel savedModel = cldsToscaService.getToscaModelsByPolicyType("tca");
131 assertNotNull(savedModel);
132 assertEquals("tca-policy-test", savedModel.getToscaModelName());
133 assertEquals("tca", savedModel.getPolicyType());