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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.sim.dmaap.parameters;
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;
34 * Class to hold/create all parameters for test cases.
36 public class CommonTestData {
37 public static final String SIM_GROUP_NAME = "DMaapSim";
39 private static final Coder coder = new StandardCoder();
42 * Gets the standard simulator parameters.
44 * @param port port to be inserted into the parameters
45 * @return the standard simulator parameters
47 public DmaapSimParameterGroup getParameterGroup(int port) {
49 return coder.decode(getParameterGroupAsString(port), DmaapSimParameterGroup.class);
51 } catch (CoderException e) {
52 throw new DmaapSimRuntimeException("cannot read simulator parameters", e);
57 * Gets the standard simulator parameters, as a String.
59 * @param port port to be inserted into the parameters
60 * @return the standard simulator parameters
62 public String getParameterGroupAsString(int port) {
65 File file = new File("src/test/resources/parameters/NormalParameters.json");
66 String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
68 json = json.replace("6845", String.valueOf(port));
72 } catch (IOException e) {
73 throw new DmaapSimRuntimeException("cannot read simulator parameters", e);
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.
82 * @param json JSON string
83 * @param field field to be nulled out
84 * @return a new JSON string with the field nulled out
86 public String nullifyField(String json, String field) {
87 return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");