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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.iaas.impl;
23 import java.lang.reflect.Field;
27 * This class is used as a utility class to support the test cases.
29 public class CommonUtility {
32 * Use reflection to locate fields and methods so that they can be manipulated during the test
33 * to change the internal state accordingly.
35 * @param privateFields
39 public static void injectMockObjects(Map<String, Object> privateFields, Object object) {
40 privateFields.forEach((fieldName, fieldInstance) -> {
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
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.
55 * @param privateFields
59 public static void injectMockObjectsInBaseClass(Map<String, Object> privateFields, Object catalogObject) {
61 privateFields.forEach((fieldName, fieldInstance) -> {
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