f5d9cc41bbf4b5307189d81623d5468f0e168a77
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / util / TestUtil.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
18
19 import com.google.common.io.ByteStreams;
20
21 import java.io.InputStream;
22 import java.lang.reflect.Constructor;
23
24 public class TestUtil {
25     /**
26      * Due to sonar issue you have to create a private constructor for classes
27      * that only have static methods to prevent the creation of instances. This
28      * constructor can not be coveraged / tested since it is private.
29      *
30      * @param clazz
31      */
32     public static void coveragePrivateConstructorForClassesWithStaticMethodsOnly(Class<?> clazz) {
33         for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
34             if (constructor.getParameterTypes().length == 0) {
35                 constructor.setAccessible(true);
36                 try {
37                     constructor.newInstance();
38                 } catch (Exception e) {
39                     throw new IllegalStateException(e);
40                 }
41             } else {
42                 throw new IllegalStateException("This should only be used if the class has a singe private constructor");
43             }
44         }
45     }
46
47     public static byte[] loadFile(String url) {
48         try {
49             InputStream stream = SystemFunctions.class.getClassLoader().getResourceAsStream(url);
50             byte[] bytes = ByteStreams.toByteArray(stream);
51             return bytes;
52         } catch (Exception e) {
53             throw new RuntimeException("Unable to load " + url, e);
54         }
55     }
56
57 }