2e3b906a5da06d4c4da82bf5135dafd588589dbb
[sdc.git] / common-be-tests-utils / src / main / java / org / openecomp / sdc / be / test / util / TestResourcesHandler.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.test.util;
21
22 import org.apache.commons.io.IOUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29 import java.nio.file.Path;
30
31 import static org.junit.Assert.fail;
32
33 /**
34  * Util class for handling test resources.
35  */
36 public class TestResourcesHandler {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesHandler.class);
39
40     private TestResourcesHandler() {
41
42     }
43
44     /**
45      * Gets the input stream of a resource file
46      *
47      * @param resourcePath      The resource file path
48      * @return
49      *  The resource input stream
50      */
51     public static InputStream getResourceAsStream(final String resourcePath) {
52         return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
53     }
54
55     public static InputStream getResourceAsStream(final Path resourcePath) {
56         return getResourceAsStream(resourcePath.toString());
57     }
58
59     /**
60      * Reads a file and coverts it to a byte array.
61      *
62      * @param resourcePath      The resource file path
63      * @return
64      *  The resource file byte array
65      * @throws IOException
66      *  When the file was not found or the input stream could not be opened
67      */
68     public static byte[] getResourceAsByteArray(final String resourcePath) throws IOException {
69         try(final InputStream inputStream = getResourceAsStream(resourcePath)) {
70             if (inputStream == null) {
71                 throw new IOException(String.format("Could not find the resource on path \"%s\"", resourcePath));
72             }
73             return IOUtils.toByteArray(inputStream);
74         } catch (final IOException ex) {
75             throw new IOException(String.format("Could not open the input stream for resource on path \"%s\"", resourcePath), ex);
76         }
77     }
78
79     public static byte[] getResourceAsByteArray(final Path resourcePath) throws IOException {
80         return getResourceAsByteArray(resourcePath.toString());
81     }
82
83     /**
84      * Reads a file in the given path.
85      * The method forces an assertion fail if the resource could not be loaded.
86      * @param resourcePath      The resource file path
87      * @return
88      *  The resource file byte array
89      */
90     public static byte[] getResourceBytesOrFail(final String resourcePath) {
91         try {
92             return getResourceAsByteArray(resourcePath);
93         } catch (final IOException e) {
94             final String errorMsg = String.format("Could not load resource '%s'", resourcePath);
95             LOGGER.error(errorMsg, e);
96             fail(errorMsg);
97         }
98
99         return null;
100     }
101
102     public static byte[] getResourceBytesOrFail(final Path resourcePath) {
103         return getResourceBytesOrFail(resourcePath.toString());
104     }
105
106     /**
107      * Gets the input stream of a resource file
108      *
109      * @param resourcePath      The resource file path
110      * @return
111      *  The resource input stream
112      */
113     public static URL getFileUrl(final String resourcePath) {
114         return Thread.currentThread().getContextClassLoader().getResource(resourcePath);
115     }
116 }