|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * Copyright 2019-2023 the original author or authors. |
| 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 | + * * * * https://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 | + * * * |
| 19 | + * * |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +package org.springdoc.core; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileWriter; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.PrintWriter; |
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.net.URL; |
| 31 | +import java.net.URLClassLoader; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +import javax.tools.JavaCompiler; |
| 37 | +import javax.tools.ToolProvider; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.Nested; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 43 | +import org.junit.jupiter.api.condition.JRE; |
| 44 | +import org.junit.jupiter.api.io.TempDir; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.MockitoAnnotations; |
| 47 | +import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer; |
| 48 | +import org.springdoc.core.providers.JavadocProvider; |
| 49 | +import org.springdoc.core.providers.ObjectMapperProvider; |
| 50 | +import org.springdoc.core.providers.WebConversionServiceProvider; |
| 51 | + |
| 52 | +import org.springframework.core.MethodParameter; |
| 53 | + |
| 54 | +import static org.junit.jupiter.api.Assertions.*; |
| 55 | +import static org.mockito.ArgumentMatchers.any; |
| 56 | +import static org.mockito.Mockito.verify; |
| 57 | +import static org.mockito.Mockito.when; |
| 58 | + |
| 59 | +/** |
| 60 | + * Tests for {@link GenericParameterService}. |
| 61 | + */ |
| 62 | +class GenericParameterServiceTest { |
| 63 | + @TempDir |
| 64 | + private File tempDir; |
| 65 | + |
| 66 | + @Mock |
| 67 | + private PropertyResolverUtils propertyResolverUtils; |
| 68 | + |
| 69 | + @Mock |
| 70 | + private DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer; |
| 71 | + |
| 72 | + @Mock |
| 73 | + private WebConversionServiceProvider webConversionServiceProvider; |
| 74 | + |
| 75 | + @Mock |
| 76 | + private ObjectMapperProvider objectMapperProvider; |
| 77 | + |
| 78 | + @Mock |
| 79 | + private JavadocProvider javadocProvider; |
| 80 | + |
| 81 | + private GenericParameterService genericParameterService; |
| 82 | + |
| 83 | + @BeforeEach |
| 84 | + void setup() { |
| 85 | + MockitoAnnotations.openMocks(this); |
| 86 | + this.genericParameterService = new GenericParameterService(propertyResolverUtils, Optional.of(delegatingMethodParameterCustomizer), Optional.of(webConversionServiceProvider), objectMapperProvider, Optional.of(javadocProvider)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests for {@link GenericParameterService#getParamJavadoc(JavadocProvider, MethodParameter)}. |
| 91 | + */ |
| 92 | + @Nested |
| 93 | + class getParamJavadoc { |
| 94 | + @Mock |
| 95 | + private DelegatingMethodParameter methodParameter; |
| 96 | + |
| 97 | + @BeforeEach |
| 98 | + void setup() { |
| 99 | + MockitoAnnotations.openMocks(this); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @EnabledForJreRange(min = JRE.JAVA_17) |
| 104 | + void hasDescriptionOfRecordObject() throws IOException, ClassNotFoundException, NoSuchMethodException { |
| 105 | + Class cls = createRecordObject(); |
| 106 | + Method method = cls.getMethod("name"); |
| 107 | + |
| 108 | + when(methodParameter.getParameterName()).thenReturn("name"); |
| 109 | + when(methodParameter.isParameterObject()).thenReturn(true); |
| 110 | + when(methodParameter.getExecutable()).thenReturn(method); |
| 111 | + |
| 112 | + Map<String, String> recordParamMap = new HashMap<>(); |
| 113 | + recordParamMap.put("id", "the id"); |
| 114 | + recordParamMap.put("name", "the name"); |
| 115 | + when(javadocProvider.getRecordClassParamJavadoc(cls)).thenReturn(recordParamMap); |
| 116 | + |
| 117 | + when(javadocProvider.getFieldJavadoc(any())).thenReturn(null); |
| 118 | + |
| 119 | + String actual = genericParameterService.getParamJavadoc(javadocProvider, methodParameter); |
| 120 | + assertEquals("the name", actual); |
| 121 | + |
| 122 | + verify(methodParameter).getParameterName(); |
| 123 | + verify(methodParameter).isParameterObject(); |
| 124 | + verify(methodParameter).getExecutable(); |
| 125 | + verify(javadocProvider).getRecordClassParamJavadoc(cls); |
| 126 | + verify(javadocProvider).getFieldJavadoc(any()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void hasDescriptionOfClassObject() throws IOException, ClassNotFoundException, NoSuchMethodException { |
| 131 | + Class cls = ClassObject.class; |
| 132 | + Method method = cls.getMethod("getName"); |
| 133 | + |
| 134 | + when(methodParameter.getParameterName()).thenReturn("name"); |
| 135 | + when(methodParameter.isParameterObject()).thenReturn(true); |
| 136 | + when(methodParameter.getExecutable()).thenReturn(method); |
| 137 | + |
| 138 | + when(javadocProvider.getFieldJavadoc(any())).thenReturn("the name"); |
| 139 | + |
| 140 | + String actual = genericParameterService.getParamJavadoc(javadocProvider, methodParameter); |
| 141 | + assertEquals("the name", actual); |
| 142 | + |
| 143 | + verify(methodParameter).getParameterName(); |
| 144 | + verify(methodParameter).isParameterObject(); |
| 145 | + verify(methodParameter).getExecutable(); |
| 146 | + verify(javadocProvider).getFieldJavadoc(any()); |
| 147 | + } |
| 148 | + |
| 149 | + private Class<?> createRecordObject() throws IOException, ClassNotFoundException { |
| 150 | + File recordObject = new File(tempDir, "RecordObject.java"); |
| 151 | + try (PrintWriter writer = new PrintWriter(new FileWriter(recordObject))) { |
| 152 | + writer.println("public record RecordObject(String id, String name){"); |
| 153 | + writer.println("}"); |
| 154 | + } |
| 155 | + String[] args = { |
| 156 | + recordObject.getAbsolutePath() |
| 157 | + }; |
| 158 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 159 | + int r = compiler.run(null, null, null, args); |
| 160 | + if (r != 0) { |
| 161 | + throw new IllegalStateException("Compilation failed"); |
| 162 | + } |
| 163 | + URL[] urls = { tempDir.toURI().toURL() }; |
| 164 | + ClassLoader loader = URLClassLoader.newInstance(urls); |
| 165 | + |
| 166 | + return loader.loadClass("RecordObject"); |
| 167 | + } |
| 168 | + |
| 169 | + private class ClassObject { |
| 170 | + /** |
| 171 | + * the id |
| 172 | + */ |
| 173 | + private String id; |
| 174 | + |
| 175 | + /** |
| 176 | + * the name |
| 177 | + */ |
| 178 | + private String name; |
| 179 | + |
| 180 | + public ClassObject(String id, String name) { |
| 181 | + this.id = id; |
| 182 | + this.name = name; |
| 183 | + } |
| 184 | + |
| 185 | + public String getId() { |
| 186 | + return id; |
| 187 | + } |
| 188 | + |
| 189 | + public String getName() { |
| 190 | + return name; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments