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-bom</module>
|
||||
<module>spring-boot</module>
|
||||
<module>spring-boot-bootstrap</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud</module>
|
||||
<module>spring-core</module>
|
||||
|
@ -12,10 +12,10 @@
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
<artifactId>parent-boot-5</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<!-- add dependency management if you have a different parent pom
|
||||
@ -90,5 +90,74 @@
|
||||
</plugins>
|
||||
</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>
|
||||
|
@ -8,6 +8,7 @@ import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
@ -18,8 +19,6 @@ public class Book {
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
//
|
||||
|
||||
public Book() {
|
||||
super();
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package org.baeldung.persistence.repo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
List<Book> findByTitle(String title);
|
||||
Optional<Book> findOne(long id);
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package org.baeldung.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import org.baeldung.persistence.repo.BookRepository;
|
||||
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.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/books")
|
||||
public class BookController {
|
||||
@ -36,12 +36,9 @@ public class BookController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Book findOne(@PathVariable Long id) {
|
||||
final Book book = bookRepository.findOne(id);
|
||||
if (book == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
return book;
|
||||
public Book findOne(@PathVariable long id) {
|
||||
return bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ -51,24 +48,19 @@ public class BookController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable Long id) {
|
||||
final Book book = bookRepository.findOne(id);
|
||||
if (book == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
public void delete(@PathVariable long id) {
|
||||
bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
bookRepository.delete(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) {
|
||||
throw new BookIdMismatchException();
|
||||
}
|
||||
final Book old = bookRepository.findOne(id);
|
||||
if (old == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,14 +19,18 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
super();
|
||||
}
|
||||
|
||||
@ExceptionHandler({ BookNotFoundException.class })
|
||||
@ExceptionHandler(BookNotFoundException.class)
|
||||
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest 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) {
|
||||
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
|
||||
public class SimpleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
String appName;
|
||||
|
||||
@ -15,5 +16,4 @@ public class SimpleController {
|
||||
model.addAttribute("appName", appName);
|
||||
return "home";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,10 +7,9 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootBootstrapApplicationTests {
|
||||
public class SpringBootBootstrapApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
@ -21,8 +21,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { Application.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
public class LiveTest {
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
public class SpringBootBootstrapIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
@ -1,53 +1,44 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
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 static org.mockito.Mockito.when;
|
||||
import java.util.Random;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringBoot.class)
|
||||
public class KeycloakConfigurationTest {
|
||||
|
||||
@Spy
|
||||
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);
|
||||
}
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
@Test
|
||||
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