Removing jackson to mitigate cve-2017-4995
[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 import java.io.InputStream;
21 import java.lang.reflect.Constructor;
22
23 public class TestUtil {
24     /**
25      * Due to sonar issue you have to create a private constructor for classes
26      * that only have static methods to prevent the creation of instances. This
27      * constructor can not be coveraged / tested since it is private.
28      *
29      * @param clazz
30      */
31     public static void coveragePrivateConstructorForClassesWithStaticMethodsOnly(Class<?> clazz) {
32         for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
33             if (constructor.getParameterTypes().length == 0) {
34                 constructor.setAccessible(true);
35                 try {
36                     constructor.newInstance();
37                 } catch (Exception e) {
38                     throw new IllegalStateException(e);
39                 }
40             } else {
41                 throw new IllegalStateException("This should only be used if the class has a singe private constructor");
42             }
43         }
44     }
45
46     public static byte[] loadFile(String url) {
47         try {
48             InputStream stream = SystemFunctions.class.getClassLoader().getResourceAsStream(url);
49             byte[] bytes = ByteStreams.toByteArray(stream);
50             return bytes;
51         } catch (Exception e) {
52             throw new RuntimeException("Unable to load " + url, e);
53         }
54     }
55
56 }