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