Merge pull request #11941 from hkhan/JAVA-10500-investigate-springdoc-buildtime

[JAVA-10500] Investigate and reduce springdoc build time
This commit is contained in:
kwoyke 2022-03-21 10:26:20 +01:00 committed by GitHub
commit fd06322ba5
6 changed files with 66 additions and 109 deletions

View File

@ -37,10 +37,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- SpringDoc -->
<dependency>
<groupId>org.springdoc</groupId>
@ -63,21 +59,6 @@
<artifactId>spring-restdocs-restassured</artifactId>
<scope>test</scope>
</dependency>
<!--kotlin dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-kotlin</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
</dependencies>
<build>
@ -109,41 +90,6 @@
</execution>
</executions>
</plugin>
<!-- kotlin -->
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>${java.version}</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
@ -208,7 +154,6 @@
<properties>
<springdoc.version>1.6.4</springdoc.version>
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
<kotlin.version>1.6.0</kotlin.version>
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
</properties>

View File

@ -40,7 +40,8 @@ public class FooController {
public ResponseEntity<Foo> getFooById(@PathVariable("id") Long id) {
Optional<Foo> foo = repository.findById(id);
return foo.isPresent() ? new ResponseEntity<>(foo.get(), HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND);
return foo.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PostMapping
@ -70,7 +71,7 @@ public class FooController {
@PutMapping("/{id}")
public ResponseEntity<Foo> updateFoo(@PathVariable("id") long id, @RequestBody Foo foo) {
boolean isFooPresent = repository.existsById(Long.valueOf(id));
boolean isFooPresent = repository.existsById(id);
if (!isFooPresent) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

View File

@ -7,10 +7,7 @@
</encoder>
</appender>
<logger name="org.springframework" level="DEBUG" />
<logger name="org.springframework.transaction" level="WARN" />
<root level="INFO">
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,6 +1,32 @@
package com.baeldung.restdocopenapi.restdoc;
import com.baeldung.restdocopenapi.Foo;
import com.baeldung.restdocopenapi.FooController;
import com.baeldung.restdocopenapi.FooRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.hateoas.MediaTypes;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.constraints.ConstraintDescriptions;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
@ -15,61 +41,47 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.requestF
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.collectionToDelimitedString;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.hateoas.MediaTypes;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.constraints.ConstraintDescriptions;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.restdocopenapi.Application;
import com.baeldung.restdocopenapi.Foo;
import com.fasterxml.jackson.databind.ObjectMapper;
@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
@SpringBootTest(classes = Application.class)
public class SpringRestDocsUnitTest {
@WebMvcTest(FooController.class)
class SpringRestDocsUnitTest {
private MockMvc mockMvc;
@MockBean
private FooRepository fooRepository;
@Autowired
private ObjectMapper objectMapper;
@BeforeEach
public void setup(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
void setup(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.build();
}
@Test
public void whenGetFoo_thenSuccessful() throws Exception {
void whenGetAllFoo_thenSuccessful() throws Exception {
when(fooRepository.findAll())
.thenReturn(singletonList(new Foo(1, "Foo 1", "Foo 1")));
this.mockMvc.perform(get("/foo"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("Foo 1")))
.andDo(document("getAllFoos"));
}
@Test
public void whenGetFooById_thenSuccessful() throws Exception {
void whenGetFooById_thenSuccessful() throws Exception {
ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class);
when(fooRepository.findById(1L))
.thenReturn(Optional.of(new Foo(1, "title", "body")));
this.mockMvc.perform(get("/foo/{id}", 1))
.andExpect(status().isOk())
.andDo(document("getAFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()),
@ -79,7 +91,7 @@ public class SpringRestDocsUnitTest {
}
@Test
public void whenPostFoo_thenSuccessful() throws Exception {
void whenPostFoo_thenSuccessful() throws Exception {
Map<String, Object> foo = new HashMap<>();
foo.put("id", 4L);
foo.put("title", "New Foo");
@ -93,14 +105,18 @@ public class SpringRestDocsUnitTest {
}
@Test
public void whenDeleteFoo_thenSuccessful() throws Exception {
void whenDeleteFoo_thenSuccessful() throws Exception {
this.mockMvc.perform(delete("/foo/{id}", 2))
.andExpect(status().isNoContent())
.andDo(document("deleteFoo", pathParameters(parameterWithName("id").description("The id of the foo to delete"))));
}
@Test
public void whenUpdateFoo_thenSuccessful() throws Exception {
void whenUpdateFoo_thenSuccessful() throws Exception {
when(fooRepository.existsById(3L)).thenReturn(true);
when(fooRepository.save(any(Foo.class)))
.thenReturn(new Foo(3, "Updated Foo", "Body of updated Foo"));
ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class);
@ -115,6 +131,4 @@ public class SpringRestDocsUnitTest {
responseFields(fieldWithPath("id").description("The id of the updated foo" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")),
fieldWithPath("title").description("The title of the updated foo"), fieldWithPath("body").description("The body of the updated foo"))));
}
}

View File

@ -1,16 +1,16 @@
package com.baeldung.springdoc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class SpringContextTest {
class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
void whenSpringContextIsBootstrapped_thenNoExceptions() {
}