Flesh out DMaaP simulator
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / sim / dmaap / parameters / CommonTestData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.sim.dmaap.parameters;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
31 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
32
33 /**
34  * Class to hold/create all parameters for test cases.
35  */
36 public class CommonTestData {
37     public static final String SIM_GROUP_NAME = "DMaapSim";
38
39     private static final Coder coder = new StandardCoder();
40
41     /**
42      * Gets the standard simulator parameters.
43      *
44      * @param port port to be inserted into the parameters
45      * @return the standard simulator parameters
46      */
47     public DmaapSimParameterGroup getParameterGroup(int port) {
48         try {
49             return coder.decode(getParameterGroupAsString(port), DmaapSimParameterGroup.class);
50
51         } catch (CoderException e) {
52             throw new DmaapSimRuntimeException("cannot read simulator parameters", e);
53         }
54     }
55
56     /**
57      * Gets the standard simulator parameters, as a String.
58      *
59      * @param port port to be inserted into the parameters
60      * @return the standard simulator parameters
61      */
62     public String getParameterGroupAsString(int port) {
63
64         try {
65             File file = new File("src/test/resources/parameters/NormalParameters.json");
66             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
67
68             json = json.replace("6845", String.valueOf(port));
69
70             return json;
71
72         } catch (IOException e) {
73             throw new DmaapSimRuntimeException("cannot read simulator parameters", e);
74         }
75     }
76
77     /**
78      * Nulls out a field within a JSON string. It does it by adding a field with the same
79      * name, having a null value, and then prefixing the original field name with "Xxx",
80      * thus causing the original field and value to be ignored.
81      *
82      * @param json JSON string
83      * @param field field to be nulled out
84      * @return a new JSON string with the field nulled out
85      */
86     public String nullifyField(String json, String field) {
87         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
88     }
89 }