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