TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-test / src / main / java / org / onap / dcae / apod / analytics / test / runner / GuiceJUnitRunner.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============================LICENSE_END===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.test.runner;
22
23 import com.google.inject.Guice;
24 import com.google.inject.Injector;
25 import com.google.inject.Module;
26 import org.junit.runners.BlockJUnit4ClassRunner;
27 import org.junit.runners.model.InitializationError;
28 import org.onap.dcae.apod.analytics.test.annotation.GuiceModules;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.Arrays;
33
34 /**
35  * A custom Junit Runner which can be used to run Guice Test with custom Test Modules
36  * <p>
37  * @author Rajiv Singla . Creation Date: 10/20/2016.
38  */
39 public class GuiceJUnitRunner extends BlockJUnit4ClassRunner {
40
41     private static final Logger LOG = LoggerFactory.getLogger(GuiceJUnitRunner.class);
42
43     private final Injector injector;
44
45     public GuiceJUnitRunner(Class<?> klass) throws InitializationError {
46         super(klass);
47         Class<?>[] classes = getModulesFor(klass);
48         injector = createInjectorFor(classes);
49     }
50
51
52     /**
53      * Returns a new fixture for running a test. Injects Guice members
54      */
55     @Override
56     public Object createTest() throws Exception {
57         Object obj = super.createTest();
58         injector.injectMembers(obj);
59         return obj;
60     }
61
62
63     /**
64      * Creates new Guice Injector and registers Guice test modules
65      *
66      * @param classes test module classes
67      * @return Guice injector with test modules
68      * @throws InitializationError
69      */
70     private Injector createInjectorFor(Class<?>[] classes) throws InitializationError {
71         Module[] modules = new Module[classes.length];
72         for (int i = 0; i < classes.length; i++) {
73             try {
74                 modules[i] = (Module) classes[i].newInstance();
75             } catch (InstantiationException | IllegalAccessException e) {
76                 throw new InitializationError(e);
77             }
78         }
79         LOG.debug("Creating Junit Test Runner with Guice Test Modules: {}", Arrays.toString(modules));
80         return Guice.createInjector(modules);
81     }
82
83     /**
84      * Extract user provide test modules from the {@link GuiceModules} annotation
85      *
86      * @param klass Target class which is running the test
87      * @return Guice modules contained passed in annotation of Guice Modules
88      * @throws InitializationError
89      */
90     private Class<?>[] getModulesFor(Class<?> klass) throws InitializationError {
91         GuiceModules annotation = klass.getAnnotation(GuiceModules.class);
92         if (annotation == null) {
93             throw new InitializationError(
94                     "Missing @GuiceModules annotation for unit test '" + klass.getName()
95                             + "'");
96         }
97         return annotation.value();
98     }
99
100 }