d72775399f54d5afe4c96a25a5c90b92f1c0cce1
[so.git] / adapters / mso-sdnc-adapter / src / test / java / org / onap / so / adapters / sdnc / FileUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.sdnc;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 /**
32  * file utility class
33  */
34 public class FileUtil {
35
36     private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
37
38     /**
39      * Read the specified resource file and return the contents as a String.
40      *
41      * @param fileName Name of the resource file
42      * @return the contents of the resource file as a String
43      * @throws IOException if there is a problem reading the file
44      */
45     public static String readResourceFile(String fileName) {
46         InputStream stream;
47         try {
48             stream = getResourceAsStream(fileName);
49             byte[] bytes;
50             bytes = new byte[stream.available()];
51             if(stream.read(bytes) > 0) {
52                 stream.close();
53                 return new String(bytes);
54             } else {
55                 stream.close();
56                 return "";
57             }
58         } catch (IOException e) {
59             logger.debug("Exception:", e);
60             return "";
61         }
62     }
63
64     /**
65      * Get an InputStream for the resource specified.
66      *
67      * @param resourceName Name of resource for which to get InputStream.
68      * @return an InputStream for the resource specified.
69      * @throws IOException If we can't get the InputStream for whatever reason.
70      */
71     private static InputStream getResourceAsStream(String resourceName) throws IOException {
72         InputStream stream =
73                 FileUtil.class.getClassLoader().getResourceAsStream(resourceName);
74         if (stream == null) {
75             throw new IOException("Can't access resource '" + resourceName + "'");
76         }
77         return stream;
78     }
79 }