34d72203cddff01ffc96bb2586c871d23c989e37
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / CommonTestData.java
1 /*-
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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.parameters;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import org.onap.policy.common.utils.coder.Coder;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.pap.main.PolicyPapRuntimeException;
33 import org.onap.policy.pap.main.rest.e2e.End2EndBase;
34
35 /**
36  * Class to hold/create all parameters for test cases.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class CommonTestData {
41     public static final String PAP_GROUP_NAME = "PapGroup";
42
43     private static final Coder coder = new StandardCoder();
44
45     public static int dbNum = 0;
46
47     public static void newDb() {
48         ++dbNum;
49     }
50
51     /**
52      * Gets the standard PAP parameters.
53      *
54      * @param port port to be inserted into the parameters
55      * @return the standard PAP parameters
56      */
57     public PapParameterGroup getPapParameterGroup(int port) {
58         try {
59             return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);
60
61         } catch (CoderException e) {
62             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
63         }
64     }
65
66     /**
67      * Gets the standard PAP parameters, as a String.
68      *
69      * @param port port to be inserted into the parameters
70      * @return the standard PAP parameters
71      */
72     public String getPapParameterGroupAsString(int port) {
73
74         try {
75             File file = new File(getParamFile());
76             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
77
78             json = json.replace("${port}", String.valueOf(port));
79             json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
80
81             return json;
82
83         } catch (IOException e) {
84             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
85         }
86     }
87
88     /**
89      * Gets the full path to the parameter file, which may vary depending on whether or
90      * not this is an end-to-end test.
91      *
92      * @return the parameter file name
93      */
94     private String getParamFile() {
95         String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";
96
97         for (StackTraceElement stack : Thread.currentThread().getStackTrace()) {
98             String classnm = stack.getClassName();
99             if (End2EndBase.class.getName().equals(classnm)) {
100                 paramFile = "src/test/resources/e2e/PapConfigParameters.json";
101                 break;
102             }
103         }
104         return paramFile;
105     }
106
107     /**
108      * Nulls out a field within a JSON string.
109      * @param json JSON string
110      * @param field field to be nulled out
111      * @return a new JSON string with the field nulled out
112      */
113     public String nullifyField(String json, String field) {
114         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
115     }
116 }