Merge "Remove MsoLogger from 'mso-openstack-adapters'"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / mock / FileUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.bpmn.mock;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * 
32  * File utility class.<br/>
33  * <p>
34  * </p>
35  * 
36  * @author
37  * @version     ONAP  Sep 15, 2017
38  */
39 public class FileUtil {
40
41     private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
42     
43         /**
44          * Read the specified resource file and return the contents as a String.
45          * 
46          * @param fileName Name of the resource file
47          * @return the contents of the resource file as a String
48          * @throws IOException if there is a problem reading the file
49          */
50         public static String readResourceFile(String fileName) {
51                 InputStream stream;
52                 try {
53                         stream = getResourceAsStream(fileName);
54                         byte[] bytes;
55                         bytes = new byte[stream.available()];
56                         if(stream.read(bytes) > 0) {
57                                 stream.close();
58                                 return new String(bytes);
59                         } else {
60                                 stream.close();
61                                 return "";
62                         }
63                 } catch (IOException e) {
64                     logger.debug("Exception:", e);
65                         return "";
66                 }
67         }
68         
69         /**
70          * Get an InputStream for the resource specified.
71          * 
72          * @param resourceName Name of resource for which to get InputStream.
73          * @return an InputStream for the resource specified.
74          * @throws IOException If we can't get the InputStream for whatever reason.
75          */
76         private static InputStream getResourceAsStream(String resourceName) throws IOException {
77                 InputStream stream =
78                                 FileUtil.class.getClassLoader().getResourceAsStream(resourceName);
79                 if (stream == null) {
80                         throw new IOException("Can't access resource '" + resourceName + "'");
81                 }
82                 return stream;
83         }               
84 }