Unit test cases for iaas impl package
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / onap / appc / adapter / iaas / impl / CommonUtility.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.adapter.iaas.impl;
22
23 import java.lang.reflect.Field;
24 import java.util.Map;
25
26 /**
27  * This class is used as a utility class to support the test cases.
28  */
29 public class CommonUtility {
30
31     /**
32      * Use reflection to locate fields and methods so that they can be manipulated during the test
33      * to change the internal state accordingly.
34      * 
35      * @param privateFields
36      * @param object
37      * 
38      */
39     public static void injectMockObjects(Map<String, Object> privateFields, Object object) {
40         privateFields.forEach((fieldName, fieldInstance) -> {
41             try {
42                 Field privateField = object.getClass().getDeclaredField(fieldName);
43                 privateField.setAccessible(true);
44                 privateField.set(object, fieldInstance);
45             } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
46                 // Exception occurred while accessing the private fields
47             }
48         });
49     }
50
51     /**
52      * Use reflection to locate fields and methods of the base class so that they can be manipulated
53      * during the test to change the internal state accordingly.
54      * 
55      * @param privateFields
56      * @param object
57      * 
58      */
59     public static void injectMockObjectsInBaseClass(Map<String, Object> privateFields, Object catalogObject) {
60         // For base class
61         privateFields.forEach((fieldName, fieldInstance) -> {
62             try {
63                 Field privateField = catalogObject.getClass().getSuperclass().getDeclaredField(fieldName);
64                 privateField.setAccessible(true);
65                 privateField.set(catalogObject, fieldInstance);
66             } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
67                 // Exception occurred while accessing the private fields
68             }
69         });
70     }
71
72 }