Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Nov 12, 2021
1 parent b9ad1a3 commit 39dc76a
Show file tree
Hide file tree
Showing 39 changed files with 660 additions and 908 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/boot-kotlin-dsl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
java-version: '17'
distribution: 'adopt'
cache: 'maven'
- name: Start up databases via Docker Compose
run: |
docker-compose up -d mongodb
sleep 5
docker ps -a
- name: Build with Maven
run: |
cd boot-kotlin-dsl
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/boot-kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
java-version: '17'
distribution: 'adopt'
cache: 'maven'
- name: Start up databases via Docker Compose
run: |
docker-compose up -d mongodb
sleep 5
docker ps -a
- name: Build with Maven
run: |
cd boot-kotlin
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/kotlin-dsl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
java-version: '16'
distribution: 'adopt'
cache: 'maven'
- name: Start up databases via Docker Compose
run: |
docker-compose up -d mongodb
sleep 5
docker ps -a
- name: Build with Maven
run: |
cd kotlin-dsl
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/kotlin-routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
java-version: '16'
distribution: 'adopt'
cache: 'maven'
- name: Start up databases via Docker Compose
run: |
docker-compose up -d mongodb
sleep 5
docker ps -a
- name: Build with Maven
run: |
cd kotlin-routes
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
java-version: '16'
distribution: 'adopt'
cache: 'maven'
- name: Start up databases via Docker Compose
run: |
docker-compose up -d mongodb
sleep 5
docker ps -a
- name: Build with Maven
run: |
cd kotlin
Expand Down
48 changes: 16 additions & 32 deletions boot-kotlin-dsl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
Expand All @@ -38,52 +35,38 @@
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<groupId>io.projectreactor.kotlin</groupId>
<artifactId>reactor-kotlin-extensions</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<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>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>

<!--<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
</dependency>-->
</dependencies>

<build>
Expand All @@ -104,6 +87,7 @@
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>11</jvmTarget>
</configuration>
<dependencies>
<dependency>
Expand Down
82 changes: 22 additions & 60 deletions boot-kotlin-dsl/src/main/kotlin/com/example/demo/DemoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.http.HttpMethod
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService
import org.springframework.security.core.userdetails.User
import org.springframework.security.crypto.factory.PasswordEncoderFactories
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.reactive.CorsWebFilter
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource
Expand Down Expand Up @@ -46,54 +39,23 @@ fun beans() = beans {
// )
// )
posts.deleteAll()
.thenMany<Post>(
posts.saveAll(
arrayListOf(
Post(null, "my first post", "content of my first post"),
Post(null, "my second post", "content of my second post")
)
)
.thenMany<Post>(
posts.saveAll(
arrayListOf(
Post(null, "my first post", "content of my first post"),
Post(null, "my second post", "content of my second post")
)
)
.log()
.subscribe(null, null, { println("data initialization done.") })
)
.log()
.subscribe { println("data initialization done.") }
}
}

bean {
PostRoutes(PostHandler(ref())).routes()
}


bean<PasswordEncoder> {
PasswordEncoderFactories.createDelegatingPasswordEncoder()
}

bean<SecurityWebFilterChain> {
ref<ServerHttpSecurity>().authorizeExchange()
.pathMatchers(HttpMethod.GET, "/posts/**").permitAll()
.pathMatchers(HttpMethod.DELETE, "/posts/**").hasRole("ADMIN")
.pathMatchers("/posts/**").authenticated()
//.pathMatchers("/users/{user}/**").access(this::currentUserMatchesPath)
.anyExchange().permitAll()
.and()
.csrf().disable()
.build()
}

bean {
val passwordEncoder = ref<PasswordEncoder>()
val user = User.withUsername("user")
.passwordEncoder { it -> passwordEncoder.encode(it) }
.password("password")
.roles("USER").build()
val admin = User.withUsername("admin")
.password("password")
.passwordEncoder { it -> passwordEncoder.encode(it) }
.roles("USER", "ADMIN")
.build()
MapReactiveUserDetailsService(user, admin)
}

bean {

val config = CorsConfiguration().apply {
Expand Down Expand Up @@ -139,36 +101,36 @@ class PostHandler(private val posts: PostRepository) {

fun create(req: ServerRequest): Mono<ServerResponse> {
return req.bodyToMono(Post::class.java)
.flatMap { this.posts.save(it) }
.flatMap { created(URI.create("/posts/" + it.id)).build() }
.flatMap { this.posts.save(it) }
.flatMap { created(URI.create("/posts/" + it.id)).build() }
}

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

fun update(req: ServerRequest): Mono<ServerResponse> {
return this.posts.findById(req.pathVariable("id"))
.zipWith(req.bodyToMono(Post::class.java))
.map { it.t1.copy(title = it.t2.title, content = it.t2.content) }
.flatMap { this.posts.save(it) }
.flatMap { noContent().build() }
.zipWith(req.bodyToMono(Post::class.java))
.map { it.t1.copy(title = it.t2.title, content = it.t2.content) }
.flatMap { this.posts.save(it) }
.flatMap { noContent().build() }
}

fun delete(req: ServerRequest): Mono<ServerResponse> {
return this.posts.deleteById(req.pathVariable("id"))
.flatMap { noContent().build() }
.flatMap { noContent().build() }
}
}

@Document
data class Post(
@Id var id: String? = null,
var title: String? = null,
var content: String? = null,
@CreatedDate var createdDate: LocalDateTime = LocalDateTime.now()
@Id var id: String? = null,
var title: String? = null,
var content: String? = null,
@CreatedDate var createdDate: LocalDateTime = LocalDateTime.now()
)

interface PostRepository : ReactiveMongoRepository<Post, String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@ package com.example.demo

import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Flux

@SpringBootTest(classes = arrayOf(DemoApplication::class),
properties = ["context.initializer.classes=com.example.demo.TestConfigInitializer"])
@SpringBootTest(
classes = [DemoApplication::class],
properties = ["context.initializer.classes=com.example.demo.TestConfigInitializer"]
)
class ApplicationTests {

@Autowired
private lateinit var routing: RouterFunction<ServerResponse>
private lateinit var routerFunction: RouterFunction<ServerResponse>

private lateinit var client: WebTestClient


@BeforeAll
fun setup() {
client = WebTestClient.bindToRouterFunction(routing).configureClient().build()
client = WebTestClient.bindToRouterFunction(routerFunction)
.configureClient()
.build()
}


@Test
fun `get all posts`() {

client.get().uri("/posts")
.exchange().expectStatus().isOk
.exchange()
.expectStatus().isOk
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo


import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -9,10 +10,10 @@ import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.context.ContextConfiguration
import org.springframework.web.reactive.function.client.WebClient
import reactor.test.test
import reactor.kotlin.test.test

@SpringBootTest(classes = [DemoApplication::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = arrayOf(TestConfigInitializer::class))
@ContextConfiguration(initializers = [TestConfigInitializer::class])
class IntegrationTests {

private lateinit var client: WebClient
Expand All @@ -28,12 +29,15 @@ class IntegrationTests {
@Test
fun `get all posts`() {
client.get()
.uri("/posts")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.test()
.expectNextMatches { it.statusCode() == HttpStatus.OK }
.verifyComplete()
.uri("/posts")
.accept(MediaType.APPLICATION_JSON)
.exchangeToFlux {
assertThat(it.statusCode()).isEqualTo(HttpStatus.OK)
it.bodyToFlux(Post::class.java)
}
.test()
.expectNextCount(2)
.verifyComplete()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ class TestConfigInitializer: ApplicationContextInitializer<GenericApplicationCon
override fun initialize(applicationContext: GenericApplicationContext) {
beans().initialize(applicationContext)
}

}
Loading

0 comments on commit 39dc76a

Please sign in to comment.