Fix api due to sonar changes in common
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / parameters / CommonTestData.java
index 50e484e..f6266de 100644 (file)
@@ -23,6 +23,9 @@
 
 package org.onap.policy.api.main.parameters;
 
+import java.io.File;
+import java.io.IOException;
+import org.onap.policy.common.utils.resources.TextFileUtils;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 
 /**
@@ -33,9 +36,13 @@ public class CommonTestData {
 
     public static final String API_GROUP_NAME = "ApiGroup";
 
+    /**
+     * Server port, as it appears within the config files.
+     */
+    private static final String REST_SERVER_PORT = "6969";
+
     private static final String REST_SERVER_PASSWORD = "zb!XztG34";
     private static final String REST_SERVER_USER = "healthcheck";
-    private static final int REST_SERVER_PORT = 6969;
     private static final String REST_SERVER_HOST = "0.0.0.0";
     private static final boolean REST_SERVER_HTTPS = false;
     private static final boolean REST_SERVER_AAF = false;
@@ -52,12 +59,13 @@ public class CommonTestData {
      * Returns an instance of RestServerParameters for test cases.
      *
      * @param isEmpty boolean value to represent that object created should be empty or not
+     * @param port server port
      * @return the RestServerParameters object
      */
-    public RestServerParameters getRestServerParameters(final boolean isEmpty) {
+    public RestServerParameters getRestServerParameters(final boolean isEmpty, int port) {
         final RestServerParameters restServerParameters;
         if (!isEmpty) {
-            restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER,
+            restServerParameters = new RestServerParameters(REST_SERVER_HOST, port, REST_SERVER_USER,
                     REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF);
         } else {
             restServerParameters = new RestServerParameters(null, 0, null, null, false, false);
@@ -87,4 +95,22 @@ public class CommonTestData {
         }
         return databaseProviderParameters;
     }
+
+    /**
+     * Copies a source file to a target file, replacing occurrances of
+     * {@link #REST_SERVER_PORT} with the given port number.
+     *
+     * @param source source file name
+     * @param target target file name
+     * @param port port to be substituted
+     * @throws IOException if an error occurs
+     */
+    public void makeParameters(String source, String target, int port) throws IOException {
+        String text = TextFileUtils.getTextFileAsString(source);
+        text = text.replace(REST_SERVER_PORT, String.valueOf(port));
+
+        File file = new File(target);
+        file.deleteOnExit();
+        TextFileUtils.putStringAsFile(text, file);
+    }
 }