10 Commits

Author SHA1 Message Date
banjjoknim
73c759be30 feat : add oauth2 login and join logic 2022-02-15 01:59:49 +09:00
banjjoknim
4f750fd176 feat : add feature 'login' 2022-02-13 00:12:29 +09:00
banjjoknim
1942d5720e feat : add OAuth2 Configurations 2022-02-07 10:58:02 +09:00
banjjoknim
1c6fea1412 refactor : disabled @EnableWebSecurity 2022-02-07 10:37:13 +09:00
banjjoknim
5af5256bd5 build : add oauth2 client dependency 2022-02-07 10:36:01 +09:00
banjjoknim
a02c3d0065 feat : add feature 'auth user' 2022-02-04 10:42:15 +09:00
banjjoknim
7ca21f70fa add class User, SecurityConfiguration settings 2022-02-04 10:13:54 +09:00
banjjoknim
e32da44a0d refactor : add PasswordEncoder, comment 2022-02-04 10:01:54 +09:00
banjjoknim
b3ea750bc4 chore : add SecurityConfiguration with Kotlin 2022-01-30 02:14:24 +09:00
banjjoknim
6392cb8c6d chore : add Spring Security Configuration 2022-01-19 10:42:37 +09:00
122 changed files with 64 additions and 4889 deletions

View File

@@ -1,37 +0,0 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/

View File

@@ -1,44 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java")
id("org.springframework.boot") version "2.6.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.0"
kotlin("plugin.spring") version "1.6.0"
kotlin("plugin.jpa") version "1.6.0"
}
group = "com.banjjoknim"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
runtimeOnly("mysql:mysql-connector-java")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}

View File

@@ -1,5 +0,0 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,234 +0,0 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@@ -1,89 +0,0 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -1 +0,0 @@
rootProject.name = "jackson"

View File

@@ -1,11 +0,0 @@
package com.banjjoknim.playground
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class PlaygroundApplication
fun main(args: Array<String>) {
runApplication<PlaygroundApplication>(*args)
}

View File

@@ -1,51 +0,0 @@
package com.banjjoknim.playground.jackson.common
import com.banjjoknim.playground.jackson.jsonserialize.ContextualCarSerializer
import com.banjjoknim.playground.jackson.jsonserialize.UsingJsonSerializeAnnotationCarSerializer
import com.fasterxml.jackson.databind.annotation.JsonSerialize
data class Car(
val name: String,
val price: Int = 10000000,
val owner: Owner = Owner()
)
data class CarUsingNoAnnotation(
val name: String = "banjjoknim",
val secret: String = "secret",
val price: Int = 10000000,
val owner: Owner = Owner()
)
data class CarUsingJsonSerializeAnnotationCarSerializer(
val name: String = "banjjoknim",
@JsonSerialize(using = UsingJsonSerializeAnnotationCarSerializer::class)
val secret: String = "secret",
val price: Int = 10000000,
val owner: Owner = Owner()
)
data class CarUsingContextualSerializerWithSecretAnnotation(
val name: String = "banjjoknim",
@JsonSerialize(using = ContextualCarSerializer::class)
@field:Secret("hello world!!")
val secret: String = "secret",
val price: Int = 10000000,
val owner: Owner = Owner()
)
data class CarUsingContextualSerializerWithNoSecretAnnotation(
val name: String = "banjjoknim",
@JsonSerialize(using = ContextualCarSerializer::class)
val secret: String = "secret",
val price: Int = 10000000,
val owner: Owner = Owner()
)
data class CarUsingSecretAnnotation(
val name: String = "banjjoknim",
@field:Secret("****")
val secret: String = "secret",
val price: Int = 10000000,
val owner: Owner = Owner()
)

View File

@@ -1,3 +0,0 @@
package com.banjjoknim.playground.jackson.common
data class Owner(val name: String = "ban", val age: Int = 30)

View File

@@ -1,16 +0,0 @@
package com.banjjoknim.playground.jackson.common
import com.fasterxml.jackson.annotation.JacksonAnnotation
/**
* [jackson-annotations](https://www.baeldung.com/jackson-annotations) 참고.
*
* @see com.fasterxml.jackson.annotation.JacksonAnnotation
* @see com.fasterxml.jackson.annotation.JacksonAnnotationsInside
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FIELD) // 현재 상황에서는 PROPERTY 로 적용할 경우 제대로 적용되지 않는다. 아마 어노테이션 자체가 자바 기반으로 사용되어 PROPERTY 를 인식하지 못하는 것 같다(자바에서는 PROPERTY 타입을 사용할 수 없음).
@JacksonAnnotation // NOTE: important; MUST be considered a 'Jackson' annotation to be seen(or recognized otherwise via AnnotationIntrospect.isHandled())
annotation class Secret(
val substituteValue: String = ""
)

View File

@@ -1,179 +0,0 @@
package com.banjjoknim.playground.jackson.jsonserialize
import com.banjjoknim.playground.jackson.common.Car
import com.banjjoknim.playground.jackson.common.Secret
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.BeanDescription
import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializationConfig
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.introspect.Annotated
import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier
import com.fasterxml.jackson.databind.ser.ContextualSerializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer
/**
* [jackson-object-mapper-tutorial](https://www.baeldung.com/jackson-object-mapper-tutorial) 참고.
*
* [how-to-(de)serialize-field-from-object-based-on-annotation-using-jackson](https://stackoverflow.com/questions/18659835/how-to-deserialize-field-from-object-based-on-annotation-using-jackson)
*
* Custom Serializer 를 만들기 위해서는 아래와 같이 StdSerializer<T> 를 상속해야 한다.
*
* 만약 어노테이션을 이용한 설정 또는 프로퍼티마다 다르게 작동하는 Serializer 를 만들고 싶다면 JsonSerializer 의 add-on interface 인 ContextualSerializer 를 구현하면 된다.
*
* JsonSerializer<T> 만 확장할 경우엔 애노테이션 정보를 얻을 수 없다. 추가적으로 ContextualSerializer 인터페이스를 구현해주면 createContextual() 메서드를 구현해줘야 하는데 두번째 인자로 넘어오는 BeanProperty 를 이용해 애노테이션 정보를 구할 수 있다.
*
* ContexutalSerializer 를 사용하는 방법은 [TestContextualSerialization](https://github.com/FasterXML/jackson-databind/blob/master/src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualSerialization.java) 참고하도록 한다.
*
* Custom Serializer 가 JsonSerializer<T> 와 ContextualSerialier 를 모두 구현할 경우 createContextual() 함수가 먼저 호출된다.
*
* @see com.fasterxml.jackson.databind.ser.std.StdSerializer
* @see com.fasterxml.jackson.databind.ser.std.StringSerializer
* @see com.fasterxml.jackson.databind.ser.ContextualSerializer
*/
class CarSerializer : StdSerializer<Car>(Car::class.java) {
override fun serialize(value: Car, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("name", value.name)
gen.writeNumberField("price", value.price)
gen.writeEndObject()
}
}
class CarNameSerializer : StdSerializer<Car>(Car::class.java) {
override fun serialize(value: Car, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("name", value.name)
gen.writeEndObject()
}
}
class CarPriceSerializer : StdSerializer<Car>(Car::class.java) {
override fun serialize(value: Car, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeNumberField("price", value.price)
gen.writeEndObject()
}
}
class CarNameOwnerSerializer : StdSerializer<Car>(Car::class.java) {
override fun serialize(value: Car, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("name", value.name)
gen.writeObjectField("owner", value.owner)
gen.writeEndObject()
}
}
class CarNameOwnerNameSerializer : StdSerializer<Car>(Car::class.java) {
override fun serialize(value: Car, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("name", value.name)
gen.writeObjectFieldStart("owner")
gen.writeStringField("name", value.owner.name)
gen.writeEndObject()
}
}
class UsingJsonSerializeAnnotationCarSerializer : StdSerializer<String>(String::class.java) {
override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeString("****")
}
}
class ContextualCarSerializer(
private val substituteValue: String = "this is default value in ContextualCarSerializer"
) : StdSerializer<String>(String::class.java), ContextualSerializer {
override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeString(substituteValue)
}
override fun createContextual(provider: SerializerProvider, property: BeanProperty): JsonSerializer<*> {
val annotation = property.getAnnotation(Secret::class.java)
if (annotation != null) {
return ContextualCarSerializer(annotation.substituteValue)
}
return ContextualCarSerializer()
// JsonMappingException: Can not write a field name, expecting a value (through reference chain: com.banjjoknim.playground.jackson.common.CarUsingContextualSerializerWithNoSecretAnnotation["secret"])
// return provider.findKeySerializer(property.type, property)
}
}
/**
* AnnotationIntrospector 를 상속한 JacksonAnnotationIntrospector 은 Jackson 라이브러리가 직렬화/역직렬화시 `JacksonAnnotation` 정보를 어떻게 처리할지에 대한 정보가 정의되어 있는 클래스다.
*
* 따라서 어노테이션별로 어떻게 처리할지 재정의하고 싶다면 이 녀석을 override 해준뒤 ObjectMapper 에 등록해주면 된다.
*
* 등록할 때는 `ObjectMapper#setAnnotationIntrospector()` 를 사용한다.
*
* [FasterXML - AnnotationIntrospector](https://github.com/FasterXML/jackson-docs/wiki/AnnotationIntrospector)
*
* @see com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector#
* @see com.fasterxml.jackson.databind.ObjectMapper
*
*/
class SecretAnnotationIntrospector : JacksonAnnotationIntrospector() {
/**
*
* `@JsonIgnore` 를 적용했을 때 무시할지 여부를 판단하는 함수이다.
*
* 따라서 직렬화 / 역직렬화시 무시하고 싶은 프로퍼티가 있다면 이 함수를 override 하면 된다.
*/
override fun hasIgnoreMarker(m: AnnotatedMember): Boolean {
return super.hasIgnoreMarker(m)
}
/**
* `@JsonSerailize` 가 붙은 어노테이션의 처리를 재정의할 때 override 하는 함수이다.
*
* 자세한 내용은 JacksonAnnotationIntrospector#findSerializer() 의 구현을 살펴보도록 한다.
*
* 특정 프로퍼티에 대해 어떤 Serializer 를 사용할 것인지 결정하는 함수이다.
*
* 따라서 특정 조건에 따라 직렬화 처리에 사용할 Serializer 를 정의하고 싶다면 이 함수를 override 하면 된다.
*/
override fun findSerializer(a: Annotated): Any? {
val annotation = a.getAnnotation(Secret::class.java)
if (annotation != null) {
return SecretAnnotationSerializer(annotation.substituteValue)
}
return super.findSerializer(a) // 기존 JacksonAnnotationIntrospector 의 것을 사용한다.
}
}
class SecretAnnotationSerializer(private val substituteValue: String) : StdSerializer<String>(String::class.java) {
override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeString(substituteValue)
}
}
/**
* 직렬화될 프로퍼티를 수정하도록 하는 방법이다.
*
* BeanSerializerModifier#changeProperties() 를 재정의한 뒤,
*
* SimpleModule 을 이용해서 ObjectMapper 에 등록한다.
*
* 아래처럼 하면 직렬화 대상에서 완전히 제외된다(`@JsonIgnore`와 동일한 효과).
*
* @see com.fasterxml.jackson.databind.ser.BeanSerializerModifier
* @see com.fasterxml.jackson.databind.module.SimpleModule
*/
class SecretBeanSerializerModifier : BeanSerializerModifier() {
override fun changeProperties(
config: SerializationConfig,
beanDesc: BeanDescription,
beanProperties: MutableList<BeanPropertyWriter>
): MutableList<BeanPropertyWriter> {
SimpleModule()
return beanProperties
.filter { property -> property.getAnnotation(Secret::class.java) == null }
.toMutableList()
// return super.changeProperties(config, beanDesc, beanProperties)
}
}

View File

@@ -1,13 +0,0 @@
package com.banjjoknim.playground
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class JacksonApplicationTests {
@Test
fun contextLoads() {
}
}

View File

@@ -1,279 +0,0 @@
package com.banjjoknim.playground.jackson.jsonserialize
import com.banjjoknim.playground.jackson.common.Car
import com.banjjoknim.playground.jackson.common.CarUsingContextualSerializerWithNoSecretAnnotation
import com.banjjoknim.playground.jackson.common.CarUsingContextualSerializerWithSecretAnnotation
import com.banjjoknim.playground.jackson.common.CarUsingJsonSerializeAnnotationCarSerializer
import com.banjjoknim.playground.jackson.common.CarUsingNoAnnotation
import com.banjjoknim.playground.jackson.common.CarUsingSecretAnnotation
import com.banjjoknim.playground.jackson.common.Owner
import com.fasterxml.jackson.databind.AnnotationIntrospector
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
/**
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see com.fasterxml.jackson.databind.module.SimpleModule
* @see com.fasterxml.jackson.databind.ser.DefaultSerializerProvider
* @see com.fasterxml.jackson.databind.ser.BeanSerializer -> 객체를 직렬화할때 사용되는 Serializer
* @see com.fasterxml.jackson.core.json.WriterBasedJsonGenerator
* @see com.fasterxml.jackson.databind.ser.std.BeanSerializerBase
* @see com.fasterxml.jackson.databind.ser.BeanPropertyWriter
*/
class CarSerializersTest {
private lateinit var mapper: ObjectMapper
companion object {
private val owner = Owner("ban", 30)
private val car = Car("banjjoknim", 10_000_000, owner)
private val carUsingNoAnnotation = CarUsingNoAnnotation()
private val carUsingJsonSerializeAnnotationCarSerializer = CarUsingJsonSerializeAnnotationCarSerializer()
private val carUsingContextualSerializerWithSecretAnnotation =
CarUsingContextualSerializerWithSecretAnnotation()
private val carUsingContextualSerializerWithNoSecretAnnotation =
CarUsingContextualSerializerWithNoSecretAnnotation()
private val carUsingSecretAnnotation = CarUsingSecretAnnotation()
}
@BeforeEach
fun setup() {
mapper = ObjectMapper().registerKotlinModule()
}
@Test
fun `기본 ObjectMapper의 동작을 테스트한다`() {
// given
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"name":"banjjoknim","price":10000000,"owner":{"name":"ban","age":30}}""")
}
@DisplayName("등록된 커스텀 직렬화기의 동작을 테스트한다")
@Nested
inner class CarSerializerTestCases {
@Test
fun `자동차의 모든 필드만 직렬화한다`() {
// given
val module = SimpleModule()
module.addSerializer(Car::class.java, CarSerializer())
mapper.registerModule(module)
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"name":"banjjoknim","price":10000000}""")
}
@Test
fun `자동차의 이름만 직렬화한다`() {
// given
val module = SimpleModule()
module.addSerializer(Car::class.java, CarNameSerializer())
mapper.registerModule(module)
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"name":"banjjoknim"}""")
}
@Test
fun `자동차의 가격만 직렬화한다`() {
// given
val module = SimpleModule()
module.addSerializer(Car::class.java, CarPriceSerializer())
mapper.registerModule(module)
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"price":10000000}""")
}
@Test
fun `자동차의 이름과 오너의 모든 필드를 직렬화한다`() {
// given
val module = SimpleModule()
module.addSerializer(Car::class.java, CarNameOwnerSerializer())
mapper.registerModule(module)
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"name":"banjjoknim","owner":{"name":"ban","age":30}}""")
}
@Test
fun `자동차의 이름과 오너의 이름만 직렬화한다`() {
// given
val module = SimpleModule()
module.addSerializer(Car::class.java, CarNameOwnerNameSerializer())
mapper.registerModule(module)
// when
val result = mapper.writeValueAsString(car)
// then
assertThat(result).isEqualTo("""{"name":"banjjoknim","owner":{"name":"ban"}}""")
}
@Test
fun `아무 어노테이션도 적용하지 않고 직렬화한다`() {
// given
// when
val actual = mapper.writeValueAsString(carUsingNoAnnotation)
// then
assertThat(actual).isEqualTo("""{"name":"banjjoknim","secret":"secret","price":10000000,"owner":{"name":"ban","age":30}}""")
}
@Test
fun `@JsonSerialize 어노테이션을 적용하여 직렬화한다`() {
// given
// when
val actual = mapper.writeValueAsString(carUsingJsonSerializeAnnotationCarSerializer)
// then
assertThat(actual).isEqualTo("""{"name":"banjjoknim","secret":"****","price":10000000,"owner":{"name":"ban","age":30}}""")
}
@Test
fun `ContextualSerializer 를 상속 받은 구현체를 이용해서 직렬화한다`() {
// given
// when
val actualWithSecretAnnotation = mapper.writeValueAsString(carUsingContextualSerializerWithSecretAnnotation)
val actualWithNoSecretAnnotation =
mapper.writeValueAsString(carUsingContextualSerializerWithNoSecretAnnotation)
// then
assertThat(actualWithSecretAnnotation).isEqualTo("""{"name":"banjjoknim","secret":"hello world!!","price":10000000,"owner":{"name":"ban","age":30}}""")
assertThat(actualWithNoSecretAnnotation).isEqualTo("""{"name":"banjjoknim","secret":"this is default value in ContextualCarSerializer","price":10000000,"owner":{"name":"ban","age":30}}""")
}
@Test
fun `@Secret 어노테이션, AnnotationIntrospector 을 적용하여 직렬화한다`() {
// given
mapper.setAnnotationIntrospector(SecretAnnotationIntrospector())
// when
val actual = mapper.writeValueAsString(carUsingSecretAnnotation)
// then
assertThat(actual).isEqualTo("""{"name":"banjjoknim","secret":"****","price":10000000,"owner":{"name":"ban","age":30}}""")
}
@Test
fun `@Secret 어노테이션, BeanSerializerModifier 를 적용하여 직렬화한다`() {
// given
val module = object : SimpleModule() {
override fun setupModule(context: SetupContext) {
super.setupModule(context)
context.addBeanSerializerModifier(SecretBeanSerializerModifier())
}
}
mapper.registerModule(module)
// when
val actual = mapper.writeValueAsString(carUsingSecretAnnotation)
// then
assertThat(actual).isEqualTo("""{"name":"banjjoknim","price":10000000,"owner":{"name":"ban","age":30}}""")
}
/**
*
* Kotlin + Spring Boot 를 사용한다면 `com.fasterxml.jackson.module:jackson-module-kotlin` 의존성을 사용할 것이다.
*
* 이를 사용하면 기본 생성자 없이도 `@RequestBody` 에서 json 을 객체로 역직렬화 할 수 있다.
*
* `com.fasterxml.jackson.module:jackson-module-kotlin` 에서 이러한 역할을 해주는 것이 KotlinAnnotationIntrospector 이다.
*
* 하지만 새로운 AnnotationIntrospector 를 등록하면 KotlinAnnotationIntrospector 가 무시되어 기본생성자 없이는 `@RequestBody` 객체를 만들지 못하게 된다.
*
* 따라서 아래와 같이 기존의 AnnotationIntrospector 도 등록해주어야 한다.
*
* 이는 AnnotationIntrospector.Pair 도우미 클래스를 사용해서 할 수 있다.
*
* 이때, 순서대로 기본 Introspector, 보조 Introspector 로 등록된다.
*
* [AnnotationIntrospector](https://github.com/FasterXML/jackson-docs/wiki/AnnotationIntrospector)
*
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair
* @see com.fasterxml.jackson.databind.AnnotationIntrospector
* @see com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector
*/
@Test
fun `Kotlin + Spring Boot를 사용하면 기본적으로 3가지의 AnnotationIntrospector 가 ObjectMapper 에 존재한다`() {
// given
val originalAnnotationIntrospector = mapper.serializationConfig.annotationIntrospector
// when
val allIntrospectorNames = mapper.serializationConfig.annotationIntrospector.allIntrospectors()
.map { annotationIntrospector -> annotationIntrospector::class.simpleName }
// then
assertThat(originalAnnotationIntrospector.allIntrospectors()).hasSize(3)
assertThat(allIntrospectorNames[0]).isEqualTo("KotlinAnnotationIntrospector")
assertThat(allIntrospectorNames[1]).isEqualTo("JacksonAnnotationIntrospector")
assertThat(allIntrospectorNames[2]).isEqualTo("KotlinNamesAnnotationIntrospector")
}
@Test
fun `Kotlin + Spring Boot 를 사용할 시 ObjectMapper 에 새로운 AnnotationIntrospector 를 추가하면 KotlinAnnotationIntrospector 가 무시된다`() {
// given
val originalAnnotationIntrospector = mapper.serializationConfig.annotationIntrospector
// when
mapper.setAnnotationIntrospector(SecretAnnotationIntrospector())
val allIntrospectorNames = mapper.serializationConfig.annotationIntrospector.allIntrospectors()
.map { annotationIntrospector -> annotationIntrospector::class.simpleName }
// then
assertThat(originalAnnotationIntrospector.allIntrospectors()).hasSize(3)
assertThat(allIntrospectorNames).hasSize(1)
assertThat(allIntrospectorNames[0]).isEqualTo("SecretAnnotationIntrospector")
}
@Test
fun `Kotlin + Spring Boot 를 사용할 시 ObjectMapper 에 새로운 AnnotationIntrospector 를 추가할 때 Pair 로 추가하면 KotlinAnnotationIntrospector 가 무시되지 않는다`() {
// given
val originalAnnotationIntrospector = mapper.serializationConfig.annotationIntrospector
// when
mapper.setAnnotationIntrospector(
AnnotationIntrospector.pair(
SecretAnnotationIntrospector(),
originalAnnotationIntrospector
) // 내부 구현은 아래와 같다.
// AnnotationIntrospectorPair(SecretAnnotationIntrospector(), originalAnnotationIntrospector)
)
val allIntrospectorNames = mapper.serializationConfig.annotationIntrospector.allIntrospectors()
.map { annotationIntrospector -> annotationIntrospector::class.simpleName }
// then
assertThat(allIntrospectorNames).hasSize(4)
assertThat(allIntrospectorNames[0]).isEqualTo("SecretAnnotationIntrospector")
assertThat(allIntrospectorNames[1]).isEqualTo("KotlinAnnotationIntrospector")
assertThat(allIntrospectorNames[2]).isEqualTo("JacksonAnnotationIntrospector")
assertThat(allIntrospectorNames[3]).isEqualTo("KotlinNamesAnnotationIntrospector")
}
}
}

View File

@@ -1,37 +0,0 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/

View File

@@ -1,257 +0,0 @@
# 만들면서 배우는 클린 아키텍처
- [《만들면서 배우는 클린 아키텍처》 홈페이지](https://wikibook.co.kr/clean-architecture/)
- [《만들면서 배우는 클린 아키텍처》 예제 코드](https://github.com/wikibook/clean-architecture)
## 요구사항
- 회원은 닉네임을 가진다.
- 닉네임은 10글자 이내여야 한다.
- 닉네임은 공백이 아니어야 한다.
## 구현
- 책에서는 계좌 송금 예제를 사용했지만, 회원의 닉네임을 변경하는 예제로 좀 더 심플하게 구성했다.
- 책에서는 `Command` 등의 단어를 사용했지만, 본 예제 코드에서는 `Request`, `Response` 등의 단어로 대체했다.
- 책에서는 `adapter.out.persistence` 내부에 `Entity`가 포함되어 있었지만, 본 예제 코드에서는 `domain.entity` 패키지 내부로 옮겼다.
- 책에서는 `domain` 이라는 패키지 명을 사용했지만, 본 예제 코드에서는 `pojo` 라는 패키지 명으로 대체했다.
- 본 예제 코드에서는 어댑터와 애플리케이션의 완전한 격리를 위해 매핑 전략으로 `'완전' 매핑 전략`을 사용했다.
## 정리
### 계층형 아키텍처
- 데이터베이스 주도 설계를 유도함.
- 영속성 계층과 도메인 계층의 강한 결합이 발생함.
- 영속성 계층은 모든 것에 접근이 가능 -> 결국 모든 계층이 영속성 계층에 의존하게 됨.
- 어느 순간에는 테스트 코드를 작성하는 것보다 의존성을 파악하고 Mocking 하는 데 더 많은 시간이 걸리게 됨. -> 테스트하기 어려워짐.
- 넓은 서비스 문제 -> 특정 유스케이스를 찾는 것이 어려워짐. -> 고도로 특화된 좁은 도메인 서비스(유스케이스별로 분리된 각각의 서비스)로 해결.
> #### *지연되는 소프트웨어 프로젝트에 인력을 더하는 것은 개발을 늦출 뿐이다.*
>
> *<<맨머스 미신: 소프트웨어 공학에 관한 에세이>>, 프레데릭 P. 브룩스*
### 육각형 아키텍처(헥사고날 아키텍처)
- [알리스테어 콕번 - 헥사고날 아키텍처 블로그 원문](https://alistair.cockburn.us/hexagonal-architecture/)
![Hexagonal Architecture](images/Hexagonal Architecture.png)
![Hexagonal Architecture Process](images/Hexagonal%20Architecture%20Process.png)
- `포트(port)와 어댑터(adapter) 아키텍처`라고도 불린다.
- `포트(port)`는 인터페이스로, 계층간 경계를 지정하는 역할을 한다.
- `포트(port)`는 애플리케이션 서비스와 어댑터 사이의 간접적인 계층이다. 각 계층에 대한 직접적인 코드 의존성을 없애준다.
- `어댑터(adapter)`는 포트의 구현체이다. 어댑터는 애플리케이션 서비스를 호출하거나, 반대로 애플리케이션 서비스에 의해서 호출되기도 한다.
- `엔티티, 유스케이스, 인커밍/아웃고잉 포트, 인커밍/아웃고잉 어댑터`가 핵심이다.
### 단일 책임 원칙
> #### *하나의 컴포넌트는 오로지 한 가지 일만 해야 하고, 그것을 올바르게 수행해야 한다.*
>#### *컴포넌트를 변경하는 이유는 오직 하나뿐이어야 한다.*
- 책임 -> 변경할 이유
- 단일 책임 원칙 -> 단일 변경 이유 원칙(Single Reason to Change Principal)
### 의존성 역전 원칙
- 도메인 코드는 애플리케이션에서 가장 중요한 코드다. 따라서 의존성을 역전시켜 의존성으로부터 보호(격리)해야 한다.
> #### *코드상의 어떤 의존성이든 그 방향을 바꿀 수(역전시킬 수) 있다.*
- 사실 의존성의 양쪽 코드를 모두 제어할 수 있을 때만 의존성을 역전시킬 수 있다.
> #### *클린 아키텍처에서는 설계가 비즈니스 규칙의 테스트를 용이하게 하고, 비즈니스 규칙은 프레임워크, 데이터베이스, UI 기술, 그 밖의 외부 애플리케이션이나 인터페이스로부터 독립적일 수 있다.*
- 도메인 코드에는 바깥으로 향하는 어떤 의존성도 없어야 한다. 클린 아키텍처에서 모든 의존성은 도메인 로직을 향해 안쪽 방향으로 향해야 한다.
### 패키지 구조
- 본 프로젝트의 패키지 구조를 참고하도록 한다. 각각의 패키지는 핵심 요소들을 표현한다.
### 의존성 주입
- 모든 계층에 의존성을 가진 중립적인 컴포넌트를 하나 도입하여 사용한다. 그렇게 함으로써 핵심 컴포넌트들을 의존성으로부터 보호할 수 있다.
### 입력 유효성 검증
- 입력 모델에서 입력 유효성을 검증하여 애플리케이션 계층에는 온전한 입력만 받도록 한다.
- [Java Bean Validation API](https://beanvalidation.org)
### 유스케이스마다 다른 입력 모델
- 각 유스케이스 전용 입력 모델은 유스케이스를 훨씬 명확하게 만들고 다른 유스케이스와의 결합도 제거해서 불필요한 부수효과가 발생하지 않게 한다.
### 비즈니스 규칙 vs 입력 유효성
- 비즈니스 규칙을 검증하는 것은 도메인 모델의 현재 상태에 접근해야 하지만 입력 유효성은 그럴 필요가 없다.
- 입력 유효성을 검증하는 일은 @NotNull 애너테이션을 붙인 것처럼 선언적으로 구현할 수 있지만 비즈니스 규칙을 검증하는 일은 조금 더 맥락이 필요하다.
- 입력 유효성을 검증하는 것은 `구문상의(syntactical)` 유효성을 검증하는 것이라고 할 수 있다.
- 비즈니스 규칙은 유스케이스의 맥락 속에서 `의미적인(semantical)` 유효성을 검증하는 것이라고 할 수 있다.
### 비즈니스 규칙 검증 구현
- 가장 좋은 방법은 비즈니스 규칙을 도메인 엔티티 안에 넣는 것이다.
- 만약 도메인 엔티티에서 비즈니스 규칙을 검증하기가 어렵다면 유스케이스 코드에서 도메인 엔티티를 사용하기 전에 해도 된다.
### 풍부한 도메인 모델 vs. 빈약한 도메인 모델
- 풍부한 도메인 모델에서는 애플리케이션의 코어에 있는 엔티티에서 가능한 한 많은 도메인 로직이 구현된다. 엔티티들은 상태를 변경하는 메서드를 제공하고, 비즈니스 규칙에 맞는 유효한 변경만을 허용한다.
- 빈약한 도메인 모델에서는 엔티티 자체가 굉장히 얇다. 일반적으로 엔티티는 상태를 표현하는 필드와 이 값을 읽고 바꾸기 위한 getter, setter 메서드만 포함하고 어떤 도메인 로직도 갖고 있지 않다.
### 유스케이스마다 다른 출력 모델
- 출력도 가능하면 각 유스케이스에 맞게 구체적일수록 좋다. 출력은 호출자에게 꼭 필요한 데이터만 들고 있어야 한다.
- 유스케이스들 간에 같은 출력 모델을 공유하게 되면 유스케이스들도 강하게 결합된다. 공유 모델은 장기적으로 봤을 때 갖가지 이유로 점점 커지게 되어 있다.
- 단일 책임 원칙을 적용하고 모델을 분리해서 유지하는 것은 유스케이스의 결합을 제거하는 데 도움이 된다.
### 읽기 전용 유스케이스
- CQS(Command-Query Separation), CQRS(Command-Query Responsibility Segregation)
### 컨트롤러 나누기
- 웹 어댑터는 한 개 이상의 클래스로 구성해도 된다. 단, 클래스들이 같은 소속이라는 것을 표현하기 위해 같은 패키지 수준(hierarchy)에 놓아야 한다.
- 컨트롤러는 너무 적은 것보다는 너무 많은 게 낫다. 각 컨트롤러가 가능한 한 좁고 다른 컨트롤러와 가능한 한 적게 공유하는 웹 어댑터 조각을 구현해야 한다.
- 클래스마다 코드는 적을수록 좋다. 테스트 코드도 마찬가지다. 클래스가 작을수록 더 찾기가 쉽다.
- 모델을 공유하지 않는 여러 작은 클래스들을 만드는 것을 두려워해서는 안 된다. 작은 클래스들은 더 파악하기 쉽고, 더 테스트하기 쉬우며, 동시 작업을 지원한다.
### 인터페이스 나누기
- 하나의 인터페이스에 모든 연산을 모아두면 모든 서비스가 실제로는 필요하지 않은 메서드에 의존하게 된다.
> #### *필요없는 화물을 운반하는 무언가에 의존하고 있으면 예상하지 못했던 문제가 생길 수 있다.*
>
> *로버트 C. 마틴*
- `인터페이스 분리 원칙(Interface Segregation Principle, ISP)`
- 클라이언트가 오로지 자신이 필요로 하는 메서드만 알면 되도록 넓은 인터페이스를 특화된 인터페이스로 분리해야 한다.
### 영속성 어댑터 나누기
- 하나의 애그리거트당 하나의 영속성 어댑터를 만들어서 여러 개의 영속성 어댑터를 만들 수도 있다. 이렇게 하면 영속성 어댑터들은 각 영속성 기능을 이용하는 도메인 경계를 따라 자동으로 나눠진다.
- 영속성 어댑터를 훨씬 많은 클래스로 나눌 수도 있다.
### 영속성, 도메인 모델 타협
- 영속성 측면과의 타협 없이 풍부한 도메인 모델을 생성하고 싶다면 도메인 모델과 영속성 모델을 매핑하는 것이 좋다.
### 테스트 피라미드
- 비용이 많이 드는 테스트는 지양하고 비용이 적게 드는 테스트를 많이 만들어야 한다.
- 단위 테스트, 통합 테스트, 시스템 테스트
### 단위 테스트로 도메인 엔티티 테스트하기
- 도메인 엔티티의 행동은 다른 클래스에 거의 의존하지 않기 때문에 다른 종류의 테스트는 필요하지 않다.
### 단위 테스트로 유스케이스 테스트하기
- 테스트 중인 유스케이스 서비스는 상태가 없기(stateless) 때문에 테스트에서 상태를 검증할 수 없다.
- 대신 의존성의 상호작용을 테스트하기 때문에 통합 테스트에 가깝다. 하지만 목으로 작업하고 실제 의존성을 관리해야 하는 것은 아니기 때문에 완전한 통합 테스트에 비해서는 만들고 유지보수 하기 쉽다.
### 통합 테스트로 웹 어댑터 테스트하기
- `@WebMvcTest`
- 웹 어댑터 테스트는 통합 테스트를 적용하는 것이 합리적이다.
- 사실, 웹 컨트롤러는 스프링 프레임워크에 강하게 묶여 있기 때문에(스프링을 사용한다면) 격리된 상태로 테스트하기보다는 프레임워크와 통합된 상태로 테스트하는 것이 합리적이다.
- 만약 평범하게 단위 테스트로 테스트하면 모든 매핑, 유효성 검증, HTTP 항목에 대한 커버리지가 낮아지고, 프레임워크를 구성하는 요소들이 프로덕션 환경에서 정상적으로 작동할지 확신할 수 없게 된다.
### 통합 테스트로 영속성 어댑터 테스트하기
- `@DataJpaTest`
- 영속성 어댑터의 테스트 역시 단위 테스트보다는 통합 테스트를 적용하는 것이 합리적이다. 단순히 어댑터의 로직만 검증하는 것이 아니라 데이터베이스 매핑도 검증해야하기 때문이다.
- 영속성 어댑터 테스트는 실제 데이터베이스에서 문제가 생길 확률이 높으므로 실제 데이터베이스를 대상으로 진행해야 한다. -> `TestContainer`와 같은 라이브러리의 도움을 받을 수 있다.
### 시스템 테스트로 주요 경로 테스트하기
- 피라미드 최상단에 있는 시스템 테스트는 전체 애플리케이션을 띄우고 API를 통해 요청을 보내고, 모든 계층이 조화롭게 잘 동작하는지 검증한다.
- `TestRestTemplate` -> 실제 HTTP 통신 이용
### 테스트 커버리지
- 라인 커버리지(line coverage)는 테스트 성공을 측정하는 데 있어서는 잘못된 지표다.
- 코드의 중요한 부분이 전혀 커버되지 않을 수 있음.
- 테스트의 성공 기준 -> 얼마나 마음 편하게 소프트웨어를 배포할 수 있는가?
- 더 자주 배포할수록 테스트를 더 신뢰할 수 있음.
### 테스트 전략
- 도메인 엔티티를 구현할 때는 단위 테스트로 커버한다.
- 유스케이스를 구현할 때는 단위 테스트로 커버한다.
- 어댑터를 구현할 때는 통합 테스트로 커버한다.
- 사용자가 취할 수 있는 중요 애플리케이션 경로는 시스템 테스트로 커버한다.
> #### *리팩터링할 때마다 테스트 코드도 변경해야 한다면 테스트는 테스트로서의 가치를 잃는다.*
### '매핑하지 않기' 전략
- No Mapping Strategy
- 포트 인터페이스가 도메인 모델을 입출력 모델로 사용하면 두 계층 간의 매핑을 할 필요가 없다.
- 웹, 애플리케이션, 영속성 계층과 관련된 이유로 인해 변경되어야 함. -> 단일 책임 원칙 위배
- 그 결과, 오로지 한 계층에서만 필요한 필드들을 포함하는 파편화된 도메인 모델로 이어질 수 있다.
### '양방향' 매핑 전략
- Two-Way Mapping Strategy
- 각 어댑터가 전용 모델을 가지고 있어서 해당 모델을 도메인 모델로, 도메인 모델을 해당 모델로 매핑할 책임을 가지고 있다.
- 각 계층이 전용 모델을 가지고 있는 덕분에 각 계층이 전용 모델을 변경하더라도 내용이 변경되지 않는 한 다른 계층에는 영향이 없다.
### '완전' 매핑 전략
- Full Mapping Strategy
- 각 연산이 전용 모델을 필요로 한다. 따라서 웹 어댑터와 애플리케이션 계층 각각이 자신의 전용 모델을 각 연산을 실행하는 데 필요한 모델로 매핑한다.
- 각 연산마다 별도의 특화된 입출력 모델을 사용한다.
- 웹 계층과 애플리케이션 계층 사이에서 상태 변경 유스케이스의 경계를 명시할 때 유용하다.
- 애플리케이션 계층과 영속성 계층 사이에서는 매핑 오버헤드 때문에 사용하지 않는 것이 좋다.
### '단방향' 매핑 전략
- One-Way Mapping Strategy
- 동일한 '상태' 인터페이스를 구현하는 도메인 모델과 어댑터 모델을 이용하면 각 계층은 다른 계층으로부터 온 객체를 단방향으로 매핑하기만 하면 된다.
- 모든 계층의 모델들이 같은 인터페이스를 구현한다. 이 인터페이스는 관련 있는 특성(attribute)에 대한 getter 메서드를 제공해서 도메인 모델의 상태를 캡슐화 한다.
### 설정 컴포넌트
- 유스케이스는 인터페이스만 알아야 하고, 런타임에 이 인터페이스의 구현을 제공받아야 한다.
- 애플리케이션의 나머지 부분을 깔끔하게 유지하고 싶다면 아키텍처에 대해 중립적이고 인스턴스 생성을 위해 모든 클래스에 대한 의존성을 가지는 설정 컴포넌트(configuration component)가 필요하다.
- 스프링의 클래스패스 스캐닝 방식
- 스프링의 자바 컨피그 설정 방식
### 소프트웨어 아키텍처
> #### *소프트웨어 아키텍처는 아키텍처 요소 간의 의존성을 관리하는 게 전부다. 만약 의존성이 거대한 진흙 덩어리(big ball of mud)가 된다면 아키텍처 역시 거대한 진흙 덩어리가 된다.*
### 깨진 창문 이론
- 품질이 떨어진 코드에서 작업할 때 더 낮은 품질의 코드를 추가하기가 쉽다.
- 코딩 규칙을 많이 어긴 코드에서 작업할 때 또 다른 규칙을 어기기도 쉽다.
- 지름길을 많이 사용한 코드에서 작업할 때 또 다른 지름길을 추가하기도 쉽다.
### 위험한 지름길
- 유스케이스 간 모델 공유하기 -> 유스케이스 간에 입출력 모델을 공유하게 되면 유스케이스들 사이에 결합이 생긴다.
- 도메인 엔티티를 입출력 모델로 사용하기 -> 도메인 엔티티를 유스케이스의 입출력 모델로 사용하면 도메인 엔티티가 유스케이스에 결합된다.
- 인커밍 포트 건너뛰기 -> 인커밍 포트가 없으면 도메인 로직의 진입점이 불분명해진다. 인커밍 포트를 유지하면 아키텍처를 쉽게 강제할 수 있다.
- 애플리케이션 서비스 건너뛰기 -> 애플리케이션 서비스가 없으면 도메인 로직을 둘 곳이 없다.
### 육각형 아키텍처 스타일
> #### *외부의 영향을 받지 않고 도메인 코드를 자유롭게 발전시킬 수 있다는 것은 육각형 아키텍처 스타일이 내세우는 가장 중요한 가치다.*
- 영속성 관심사나 외부 시스템에 대한 의존성 등의 변화로부터 자유롭게 도메인 코드를 개발할 수 있다.
- 만약 도메인 코드가 애플리케이션에서 가장 중요한 것이 아니라면 이 아키텍처 스타일은 필요하지 않을 것이다.
## 참고자료
- [Best way to dynamically load adapters in hexagonal architecture?](https://stackoverflow.com/questions/50436649/best-way-to-dynamically-load-adapters-in-hexagonal-architecture)
- [hexagonal architecture with spring data](https://stackoverflow.com/questions/46509252/hexagonal-architecture-with-spring-data)
- [Ports-And-Adapters](https://www.dossier-andreas.net/software_architecture/ports_and_adapters.html)
- [Ports And Adapters Architecture](http://wiki.c2.com/?PortsAndAdaptersArchitecture)
- [Hexagonal architecture with Domain, Presenter & Entity segregation on Spring WebFlux](https://medium.com/javarevisited/hexagonal-architecture-with-domain-presenter-entity-segregation-on-spring-webflux-ef053a495bdc)
- [지속 가능한 소프트웨어 설계 패턴: 포트와 어댑터 아키텍처 적용하기](https://engineering.linecorp.com/ko/blog/port-and-adapter-architecture/)
- [JPA Week3 Entity Mapping / Hexagonal Architecture](https://www.slideshare.net/ssuser8f4c99/jpa-week3-entity-mapping-hexagonal-architecture-250068805)
- [Hexagonal Architecture Articles](https://jmgarridopaz.github.io/content/articles.html)
- [Domain-Driven Design and the Hexagonal Architecture](https://vaadin.com/learn/tutorials/ddd/ddd_and_hexagonal)
- [The Pattern: Ports and Adapters (Object Structural)](https://alistair.cockburn.us/hexagonal-architecture/)

View File

@@ -1,48 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java")
id("org.springframework.boot") version "2.6.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.0"
kotlin("plugin.spring") version "1.6.0"
kotlin("plugin.jpa") version "1.6.0"
}
group = "com.banjjoknim"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
// implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
runtimeOnly("mysql:mysql-connector-java")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
// testImplementation("org.springframework.security:spring-security-test")
testImplementation("io.mockk:mockk:1.12.3")
testImplementation("com.ninja-squad:springmockk:3.1.1")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}

View File

@@ -1,5 +0,0 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,234 +0,0 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@@ -1,89 +0,0 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -1 +0,0 @@
rootProject.name = "learn-with-making-clean-architecture"

View File

@@ -1,11 +0,0 @@
package com.banjjoknim.cleanarchitecture
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class LearnWithMakingCleanArchitectureApplication
fun main(args: Array<String>) {
runApplication<LearnWithMakingCleanArchitectureApplication>(*args)
}

View File

@@ -1,20 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.`in`.web
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameRequestData
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Size
data class ChangeNicknameRequest(
val userId: Long,
@field:NotBlank
@field:Size(max = NICKNAME_LENGTH_LIMIT)
val newNickname: String
) {
fun toData(): ChangeNicknameRequestData {
return ChangeNicknameRequestData(userId, newNickname)
}
companion object {
private const val NICKNAME_LENGTH_LIMIT = 10
}
}

View File

@@ -1,3 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.`in`.web
data class ChangeNicknameResponse(val userId: Long)

View File

@@ -1,21 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.`in`.web
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameUseCase
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid
@RequestMapping("/users")
@RestController
class ChangeNicknameWebAdapter(
private val changeNicknameWebPort: ChangeNicknameUseCase
) {
@PostMapping("")
fun changeNickname(@RequestBody @Valid changeNicknameRequest: ChangeNicknameRequest): ChangeNicknameResponse {
val requestData = changeNicknameRequest.toData()
val responseData = changeNicknameWebPort.changeNickname(requestData)
return ChangeNicknameResponse(responseData.userId)
}
}

View File

@@ -1,18 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.application.port.out.LoadUserPersistencePort
import com.banjjoknim.cleanarchitecture.user.pojo.User
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
@Component
class LoadUserPersistenceAdapter(
private val userMapper: UserMapper,
private val userEntityRepository: UserEntityRepository
) : LoadUserPersistencePort {
override fun loadUser(userId: Long): User {
val userEntity = userEntityRepository.findByIdOrNull(userId)
?: throw NoSuchElementException("존재하지 않는 회원입니다. userId: $userId")
return userMapper.mapToDomainModel(userEntity)
}
}

View File

@@ -1,10 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import javax.persistence.Column
import javax.persistence.Embeddable
@Embeddable
data class NicknameColumn(
@Column(name = "nickname")
val value: String
)

View File

@@ -1,16 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.application.port.out.UpsertUserPersistencePort
import com.banjjoknim.cleanarchitecture.user.pojo.User
import org.springframework.stereotype.Component
@Component
class UpsertUserPersistenceAdapter(
private val userMapper: UserMapper,
private val userEntityRepository: UserEntityRepository
) : UpsertUserPersistencePort {
override fun upsertUser(user: User) {
val userEntity = userMapper.mapToDomainEntity(user)
userEntityRepository.save(userEntity)
}
}

View File

@@ -1,20 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import javax.persistence.Embedded
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
/**
* 패키지를 독립적 배포의 관점에서 바라봤을 때, UserEntity 는 Persistence 영역에 존재하는 것이 타당하다.
*
* 만약 그렇지 않다면 UserEntity 가 존재하지 않는 UserEntityRepository 가 배포될 것이기 때문이다.
*/
@Table(name = "User")
@Entity
class UserEntity(
@Id
val id: Long = 0L,
@Embedded
var nickname: NicknameColumn
)

View File

@@ -1,6 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import org.springframework.data.jpa.repository.JpaRepository
interface UserEntityRepository: JpaRepository<UserEntity, Long> {
}

View File

@@ -1,16 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.pojo.Nickname
import com.banjjoknim.cleanarchitecture.user.pojo.User
import org.springframework.stereotype.Component
@Component
class UserMapper {
fun mapToDomainEntity(user: User): UserEntity {
return UserEntity(user.id, NicknameColumn(user.nickname.value))
}
fun mapToDomainModel(userEntity: UserEntity): User {
return User(userEntity.id, Nickname(userEntity.nickname.value))
}
}

View File

@@ -1,3 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.port.`in`
data class ChangeNicknameRequestData(val userId: Long, val newNickname: String)

View File

@@ -1,3 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.port.`in`
data class ChangeNicknameResponseData(val userId: Long)

View File

@@ -1,5 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.port.`in`
interface ChangeNicknameUseCase {
fun changeNickname(data: ChangeNicknameRequestData): ChangeNicknameResponseData
}

View File

@@ -1,7 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.port.out
import com.banjjoknim.cleanarchitecture.user.pojo.User
interface LoadUserPersistencePort {
fun loadUser(userId: Long): User
}

View File

@@ -1,7 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.port.out
import com.banjjoknim.cleanarchitecture.user.pojo.User
interface UpsertUserPersistencePort {
fun upsertUser(user: User)
}

View File

@@ -1,23 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.service
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameRequestData
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameResponseData
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameUseCase
import com.banjjoknim.cleanarchitecture.user.application.port.out.LoadUserPersistencePort
import com.banjjoknim.cleanarchitecture.user.application.port.out.UpsertUserPersistencePort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Transactional
@Service
class ChangeNicknameService(
private val loadUserPersistencePort: LoadUserPersistencePort,
private val upsertUserPersistencePort: UpsertUserPersistencePort
) : ChangeNicknameUseCase {
override fun changeNickname(data: ChangeNicknameRequestData): ChangeNicknameResponseData {
val user = loadUserPersistencePort.loadUser(data.userId)
user.changeNickname(data.newNickname)
upsertUserPersistencePort.upsertUser(user)
return ChangeNicknameResponseData(user.id)
}
}

View File

@@ -1,3 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.pojo
data class Nickname(val value: String)

View File

@@ -1,10 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.pojo
class User(
var id: Long = 0L,
var nickname: Nickname
) {
fun changeNickname(newNickname: String) {
this.nickname = Nickname(newNickname)
}
}

View File

@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb;MODE=MySQL
username: sa
password:
driver-class-name: org.h2.Driver
h2:
console:
enabled: true

View File

@@ -1,9 +0,0 @@
<html>
<header>
<meta lang="ko" charset="UTF-8">
<title>만들면서 배우는 클린 아키텍처</title>
</header>
<body>
<h1>만들면서 배우는 클린 아키텍처</h1>
</body>
</html>

View File

@@ -1,13 +0,0 @@
package com.banjjoknim.cleanarchitecture
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class LearnWithMakingCleanArchitectureApplicationTests {
@Test
fun contextLoads() {
}
}

View File

@@ -1,72 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.`in`.web
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameResponseData
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameUseCase
import com.fasterxml.jackson.databind.ObjectMapper
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.post
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.filter.CharacterEncodingFilter
@WebMvcTest(ChangeNicknameWebAdapter::class)
class ChangeNicknameWebAdapterTest {
@Autowired
private lateinit var mockmvc: MockMvc
@Autowired
private lateinit var objectMapper: ObjectMapper
@MockkBean
private lateinit var changeNicknameUseCase: ChangeNicknameUseCase
@BeforeEach
fun setUp(webApplicationContext: WebApplicationContext) {
mockmvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter<DefaultMockMvcBuilder>(CharacterEncodingFilter("UTF-8"))
.alwaysDo<DefaultMockMvcBuilder>(MockMvcResultHandlers.print())
.build()
}
@DisplayName("닉네임 변경 테스트 케이스")
@Nested
inner class ChangeNicknameTestCases {
@Test
fun `닉네임을 변경한다`() {
every { changeNicknameUseCase.changeNickname(any()) } returns ChangeNicknameResponseData(1L)
val request = ChangeNicknameRequest(1L, "banjjoknim")
mockmvc.post("/users") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(request)
}.andExpect {
content { json("""{"userId":1}""") }
status { isOk() }
}
}
@Test
fun `잘못된 닉네임 변경 요청에 BadRequest 응답을 반환한다`() {
val request = ChangeNicknameRequest(1L, "banjjoknim!!")
mockmvc.post("/users") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(request)
}.andExpect {
status { isBadRequest() }
}
}
}
}

View File

@@ -1,43 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.pojo.Nickname
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.context.annotation.Import
import org.springframework.test.context.jdbc.Sql
@DataJpaTest
@Import(value = [UserMapper::class, LoadUserPersistenceAdapter::class])
class LoadUserPersistenceAdapterTest {
@Autowired
private lateinit var loadUserPersistenceAdapter: LoadUserPersistenceAdapter
@DisplayName("회원 조회 테스트 케이스")
@Nested
inner class LoadUserTestCases {
@Sql(statements = ["INSERT INTO USER VALUES (1, 'old')"])
@Test
fun `회원이 존재하지 않을 경우 예외가 발생한다`() {
assertThrows<NoSuchElementException>("존재하지 않는 회원입니다. userId: 100") {
loadUserPersistenceAdapter.loadUser(
100L
)
}
}
@Sql(statements = ["INSERT INTO USER VALUES (1, 'old')"])
@Test
fun `회원이 존재할 경우 회원을 조회할 수 있다`() {
val user = loadUserPersistenceAdapter.loadUser(1L)
Assertions.assertThat(user.id).isEqualTo(1)
Assertions.assertThat(user.nickname).isEqualTo(Nickname("old"))
}
}
}

View File

@@ -1,39 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.pojo.Nickname
import com.banjjoknim.cleanarchitecture.user.pojo.User
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.context.annotation.Import
import org.springframework.data.repository.findByIdOrNull
import org.springframework.test.context.jdbc.Sql
@DataJpaTest
@Import(value = [UserMapper::class, UpsertUserPersistenceAdapter::class])
class UpsertUserPersistenceAdapterTest {
@Autowired
private lateinit var upsertUserPersistenceAdapter: UpsertUserPersistenceAdapter
@Autowired
private lateinit var userEntityRepository: UserEntityRepository
@DisplayName("회원 상태 저장 및 수정 테스트 케이스")
@Nested
inner class UpsertUserTestCases {
@Sql(statements = ["INSERT INTO USER VALUES (1, 'old')"])
@Test
fun `회원 상태를 저장하거나 수정한다`() {
val updatedUser = User(1L, Nickname("new"))
upsertUserPersistenceAdapter.upsertUser(updatedUser)
val userEntity = userEntityRepository.findByIdOrNull(1L)
assertThat(userEntity?.nickname).isEqualTo(NicknameColumn("new"))
}
}
}

View File

@@ -1,41 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.adapter.out.persistence
import com.banjjoknim.cleanarchitecture.user.pojo.Nickname
import com.banjjoknim.cleanarchitecture.user.pojo.User
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest(classes = [UserMapper::class])
class UserMapperTest {
@Autowired
private lateinit var userMapper: UserMapper
@DisplayName("User POJO <-> User Entity 매핑 테스트 케이스")
@Nested
inner class UserMapperTestCases {
@Test
fun `User POJO 를 User Entity 로 변환한다`() {
val user = User(1L, Nickname("banjjoknim"))
val userEntity = userMapper.mapToDomainEntity(user)
assertThat(userEntity.id).isEqualTo(1)
assertThat(userEntity.nickname).isEqualTo(NicknameColumn("banjjoknim"))
}
@Test
fun `User Entity 를 User POJO 로 변환한다`() {
val userEntity = UserEntity(1L, NicknameColumn("banjjoknim"))
val user = userMapper.mapToDomainModel(userEntity)
assertThat(user.id).isEqualTo(1)
assertThat(user.nickname).isEqualTo(Nickname("banjjoknim"))
}
}
}

View File

@@ -1,46 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.application.service
import com.banjjoknim.cleanarchitecture.user.application.port.`in`.ChangeNicknameRequestData
import com.banjjoknim.cleanarchitecture.user.application.port.out.LoadUserPersistencePort
import com.banjjoknim.cleanarchitecture.user.application.port.out.UpsertUserPersistencePort
import com.banjjoknim.cleanarchitecture.user.pojo.Nickname
import com.banjjoknim.cleanarchitecture.user.pojo.User
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class ChangeNicknameServiceTest {
private val loadUserPersistencePort = mockk<LoadUserPersistencePort>()
private val upsertUserPersistencePort = mockk<UpsertUserPersistencePort>()
private val changeNicknameService = ChangeNicknameService(loadUserPersistencePort, upsertUserPersistencePort)
@DisplayName("닉네임 변경 테스트 케이스")
@Nested
inner class ChangeNicknameTestCases {
private lateinit var testUser: User
private val testChangeNicknameRequestData = ChangeNicknameRequestData(1L, "new")
@BeforeEach
fun setUp() {
testUser = User(1L, Nickname("old"))
}
@Test
fun `닉네임을 변경한다`() {
every { loadUserPersistencePort.loadUser(any()) } returns testUser
every { upsertUserPersistencePort.upsertUser(any()) } just Runs
changeNicknameService.changeNickname(testChangeNicknameRequestData)
assertThat(testUser.nickname).isEqualTo(Nickname("new"))
}
}
}

View File

@@ -1,26 +0,0 @@
package com.banjjoknim.cleanarchitecture.user.pojo
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class UserTest {
@DisplayName("회원 닉네임 변경 테스트 케이스")
@Nested
inner class ChangeNicknameTestCases {
@Test
fun `회원 닉네임을 변경한다`() {
// given
val user = User(nickname = Nickname("banjjoknim"))
// when
user.changeNickname("colt")
// then
assertThat(user.nickname).isEqualTo(Nickname("colt"))
}
}
}

View File

@@ -20,6 +20,10 @@ repositories {
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
// OAuth2 로그인을 위해 추가. spring-boot-starter-security 의존성이 있어도 기본적으로 추가되지 않기 때문.
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

View File

@@ -0,0 +1,21 @@
package com.banjjoknim.playground.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
// @EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeHttpRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").hasAnyRole("MANAGER", "ADMIN")
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login");
}
}

View File

@@ -1,8 +1,7 @@
package com.banjjoknim.playground.daooauth.config.security
package com.banjjoknim.playground.config.security
import com.banjjoknim.playground.daooauth.domain.auth.OAuth2Type
import com.banjjoknim.playground.daooauth.domain.user.User
import com.banjjoknim.playground.daooauth.domain.user.UserRepository
import com.banjjoknim.playground.domain.user.User
import com.banjjoknim.playground.domain.user.UserRepository
import org.springframework.context.annotation.Bean
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
@@ -30,29 +29,6 @@ import org.springframework.stereotype.Service
* 구글과 페이스북 측에서 우리에게 보내는 Request에 액세스 토큰과 사용자 정보등의 OAUth2 정보가 모두 포함되어 있다.
*
* 하지만 네이버, 카카오는 스프링 부트에서 기본적인 정보를 제공하지 않기 때문에 따로 해당 정보를 제공하는 클래스를 작성해야 한다.
*
* 우리는 OAuth2-Client 라는 라이브러리를 사용하고 있다.
*
* OAuth2-Client 라이브러리는 구글, 페이스북, 깃허브 등의 Provider를 기본적으로 제공해주지만, 네이버 카카오는 제공해주지 않는다.
*
* 이는 나라별로 OAuth2 지원해주는 서드 파티가 제공하는 attribute 모두 다르기 때문이다. 그래서 현실적으로 모든 곳을 지원해줄 수가 없다.
*
* OAuth2-Client OAuth2ClientProperties 라는 클래스를 통한 자동 설정을 지원해주고 있다.
*
* OAuth2는 여러가지 방식이 있다. Authorization Code Grant Type 방식 등등..
*
* `@EnableGlobalMethodSecurity` 어노테이션을 사용하면 스프링 시큐리티 관련 특정 어노테이션에 대한 활성화 설정을 있다.
* [spring-security-method-security](https://www.baeldung.com/spring-security-method-security) 참고.
*
* @see org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties
* @see org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter
* @see org.springframework.security.config.oauth2.client.CommonOAuth2Provider
* @see org.springframework.security.access.prepost.PreAuthorize
* @see org.springframework.security.access.prepost.PostAuthorize
* @see org.springframework.security.access.prepost.PreFilter
* @see org.springframework.security.access.prepost.PostFilter
* @see org.springframework.security.access.annotation.Secured
* @see javax.annotation.security.RolesAllowed
*/
@EnableWebSecurity // 스프링 시큐리티 필터가 스프링 필터체인에 등록되도록 해준다.
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) // 스프링 시큐리티 관련 특정 어노테이션에 대한 활성화 설정을 할 수 있다.
@@ -156,7 +132,7 @@ class PrincipalDetails(
}
// 해당 User 의 권한을 반환하는 함수
override fun getAuthorities(): Collection<GrantedAuthority> {
override fun getAuthorities(): Collection<out GrantedAuthority> {
return listOf(GrantedAuthority { user.role })
}
@@ -195,8 +171,8 @@ class PrincipalDetails(
* 이때 UserDetailsService 타입으로 등록되어 있는 빈을 찾아서 해당 빈에 정의된 loadUserByUsername() 실행한다.
* ```
*
* @see org.springframework.security.authentication.dao.DaoAuthenticationProvider
* @see org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider
* @see DaoAuthenticationProvider
* @see AbstractUserDetailsAuthenticationProvider
*/
@Service
class PrincipalDetailService(private val userRepository: UserRepository) : UserDetailsService {
@@ -223,9 +199,9 @@ class PrincipalDetailService(private val userRepository: UserRepository) : UserD
* OAuth2UserRequest 정보를 이용해서 loadUser 함수 호출 -> 구글로부터 회원프로필을 받아준다.
* ```
*
* @see org.springframework.security.oauth2.client.userinfo.OAuth2UserService
* @see org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
* @see org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest
* @see OAuth2UserService
* @see DefaultOAuth2UserService
* @see OAuth2UserRequest
*/
@Service
class PrincipalOAuth2UserService(
@@ -248,15 +224,12 @@ class PrincipalOAuth2UserService(
println("${userRequest.additionalParameters}") // 5.1 버전 이후일 경우.
// 강제로 회원가입 진행
val oAuth2Type = OAuth2Type.findByProvider(userRequest.clientRegistration.registrationId)
val oAuth2User = super.loadUser(userRequest)
val oAuth2UserInfo = oAuth2Type.createOAuth2UserInfo(oAuth2User.attributes)
val provider = oAuth2UserInfo.getProvider() // 값의 유무로 일반 로그인, OAuth2 로그인을 구분한다.
val providerId = oAuth2UserInfo.getProviderId()
val provider = userRequest.clientRegistration.clientId // google
val providerId = oAuth2User.attributes["sub"] // googleId(PK)
val username = "${provider}_${providerId}" // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
val password = passwordEncoder.encode("비밀번호") // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
val email = oAuth2UserInfo.getEmail()
val email = oAuth2User.attributes["email"]
val role = "ROLE_USER"
// 회원가입 여부 확인 및 저장
@@ -265,10 +238,10 @@ class PrincipalOAuth2UserService(
user = User(
username = username,
password = password,
email = email,
email = email as String,
role = role,
provider = provider,
providerId = providerId
providerId = providerId as String
)
userRepository.save(user) // 회원정보 저장

View File

@@ -1,4 +1,4 @@
package com.banjjoknim.playground.daooauth.domain.user
package com.banjjoknim.playground.domain.user
import javax.persistence.Entity
import javax.persistence.GeneratedValue

View File

@@ -1,7 +1,7 @@
package com.banjjoknim.playground.daooauth.domain.user
package com.banjjoknim.playground.domain.user
import com.banjjoknim.playground.daooauth.config.security.PrincipalDetails
import com.banjjoknim.playground.daooauth.config.security.PrincipalOAuth2UserService
import com.banjjoknim.playground.config.security.PrincipalDetails
import com.banjjoknim.playground.config.security.PrincipalOAuth2UserService
import org.springframework.security.core.Authentication
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.userdetails.UserDetails
@@ -64,13 +64,16 @@ class UserController {
* @see PrincipalDetailsService
*
*/
@GetMapping("/login") // OAuth2 로그인 및 일반 로그인 모두 principalDetails 로 세션 정보를 얻어올 수 있다(다운 캐스팅을 하지 않아도 된다!).
@GetMapping("/login") // OAuth2 로그인 및 일반 로그인 모두 principalDetails 로 세션 정보를 얻어올 수 있다.
fun login(@AuthenticationPrincipal principalDetails: PrincipalDetails) { // DI(의존성 주입)
println("principalDetailsUser : ${principalDetails.user}")
}
@GetMapping("/test/login")
fun testLogin(authentication: Authentication, @AuthenticationPrincipal userDetails: UserDetails) { // DI(의존성 주입)
fun testLogin(
authentication: Authentication,
@AuthenticationPrincipal userDetails: UserDetails
) { // DI(의존성 주입)
val principalDetailsFromAuthentication = authentication.principal as PrincipalDetails // 다운 캐스팅
println("principalDetailsFromAuthentication : ${principalDetailsFromAuthentication.user}")
println("principalDetailsFromAuthentication : ${principalDetailsFromAuthentication.username}")
@@ -80,7 +83,10 @@ class UserController {
}
@GetMapping("/test/oauth2/login")
fun testOAuth2Login(authentication: Authentication, @AuthenticationPrincipal oauth: OAuth2User) { // DI(의존성 주입)
fun testOAuth2Login(
authentication: Authentication,
@AuthenticationPrincipal oauth: OAuth2User
) { // DI(의존성 주입)
val oAuth2User = authentication.principal as OAuth2User // 다운 캐스팅
println("authentication : ${oAuth2User.attributes}") // OAuth2Service 의 super.loadUser(userRequest).attributes 와 같다.
println("oAuth2User : ${oauth.attributes}")

View File

@@ -3,4 +3,5 @@ package com.banjjoknim.playground.domain.user
import org.springframework.data.jpa.repository.JpaRepository
interface UserRepository : JpaRepository<User, Long> {
fun findByUsername(username: String): User?
}

View File

@@ -0,0 +1,11 @@
spring:
security:
oauth2:
client:
registration:
google:
client-id: my-google-client-id
client-secret: my-google-client-secret
facebook:
client-id: my-facebook-client-id
client-secret: my-facebook-client-secret

View File

@@ -1,37 +0,0 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/

View File

@@ -1,39 +0,0 @@
### 🚀 step1
- 현재 코드를 분석 하면서 어떤 점을 고치고 싶은지 니즈를 파악해본다.
### 🚀 step2
- 상속 기반의 이벤트로 변경 해본다.
- 이벤트 객체를 만들기 위해 ApplicationEvent 를 상속 받아 데이터 전달을 위해 객체를 만들어본다.
- 이벤트 객체로 부터 전달 받을 EventListener 구현체를 만들어본다.
- ApplicationEventPublisher 를 주입 받아서 이벤트를 발행 시킨다.
- 테스트 코드 작성
> Mockito 활용
### 📖 살펴볼 객체
- `AbstractApplicationContext` 를 intellij 의 diagram 을 통해서 구조를 살펴보면 `ApplicationEventPublisher` 를 상속 받고 있습니다.
- `AbstractApplicationContext`::publishEvent 메소드를 보면 이벤트의 발행이 어떻게 되는지 알 수 있습니다.
> 조금 설명을 드리면 위의 메소드 안에서 ApplicationEventMulticaster 를 주입 받아 이벤트를 실행 시키는데 구현체 중 `SimpleApplicationEventMulticater` 를 살펴보면 (위와 같이 다이어그램으로 표현) multicastEvent 메소드를 통해서 빈으로 등록된 이벤트 객체를 실행을 합니다
### 🚀 step3
- 추가 기능(어드민에서 쿠폰 및 환영 메시지를 보낼 수 있는 기능이 있다.)
- `@EventListener` 어노테이션을 활용해 코드를 변경 해보자.
- > 스프링 4.2 부터 ApplicationEvent 를 상속 받아 객체를 생성하지 않아도 이벤트 객체 처럼 활용할 수 있다. `ApplicationListenerMethodAdapter` 객체에서 어노테이션을 찾아서 실행
- 어드민에 알람을 보내는 중에 예외처리를 해보자
- > `@TransactionalEventListener` 를 활용해 트랜잭션 단위를 제어해보자.
- 비동기 활용
- > `@EnableAsync``@Async` 를 활용해 비동기 처리를 해보자.
### 🚀 step4
- Domain Event
- > AbstractAggregateRoot 를 상속 받아 이벤트를 제공
- > registerEvent 를 통해 jpa의 save 를 명시적으로 할 때 마다 이벤트를 발행
---
#### 출처 : https://github.com/tongnamuu/SpringEvent

View File

@@ -1,24 +0,0 @@
# 프로젝트 세팅 #
Java 11을 사용합니다.
처음부터 세팅하고 싶다면
![img.png](images/img.png)
이후 annotation processing 옵션을 켜주세요
![img_1.png](images/img_1.png)
이후 프로젝트 repository의 커밋을 따라오시면 그대로 실습하실 수 있습니다.
# 우리가 실습해볼 것 #
- 회원 가입 api를 이벤트를 활용해 개선해 봅니다.
- 회원가입 플로우는 아래와 같습니다.
- 회원가입을 하면 유저를 저장합니다
- 알람이 나갑니다
- 쿠폰을 발급해 줍니다
- 이메일과 SMS로 환영 메시지를 보냅니다
---
#### 출처 : https://github.com/tongnamuu/SpringEvent

View File

@@ -1,46 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java")
id("org.springframework.boot") version "2.6.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.0"
kotlin("plugin.spring") version "1.6.0"
kotlin("plugin.jpa") version "1.6.0"
}
group = "com.banjjoknim"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
runtimeOnly("mysql:mysql-connector-java")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.named("jar") {
enabled = false
}

View File

@@ -1,5 +0,0 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,234 +0,0 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@@ -1,89 +0,0 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

View File

@@ -1 +0,0 @@
rootProject.name = "springEvent"

View File

@@ -1,11 +0,0 @@
package com.banjjoknim.playground
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class SpringEventApplication
fun main(args: Array<String>) {
runApplication<SpringEventApplication>(*args)
}

View File

@@ -1,41 +0,0 @@
package com.banjjoknim.playground.common
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
@RestControllerAdvice
class ExceptionHandler : ResponseEntityExceptionHandler() {
override fun handleMethodArgumentNotValid(
ex: MethodArgumentNotValidException,
headers: HttpHeaders,
status: HttpStatus,
request: WebRequest
): ResponseEntity<Any> {
logger.error("message", ex)
val errors = ex.fieldErrors.map { fieldError ->
mapOf(
("propertyName" to fieldError.field),
("reason" to fieldError.defaultMessage)
)
}
return ResponseEntity.badRequest().body(errors)
}
@ExceptionHandler(value = [NoSuchElementException::class])
fun handleNoSuchElement(ex: NoSuchElementException): ResponseEntity<String> {
logger.error("message", ex)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.message)
}
@ExceptionHandler(value = [Exception::class])
fun handleException(ex: Exception): ResponseEntity<String> {
logger.error("message", ex)
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.message)
}
}

View File

@@ -1,19 +0,0 @@
package com.banjjoknim.playground.config
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableAsync
/**
* ```
* @EnableAsync 어노테이션을 이용해서 비동기 설정을 활성화할 수 있다.
*
* 비동기 설정을 활성화했다면, 비동기로 실행할 이벤트 리스너에 @Async 어노테이션을 붙이면 된다.
* ```
*
* @see EnableAsync
* @see Async
*/
@EnableAsync
@Configuration
class AsyncConfiguration {
}

View File

@@ -1,13 +0,0 @@
package com.banjjoknim.playground.domain.admin
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class AdminService {
private val log = LoggerFactory.getLogger(this::class.java)
fun alarm(username: String) {
log.info("어드민 서비스 : {}님이 가입했습니다.", username)
}
}

View File

@@ -1,13 +0,0 @@
package com.banjjoknim.playground.domain.admin
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class CouponService {
private val log = LoggerFactory.getLogger(this::class.java)
fun registerCoupon(email: String) {
log.info("쿠폰 등록 완료 : {}", email)
}
}

View File

@@ -1,30 +0,0 @@
package com.banjjoknim.playground.domain.user
import org.springframework.data.domain.AbstractAggregateRoot
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
/**
* ```
* AbstractAggregateRoot<T> 를 이용하면 쉽게 이벤트를 구현할 수 있다.
*
* 단, 명시적으로 AggregateRootRepository<T, ID> 에서 save()가 호출되어야 이벤트가 발행된다.
* ```
* @see AbstractAggregateRoot
*/
@Entity
class AggregateRootUser(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L
) : AbstractAggregateRoot<AggregateRootUser>() {
// AggregateRootUser Entity를 사용하는 AggregateRootUserRepository 에서 명시적으로 save() 가 호출되면 이벤트가 발행된다.
fun publishAggregateRootEvent() {
super.registerEvent(AggregateRootEvent("colt"))
}
data class AggregateRootEvent(val name: String)
}

View File

@@ -1,164 +0,0 @@
package com.banjjoknim.playground.domain.event
import org.slf4j.LoggerFactory
import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener
/**
* ```
* 기존에는 이벤트 리스너를 사용하기 위해서 ApplicationListener<T> 를 상속(또는 구현)해서 사용해야 했기에, 이벤트가 상속에 종속적이 되어 문제가 될 수 있었다.
*
* 하지만 스프링 4.2부터는 이벤트 리스너의 이벤트 처리를 어노테이션 기반으로 작성할 수 있도록 개선되었다.
*
* ApplicationListenerMethodAdapter 객체가 @EventListener 어노테이션을 찾아서 실행시켜준다.
*
* 빈 단위로 정의된 리스너(ApplicationListener<T> 를 상속하고 Component 로 등록된)에 대해서는 구현체로 SimpleApplicationEventMulticaster 를 사용한다. SimpleApplicationEventMulticaster 는 빈 단위로 정의된 작업을 실행시켜준다.
*
* 리스너는 메소드 단위로도 정의할 수 있다. 메소드에 @EventListener 어노테이션을 붙이면 이때는 구현체로 ApplicationListenerMethodAdapter 를 사용한다(구현체가 변경된다).
* ```
*
* @see AbstractApplicationContext
* @see ApplicationListenerMethodAdapter
* @see SimpleApplicationEventMulticaster
* @see ApplicationListenerMethodAdapter
* @see EventListener(org.springframework.context.event)
*/
@Component
class AdminAnnotationEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun onApplicationEvent(event: AdminAnnotationEvent) {
log.info("어드민 서비스 : {}님이 가입했습니다.", event.username)
}
}
@Component
class CouponAnnotationEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun onApplicationEvent(event: CouponAnnotationEvent) {
log.info("쿠폰 등록 완료 : {}", event.email)
}
}
@Component
class SenderAnnotationEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun handleEmail(event: SenderAnnotationEvent) {
log.info("환영 이메일 발송 성공 : {}", event.email)
}
@EventListener
fun handleSMS(event: SenderAnnotationEvent) {
log.info("환영 SMS 발송 성공 : {}", event.phoneNumber)
}
}
/**
* 트랜잭션 단위를 이벤트에서 관리(제어)하기 위해서 @TransactionalEventListener 를 사용할 수 있다.
*
* 기본 설정은 TransactionPhase.AFTER_COMMIT 이다. 이 외에도 여러 상태가 있으니 참고하여 설계하는데 사용하도록 하자.
*
* @see TransactionalEventListener
*/
@Component
class AdminTransactionalEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun onApplicationEvent(event: AdminTransactionalEvent) {
throw RuntimeException()
// log.info("어드민 서비스 : {}님이 가입했습니다.", event.username)
}
}
@Component
class CouponTransactionalEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
/**
* 아래의 @TransactionalEventListener 로 인해
*
* 트랜잭션 커밋이 정상적으로 성공한 이후에 이벤트가 실행(처리)된다.
*/
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
fun onApplicationEvent(event: CouponTransactionalEvent) {
log.info("쿠폰 등록 완료 : {}", event.email)
}
}
@Component
class SenderTransactionalEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun handleEmail(event: SenderTransactionalEvent) {
log.info("환영 이메일 발송 성공 : {}", event.email)
}
@EventListener
fun handleSMS(event: SenderTransactionalEvent) {
log.info("환영 SMS 발송 성공 : {}", event.phoneNumber)
}
}
@Component
class AdminAsyncEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun onApplicationEvent(event: AdminAsyncEvent) {
log.info("어드민 서비스 : {}님이 가입했습니다.", event.username)
}
}
@Component
class CouponAsyncEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
@EventListener
fun onApplicationEvent(event: CouponAsyncEvent) {
log.info("쿠폰 등록 완료 : {}", event.email)
}
}
@Component
class SenderAsyncEventListener {
private val log = LoggerFactory.getLogger(this::class.java)
/**
* 비동기로 설정하기 위해 @Async 어노테이션을 붙였다.
*
* 비동기로 설정했기 때문에 다른 쓰레드에서 이벤트가 실행된다.
*
* 그에 따라 예외가 발생해도 나머지 로직(회원 저장, 이벤트)은 제대로 처리된다(다른 쓰레드에서 예외가 발생한 것이기 때문에).
*
*/
@Async
@EventListener
fun handleEmail(event: SenderAsyncEvent) {
throw RuntimeException()
// log.info("환영 이메일 발송 성공 : {}", event.email)
}
/**
* 비동기로 설정하기 위해 @Async 어노테이션을 붙였다.
*
* 비동기로 설정했기 때문에 다른 쓰레드에서 이벤트가 실행된다.
*/
@Async
@EventListener
fun handleSMS(event: SenderAsyncEvent) {
log.info("환영 SMS 발송 성공 : {}", event.phoneNumber)
}
}

View File

@@ -1,27 +0,0 @@
package com.banjjoknim.playground.domain.event
/**
* 어노테이션을 사용한 이벤트
*
* 어노테이션을 사용하면 상속을 받지 않아도 되기 때문에 스프링에 대한 의존성이 제거된 순수한 자바 객체를 이벤트 객체로 사용할 수 있다.
*
* @see EventListener
*/
class AdminAnnotationEvent(val username: String)
class CouponAnnotationEvent(val email: String)
class SenderAnnotationEvent(val email: String, val phoneNumber: String)
class AdminTransactionalEvent(val username: String)
class CouponTransactionalEvent(val email: String)
class SenderTransactionalEvent(val email: String, val phoneNumber: String)
class AdminAsyncEvent(val username: String)
class CouponAsyncEvent(val email: String)
class SenderAsyncEvent(val email: String, val phoneNumber: String)

View File

@@ -1,41 +0,0 @@
package com.banjjoknim.playground.domain.event
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
/**
* 상속 기반 이벤트 리스너
*
* 스프링의 컨텍스트를 사용하기 때문에 빈으로 등록해줘야 사용할 수 있다
*
* @see ApplicationListener
*/
@Component
class AdminInheritanceEventListener : ApplicationListener<AdminInheritanceEvent> {
private val log = LoggerFactory.getLogger(this::class.java)
override fun onApplicationEvent(event: AdminInheritanceEvent) {
log.info("어드민 서비스 : {}님이 가입했습니다.", event.username)
}
}
@Component
class CouponInheritanceEventListener : ApplicationListener<CouponInheritanceEvent> {
private val log = LoggerFactory.getLogger(this::class.java)
override fun onApplicationEvent(event: CouponInheritanceEvent) {
log.info("쿠폰 등록 완료 : {}", event.email)
}
}
@Component
class SenderInheritanceEventListener : ApplicationListener<SenderInheritanceEvent> {
private val log = LoggerFactory.getLogger(this::class.java)
override fun onApplicationEvent(event: SenderInheritanceEvent) {
log.info("환영 이메일 발송 성공 : {}", event.email)
log.info("환영 SMS 발송 성공 : {}", event.phoneNumber)
}
}

View File

@@ -1,17 +0,0 @@
package com.banjjoknim.playground.domain.event
import org.springframework.context.ApplicationEvent
/**
* 상속 기반 이벤트
*
* ApplicationEvent 를 상속받아서 사용한다.
*
* @see ApplicationEvent
*/
class AdminInheritanceEvent(source: Any, val username: String) : ApplicationEvent(source)
class CouponInheritanceEvent(source: Any, val email: String) : ApplicationEvent(source)
class SenderInheritanceEvent(source: Any, val email: String, val phoneNumber: String) : ApplicationEvent(source)

View File

@@ -1,47 +0,0 @@
package com.banjjoknim.playground.domain.event
import org.hibernate.SessionFactory
import org.hibernate.event.service.spi.EventListenerRegistry
import org.hibernate.event.spi.EventType
import org.hibernate.event.spi.PostInsertEvent
import org.hibernate.event.spi.PostInsertEventListener
import org.hibernate.internal.SessionFactoryImpl
import org.hibernate.persister.entity.EntityPersister
import org.springframework.stereotype.Component
/**
* ```
* org.hibernate.event.spi.EventType 을 살펴보면, 다양한 이벤트 발행시점(상태)을 볼 수 있다.
*
* 다양한 이벤트 발행시점(상태)과 이벤트를 이용해서 비즈니스 로직을 처리할 수 있다.
*
* 이벤트를 심도있게, 더 잘 활용하고 싶다면 Hibernate Session Event (Hibernate Event Session) 를 공부하도록 하자.
*
* Hibernate Session 의 이벤트 인터셉트를 이용해서 다양하게 활용할 수 있다.
* ```
* @see EventType
*/
@Component
class DomainEvent {
private lateinit var sessionFactory: SessionFactory
fun sample() {
val registry = (sessionFactory as SessionFactoryImpl).serviceRegistry.getService(EventListenerRegistry::class.java)
registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(CustomEventListener())
}
}
/**
*
*/
@Component
class CustomEventListener : PostInsertEventListener {
override fun requiresPostCommitHanding(persister: EntityPersister?): Boolean {
TODO("Not yet implemented")
}
override fun onPostInsert(event: PostInsertEvent?) {
TODO("Not yet implemented")
}
}

View File

@@ -1,17 +0,0 @@
package com.banjjoknim.playground.domain.sender
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class SenderService {
private val log = LoggerFactory.getLogger(this::class.java)
fun sendEmail(email: String) {
log.info("환영 이메일 발송 성공 : {}", email)
}
fun sendSMS(phoneNumber: String) {
log.info("환영 SMS 발송 성공 : {}", phoneNumber)
}
}

View File

@@ -1,111 +0,0 @@
package com.banjjoknim.playground.domain.user
import com.banjjoknim.playground.domain.event.AdminAnnotationEvent
import com.banjjoknim.playground.domain.event.AdminAsyncEvent
import com.banjjoknim.playground.domain.event.AdminInheritanceEvent
import com.banjjoknim.playground.domain.event.AdminTransactionalEvent
import com.banjjoknim.playground.domain.event.CouponAnnotationEvent
import com.banjjoknim.playground.domain.event.CouponAsyncEvent
import com.banjjoknim.playground.domain.event.CouponInheritanceEvent
import com.banjjoknim.playground.domain.event.CouponTransactionalEvent
import com.banjjoknim.playground.domain.event.SenderAnnotationEvent
import com.banjjoknim.playground.domain.event.SenderAsyncEvent
import com.banjjoknim.playground.domain.event.SenderInheritanceEvent
import com.banjjoknim.playground.domain.event.SenderTransactionalEvent
import org.springframework.context.ApplicationEventPublisher
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
class User(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = 0L,
@Column(name = "name")
val name: String = "",
@Column(name = "email")
val email: String = "",
@Column(name = "phoneNumber")
val phoneNumber: String = ""
) {
/**
* 상속 기반의 이벤트 사용
*/
fun publishInheritanceEvent(eventPublisher: ApplicationEventPublisher) {
publishInheritanceAdminEvent(eventPublisher)
publishInheritanceCouponEvent(eventPublisher)
publishInheritanceSenderEvent(eventPublisher)
}
private fun publishInheritanceAdminEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(AdminInheritanceEvent(this, name))
}
private fun publishInheritanceCouponEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(CouponInheritanceEvent(this, email))
}
private fun publishInheritanceSenderEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(SenderInheritanceEvent(this, email, phoneNumber))
}
fun publishAnnotationEvent(eventPublisher: ApplicationEventPublisher) {
publishAnnotationAdminEvent(eventPublisher)
publishAnnotationCouponEvent(eventPublisher)
publishAnnotationSenderEvent(eventPublisher)
}
private fun publishAnnotationAdminEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(AdminAnnotationEvent(name))
}
private fun publishAnnotationCouponEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(CouponAnnotationEvent(email))
}
private fun publishAnnotationSenderEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(SenderAnnotationEvent(email, phoneNumber))
}
fun publishWithTransactionalEventListener(eventPublisher: ApplicationEventPublisher) {
publishWithTransactionalCouponEvent(eventPublisher)
publishWithTransactionalAdminEvent(eventPublisher)
publishWithTransactionalSenderEvent(eventPublisher)
}
private fun publishWithTransactionalAdminEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(AdminTransactionalEvent(name))
}
private fun publishWithTransactionalCouponEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(CouponTransactionalEvent(email))
}
private fun publishWithTransactionalSenderEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(SenderTransactionalEvent(email, phoneNumber))
}
fun publishWithAsyncEventListener(eventPublisher: ApplicationEventPublisher) {
publishWithAsyncCouponEvent(eventPublisher)
publishWithAsyncAdminEvent(eventPublisher)
publishWithAsyncSenderEvent(eventPublisher)
}
private fun publishWithAsyncAdminEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(AdminAsyncEvent(name))
}
private fun publishWithAsyncCouponEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(CouponAsyncEvent(email))
}
private fun publishWithAsyncSenderEvent(eventPublisher: ApplicationEventPublisher) {
eventPublisher.publishEvent(SenderAsyncEvent(email, phoneNumber))
}
}

View File

@@ -1,69 +0,0 @@
package com.banjjoknim.playground.domain.user
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid
@RequestMapping("/users")
@RestController
class UserApi(
private val userService: UserService,
private val userServiceBaseOnInheritanceEvent: UserServiceBaseOnInheritanceEvent,
private val userServiceBaseOnAnnotationEvent: UserServiceBaseOnAnnotationEvent
) {
/**
* 의존성을 모두 갖고 서비스 내에서 서비스를 호출하는 방식
*/
@PostMapping("")
fun createUser(@RequestBody @Valid request: CreateUserRequest): ResponseEntity<Unit> {
userService.createUser(request)
return ResponseEntity.ok().build()
}
/**
* 상속 기반의 이벤트 사용 방식
*/
@PostMapping("/inheritance")
fun createUserWithInheritanceEvent(@RequestBody @Valid request: CreateUserRequest): ResponseEntity<Unit> {
userServiceBaseOnInheritanceEvent.createUser(request)
return ResponseEntity.ok().build()
}
/**
* 어노테이션 기반의 이벤트 사용 방식
*/
@PostMapping("/annotation")
fun createUserWithAnnotationEvent(@RequestBody @Valid request: CreateUserRequest): ResponseEntity<Unit> {
userServiceBaseOnAnnotationEvent.createUser(request)
return ResponseEntity.ok().build()
}
/**
* 트랜잭션 이벤트 리스너 어노테이션을 이용한 이벤트 사용 방식
*/
@PostMapping("/transactional")
fun createUserWithTransactionalEventListener(@RequestBody @Valid request: CreateUserRequest): ResponseEntity<Unit> {
userServiceBaseOnAnnotationEvent.createUserWithTransactionalEventListener(request)
return ResponseEntity.ok().build()
}
/**
* 비동기 이벤트 리스너를 이용한 이벤트 사용 방식
*/
@PostMapping("/async")
fun createUserWithAsyncEventListener(@RequestBody @Valid request: CreateUserRequest): ResponseEntity<Unit> {
userServiceBaseOnAnnotationEvent.createUserWithAsyncEventListener(request)
return ResponseEntity.ok().build()
}
@GetMapping("/{userId}")
fun retrieveUser(@PathVariable userId: Long): ResponseEntity<RetrieveUserResponse> {
val response = userService.retrieveUser(userId)
return ResponseEntity.ok(response)
}
}

View File

@@ -1,24 +0,0 @@
package com.banjjoknim.playground.domain.user
import javax.validation.constraints.NotBlank
data class CreateUserRequest(
@field:NotBlank(message = "이름을 입력해주세요")
val name: String = "",
@field:NotBlank(message = "이메일을 입력해주세요")
val email: String = "",
@field:NotBlank(message = "휴대폰 번호를 입력해주세요")
val phoneNumber: String = ""
) {
fun toUser(): User {
return User(name = name, email = email, phoneNumber = phoneNumber)
}
}
data class RetrieveUserResponse(
val name: String,
val email: String,
val phoneNumber: String
) {
constructor(user: User) : this(user.name, user.email, user.phoneNumber)
}

View File

@@ -1,35 +0,0 @@
package com.banjjoknim.playground.domain.user
import com.banjjoknim.playground.domain.admin.AdminService
import com.banjjoknim.playground.domain.admin.CouponService
import com.banjjoknim.playground.domain.sender.SenderService
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
* 의존성을 모두 갖고 서비스 내에서 서비스를 호출하는 방식
*/
@Transactional
@Service
class UserService(
private val userRepository: UserRepository,
private val adminService: AdminService,
private val senderService: SenderService,
private val couponService: CouponService
) {
fun createUser(request: CreateUserRequest) {
val user = request.toUser()
userRepository.save(user)
adminService.alarm(user.name)
couponService.registerCoupon(user.email)
senderService.sendSMS(user.phoneNumber)
senderService.sendEmail(user.email)
}
fun retrieveUser(userId: Long): RetrieveUserResponse {
val user = userRepository.findByIdOrNull(userId)
?: throw NoSuchElementException("회원이 존재하지 않습니다. [userId: $userId]")
return RetrieveUserResponse(user)
}
}

View File

@@ -1,30 +0,0 @@
package com.banjjoknim.playground.domain.user
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Transactional
@Service
class UserServiceBaseOnAnnotationEvent(
private val userRepository: UserRepository,
private val eventPublisher: ApplicationEventPublisher
) {
fun createUser(request: CreateUserRequest) {
val user = request.toUser()
userRepository.save(user)
user.publishAnnotationEvent(eventPublisher)
}
fun createUserWithTransactionalEventListener(request: CreateUserRequest) {
val user = request.toUser()
userRepository.save(user)
user.publishWithTransactionalEventListener(eventPublisher)
}
fun createUserWithAsyncEventListener(request: CreateUserRequest) {
val user = request.toUser()
userRepository.save(user)
user.publishWithAsyncEventListener(eventPublisher)
}
}

View File

@@ -1,24 +0,0 @@
package com.banjjoknim.playground.domain.user
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
* 상속 기반의 이벤트를 사용하는 서비스
*
* @see BaseOnInheritanceEventListeners
* @see BaseOnInheritanceEvents
*/
@Transactional
@Service
class UserServiceBaseOnInheritanceEvent(
private val userRepository: UserRepository,
private val eventPublisher: ApplicationEventPublisher
) {
fun createUser(request: CreateUserRequest) {
val user = request.toUser()
userRepository.save(user)
user.publishInheritanceEvent(eventPublisher)
}
}

View File

@@ -1,52 +0,0 @@
### 고전적인 방식
POST http://localhost:8080/users
Content-Type: application/json
{
"name": "banjjoknim",
"email": "banjjoknim@github.com",
"phoneNumber": "010-1234-5678"
}
### 상속 기반의 이벤트를 사용하는 방식
POST http://localhost:8080/users/inheritance
Content-Type: application/json
{
"name": "banjjoknim",
"email": "banjjoknim@github.com",
"phoneNumber": "010-1234-5678"
}
### 어노테이션 기반의 이벤트를 사용하는 방식
POST http://localhost:8080/users/annotation
Content-Type: application/json
{
"name": "banjjoknim",
"email": "banjjoknim@github.com",
"phoneNumber": "010-1234-5678"
}
### 트랜잭션 이벤트 리스너 어노테이션을 사용하는 방식
POST http://localhost:8080/users/transactional
Content-Type: application/json
{
"name": "banjjoknim",
"email": "banjjoknim@github.com",
"phoneNumber": "010-1234-5678"
}
### 비동기 이벤트 리스너 어노테이션을 사용하는 방식
POST http://localhost:8080/users/async
Content-Type: application/json
{
"name": "banjjoknim",
"email": "banjjoknim@github.com",
"phoneNumber": "010-1234-5678"
}
###
GET http://localhost:8080/users/1

View File

@@ -1,19 +0,0 @@
spring:
h2:
console:
enabled: true
datasource:
driver-class-name: org.h2.Driver
username: sa
password:
url: jdbc:h2:mem:testdb;MODE=MySQL;
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
properties:
hibernate:
format_sql: true

View File

@@ -1,13 +0,0 @@
package com.banjjoknim.playground
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class SpringEventApplicationTests {
@Test
fun contextLoads() {
}
}

View File

@@ -1,60 +0,0 @@
package com.banjjoknim.playground.domain.user
import com.banjjoknim.playground.domain.event.AdminAnnotationEvent
import com.banjjoknim.playground.domain.event.CouponAnnotationEvent
import com.banjjoknim.playground.domain.event.SenderAnnotationEvent
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentCaptor
import org.mockito.BDDMockito
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.springframework.context.ApplicationEventPublisher
@ExtendWith(MockitoExtension::class)
class UserServiceBaseOnAnnotationEventTest {
@Mock
private lateinit var userRepository: UserRepository
@Mock
private lateinit var eventPublisher: ApplicationEventPublisher
/**
* 발행된 이벤트를 캡쳐해서 데이터를 저장할 수 있다.
*
* 이때, 순수한 자바 객체를 이벤트 객체로 사용하므로 ArgumentCaptor 의 제네릭을 제네릭을 Any(Object)로 지정한다.
*/
@Captor
private lateinit var eventPublisherCaptor: ArgumentCaptor<Any>
private lateinit var userServiceBaseOnAnnotationEvent: UserServiceBaseOnAnnotationEvent
@BeforeEach
fun setup() {
userServiceBaseOnAnnotationEvent = UserServiceBaseOnAnnotationEvent(userRepository, eventPublisher)
}
/**
* 아래와 같이 테스트는 작성할 수 있지만, 통합테스트를 돌려야 실제로 이벤트가 발행되고 이벤트 리스너가 이벤트를 처리하는 것을 확인할 수 있다는 한계점이 있다.
*/
@Test
fun `회원 생성시 이벤트의 총 발행 횟수와 각각의 이벤트의 타입을 검사한다`() {
BDDMockito.given(userRepository.save(BDDMockito.any()))
.willReturn(User(name = "banjjoknim", email = "banjjoknim@github.com", phoneNumber = "010-1234-5678"))
val request =
CreateUserRequest(name = "banjjoknim", email = "banjjoknim@github.com", phoneNumber = "010-1234-5678")
userServiceBaseOnAnnotationEvent.createUser(request)
BDDMockito.then(eventPublisher).should(BDDMockito.times(3)).publishEvent(eventPublisherCaptor.capture())
// 캡쳐한 이벤트는 순차적으로 저장되므로 아래와 같이 검증할 수도 있다.
val events = eventPublisherCaptor.allValues
Assertions.assertThat(events[0]).isInstanceOf(AdminAnnotationEvent::class.java)
Assertions.assertThat(events[1]).isInstanceOf(CouponAnnotationEvent::class.java)
Assertions.assertThat(events[2]).isInstanceOf(SenderAnnotationEvent::class.java)
}
}

View File

@@ -1,64 +0,0 @@
package com.banjjoknim.playground.domain.user
import com.banjjoknim.playground.domain.event.AdminInheritanceEvent
import com.banjjoknim.playground.domain.event.CouponInheritanceEvent
import com.banjjoknim.playground.domain.event.SenderInheritanceEvent
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentCaptor
import org.mockito.BDDMockito.any
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.then
import org.mockito.BDDMockito.times
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.springframework.context.ApplicationEvent
import org.springframework.context.ApplicationEventPublisher
@ExtendWith(MockitoExtension::class)
class UserServiceBaseOnInheritanceEventTest {
@Mock
private lateinit var userRepository: UserRepository
@Mock
private lateinit var eventPublisher: ApplicationEventPublisher
/**
* 발행된 이벤트를 캡쳐해서 데이터를 저장할 수 있다.
*
* 이때, ApplicationEvent 를 상속받은 객체를 이벤트 객체로 사용하므로 ArgumentCaptor 의 제네릭을 ApplicationEvent 로 지정한다.
*/
@Captor
private lateinit var eventPublisherCaptor: ArgumentCaptor<ApplicationEvent>
private lateinit var userServiceBaseOnInheritanceEvent: UserServiceBaseOnInheritanceEvent
@BeforeEach
fun setup() {
userServiceBaseOnInheritanceEvent = UserServiceBaseOnInheritanceEvent(userRepository, eventPublisher)
}
/**
* 아래와 같이 테스트는 작성할 수 있지만, 통합테스트를 돌려야 실제로 이벤트가 발행되고 이벤트 리스너가 이벤트를 처리하는 것을 확인할 수 있다는 한계점이 있다.
*/
@Test
fun `회원 생성시 이벤트의 총 발행 횟수와 각각의 이벤트의 타입을 검사한다`() {
given(userRepository.save(any()))
.willReturn(User(name = "banjjoknim", email = "banjjoknim@github.com", phoneNumber = "010-1234-5678"))
val request =
CreateUserRequest(name = "banjjoknim", email = "banjjoknim@github.com", phoneNumber = "010-1234-5678")
userServiceBaseOnInheritanceEvent.createUser(request)
then(eventPublisher).should(times(3)).publishEvent(eventPublisherCaptor.capture())
// 캡쳐한 이벤트는 순차적으로 저장되므로 아래와 같이 검증할 수도 있다.
val events = eventPublisherCaptor.allValues
assertThat(events[0]).isInstanceOf(AdminInheritanceEvent::class.java)
assertThat(events[1]).isInstanceOf(CouponInheritanceEvent::class.java)
assertThat(events[2]).isInstanceOf(SenderInheritanceEvent::class.java)
}
}

View File

@@ -1,37 +0,0 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/

View File

@@ -1,52 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java")
id("org.springframework.boot") version "2.6.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.0"
kotlin("plugin.spring") version "1.6.0"
kotlin("plugin.jpa") version "1.6.0"
}
group = "com.banjjoknim"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
// OAuth2 로그인을 위해 추가. spring-boot-starter-security 의존성이 있어도 기본적으로 추가되지 않기 때문.
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
// https://mvnrepository.com/artifact/com.auth0/java-jwt
// JWT 사용을 위해 라이브러리 추가. 2022.03.25 기준 최신 바로 전 버전.
implementation("com.auth0:java-jwt:3.18.3")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
runtimeOnly("mysql:mysql-connector-java")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}

View File

@@ -1,5 +0,0 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,234 +0,0 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

Some files were not shown because too many files have changed in this diff Show More