df16742563f930dc90b2967c2f6dcf59d930fb63
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA 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.dcaegen2.collectors.datafile.integration.junit5.mockito;
22
23 import static org.mockito.Mockito.mock;
24
25 import java.lang.reflect.Parameter;
26
27 import org.junit.jupiter.api.extension.ExtensionContext;
28 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
29 import org.junit.jupiter.api.extension.ExtensionContext.Store;
30 import org.junit.jupiter.api.extension.ParameterContext;
31 import org.junit.jupiter.api.extension.ParameterResolver;
32 import org.junit.jupiter.api.extension.TestInstancePostProcessor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/27/18
38  *
39  *  {@code MockitoExtension } showcases the {@link TestInstancePostProcessor} and {@link ParameterResolver} extension
40  *      APIs of JUnit 5 by providing dependency injection support at the field level and at the method parameter level
41  *      viaMockito 2.x's {@link Mock @Mock} annotation.
42  */
43 public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
44
45     @Override
46     public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
47         MockitoAnnotations.initMocks(testInstance);
48     }
49
50     @Override
51     public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
52         return parameterContext.getParameter().isAnnotationPresent(Mock.class);
53     }
54
55     @Override
56     public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
57         return getMock(parameterContext.getParameter(), extensionContext);
58     }
59
60     private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
61         Class<?> mockType = parameter.getType();
62         Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
63         String mockName = getMockName(parameter);
64
65         if (mockName != null) {
66             return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
67         } else {
68             return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
69         }
70     }
71
72     private String getMockName(Parameter parameter) {
73         String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
74         if (!explicitMockName.isEmpty()) {
75             return explicitMockName;
76         } else if (parameter.isNamePresent()) {
77             return parameter.getName();
78         }
79         return null;
80     }
81
82
83 }