diff --git a/build.gradle.kts b/build.gradle.kts index 9a7cabb..e4f83e1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,6 +5,7 @@ plugins { id("io.spring.dependency-management") version "1.0.9.RELEASE" kotlin("jvm") version "1.3.72" kotlin("plugin.spring") version "1.3.72" + kotlin("plugin.jpa") version "1.3.72" } group = "com.example" @@ -20,6 +21,11 @@ dependencies { implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("junit:junit:4.12") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.+") + runtimeOnly("mysql:mysql-connector-java") + testImplementation("com.h2database:h2") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") } diff --git a/dashboard/src/main/kotlin/com/kong/dashboard/model/createUserDto.kt b/dashboard/src/main/kotlin/com/kong/dashboard/model/createUserDto.kt new file mode 100644 index 0000000..0d97e81 --- /dev/null +++ b/dashboard/src/main/kotlin/com/kong/dashboard/model/createUserDto.kt @@ -0,0 +1,6 @@ +package com.kong.dashboard.model + +class CreateUserDto { + var name: String = ""; + var age: Int = 0; +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/config/jpaConfig.kt b/src/main/kotlin/com/example/kotlinweb/config/jpaConfig.kt new file mode 100644 index 0000000..4a93baa --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/config/jpaConfig.kt @@ -0,0 +1,24 @@ +package com.example.kotlinweb.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.data.domain.AuditorAware + +import org.springframework.data.jpa.repository.config.EnableJpaAuditing +import java.util.* + + +@Configuration +@EnableJpaAuditing(auditorAwareRef = "auditorAware") +internal class JpaConfig { + @Bean + fun auditorAware(): AuditorAware { + return AuditorAwareImpl() + } +} + +internal class AuditorAwareImpl : AuditorAware { + override fun getCurrentAuditor(): Optional { + return Optional.of("Kong.jino") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/controller/HealthController.kt b/src/main/kotlin/com/example/kotlinweb/controller/HealthCheckController.kt similarity index 61% rename from src/main/kotlin/com/example/kotlinweb/controller/HealthController.kt rename to src/main/kotlin/com/example/kotlinweb/controller/HealthCheckController.kt index fed3266..87378fb 100644 --- a/src/main/kotlin/com/example/kotlinweb/controller/HealthController.kt +++ b/src/main/kotlin/com/example/kotlinweb/controller/HealthCheckController.kt @@ -7,11 +7,10 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.ResponseBody @Controller -class HealthController{ - - @get:ResponseBody - @get:GetMapping(value = ["/health_check.html"]) - val ratingStatus: ResponseEntity - get() = ResponseEntity("Health", HttpStatus.OK) +class HealthCheckController{ + @GetMapping(value = ["/health_check.html"]) + fun getStatus():ResponseEntity{ + return ResponseEntity("Health", HttpStatus.OK) + } } diff --git a/src/main/kotlin/com/example/kotlinweb/controller/board.kt b/src/main/kotlin/com/example/kotlinweb/controller/board.kt new file mode 100644 index 0000000..3ca4d94 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/controller/board.kt @@ -0,0 +1,31 @@ +package com.example.kotlinweb.controller + +import com.example.kotlinweb.entity.Board +import com.example.kotlinweb.model.CreateBoardRequest +import com.example.kotlinweb.model.UpdateBoardRequest +import com.example.kotlinweb.service.BoardService +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.* + +@Controller +class BoardController(private val boardService: BoardService){ + @GetMapping("/board") + fun findAll(): ResponseEntity> { + return ResponseEntity.ok(boardService.findAll()) + } + @GetMapping("/board/{id}") + fun findOne(@PathVariable id:Long):ResponseEntity{ + return ResponseEntity.ok(boardService.getBoardById(id)) + } + @PostMapping("/board") + fun create(@RequestBody requestBody: CreateBoardRequest):ResponseEntity{ + return ResponseEntity.ok(boardService.save(requestBody)) + } + + @PatchMapping("/board/{id}") + fun update(@PathVariable id:Long, @RequestBody requestBody: UpdateBoardRequest):ResponseEntity{ + return ResponseEntity.ok(boardService.update(id,requestBody)) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/controller/comment.kt b/src/main/kotlin/com/example/kotlinweb/controller/comment.kt new file mode 100644 index 0000000..17f35cd --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/controller/comment.kt @@ -0,0 +1,31 @@ +package com.example.kotlinweb.controller + +import com.example.kotlinweb.entity.Comment +import com.example.kotlinweb.model.CreateCommentRequest +import com.example.kotlinweb.model.UpdateCommentRequest +import com.example.kotlinweb.service.CommentService +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.* + +@Controller +class CommentController(private val commentService: CommentService){ + @GetMapping("/comment") + fun findAll(): ResponseEntity> { + return ResponseEntity.ok(commentService.findAll()) + } + @GetMapping("/comment/{id}") + fun findOne(@PathVariable id:Long):ResponseEntity{ + return ResponseEntity.ok(commentService.getCommentById(id)) + } + @PostMapping("/comment") + fun create(@RequestBody requestBody: CreateCommentRequest):ResponseEntity{ + return ResponseEntity.ok(commentService.save(requestBody)) + } + + @PatchMapping("/comment/{id}") + fun update(@PathVariable id:Long, @RequestBody requestBody: UpdateCommentRequest):ResponseEntity{ + return ResponseEntity.ok(commentService.update(id,requestBody)) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/controller/post.kt b/src/main/kotlin/com/example/kotlinweb/controller/post.kt new file mode 100644 index 0000000..04384a1 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/controller/post.kt @@ -0,0 +1,31 @@ +package com.example.kotlinweb.controller + +import com.example.kotlinweb.entity.Post +import com.example.kotlinweb.model.CreatePostRequest +import com.example.kotlinweb.model.UpdatePostRequest +import com.example.kotlinweb.service.PostService +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.* + +@Controller +class PostController(private val postService: PostService){ + @GetMapping("/post") + fun findAll(): ResponseEntity> { + return ResponseEntity.ok(postService.findAll()) + } + @GetMapping("/post/{id}") + fun findOne(@PathVariable id:Long):ResponseEntity{ + return ResponseEntity.ok(postService.getPostById(id)) + } + @PostMapping("/post") + fun create(@RequestBody requestBody: CreatePostRequest):ResponseEntity{ + return ResponseEntity.ok(postService.save(requestBody)) + } + + @PatchMapping("/post/{id}") + fun update(@PathVariable id:Long, @RequestBody requestBody: UpdatePostRequest):ResponseEntity{ + return ResponseEntity.ok(postService.update(id,requestBody)) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/controller/user.kt b/src/main/kotlin/com/example/kotlinweb/controller/user.kt new file mode 100644 index 0000000..71bfbed --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/controller/user.kt @@ -0,0 +1,31 @@ +package com.example.kotlinweb.controller + +import com.example.kotlinweb.entity.User +import com.example.kotlinweb.model.CreateUserRequest +import com.example.kotlinweb.model.UpdateUserRequest +import com.example.kotlinweb.service.UserService +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.* + +@Controller +class UserController(private val userService: UserService){ + @GetMapping("/user") + fun findAll(): ResponseEntity> { + return ResponseEntity.ok(userService.findAll()) + } + @GetMapping("/user/{id}") + fun findOne(@PathVariable id:Long):ResponseEntity{ + return ResponseEntity.ok(userService.getUserById(id)) + } + @PostMapping("/user") + fun create(@RequestBody requestBody: CreateUserRequest):ResponseEntity{ + return ResponseEntity.ok(userService.save(requestBody)) + } + + @PatchMapping("/user/{id}") + fun update(@PathVariable id:Long, @RequestBody requestBody: UpdateUserRequest):ResponseEntity{ + return ResponseEntity.ok(userService.update(id,requestBody)) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/entity/baseEntity.kt b/src/main/kotlin/com/example/kotlinweb/entity/baseEntity.kt new file mode 100644 index 0000000..fbc680a --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/entity/baseEntity.kt @@ -0,0 +1,32 @@ +package com.example.kotlinweb.entity + + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer +import org.springframework.data.annotation.CreatedBy +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedBy +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import java.time.LocalDateTime +import javax.persistence.EntityListeners +import javax.persistence.MappedSuperclass + + +@EntityListeners(value = [AuditingEntityListener::class]) +@MappedSuperclass +abstract class Auditable{ + @CreatedBy + var createdBy: U? = null + + @JsonDeserialize(using = LocalDateTimeDeserializer::class) + @CreatedDate + var createdAt: LocalDateTime? = null + + @LastModifiedBy + var updateBy: U? = null + + @JsonDeserialize(using = LocalDateTimeDeserializer::class) + @LastModifiedDate + var updatedAt: LocalDateTime? = null +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/entity/board.kt b/src/main/kotlin/com/example/kotlinweb/entity/board.kt new file mode 100644 index 0000000..9f3dcfa --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/entity/board.kt @@ -0,0 +1,14 @@ +package com.example.kotlinweb.entity +import com.fasterxml.jackson.annotation.JsonIgnore +import javax.persistence.* + +@Entity +@Table +data class Board ( + @Column + var title:String +): Auditable() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id:Long? = null +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/entity/comment.kt b/src/main/kotlin/com/example/kotlinweb/entity/comment.kt new file mode 100644 index 0000000..f52edc8 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/entity/comment.kt @@ -0,0 +1,14 @@ +package com.example.kotlinweb.entity + +import javax.persistence.* + +@Entity +@Table +data class Comment ( + @Column + var title:String +): Auditable() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id:Long? = null +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/entity/post.kt b/src/main/kotlin/com/example/kotlinweb/entity/post.kt new file mode 100644 index 0000000..5683e90 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/entity/post.kt @@ -0,0 +1,14 @@ +package com.example.kotlinweb.entity + +import javax.persistence.* + +@Entity +@Table +data class Post ( + @Column + var title:String +): Auditable() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id:Long? = null +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/entity/user.kt b/src/main/kotlin/com/example/kotlinweb/entity/user.kt new file mode 100644 index 0000000..bd17ef4 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/entity/user.kt @@ -0,0 +1,15 @@ +package com.example.kotlinweb.entity + +import javax.persistence.* + + +@Entity +@Table +data class User ( + @Column + var name: String +): Auditable() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/createBoardRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/createBoardRequest.kt new file mode 100644 index 0000000..c3e633f --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/createBoardRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class CreateBoardRequest( + val title:String +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/createCommentRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/createCommentRequest.kt new file mode 100644 index 0000000..6ec3095 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/createCommentRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class CreateCommentRequest( + val title:String +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/createPostRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/createPostRequest.kt new file mode 100644 index 0000000..17695b5 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/createPostRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class CreatePostRequest( + val title:String +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/createUserRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/createUserRequest.kt new file mode 100644 index 0000000..adda245 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/createUserRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class CreateUserRequest( + val name:String +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/updateBoardRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/updateBoardRequest.kt new file mode 100644 index 0000000..8bafa15 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/updateBoardRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class UpdateBoardRequest( + val title:String? +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/updateCommentRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/updateCommentRequest.kt new file mode 100644 index 0000000..8ea9001 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/updateCommentRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class UpdateCommentRequest( + val title:String? +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/updatePostRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/updatePostRequest.kt new file mode 100644 index 0000000..1b7df2a --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/updatePostRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class UpdatePostRequest( + val title:String? +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/model/updateUserRequest.kt b/src/main/kotlin/com/example/kotlinweb/model/updateUserRequest.kt new file mode 100644 index 0000000..78785fc --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/model/updateUserRequest.kt @@ -0,0 +1,5 @@ +package com.example.kotlinweb.model + +data class UpdateUserRequest( + val name:String? +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/repository/board.kt b/src/main/kotlin/com/example/kotlinweb/repository/board.kt new file mode 100644 index 0000000..dd707b1 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/repository/board.kt @@ -0,0 +1,10 @@ +package com.example.kotlinweb.repository + +import com.example.kotlinweb.entity.Board +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@Repository +interface BoardRepository: JpaRepository { + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/repository/comment.kt b/src/main/kotlin/com/example/kotlinweb/repository/comment.kt new file mode 100644 index 0000000..8f9ad44 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/repository/comment.kt @@ -0,0 +1,10 @@ +package com.example.kotlinweb.repository + +import com.example.kotlinweb.entity.Comment +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@Repository +interface CommentRepository: JpaRepository { + +} diff --git a/src/main/kotlin/com/example/kotlinweb/repository/post.kt b/src/main/kotlin/com/example/kotlinweb/repository/post.kt new file mode 100644 index 0000000..67989a5 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/repository/post.kt @@ -0,0 +1,10 @@ +package com.example.kotlinweb.repository + +import com.example.kotlinweb.entity.Post +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@Repository +interface PostRepository: JpaRepository { + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/repository/user.kt b/src/main/kotlin/com/example/kotlinweb/repository/user.kt new file mode 100644 index 0000000..4203104 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/repository/user.kt @@ -0,0 +1,10 @@ +package com.example.kotlinweb.repository + +import com.example.kotlinweb.entity.User +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@Repository +interface UserRepository:JpaRepository{ + +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/service/board.kt b/src/main/kotlin/com/example/kotlinweb/service/board.kt new file mode 100644 index 0000000..e8287c8 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/service/board.kt @@ -0,0 +1,28 @@ +package com.example.kotlinweb.service + +import com.example.kotlinweb.entity.Board +import com.example.kotlinweb.model.CreateBoardRequest +import com.example.kotlinweb.model.UpdateBoardRequest +import com.example.kotlinweb.repository.BoardRepository +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class BoardService(val boardRepository: BoardRepository){ + fun findAll():List { + return boardRepository.findAll() + } + + fun save(createBoardRequest: CreateBoardRequest): Board { + return boardRepository.save(Board(createBoardRequest.title)) + } + + fun getBoardById(id:Long): Board { + return boardRepository.findByIdOrNull(id) ?: throw Exception("There is no board with that ID.") + } + + fun update(id:Long,updateBoardRequest: UpdateBoardRequest): Board { + val targetBoard: Board = getBoardById(id) + return boardRepository.save(targetBoard) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/service/comment.kt b/src/main/kotlin/com/example/kotlinweb/service/comment.kt new file mode 100644 index 0000000..f0a49bf --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/service/comment.kt @@ -0,0 +1,28 @@ +package com.example.kotlinweb.service + +import com.example.kotlinweb.entity.Comment +import com.example.kotlinweb.model.CreateCommentRequest +import com.example.kotlinweb.model.UpdateCommentRequest +import com.example.kotlinweb.repository.CommentRepository +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class CommentService(val commentRepository: CommentRepository){ + fun findAll():List { + return commentRepository.findAll() + } + + fun save(createCommentRequest: CreateCommentRequest): Comment { + return commentRepository.save(Comment(createCommentRequest.title)) + } + + fun getCommentById(id:Long): Comment { + return commentRepository.findByIdOrNull(id) ?: throw Exception("There is no comment with that ID.") + } + + fun update(id:Long,updateCommentRequest: UpdateCommentRequest): Comment { + val targetComment: Comment = getCommentById(id) + return commentRepository.save(targetComment) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/service/post.kt b/src/main/kotlin/com/example/kotlinweb/service/post.kt new file mode 100644 index 0000000..4714620 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/service/post.kt @@ -0,0 +1,28 @@ +package com.example.kotlinweb.service + +import com.example.kotlinweb.entity.Post +import com.example.kotlinweb.model.CreatePostRequest +import com.example.kotlinweb.model.UpdatePostRequest +import com.example.kotlinweb.repository.PostRepository +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class PostService(val postRepository: PostRepository){ + fun findAll():List { + return postRepository.findAll() + } + + fun save(createPostRequest: CreatePostRequest): Post { + return postRepository.save(Post(createPostRequest.title)) + } + + fun getPostById(id:Long): Post { + return postRepository.findByIdOrNull(id) ?: throw Exception("There is no post with that ID.") + } + + fun update(id:Long,updatePostRequest: UpdatePostRequest): Post { + val targetPost: Post = getPostById(id) + return postRepository.save(targetPost) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/kotlinweb/service/user.kt b/src/main/kotlin/com/example/kotlinweb/service/user.kt new file mode 100644 index 0000000..a985be4 --- /dev/null +++ b/src/main/kotlin/com/example/kotlinweb/service/user.kt @@ -0,0 +1,29 @@ +package com.example.kotlinweb.service + +import com.example.kotlinweb.entity.User +import com.example.kotlinweb.model.CreateUserRequest +import com.example.kotlinweb.model.UpdateUserRequest +import com.example.kotlinweb.repository.UserRepository +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class UserService(val userRepository: UserRepository){ + fun findAll():List { + return userRepository.findAll() + } + + fun save(createUserRequest: CreateUserRequest): User { + return userRepository.save(User(name = createUserRequest.name)) + } + + fun getUserById(id:Long):User{ + return userRepository.findByIdOrNull(id) ?: throw Exception("There is no user with that ID.") + } + + fun update(id:Long,updateUserRequest: UpdateUserRequest): User { + val targetUser:User = getUserById(id) + targetUser.name=updateUserRequest.name?:targetUser.name + return userRepository.save(targetUser) + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..b29c74b --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,19 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/data?serverTimezone=UTC&characterEncoding=UTF-8 + username: root + password: 1234 + hikari: + maximum-pool-size: 50 + minimum-idle: 10 + idle-timeout: 30000 + connection-test-query: "SELECT 1" + jpa: + show-sql: true + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true \ No newline at end of file diff --git a/src/test/kotlin/com/example/kotlinweb/KotlinwebApplicationTests.kt b/src/test/kotlin/com/example/kotlinweb/KotlinwebApplicationTests.kt index 02d9c00..3c4b3dd 100644 --- a/src/test/kotlin/com/example/kotlinweb/KotlinwebApplicationTests.kt +++ b/src/test/kotlin/com/example/kotlinweb/KotlinwebApplicationTests.kt @@ -1,13 +1,33 @@ package com.example.kotlinweb 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.AutoConfigureMockMvc import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ActiveProfiles +import org.springframework.test.context.junit.jupiter.SpringExtension +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print +import javax.transaction.Transactional @SpringBootTest -class KotlinwebApplicationTests { +@AutoConfigureMockMvc +@ExtendWith(SpringExtension::class) +@ActiveProfiles(value = ["test"]) +class StudyApplicationTests { + + @Autowired + lateinit var mockMvc: MockMvc @Test + @Transactional fun contextLoads() { + mockMvc.perform(get("/health_check.html")) + .andExpect(status().isOk) + .andDo(print()) } -} +} \ No newline at end of file diff --git a/src/test/kotlin/com/example/kotlinweb/e2e/user/controller.kt b/src/test/kotlin/com/example/kotlinweb/e2e/user/controller.kt new file mode 100644 index 0000000..b5f0d1d --- /dev/null +++ b/src/test/kotlin/com/example/kotlinweb/e2e/user/controller.kt @@ -0,0 +1,109 @@ +package com.example.kotlinweb.e2e.user + +import com.example.kotlinweb.entity.User +import com.example.kotlinweb.model.CreateUserRequest +import com.example.kotlinweb.model.UpdateUserRequest +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +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.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ActiveProfiles +import org.springframework.test.context.junit.jupiter.SpringExtension +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import javax.transaction.Transactional +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.springframework.http.MediaType +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* + +@SpringBootTest +@AutoConfigureMockMvc +@ExtendWith(SpringExtension::class) +@ActiveProfiles(value = ["test"]) +class UserControllerTest { + + @Autowired + lateinit var mockMvc: MockMvc + val objectMapper = jacksonObjectMapper() + + @Test + @Transactional + fun getAllUser() { + // GIVEN + // WHEN + val apiResult = mockMvc.perform(get("/user")) + .andExpect(status().isOk) + .andReturn() + // THEN + val result =objectMapper.readValue(apiResult.response.contentAsString, object : TypeReference>(){}) + + assertEquals( result , listOf()) + } + + @Test + @Transactional + fun createUser() { + // GIVEN + val randomName = Math.random().toString() + // WHEN + val createUserRequestApi = mockMvc.perform( + post("/user") + .contentType(MediaType.APPLICATION_JSON) + .content( objectMapper.writeValueAsString(CreateUserRequest(name = randomName))) + ) + .andExpect(status().isOk) + .andReturn() + val createUserResult = objectMapper.readValue(createUserRequestApi.response.contentAsString, object : TypeReference(){}) + + val userId = createUserResult.id + val getUserRequestApi = mockMvc.perform(get("/user/$userId")) + .andExpect(status().isOk) + .andReturn() + + val result = objectMapper.readValue(getUserRequestApi.response.contentAsString, object : TypeReference(){}) + // THEN + assertEquals(result,createUserResult) + } + + @Test + @Transactional + fun updateUser() { + // GIVEN + val randomName1 = Math.random().toString() + val randomName2 = Math.random().toString() + // WHEN + val createUserRequestApi = mockMvc.perform( + post("/user") + .contentType(MediaType.APPLICATION_JSON) + .content( objectMapper.writeValueAsString(CreateUserRequest(name = randomName1))) + ) + .andExpect(status().isOk) + .andReturn() + val createUserResult = objectMapper.readValue(createUserRequestApi.response.contentAsString, object : TypeReference(){}) + + val userId = createUserResult.id + + val updateUserRequestApi = mockMvc.perform( + patch("/user/$userId") + .contentType(MediaType.APPLICATION_JSON) + .content( objectMapper.writeValueAsString(UpdateUserRequest(name = randomName2))) + ) + .andExpect(status().isOk) + .andReturn() + val updateUserRequest = objectMapper.readValue(updateUserRequestApi.response.contentAsString, object : TypeReference(){}) + + + val getUserRequestApi = mockMvc.perform(get("/user/$userId")) + .andExpect(status().isOk) + .andReturn() + + val getUserResult = objectMapper.readValue(getUserRequestApi.response.contentAsString, object : TypeReference(){}) + // THEN + assertNotEquals(getUserResult,createUserResult) + assertEquals(getUserResult,updateUserRequest) + } +} \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..795f206 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,20 @@ +spring: + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:default;mode=mysql + username: root + password: 1234 + hikari: + maximum-pool-size: 50 + minimum-idle: 10 + idle-timeout: 30000 + connection-test-query: "SELECT 1" + jpa: + show-sql: true + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true + database: h2 + generate-ddl: true \ No newline at end of file