aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java
blob: 14000e605cc9f86525a994c49d4fb5f9be1f4dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.parameters;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.pap.main.PolicyPapRuntimeException;
import org.onap.policy.pap.main.rest.e2e.End2EndBase;

/**
 * Class to hold/create all parameters for test cases.
 *
 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
 */
public class CommonTestData {
    public static final String PAP_GROUP_NAME = "PapGroup";

    private static final Coder coder = new StandardCoder();

    private static int dbNum = 0;

    public static void newDb() {
        ++dbNum;
    }

    /**
     * Gets the standard PAP parameters.
     *
     * @param port port to be inserted into the parameters
     * @return the standard PAP parameters
     */
    public PapParameterGroup getPapParameterGroup(int port) {
        try {
            return coder.decode(getPapParameterGroupAsString(port), PapParameterGroup.class);

        } catch (CoderException e) {
            throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
        }
    }

    /**
     * Gets the standard PAP parameters, as a String.
     *
     * @param port port to be inserted into the parameters
     * @return the standard PAP parameters
     */
    public String getPapParameterGroupAsString(int port) {

        try {
            File file = new File(getParamFile());
            String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

            json = json.replace("${port}", String.valueOf(port));
            json = json.replace("${dbName}", "jdbc:h2:mem:testdb" + dbNum);

            return json;

        } catch (IOException e) {
            throw new PolicyPapRuntimeException("cannot read PAP parameters", e);
        }
    }

    /**
     * Gets the full path to the parameter file, which may vary depending on whether or
     * not this is an end-to-end test.
     *
     * @return the parameter file name
     */
    private String getParamFile() {
        String paramFile = "src/test/resources/parameters/PapConfigParametersStd.json";

        for (StackTraceElement stack : Thread.currentThread().getStackTrace()) {
            String classnm = stack.getClassName();
            if (End2EndBase.class.getName().equals(classnm)) {
                paramFile = "src/test/resources/e2e/PapConfigParameters.json";
                break;
            }
        }
        return paramFile;
    }

    /**
     * Nulls out a field within a JSON string.
     * @param json JSON string
     * @param field field to be nulled out
     * @return a new JSON string with the field nulled out
     */
    public String nullifyField(String json, String field) {
        return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
    }
}