2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * Modification Copyright 2022. 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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.pap.main.parameters;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import org.onap.policy.common.utils.coder.Coder;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.pap.main.PolicyPapRuntimeException;
34 import org.onap.policy.pap.main.rest.e2e.End2EndBase;
37 * Class to hold/create all parameters for test cases.
39 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
41 public class CommonTestData {
42 public static final String PAP_GROUP_NAME = "PapGroup";
44 private static final Coder coder = new StandardCoder();
46 public static int dbNum = 0;
48 public static void newDb() {
53 * Gets the standard PAP parameters.
55 * @param port port to be inserted into the parameters
56 * @return the standard PAP parameters
58 public PapParameterGroup getPapParameterGroup(int port) {
60 return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);
62 } catch (CoderException e) {
63 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
68 * Gets the standard PAP parameters, as a String.
70 * @param port port to be inserted into the parameters
71 * @return the standard PAP parameters
73 public String getPapParameterGroupAsString(int port) {
76 File file = new File(getParamFile());
77 String json = Files.readString(file.toPath());
79 json = json.replace("${port}", String.valueOf(port));
80 json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
84 } catch (IOException e) {
85 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
90 * Gets the postgres PAP parameters, as a String.
92 * @param port port to be inserted into the parameters
93 * @return the postgres PAP parameters
95 public String getPapPostgresParameterGroupAsString(int port) {
98 String json = new String(Files.readAllBytes(Paths.get(
99 "src/test/resources/parameters/PapConfigParameters_Postgres.json")));
101 json = json.replace("${port}", String.valueOf(port));
102 json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
106 } catch (IOException e) {
107 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
112 * Gets the full path to the parameter file, which may vary depending on whether
113 * this is an end-to-end test.
115 * @return the parameter file name
117 private String getParamFile() {
118 String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";
120 for (StackTraceElement stack : Thread.currentThread().getStackTrace()) {
121 String classnm = stack.getClassName();
122 if (End2EndBase.class.getName().equals(classnm)) {
123 paramFile = "src/test/resources/e2e/PapConfigParameters.json";
131 * Nulls out a field within a JSON string.
132 * @param json JSON string
133 * @param field field to be nulled out
134 * @return a new JSON string with the field nulled out
136 public String nullifyField(String json, String field) {
137 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");