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.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.pap.main.PolicyPapRuntimeException;
35 import org.onap.policy.pap.main.rest.e2e.End2EndBase;
38 * Class to hold/create all parameters for test cases.
40 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42 public class CommonTestData {
43 public static final String PAP_GROUP_NAME = "PapGroup";
45 private static final Coder coder = new StandardCoder();
47 public static int dbNum = 0;
49 public static void newDb() {
54 * Gets the standard PAP parameters.
56 * @param port port to be inserted into the parameters
57 * @return the standard PAP parameters
59 public PapParameterGroup getPapParameterGroup(int port) {
61 return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);
63 } catch (CoderException e) {
64 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
69 * Gets the standard PAP parameters, as a String.
71 * @param port port to be inserted into the parameters
72 * @return the standard PAP parameters
74 public String getPapParameterGroupAsString(int port) {
77 File file = new File(getParamFile());
78 String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
80 json = json.replace("${port}", String.valueOf(port));
81 json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
85 } catch (IOException e) {
86 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
91 * Gets the postgres PAP parameters, as a String.
93 * @param port port to be inserted into the parameters
94 * @return the postgres PAP parameters
96 public String getPapPostgresParameterGroupAsString(int port) {
99 String json = new String(Files.readAllBytes(Paths.get(
100 "src/test/resources/parameters/PapConfigParameters_Postgres.json")));
102 json = json.replace("${port}", String.valueOf(port));
103 json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
107 } catch (IOException e) {
108 throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
113 * Gets the full path to the parameter file, which may vary depending on whether or
114 * not this is an end-to-end test.
116 * @return the parameter file name
118 private String getParamFile() {
119 String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";
121 for (StackTraceElement stack : Thread.currentThread().getStackTrace()) {
122 String classnm = stack.getClassName();
123 if (End2EndBase.class.getName().equals(classnm)) {
124 paramFile = "src/test/resources/e2e/PapConfigParameters.json";
132 * Nulls out a field within a JSON string.
133 * @param json JSON string
134 * @param field field to be nulled out
135 * @return a new JSON string with the field nulled out
137 public String nullifyField(String json, String field) {
138 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");