Allow topics to be registered with lower case for kafka compability
[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.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;
35
36 /**
37  * Class to hold/create all parameters for test cases.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40  */
41 public class CommonTestData {
42     public static final String PAP_GROUP_NAME = "PapGroup";
43
44     private static final Coder coder = new StandardCoder();
45
46     public static int dbNum = 0;
47
48     public static void newDb() {
49         ++dbNum;
50     }
51
52     /**
53      * Gets the standard PAP parameters.
54      *
55      * @param port port to be inserted into the parameters
56      * @return the standard PAP parameters
57      */
58     public PapParameterGroup getPapParameterGroup(int port) {
59         try {
60             return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);
61
62         } catch (CoderException e) {
63             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
64         }
65     }
66
67     /**
68      * Gets the standard PAP parameters, as a String.
69      *
70      * @param port port to be inserted into the parameters
71      * @return the standard PAP parameters
72      */
73     public String getPapParameterGroupAsString(int port) {
74
75         try {
76             File file = new File(getParamFile());
77             String json = Files.readString(file.toPath());
78
79             json = json.replace("${port}", String.valueOf(port));
80             json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
81
82             return json;
83
84         } catch (IOException e) {
85             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
86         }
87     }
88
89     /**
90      * Gets the postgres PAP parameters, as a String.
91      *
92      * @param port port to be inserted into the parameters
93      * @return the postgres PAP parameters
94      */
95     public String getPapPostgresParameterGroupAsString(int port) {
96
97         try {
98             String json = new String(Files.readAllBytes(Paths.get(
99                     "src/test/resources/parameters/PapConfigParameters_Postgres.json")));
100
101             json = json.replace("${port}", String.valueOf(port));
102             json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);
103
104             return json;
105
106         } catch (IOException e) {
107             throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
108         }
109     }
110
111     /**
112      * Gets the full path to the parameter file, which may vary depending on whether
113      * this is an end-to-end test.
114      *
115      * @return the parameter file name
116      */
117     private String getParamFile() {
118         String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";
119
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";
124                 break;
125             }
126         }
127         return paramFile;
128     }
129
130     /**
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
135      */
136     public String nullifyField(String json, String field) {
137         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
138     }
139 }