34559f5386e5f6efd7a8871e098b42ddd05b0a48
[sdc.git] /
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.core.util;
21
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import org.apache.commons.io.IOUtils;
27 import org.openecomp.sdc.logging.api.Logger;
28 import org.openecomp.sdc.logging.api.LoggerFactory;
29
30 /**
31  * Test resources utility class.
32  */
33 public class TestResourcesUtil {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesUtil.class);
36
37     private TestResourcesUtil() {
38
39     }
40
41     /**
42      * Reads a file and coverts it to a byte array.
43      *
44      * @param resourcePath      The resource file path
45      * @return
46      *  The resource file byte array
47      * @throws IOException
48      *  When the file was not found or the input stream could not be opened
49      */
50     public static byte[] getFileResource(final String resourcePath) throws IOException {
51         try(final InputStream inputStream = ClassLoader.class.getResourceAsStream(resourcePath)) {
52             if (inputStream == null) {
53                 throw new IOException(String.format("Could not find the resource on path \"%s\"", resourcePath));
54             }
55             return IOUtils.toByteArray(inputStream);
56         } catch (final IOException ex) {
57             throw new IOException(String.format("Could not open the input stream for resource on path \"%s\"", resourcePath), ex);
58         }
59     }
60
61     /**
62      * Reads a file in the given path.
63      * The method forces an assertion fail if the resource could not be loaded.
64      * @param resourcePath      The resource file path
65      * @return
66      *  The resource file byte array
67      */
68     public static byte[] getResourceBytesOrFail(final String resourcePath) {
69         try {
70             return getFileResource(resourcePath);
71         } catch (final IOException e) {
72             final String errorMsg = String.format("Could not load resource '%s'", resourcePath);
73             LOGGER.error(errorMsg, e);
74             fail(errorMsg);
75         }
76
77         return null;
78     }
79
80 }