Implement PNFD Model driven conversion
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / util / TestResourcesUtil.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.core.util;
21
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import org.apache.commons.io.IOUtils;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30
31 /**
32  * Test resources utility class.
33  */
34 public class TestResourcesUtil {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesUtil.class);
37
38     private TestResourcesUtil() {
39
40     }
41
42     /**
43      * Reads a file and coverts it to a byte array.
44      *
45      * @param resourcePath      The resource file path
46      * @return
47      *  The resource file byte array
48      * @throws IOException
49      *  When the file was not found or the input stream could not be opened
50      */
51     public static byte[] getFileResourceBytes(final String resourcePath) throws IOException {
52         try(final InputStream inputStream = ClassLoader.class.getResourceAsStream(resourcePath)) {
53             if (inputStream == null) {
54                 throw new IOException(String.format("Could not find the resource on path \"%s\"", resourcePath));
55             }
56             return IOUtils.toByteArray(inputStream);
57         } catch (final IOException ex) {
58             throw new IOException(String.format("Could not open the input stream for resource on path \"%s\"", resourcePath), ex);
59         }
60     }
61
62     /**
63      * Reads a file in the given path.
64      * The method forces an assertion fail if the resource could not be loaded.
65      * @param resourcePath      The resource file path
66      * @return
67      *  The resource file byte array
68      */
69     public static byte[] getResourceBytesOrFail(final String resourcePath) {
70         try {
71             return getFileResourceBytes(resourcePath);
72         } catch (final IOException e) {
73             final String errorMsg = String.format("Could not load resource '%s'", resourcePath);
74             LOGGER.error(errorMsg, e);
75             fail(errorMsg);
76         }
77
78         return null;
79     }
80
81     /**
82      * Gets the input stream of a resource file
83      *
84      * @param resourcePath      The resource file path
85      * @return
86      *  The resource input stream
87      */
88     public static InputStream getFileResourceAsStream(final String resourcePath) {
89         return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
90     }
91
92     /**
93      * Gets the input stream of a resource file
94      *
95      * @param resourcePath      The resource file path
96      * @return
97      *  The resource input stream
98      */
99     public static URL getFileUrl(final String resourcePath) {
100         return Thread.currentThread().getContextClassLoader().getResource(resourcePath);
101     }
102
103 }