diff --git a/coverage/.gitattributes b/coverage/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/coverage/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/coverage/.gitignore b/coverage/.gitignore new file mode 100644 index 0000000..89add36 --- /dev/null +++ b/coverage/.gitignore @@ -0,0 +1,43 @@ +HELP.md +.DS_Store +.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/* +!/.idea/icon.png +!/.idea/codeStyleSettings.xml +!/.idea/codeStyles +!/.idea/dictionaries +!/.idea/encodings.xml +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/coverage/build.gradle.kts b/coverage/build.gradle.kts new file mode 100644 index 0000000..a222465 --- /dev/null +++ b/coverage/build.gradle.kts @@ -0,0 +1,69 @@ +plugins { + kotlin("jvm") version "1.9.25" + kotlin("plugin.spring") version "1.9.25" + kotlin("plugin.jpa") version "1.9.25" + id("org.springframework.boot") version "3.3.5" + id("io.spring.dependency-management") version "1.1.6" + jacoco +} + +group = "com.tistory.namocom" +version = "0.0.1-SNAPSHOT" + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("org.jetbrains.kotlin:kotlin-reflect") + runtimeOnly("com.h2database:h2") + testImplementation(dependencyNotation = "org.springframework.boot:spring-boot-starter-test") { + exclude(module = "mockito-core") + } + testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + val kotestVersion = "5.9.1" + testImplementation("io.kotest:kotest-runner-junit5:${kotestVersion}") + testImplementation("io.kotest:kotest-property-jvm:${kotestVersion}") + testImplementation("io.kotest:kotest-extensions-now:${kotestVersion}") + testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.3") + testImplementation("io.mockk:mockk:1.13.9") + testImplementation("com.ninja-squad:springmockk:4.0.2") + testImplementation("com.appmattus.fixture:fixture:1.2.0") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") + testRuntimeOnly("com.h2database:h2") +} + +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + } +} + +allOpen { + annotation("jakarta.persistence.Entity") + annotation("jakarta.persistence.MappedSuperclass") + annotation("jakarta.persistence.Embeddable") +} + +tasks.withType { + useJUnitPlatform() + jvmArgs = listOf("-Xshare:off", "-XX:+EnableDynamicAgentLoading") +} + +tasks.test { + maxHeapSize = "6400m" +} + +tasks.jacocoTestReport { + dependsOn(tasks.named("test")) + reports { + xml.required.set(true) + } +} diff --git a/coverage/gradle/wrapper/gradle-wrapper.jar b/coverage/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..a4b76b9 Binary files /dev/null and b/coverage/gradle/wrapper/gradle-wrapper.jar differ diff --git a/coverage/gradle/wrapper/gradle-wrapper.properties b/coverage/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..df97d72 --- /dev/null +++ b/coverage/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/coverage/gradlew b/coverage/gradlew new file mode 100755 index 0000000..f5feea6 --- /dev/null +++ b/coverage/gradlew @@ -0,0 +1,252 @@ +#!/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. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# 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/HEAD/platforms/jvm/plugins-application/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 + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# 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 + if ! command -v java >/dev/null 2>&1 + then + 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 +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + 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 + + +# 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"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# 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" "$@" diff --git a/coverage/gradlew.bat b/coverage/gradlew.bat new file mode 100644 index 0000000..9d21a21 --- /dev/null +++ b/coverage/gradlew.bat @@ -0,0 +1,94 @@ +@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 +@rem SPDX-License-Identifier: Apache-2.0 +@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=. +@rem This is normally unused +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% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +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% equ 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! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/coverage/settings.gradle.kts b/coverage/settings.gradle.kts new file mode 100644 index 0000000..6e97153 --- /dev/null +++ b/coverage/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "coverage" diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/CoverageApplication.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/CoverageApplication.kt new file mode 100644 index 0000000..89ba2b3 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/CoverageApplication.kt @@ -0,0 +1,50 @@ +package com.tistory.namocom.coverage + +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoCreationUseCase +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoEditUseCase +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoQueryUseCase +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId +import org.springframework.boot.CommandLineRunner +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Profile +import org.springframework.transaction.support.TransactionTemplate + +@Suppress("SpellCheckingInspection") +@SpringBootApplication +class CoverageApplication { + + @Profile("!test") + @Bean + fun run( + memoCreationUseCase: MemoCreationUseCase, + memoQueryUseCase: MemoQueryUseCase, + memoEditUseCase: MemoEditUseCase, + transactionTemplate: TransactionTemplate, + ) = CommandLineRunner { + transactionTemplate.execute { + val writer = UserId(value = 1004) + val memo = NewMemo(writer = writer, title = "제목", content = "내용입니다.") + val memoId: MemoId = memoCreationUseCase.createMemo(memo = memo) + + val savedMemo1: Memo = memoQueryUseCase.getMemo(id = memoId) + println(savedMemo1) + + val editedMemo = EditMemo(id = memoId, writer = writer, title = "수정된 제목", content = "수정된 내용입니다.") + memoEditUseCase.editMemo(memo = editedMemo) + + val savedMemo2: Memo = memoQueryUseCase.getMemo(id = memoId) + println(savedMemo2) + } + } + +} + +fun main(args: Array) { + runApplication(*args) +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoJpaEntity.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoJpaEntity.kt new file mode 100644 index 0000000..60acf26 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoJpaEntity.kt @@ -0,0 +1,63 @@ +package com.tistory.namocom.coverage.app.memo.adapter.out.persistence + +import com.tistory.namocom.coverage.core.common.entity.Identifiable +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId +import jakarta.persistence.* +import java.time.LocalDateTime + +@Entity +@Table(name = "memo") +data class MemoJpaEntity( + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(nullable = false) + val id: Long, + + @Column(nullable = false) + val writer: Long, + + @Column(nullable = false, length = 255) + val title: String, + + @Column(nullable = false, length = 1024) + val content: String, + + @Column(name = "created_at", nullable = false, updatable = false) + var createdAt: LocalDateTime, + + @Column(name = "updated_at", nullable = false) + var updatedAt: LocalDateTime, +) { + + fun toDomain(): Memo { + return Memo( + id = MemoId(value = id), + writer = UserId(value = writer), + title = title, + content = content, + createdAt = createdAt, + updatedAt = updatedAt, + ) + } + + fun update(memo: EditMemo): MemoJpaEntity = copy( + title = memo.title, + content = memo.content, + updatedAt = LocalDateTime.now(), + ) + +} + +fun NewMemo.toJpaEntity() = LocalDateTime.now().let { now -> + MemoJpaEntity( + id = Identifiable.NEW_ID, + writer = writer.value, + title = title, + content = content, + createdAt = now, + updatedAt = now, + ) +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapter.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapter.kt new file mode 100644 index 0000000..cc0c1a3 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapter.kt @@ -0,0 +1,32 @@ +package com.tistory.namocom.coverage.app.memo.adapter.out.persistence + +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoEditPort +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoQueryPort +import com.tistory.namocom.coverage.core.memo.application.port.out.SaveMemoPort +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Component + +@Component +class MemoPersistenceAdapter( + private val memoRepository: MemoRepository, +) : SaveMemoPort, MemoQueryPort, MemoEditPort { + + override fun saveMemo(memo: NewMemo): MemoId { + val newMemo: MemoJpaEntity = memo.toJpaEntity() + val savedMemo: MemoJpaEntity = memoRepository.save(newMemo) + return MemoId(value = savedMemo.id) + } + + override fun findMemo(id: MemoId): Memo? = + memoRepository.findByIdOrNull(id.value) + ?.toDomain() + + override fun editMemo(memo: EditMemo) = + memoRepository.findById(memo.id.value) + .ifPresent { memoRepository.save(it.update(memo)) } + +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoRepository.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoRepository.kt new file mode 100644 index 0000000..c556d84 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoRepository.kt @@ -0,0 +1,5 @@ +package com.tistory.namocom.coverage.app.memo.adapter.out.persistence + +import org.springframework.data.jpa.repository.JpaRepository + +interface MemoRepository : JpaRepository diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/annotation/ApplicationService.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/annotation/ApplicationService.kt new file mode 100644 index 0000000..f076ead --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/annotation/ApplicationService.kt @@ -0,0 +1,10 @@ +package com.tistory.namocom.coverage.core.common.annotation + +import org.springframework.core.annotation.AliasFor +import org.springframework.stereotype.Service + +@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@Service +annotation class ApplicationService(@get:AliasFor(annotation = Service::class) val value: String = "") diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/entity/Identifiable.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/entity/Identifiable.kt new file mode 100644 index 0000000..676b7e6 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/common/entity/Identifiable.kt @@ -0,0 +1,7 @@ +package com.tistory.namocom.coverage.core.common.entity + +interface Identifiable { + companion object { + const val NEW_ID = 0L + } +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoCreationUseCase.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoCreationUseCase.kt new file mode 100644 index 0000000..aa207ef --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoCreationUseCase.kt @@ -0,0 +1,8 @@ +package com.tistory.namocom.coverage.core.memo.application.port.`in` + +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo + +fun interface MemoCreationUseCase { + fun createMemo(memo: NewMemo): MemoId +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoEditUseCase.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoEditUseCase.kt new file mode 100644 index 0000000..3a391f0 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoEditUseCase.kt @@ -0,0 +1,7 @@ +package com.tistory.namocom.coverage.core.memo.application.port.`in` + +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo + +fun interface MemoEditUseCase { + fun editMemo(memo: EditMemo) +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoQueryUseCase.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoQueryUseCase.kt new file mode 100644 index 0000000..b013872 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/in/MemoQueryUseCase.kt @@ -0,0 +1,8 @@ +package com.tistory.namocom.coverage.core.memo.application.port.`in` + +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId + +fun interface MemoQueryUseCase { + fun getMemo(id: MemoId): Memo +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoEditPort.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoEditPort.kt new file mode 100644 index 0000000..3d62de4 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoEditPort.kt @@ -0,0 +1,7 @@ +package com.tistory.namocom.coverage.core.memo.application.port.out + +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo + +fun interface MemoEditPort { + fun editMemo(memo: EditMemo) +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoQueryPort.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoQueryPort.kt new file mode 100644 index 0000000..7952689 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/MemoQueryPort.kt @@ -0,0 +1,8 @@ +package com.tistory.namocom.coverage.core.memo.application.port.out + +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId + +fun interface MemoQueryPort { + fun findMemo(id: MemoId): Memo? +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/SaveMemoPort.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/SaveMemoPort.kt new file mode 100644 index 0000000..d8ce4f7 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/port/out/SaveMemoPort.kt @@ -0,0 +1,8 @@ +package com.tistory.namocom.coverage.core.memo.application.port.out + +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId + +fun interface SaveMemoPort { + fun saveMemo(memo: NewMemo): MemoId +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationService.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationService.kt new file mode 100644 index 0000000..a4b8b0f --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationService.kt @@ -0,0 +1,18 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import com.tistory.namocom.coverage.core.common.annotation.ApplicationService +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoCreationUseCase +import com.tistory.namocom.coverage.core.memo.application.port.out.SaveMemoPort +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import org.springframework.transaction.annotation.Transactional + +@ApplicationService +class MemoCreationService( + private val saveMemoPort: SaveMemoPort, +): MemoCreationUseCase { + + @Transactional + override fun createMemo(memo: NewMemo): MemoId = saveMemoPort.saveMemo(memo = memo) + +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditService.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditService.kt new file mode 100644 index 0000000..3e33ca6 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditService.kt @@ -0,0 +1,28 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import com.tistory.namocom.coverage.core.common.annotation.ApplicationService +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoEditUseCase +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoEditPort +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoQueryPort +import com.tistory.namocom.coverage.core.memo.domain.error.NotFoundMemoException +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import org.springframework.transaction.annotation.Transactional + +@ApplicationService +@Suppress("SpellCheckingInspection") +class MemoEditService( + private val memoQueryPort: MemoQueryPort, + private val memoEditPort: MemoEditPort, +) : MemoEditUseCase { + + @Transactional + override fun editMemo(memo: EditMemo) { + val savedMemo: Memo = memoQueryPort.findMemo(memo.id) ?: throw NotFoundMemoException(memoId = memo.id) + if (savedMemo.writer != memo.writer) { + throw IllegalAccessException("작성자만 수정할 수 있습니다.") + } + memoEditPort.editMemo(memo = memo) + } + +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryService.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryService.kt new file mode 100644 index 0000000..a8a4f0a --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryService.kt @@ -0,0 +1,18 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import com.tistory.namocom.coverage.core.common.annotation.ApplicationService +import com.tistory.namocom.coverage.core.memo.application.port.`in`.MemoQueryUseCase +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoQueryPort +import com.tistory.namocom.coverage.core.memo.domain.error.NotFoundMemoException +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId + +@ApplicationService +class MemoQueryService( + private val memoQueryPort: MemoQueryPort, +) : MemoQueryUseCase { + + override fun getMemo(id: MemoId): Memo = memoQueryPort.findMemo(id = id) + ?: throw NotFoundMemoException(memoId = id) + +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/error/NotFoundMemoException.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/error/NotFoundMemoException.kt new file mode 100644 index 0000000..217f8de --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/error/NotFoundMemoException.kt @@ -0,0 +1,10 @@ +package com.tistory.namocom.coverage.core.memo.domain.error + +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId + +class NotFoundMemoException( + private val memoId: MemoId, +) : RuntimeException() { + override val message: String + get() = "메모를 찾을 수 없습니다. ID: ${memoId.value}" +} diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/EditMemo.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/EditMemo.kt new file mode 100644 index 0000000..bac3d93 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/EditMemo.kt @@ -0,0 +1,11 @@ +package com.tistory.namocom.coverage.core.memo.domain.model + +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId + +data class EditMemo( + val id: MemoId, + val writer: UserId, + val title: String, + val content: String, +) diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/Memo.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/Memo.kt new file mode 100644 index 0000000..b16ed22 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/Memo.kt @@ -0,0 +1,14 @@ +package com.tistory.namocom.coverage.core.memo.domain.model + +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId +import java.time.LocalDateTime + +data class Memo( + val id: MemoId, + val writer: UserId, + val title: String, + val content: String, + val createdAt: LocalDateTime, + val updatedAt: LocalDateTime, +) diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/NewMemo.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/NewMemo.kt new file mode 100644 index 0000000..ecb0644 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/model/NewMemo.kt @@ -0,0 +1,9 @@ +package com.tistory.namocom.coverage.core.memo.domain.model + +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId + +data class NewMemo( + val writer: UserId, + val title: String, + val content: String, +) diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/MemoId.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/MemoId.kt new file mode 100644 index 0000000..19554cf --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/MemoId.kt @@ -0,0 +1,4 @@ +package com.tistory.namocom.coverage.core.memo.domain.vo + +@JvmInline +value class MemoId(val value: Long) diff --git a/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/UserId.kt b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/UserId.kt new file mode 100644 index 0000000..3f59b38 --- /dev/null +++ b/coverage/src/main/kotlin/com/tistory/namocom/coverage/core/memo/domain/vo/UserId.kt @@ -0,0 +1,4 @@ +package com.tistory.namocom.coverage.core.memo.domain.vo + +@JvmInline +value class UserId(val value: Long) diff --git a/coverage/src/main/resources/application.properties b/coverage/src/main/resources/application.properties new file mode 100644 index 0000000..59f6a52 --- /dev/null +++ b/coverage/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=coverage +spring.jpa.hibernate.ddl-auto=create +spring.jpa.properties.hibernate.show_sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.use_sql_comments=false +logging.level.org.hibernate.orm.jdbc.bind=TRACE diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/CoverageApplicationTests.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/CoverageApplicationTests.kt new file mode 100644 index 0000000..7f24342 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/CoverageApplicationTests.kt @@ -0,0 +1,13 @@ +package com.tistory.namocom.coverage + +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class CoverageApplicationTests { + + @Test + fun contextLoads() { + } + +} diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/KotlinFixture.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/KotlinFixture.kt new file mode 100644 index 0000000..aa8b0d8 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/KotlinFixture.kt @@ -0,0 +1,7 @@ +package com.tistory.namocom.coverage + +import com.appmattus.kotlinfixture.kotlinFixture + +object KotlinFixture { + val fixture = kotlinFixture() +} diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapterSpec.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapterSpec.kt new file mode 100644 index 0000000..904af77 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/app/memo/adapter/out/persistence/MemoPersistenceAdapterSpec.kt @@ -0,0 +1,80 @@ +package com.tistory.namocom.coverage.app.memo.adapter.out.persistence + +import com.tistory.namocom.coverage.core.memo.domain.model.EditMemo +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import com.tistory.namocom.coverage.core.memo.domain.vo.UserId +import io.kotest.core.spec.style.FunSpec +import io.kotest.extensions.time.withConstantNow +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest +import org.springframework.test.context.ActiveProfiles +import java.time.LocalDateTime + +@ActiveProfiles("test") +@AutoConfigureTestDatabase +@DataJpaTest +@Suppress("SpellCheckingInspection") +class MemoPersistenceAdapterSpec( + private val memoRepository: MemoRepository, +) : FunSpec({ + + val sut = MemoPersistenceAdapter( + memoRepository = memoRepository, + ) + val writer = UserId(value = 1004) + + test("저장하지 않은 메모 조회시 null 반환") { + // given + val notSavedMemoId = MemoId(value = 0) + + // when + val savedMemo: Memo? = sut.findMemo(id = notSavedMemoId) + + // then + savedMemo shouldBe null + } + + test("saveMemo & findMemo") { + // given + val memo = NewMemo(writer = writer, title = "제목", content = "내용입니다.") + + // when + val memoId: MemoId = sut.saveMemo(memo = memo) + + // then + val savedMemo: Memo = sut.findMemo(id = memoId)!! + with(savedMemo) { + writer shouldBe writer + title shouldBe "제목" + content shouldBe "내용입니다." + } + } + + test("editMemo") { + // given + val memo = NewMemo(writer = writer, title = "제목", content = "내용입니다.") + val memoId: MemoId = sut.saveMemo(memo = memo) + + // when + val someLater: LocalDateTime = LocalDateTime.now().withHour(1) + withConstantNow(someLater) { + val edit = EditMemo(id = memoId, writer = writer, title = "수정된 제목", content = "수정된 내용입니다.") + sut.editMemo(memo = edit) + + // then + val memoJpaEntity: MemoJpaEntity = memoRepository.findById(memoId.value).get() + with(memoJpaEntity) { + this.writer shouldBe writer.value + title shouldBe "수정된 제목" + content shouldBe "수정된 내용입니다." + createdAt shouldNotBe updatedAt + updatedAt shouldBe someLater + } + } + } + +}) diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationServiceSpec.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationServiceSpec.kt new file mode 100644 index 0000000..4673fc0 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoCreationServiceSpec.kt @@ -0,0 +1,33 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import com.tistory.namocom.coverage.KotlinFixture.fixture +import com.tistory.namocom.coverage.core.memo.application.port.out.SaveMemoPort +import com.tistory.namocom.coverage.core.memo.domain.model.NewMemo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import io.mockk.CapturingSlot +import io.mockk.every +import io.mockk.mockk +import io.mockk.slot + +class MemoCreationServiceSpec : FunSpec({ + + val saveMemoPort: SaveMemoPort = mockk() + val sut = MemoCreationService(saveMemoPort = saveMemoPort) + + test("createMemo") { + // given + val memo: NewMemo = fixture() + val memoSlot: CapturingSlot = slot() + val memoId = MemoId(value = 1) + every { saveMemoPort.saveMemo(memo = capture(memoSlot)) } returns memoId + + // when + val result: MemoId = sut.createMemo(memo = memo) + + // then + result shouldBe memoId + } + +}) diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditServiceSpec.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditServiceSpec.kt new file mode 100644 index 0000000..b6dde82 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoEditServiceSpec.kt @@ -0,0 +1,9 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +class MemoEditServiceSpec : FunSpec({ + + test("editMemo") { } +}) diff --git a/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryServiceSpec.kt b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryServiceSpec.kt new file mode 100644 index 0000000..c52e2f9 --- /dev/null +++ b/coverage/src/test/kotlin/com/tistory/namocom/coverage/core/memo/application/service/MemoQueryServiceSpec.kt @@ -0,0 +1,47 @@ +package com.tistory.namocom.coverage.core.memo.application.service + +import com.tistory.namocom.coverage.KotlinFixture.fixture +import com.tistory.namocom.coverage.core.memo.application.port.out.MemoQueryPort +import com.tistory.namocom.coverage.core.memo.domain.error.NotFoundMemoException +import com.tistory.namocom.coverage.core.memo.domain.model.Memo +import com.tistory.namocom.coverage.core.memo.domain.vo.MemoId +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk + +@Suppress("SpellCheckingInspection") +class MemoQueryServiceSpec : FunSpec({ + + val memoQueryPort: MemoQueryPort = mockk() + val sut = MemoQueryService(memoQueryPort = memoQueryPort) + + context("getMemo") { + test("메모를 찾을 수 없으면 예외가 발생한다.") { + // given + val memoId = MemoId(value = 1) + every { memoQueryPort.findMemo(id = memoId) } returns null + + // when & then + val ex = shouldThrow { + sut.getMemo(id = memoId) + } + ex.message shouldBe "메모를 찾을 수 없습니다. ID: 1" + } + + test("메모를 찾을 수 있으면 메모를 반환한다.") { + // given + val memoId = MemoId(value = 2) + val memo: Memo = fixture().copy(id = memoId) + every { memoQueryPort.findMemo(id = memoId) } returns memo + + // when + val result = sut.getMemo(id = memoId) + + // then + result shouldBe memo + } + } + +}) diff --git a/coverage/src/test/resources/application-test.properties b/coverage/src/test/resources/application-test.properties new file mode 100644 index 0000000..5252093 --- /dev/null +++ b/coverage/src/test/resources/application-test.properties @@ -0,0 +1,2 @@ +spring.main.banner-mode=off +spring.jpa.properties.hibernate.connection.provider_disables_autocommit=false