98c33c761dbf61c07976aaed9d9b5e5a9834a127
[policy/drools-applications.git] / controlloop / m2 / guard / src / test / java / org / onap / policy / guard / SupportTextFileUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * guard
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * Modifications 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  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.guard;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import org.drools.core.util.IoUtils;
29
30 /**
31  * The Class TextFileUtils is class that provides useful functions for handling text files.
32  * Functions to read and wrtie text files to strings and strings are provided.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class SupportTextFileUtils {
37
38     private SupportTextFileUtils() {
39         // do nothing
40     }
41
42     /**
43      * Method to return the contents of a text file as a string.
44      *
45      * @param textFilePath The path to the file as a string
46      * @return A string containing the contents of the file
47      * @throws IOException on errors reading text from the file
48      */
49     public static String getTextFileAsString(final String textFilePath) {
50         return IoUtils.readFileAsString(new File(textFilePath));
51     }
52
53     /**
54      * Method to write contents of a string to a text file.
55      *
56      * @param outString The string to write
57      * @param textFile The file to write the string to
58      * @throws IOException on errors reading text from the file
59      */
60     public static void putStringAsFile(final String outString, final File textFile) throws IOException {
61         try (final FileOutputStream textFileOutputStream = new FileOutputStream(textFile)) {
62             textFileOutputStream.write(outString.getBytes(StandardCharsets.UTF_8));
63         }
64     }
65 }