|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 Firefly Software Solutions Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.fireflyframework.web.openapi; |
| 18 | + |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.springframework.beans.BeansException; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 26 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 27 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.context.annotation.Profile; |
| 30 | +import org.springframework.web.bind.annotation.RestController; |
| 31 | + |
| 32 | +import java.lang.reflect.Constructor; |
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.util.LinkedHashMap; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +/** |
| 38 | + * Automatically registers Mockito mock beans for every dependency of |
| 39 | + * {@code @RestController} classes that is not already present in the bean |
| 40 | + * registry. Supports both {@code @Autowired} field injection and |
| 41 | + * constructor injection. |
| 42 | + * |
| 43 | + * <p>Mocks are registered as pre-existing singletons via |
| 44 | + * {@link ConfigurableListableBeanFactory#registerSingleton} so that Spring |
| 45 | + * does not attempt to process {@code @Autowired} annotations on the mock |
| 46 | + * objects themselves (which would fail for concrete service classes that |
| 47 | + * have their own unsatisfied dependencies). |
| 48 | + * |
| 49 | + * <p>Activated only under the {@code openapi-gen} Spring profile. |
| 50 | + */ |
| 51 | +@Configuration |
| 52 | +@Profile("openapi-gen") |
| 53 | +public class AutoMockMissingBeansConfig implements BeanDefinitionRegistryPostProcessor { |
| 54 | + |
| 55 | + private static final Logger logger = LoggerFactory.getLogger(AutoMockMissingBeansConfig.class); |
| 56 | + |
| 57 | + private final Map<String, Class<?>> mocksToRegister = new LinkedHashMap<>(); |
| 58 | + |
| 59 | + @Override |
| 60 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 61 | + for (String beanName : registry.getBeanDefinitionNames()) { |
| 62 | + BeanDefinition bd = registry.getBeanDefinition(beanName); |
| 63 | + String beanClassName = bd.getBeanClassName(); |
| 64 | + if (beanClassName == null) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + Class<?> beanClass; |
| 69 | + try { |
| 70 | + beanClass = Class.forName(beanClassName); |
| 71 | + } catch (ClassNotFoundException e) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + if (!beanClass.isAnnotationPresent(RestController.class)) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + // Handle @Autowired field injection |
| 80 | + for (Field field : beanClass.getDeclaredFields()) { |
| 81 | + if (!field.isAnnotationPresent(Autowired.class)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + collectMissingBean(registry, field.getType()); |
| 85 | + } |
| 86 | + |
| 87 | + // Handle constructor injection |
| 88 | + Constructor<?>[] constructors = beanClass.getDeclaredConstructors(); |
| 89 | + if (constructors.length == 1 && constructors[0].getParameterCount() > 0) { |
| 90 | + for (Class<?> paramType : constructors[0].getParameterTypes()) { |
| 91 | + collectMissingBean(registry, paramType); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 99 | + for (Map.Entry<String, Class<?>> entry : mocksToRegister.entrySet()) { |
| 100 | + Class<?> fieldType = entry.getValue(); |
| 101 | + String mockBeanName = "mock_" + fieldType.getSimpleName(); |
| 102 | + |
| 103 | + logger.info("Registering Mockito mock singleton: {} (type: {})", |
| 104 | + mockBeanName, fieldType.getName()); |
| 105 | + beanFactory.registerSingleton(mockBeanName, Mockito.mock(fieldType)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void collectMissingBean(BeanDefinitionRegistry registry, Class<?> type) { |
| 110 | + if (mocksToRegister.containsKey(type.getName())) { |
| 111 | + return; |
| 112 | + } |
| 113 | + if (isBeanTypeRegistered(registry, type)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + String mockBeanName = "mock_" + type.getSimpleName(); |
| 117 | + logger.info("Will register Mockito mock for missing bean: {} (type: {})", |
| 118 | + mockBeanName, type.getName()); |
| 119 | + mocksToRegister.put(type.getName(), type); |
| 120 | + } |
| 121 | + |
| 122 | + private boolean isBeanTypeRegistered(BeanDefinitionRegistry registry, Class<?> type) { |
| 123 | + for (String name : registry.getBeanDefinitionNames()) { |
| 124 | + BeanDefinition bd = registry.getBeanDefinition(name); |
| 125 | + String className = bd.getBeanClassName(); |
| 126 | + if (className == null) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + try { |
| 130 | + Class<?> clazz = Class.forName(className); |
| 131 | + if (type.isAssignableFrom(clazz)) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + } catch (ClassNotFoundException e) { |
| 135 | + // Skip |
| 136 | + } |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | +} |
0 commit comments