2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
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=========================================================
22 package org.onap.policy.pap.main.rest.e2e;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.util.List;
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpGroups;
41 import org.onap.policy.models.pdp.concepts.PdpStatistics;
42 import org.onap.policy.models.provider.PolicyModelsProvider;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
45 import org.onap.policy.pap.main.PolicyPapRuntimeException;
46 import org.onap.policy.pap.main.parameters.PapParameterGroup;
47 import org.onap.policy.pap.main.rest.CommonPapRestServer;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.yaml.snakeyaml.Yaml;
52 public class End2EndBase extends CommonPapRestServer {
53 private static final Logger logger = LoggerFactory.getLogger(End2EndBase.class);
55 private static final Coder coder = new StandardCoder();
56 private static final Yaml yaml = new Yaml();
59 * DB connection. This is kept open until {@link #stop()} is invoked so that the in-memory DB is not destroyed.
61 private static PolicyModelsProvider dbConn;
64 * DAO provider factory.
66 private static PolicyModelsProviderFactoryWrapper daoFactory;
69 * Context - should be initialized by setUp() method.
71 protected End2EndContext context = null;
75 * Starts Main and connects to the DB.
77 * @throws Exception if an error occurs
80 public static void setUpBeforeClass() throws Exception {
81 setUpBeforeClass(true);
85 * Starts Main, if specified, and connects to the DB.
87 * @param shouldStart {@code true} if Main should be started, {@code false} otherwise
88 * @throws Exception if an error occurs
90 public static void setUpBeforeClass(final boolean shouldStart) throws Exception {
91 CommonPapRestServer.setUpBeforeClass(shouldStart);
93 final PapParameterGroup params = new StandardCoder().decode(new File(CONFIG_FILE), PapParameterGroup.class);
94 daoFactory = new PolicyModelsProviderFactoryWrapper(params.getDatabaseProviderParameters());
95 dbConn = daoFactory.create();
102 public static void tearDownAfterClass() {
105 } catch (final PfModelException e) {
106 logger.warn("failed to close the DB", e);
111 } catch (final Exception e) {
112 logger.warn("failed to close DAO factory", e);
115 CommonPapRestServer.teardownAfterClass();
123 public void tearDown() {
124 if (context != null) {
127 } catch (final Exception e) {
128 logger.warn("failed to stop end-to-end context", e);
137 * Adds Tosca Policy Types to the DB.
139 * @param yamlFile name of the YAML file specifying the data to be loaded
140 * @throws PfModelException if a DAO error occurs
142 public static void addToscaPolicyTypes(final String yamlFile) throws PfModelException {
143 final ToscaServiceTemplate serviceTemplate = loadYamlFile(yamlFile, ToscaServiceTemplate.class);
144 dbConn.createPolicyTypes(serviceTemplate);
148 * Adds Tosca Policies to the DB.
150 * @param yamlFile name of the YAML file specifying the data to be loaded
151 * @throws PfModelException if a DAO error occurs
153 public static void addToscaPolicies(final String yamlFile) throws PfModelException {
154 final ToscaServiceTemplate serviceTemplate = loadYamlFile(yamlFile, ToscaServiceTemplate.class);
155 dbConn.createPolicies(serviceTemplate);
159 * Adds PDP groups to the DB.
161 * @param jsonFile name of the JSON file specifying the data to be loaded
162 * @throws PfModelException if a DAO error occurs
164 public static void addGroups(final String jsonFile) throws PfModelException {
165 final PdpGroups groups = loadJsonFile(jsonFile, PdpGroups.class);
167 final ValidationResult result = groups.validatePapRest();
168 if (!result.isValid()) {
169 throw new PolicyPapRuntimeException("cannot init DB groups from " + jsonFile + ":\n" + result.getResult());
172 dbConn.createPdpGroups(groups.getGroups());
176 * Fetch PDP groups from the DB.
178 * @param name name of the pdpGroup
179 * @throws PfModelException if a DAO error occurs
181 public static List<PdpGroup> fetchGroups(final String name) throws PfModelException {
182 return dbConn.getPdpGroups(name);
186 * Fetch PDP statistics from the DB.
188 * @param instanceId name of the pdpStatistics
189 * @param groupName name of the pdpGroup
190 * @param subGroupName name of the pdpSubGroup
191 * @throws PfModelException if a DAO error occurs
193 public static List<PdpStatistics> fetchPdpStatistics(final String instanceId, final String groupName,
194 final String subGroupName) throws PfModelException {
195 return dbConn.getFilteredPdpStatistics(instanceId, groupName, subGroupName, null, null, null, 0);
199 * Loads an object from a YAML file.
201 * @param fileName name of the file from which to load
202 * @param clazz the class of the object to be loaded
203 * @return the object that was loaded from the file
205 protected static <T> T loadYamlFile(final String fileName, final Class<T> clazz) {
206 final File propFile = new File(ResourceUtils.getFilePath4Resource("e2e/" + fileName));
208 try (FileInputStream input = new FileInputStream(propFile)) {
209 final Object yamlObject = yaml.load(input);
210 final String json = coder.encode(yamlObject);
211 final T result = coder.decode(json, clazz);
213 if (result == null) {
214 throw new PolicyPapRuntimeException("cannot decode " + clazz.getSimpleName() + " from " + fileName);
219 } catch (final FileNotFoundException e) {
220 throw new PolicyPapRuntimeException("cannot find " + fileName, e);
222 } catch (IOException | CoderException e) {
223 throw new PolicyPapRuntimeException("cannot decode " + fileName, e);
228 * Loads an object from a JSON file.
230 * @param fileName name of the file from which to load
231 * @param clazz the class of the object to be loaded
232 * @return the object that was loaded from the file
234 protected static <T> T loadJsonFile(final String fileName, final Class<T> clazz) {
235 final String fileName2 = (fileName.startsWith("src/") ? fileName : "e2e/" + fileName);
236 final File propFile = new File(ResourceUtils.getFilePath4Resource(fileName2));
238 final T result = coder.decode(propFile, clazz);
240 if (result == null) {
241 throw new PolicyPapRuntimeException("cannot decode " + clazz.getSimpleName() + " from " + fileName);
246 } catch (final CoderException e) {
247 throw new RuntimeException(e);