Skip to content

Commit

Permalink
clean boot-groovy
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Mar 4, 2020
1 parent 8f55df5 commit 06aa9ac
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 110 deletions.
39 changes: 20 additions & 19 deletions boot-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>11</java.version>
</properties>

<dependencies>
Expand All @@ -33,26 +33,28 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand All @@ -63,15 +65,14 @@
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6.2</version>
<version>1.8.1</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
Expand Down
107 changes: 33 additions & 74 deletions boot-groovy/src/main/groovy/com/example/demo/DemoApplication.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.demo


import groovy.transform.builder.Builder
import org.slf4j.LoggerFactory
import org.springframework.boot.CommandLineRunner
Expand All @@ -12,16 +11,6 @@ import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.config.EnableMongoAuditing
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.http.HttpMethod
import org.springframework.security.authorization.AuthorizationDecision
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.core.Authentication
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.security.web.server.authorization.AuthorizationContext
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.ServerRequest
Expand All @@ -45,40 +34,10 @@ class DemoApplication {
@Bean
RouterFunction<ServerResponse> routes(PostHandler postController) {
return route(GET("/posts"), postController.&all)
.andRoute(POST("/posts"), postController.&create)
.andRoute(GET("/posts/{id}"), postController.&get)
.andRoute(PUT("/posts/{id}"), postController.&update)
.andRoute(DELETE("/posts/{id}"), postController.&delete)
}

}

@EnableWebFluxSecurity
class SecurityConfig {

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
return http
.authorizeExchange()
.pathMatchers(HttpMethod.GET, "/posts/**").permitAll()
.pathMatchers(HttpMethod.DELETE, "/posts/**").hasRole("ADMIN")
//.pathMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
.anyExchange().authenticated()
.and()
.build()
}

private Mono<AuthorizationDecision> currentUserMatchesPath(Mono<Authentication> authentication, AuthorizationContext context) {
return authentication
.map { (context.getVariables().get("user") == it.getName()) }
.map { new AuthorizationDecision(it) }
}

@Bean
MapReactiveUserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build()
UserDetails admin = User.withDefaultPasswordEncoder().username("admin").password("password").roles("USER", "ADMIN").build()
return new MapReactiveUserDetailsService(user, admin)
.andRoute(POST("/posts"), postController.&create)
.andRoute(GET("/posts/{id}"), postController.&get)
.andRoute(PUT("/posts/{id}"), postController.&update)
.andRoute(DELETE("/posts/{id}"), postController.&delete)
}

}
Expand All @@ -97,19 +56,19 @@ class DataInitializer implements CommandLineRunner {
void run(String[] args) {
log.info("start data initialization ...")
this.posts
.deleteAll()
.thenMany(
Flux.just("Post one", "Post two")
.flatMap { it -> this.posts.save(Post.builder().title(it).content("content of " + it).build()) }
.deleteAll()
.thenMany(
Flux.just("Post one", "Post two")
.flatMap { it -> this.posts.save(Post.builder().title(it).content("content of " + it).build()) }


)
.log()
.subscribe(
null,
null,
{ log.info("done initialization...") }
)
)
.log()
.subscribe(
null,
null,
{ log.info("done initialization...") }
)

}

Expand All @@ -130,33 +89,33 @@ class PostHandler {

Mono<ServerResponse> create(ServerRequest req) {
return req.bodyToMono(Post.class)
.flatMap { this.posts.save(it) }
.flatMap { ServerResponse.created(URI.create("/posts/".concat(it.getId()))).build() }
.flatMap { this.posts.save(it) }
.flatMap { ServerResponse.created(URI.create("/posts/".concat(it.getId()))).build() }
}

Mono<ServerResponse> get(ServerRequest req) {
return this.posts.findById(req.pathVariable("id"))
.flatMap { ServerResponse.ok().body(Mono.just(it), Post.class) }
.switchIfEmpty { ServerResponse.notFound().build() }
.flatMap { ServerResponse.ok().body(Mono.just(it), Post.class) }
.switchIfEmpty { ServerResponse.notFound().build() }
}

Mono<ServerResponse> update(ServerRequest req) {

return Mono
.zip(
{
Post p = (Post) it[0]
Post p2 = (Post) it[1]
p.title = p2.title
p.content = p2.content
p
},
this.posts.findById(req.pathVariable("id")),
req.bodyToMono(Post.class)
)
.cast(Post.class)
.flatMap { this.posts.save(it) }
.flatMap { ServerResponse.noContent().build() }
.zip(
{
Post p = (Post) it[0]
Post p2 = (Post) it[1]
p.title = p2.title
p.content = p2.content
p
},
this.posts.findById(req.pathVariable("id")),
req.bodyToMono(Post.class)
)
.cast(Post.class)
.flatMap { this.posts.save(it) }
.flatMap { ServerResponse.noContent().build() }

}

Expand Down
2 changes: 1 addition & 1 deletion boot-groovy/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ spring:
grid-fs-database: images
logging:
level:
org.springframework.web: DEBUG
org.springframework.web: DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.demo

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContext
import org.springframework.test.web.reactive.server.WebTestClient

@SpringBootTest
class ApplicationTests {

@Autowired
ApplicationContext context;

WebTestClient client;

@BeforeEach
void setup() {
client = WebTestClient.bindToApplicationContext(context)
.configureClient()
.build();
}

@Test
void contextLoads() {
client.get()
.uri("/posts")
.exchange()
.expectStatus().isOk()
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.demo


import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.test.web.reactive.server.WebTestClient

import java.time.Duration

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class IntegrationTests {

@LocalServerPort
int port;

WebTestClient client

@BeforeEach
void setup() {
this.client = WebTestClient
.bindToServer()
.responseTimeout(Duration.ofSeconds(10))
.baseUrl("http://localhost:" + this.port)
.build();
}

@Test
void getAllPostsWillBeOk() throws Exception {
this.client
.get()
.uri("/posts")
.exchange()
.expectStatus()
.isOk();
}

}

0 comments on commit 06aa9ac

Please sign in to comment.