Spring Boot Bootstrap upgrade (#2904)
This commit is contained in:
parent
f1831535cb
commit
95cf16de28
1
pom.xml
1
pom.xml
@ -149,6 +149,7 @@
|
|||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
<module>spring-bom</module>
|
<module>spring-bom</module>
|
||||||
<module>spring-boot</module>
|
<module>spring-boot</module>
|
||||||
|
<module>spring-boot-bootstrap</module>
|
||||||
<module>spring-cloud-data-flow</module>
|
<module>spring-cloud-data-flow</module>
|
||||||
<module>spring-cloud</module>
|
<module>spring-cloud</module>
|
||||||
<module>spring-core</module>
|
<module>spring-core</module>
|
||||||
|
@ -12,10 +12,10 @@
|
|||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<artifactId>parent-boot-5</artifactId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>1.5.3.RELEASE</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath /> <!-- lookup parent from repository -->
|
<relativePath>../parent-boot-5</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<!-- add dependency management if you have a different parent pom
|
<!-- add dependency management if you have a different parent pom
|
||||||
@ -90,5 +90,74 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
<exclude>**/AutoconfigurationTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>autoconfiguration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/AutoconfigurationTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -8,6 +8,7 @@ import javax.persistence.Id;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Book {
|
public class Book {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private long id;
|
private long id;
|
||||||
@ -18,8 +19,6 @@ public class Book {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
public Book() {
|
public Book() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package org.baeldung.persistence.repo;
|
package org.baeldung.persistence.repo;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Book;
|
import org.baeldung.persistence.model.Book;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||||
List<Book> findByTitle(String title);
|
List<Book> findByTitle(String title);
|
||||||
|
Optional<Book> findOne(long id);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.baeldung.web;
|
package org.baeldung.web;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Book;
|
import org.baeldung.persistence.model.Book;
|
||||||
import org.baeldung.persistence.repo.BookRepository;
|
import org.baeldung.persistence.repo.BookRepository;
|
||||||
import org.baeldung.web.exception.BookIdMismatchException;
|
import org.baeldung.web.exception.BookIdMismatchException;
|
||||||
@ -18,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/books")
|
@RequestMapping("/api/books")
|
||||||
public class BookController {
|
public class BookController {
|
||||||
@ -36,12 +36,9 @@ public class BookController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Book findOne(@PathVariable Long id) {
|
public Book findOne(@PathVariable long id) {
|
||||||
final Book book = bookRepository.findOne(id);
|
return bookRepository.findOne(id)
|
||||||
if (book == null) {
|
.orElseThrow(BookNotFoundException::new);
|
||||||
throw new BookNotFoundException();
|
|
||||||
}
|
|
||||||
return book;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ -51,24 +48,19 @@ public class BookController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void delete(@PathVariable Long id) {
|
public void delete(@PathVariable long id) {
|
||||||
final Book book = bookRepository.findOne(id);
|
bookRepository.findOne(id)
|
||||||
if (book == null) {
|
.orElseThrow(BookNotFoundException::new);
|
||||||
throw new BookNotFoundException();
|
|
||||||
}
|
|
||||||
bookRepository.delete(id);
|
bookRepository.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public Book updateBook(@RequestBody Book book, @PathVariable Long id) {
|
public Book updateBook(@RequestBody Book book, @PathVariable long id) {
|
||||||
if (book.getId() != id) {
|
if (book.getId() != id) {
|
||||||
throw new BookIdMismatchException();
|
throw new BookIdMismatchException();
|
||||||
}
|
}
|
||||||
final Book old = bookRepository.findOne(id);
|
bookRepository.findOne(id)
|
||||||
if (old == null) {
|
.orElseThrow(BookNotFoundException::new);
|
||||||
throw new BookNotFoundException();
|
|
||||||
}
|
|
||||||
return bookRepository.save(book);
|
return bookRepository.save(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,18 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler({ BookNotFoundException.class })
|
@ExceptionHandler(BookNotFoundException.class)
|
||||||
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {
|
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {
|
||||||
return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler({ BookIdMismatchException.class, ConstraintViolationException.class, DataIntegrityViolationException.class })
|
@ExceptionHandler({
|
||||||
|
BookIdMismatchException.class,
|
||||||
|
ConstraintViolationException.class,
|
||||||
|
DataIntegrityViolationException.class
|
||||||
|
})
|
||||||
public ResponseEntity<Object> handleBadRequest(Exception ex, WebRequest request) {
|
public ResponseEntity<Object> handleBadRequest(Exception ex, WebRequest request) {
|
||||||
return handleExceptionInternal(ex, ex.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
return handleExceptionInternal(ex, ex
|
||||||
|
.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class SimpleController {
|
public class SimpleController {
|
||||||
|
|
||||||
@Value("${spring.application.name}")
|
@Value("${spring.application.name}")
|
||||||
String appName;
|
String appName;
|
||||||
|
|
||||||
@ -15,5 +16,4 @@ public class SimpleController {
|
|||||||
model.addAttribute("appName", appName);
|
model.addAttribute("appName", appName);
|
||||||
return "home";
|
return "home";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,9 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class SpringBootBootstrapApplicationTests {
|
public class SpringBootBootstrapApplicationIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -21,8 +21,8 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = { Application.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
|
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||||
public class LiveTest {
|
public class SpringBootBootstrapIntegrationTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
@ -1,53 +1,44 @@
|
|||||||
package com.baeldung.keycloak;
|
package com.baeldung.keycloak;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.keycloak.KeycloakPrincipal;
|
|
||||||
import org.keycloak.KeycloakSecurityContext;
|
|
||||||
import org.keycloak.adapters.springboot.client.KeycloakSecurityContextClientRequestInterceptor;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
import org.mockito.Spy;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import java.util.Random;
|
||||||
import static org.mockito.Mockito.when;
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@SpringBootTest(classes = SpringBoot.class)
|
|
||||||
public class KeycloakConfigurationTest {
|
public class KeycloakConfigurationTest {
|
||||||
|
|
||||||
@Spy
|
private static final Random RANDOM = new Random();
|
||||||
private KeycloakSecurityContextClientRequestInterceptor factory;
|
|
||||||
|
|
||||||
private MockHttpServletRequest servletRequest;
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
public KeycloakSecurityContext keycloakSecurityContext;
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private KeycloakPrincipal keycloakPrincipal;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
servletRequest = new MockHttpServletRequest();
|
|
||||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(servletRequest));
|
|
||||||
servletRequest.setUserPrincipal(keycloakPrincipal);
|
|
||||||
when(keycloakPrincipal.getKeycloakSecurityContext()).thenReturn(keycloakSecurityContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetKeycloakSecurityContext() throws Exception {
|
public void testGetKeycloakSecurityContext() throws Exception {
|
||||||
assertNotNull(keycloakPrincipal.getKeycloakSecurityContext());
|
IntStream.generate(this::getResultWithSwitching)
|
||||||
|
.limit(10000000)
|
||||||
|
.average().ifPresent(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getResultWithoutSwitching() {
|
||||||
|
boolean[] gates = {false, false, false};
|
||||||
|
gates[RANDOM.nextInt(3)] = true;
|
||||||
|
int pick = RANDOM.nextInt(3);
|
||||||
|
return gates[pick] ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getResultWithSwitching() {
|
||||||
|
boolean[] gates = {false, false, false};
|
||||||
|
int win = RANDOM.nextInt(3);
|
||||||
|
gates[win] = true;
|
||||||
|
int pick = RANDOM.nextInt(3);
|
||||||
|
int empty = Stream.of(0, 1, 2)
|
||||||
|
.filter(i -> i != pick)
|
||||||
|
.filter(i -> !gates[i])
|
||||||
|
.findFirst().get();
|
||||||
|
|
||||||
|
int newPick = Stream.of(0, 1, 2)
|
||||||
|
.filter(i -> i != pick)
|
||||||
|
.filter(i -> i != empty)
|
||||||
|
.findFirst().get();
|
||||||
|
|
||||||
|
return gates[newPick] ? 1 : 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user