130f1efcf78c84d605b27ad15ee49b9cc862f9ac
[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  *  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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.pap.main.parameters;
25
26 import java.io.File;
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;
36
37 /**
38  * Class to hold/create all parameters for test cases.
39  *
40  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
41  */
42 public class CommonTestData {
43     public static final String PAP_GROUP_NAME = "PapGroup";
44
45     private static final Coder coder = new StandardCoder();
46
47     public static int dbNum = 0;
48
49     public static void newDb() {
50         ++dbNum;
51     }
52
53     /**
54      * Gets the standard PAP parameters.
55      *
56      * @param port port to be inserted into the parameters
57      * @return the standard PAP parameters
58      */
59     public PapParameterGroup getPapParameterGroup(int port) {
60         try {
61             return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);
62
63         } catch (CoderException e) {
64             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
65         }
66     }
67
68     /**
69      * Gets the standard PAP parameters, as a String.
70      *
71      * @param port port to be inserted into the parameters
72      * @return the standard PAP parameters
73      */
74     public String getPapParameterGroupAsString(int port) {
75
76         try {
77             File file = new File(getParamFile());
78             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
79
80             json = json.replace("${port}", String.valueOf(port));
81             json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
82
83             return json;
84
85         } catch (IOException e) {
86             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
87         }
88     }
89
90     /**
91      * Gets the postgres PAP parameters, as a String.
92      *
93      * @param port port to be inserted into the parameters
94      * @return the postgres PAP parameters
95      */
96     public String getPapPostgresParameterGroupAsString(int port) {
97
98         try {
99             String json = new String(Files.readAllBytes(Paths.get(
100                     "src/test/resources/parameters/PapConfigParameters_Postgres.json")));
101
102             json = json.replace("${port}", String.valueOf(port));
103             json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
104
105             return json;
106
107         } catch (IOException e) {
108             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
109         }
110     }
111
112     /**
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.
115      *
116      * @return the parameter file name
117      */
118     private String getParamFile() {
119         String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";
120
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";
125                 break;
126             }
127         }
128         return paramFile;
129     }
130
131     /**
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
136      */
137     public String nullifyField(String json, String field) {
138         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
139     }
140 }