Fix api due to sonar changes in common
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / parameters / CommonTestData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.api.main.parameters;
25
26 import java.io.File;
27 import java.io.IOException;
28 import org.onap.policy.common.utils.resources.TextFileUtils;
29 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
30
31 /**
32  * Class to hold/create all parameters for test cases.
33  *
34  */
35 public class CommonTestData {
36
37     public static final String API_GROUP_NAME = "ApiGroup";
38
39     /**
40      * Server port, as it appears within the config files.
41      */
42     private static final String REST_SERVER_PORT = "6969";
43
44     private static final String REST_SERVER_PASSWORD = "zb!XztG34";
45     private static final String REST_SERVER_USER = "healthcheck";
46     private static final String REST_SERVER_HOST = "0.0.0.0";
47     private static final boolean REST_SERVER_HTTPS = false;
48     private static final boolean REST_SERVER_AAF = false;
49
50     private static final String PROVIDER_GROUP_NAME = "PolicyProviderParameterGroup";
51     private static final String PROVIDER_IMPL = "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl";
52     private static final String DATABASE_DRIVER = "org.h2.Driver";
53     private static final String DATABASE_URL = "jdbc:h2:mem:testdb";
54     private static final String DATABASE_USER = "policy";
55     private static final String DATABASE_PASSWORD = "P01icY";
56     private static final String PERSISTENCE_UNIT = "ToscaConceptTest";
57
58     /**
59      * Returns an instance of RestServerParameters for test cases.
60      *
61      * @param isEmpty boolean value to represent that object created should be empty or not
62      * @param port server port
63      * @return the RestServerParameters object
64      */
65     public RestServerParameters getRestServerParameters(final boolean isEmpty, int port) {
66         final RestServerParameters restServerParameters;
67         if (!isEmpty) {
68             restServerParameters = new RestServerParameters(REST_SERVER_HOST, port, REST_SERVER_USER,
69                     REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF);
70         } else {
71             restServerParameters = new RestServerParameters(null, 0, null, null, false, false);
72         }
73         return restServerParameters;
74     }
75
76     /**
77      * Returns an instance of PolicyModelsProviderParameters for test cases.
78      *
79      * @param isEmpty boolean value to represent that object created should be empty or not
80      * @return the PolicyModelsProviderParameters object
81      */
82     public PolicyModelsProviderParameters getDatabaseProviderParameters(final boolean isEmpty) {
83         final PolicyModelsProviderParameters databaseProviderParameters;
84         if (!isEmpty) {
85             databaseProviderParameters = new PolicyModelsProviderParameters();
86             databaseProviderParameters.setName(PROVIDER_GROUP_NAME);
87             databaseProviderParameters.setImplementation(PROVIDER_IMPL);
88             databaseProviderParameters.setDatabaseDriver(DATABASE_DRIVER);
89             databaseProviderParameters.setDatabaseUrl(DATABASE_URL);
90             databaseProviderParameters.setDatabaseUser(DATABASE_USER);
91             databaseProviderParameters.setDatabasePassword(DATABASE_PASSWORD);
92             databaseProviderParameters.setPersistenceUnit(PERSISTENCE_UNIT);
93         } else {
94             databaseProviderParameters = new PolicyModelsProviderParameters();
95         }
96         return databaseProviderParameters;
97     }
98
99     /**
100      * Copies a source file to a target file, replacing occurrances of
101      * {@link #REST_SERVER_PORT} with the given port number.
102      *
103      * @param source source file name
104      * @param target target file name
105      * @param port port to be substituted
106      * @throws IOException if an error occurs
107      */
108     public void makeParameters(String source, String target, int port) throws IOException {
109         String text = TextFileUtils.getTextFileAsString(source);
110         text = text.replace(REST_SERVER_PORT, String.valueOf(port));
111
112         File file = new File(target);
113         file.deleteOnExit();
114         TextFileUtils.putStringAsFile(text, file);
115     }
116 }