diff --git a/.gitignore b/.gitignore index 52d8c75..1613366 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ out/ ### VS Code ### .vscode/ + +src/main/resources/parkyounghwan/application-password.properties \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index dbc851e..d58db9f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,6 +18,7 @@ repositories { dependencies { implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-validation") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.springframework.boot:spring-boot-starter-jdbc") diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/config/CustomDataSourceConfig.kt b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/config/CustomDataSourceConfig.kt index d08ce29..bce41ab 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/config/CustomDataSourceConfig.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/config/CustomDataSourceConfig.kt @@ -9,7 +9,10 @@ import org.springframework.core.env.Environment import javax.sql.DataSource @Configuration -@PropertySource("classpath:parkyounghwan/application-parkyounghwan.yml") +@PropertySource( + "classpath:parkyounghwan/application-parkyounghwan.properties", + "classpath:parkyounghwan/application-password.properties" +) class CustomDataSourceConfig { val env: Environment @@ -19,12 +22,12 @@ class CustomDataSourceConfig { this.env = env } - @Bean("dataSource") + @Bean fun customDataSource(): DataSource = DataSourceBuilder.create() - .driverClassName(this.env.getProperty("spring.datasource.driver-class-name")) - .url(this.env.getProperty("spring.datasource.url")) - .username(this.env.getProperty("spring.datasource.username")) - .password(this.env.getProperty("spring.datasource.password")) + .driverClassName(env.getProperty("spring.datasource.driver-class-name")) + .url(env.getProperty("spring.datasource.url")) + .username(env.getProperty("spring.datasource.username")) + .password(env.getProperty("spring.datasource.password")) .build() } \ No newline at end of file diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/domain/User.kt b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/domain/User.kt index bbdb057..30c3ec4 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/domain/User.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/domain/User.kt @@ -1,70 +1,8 @@ package com.flabedu.blackpostoffice.parkyounghwan.domain -import java.lang.IllegalArgumentException - -data class User (val id: Long, val name: String, val email: String, val password: String) - -/* -class User() { - var id: Int = 0 - - var name: String = "" - set(value) { - if (value.length < 2 || value.length > 13) { - throw IllegalArgumentException("INVALID NAME VALUE") - } else { - try { - field = value - } catch (e: Exception) { - // TODO: 회원 이름 등록 시 예상하지 못한 예외 처리 - throw Exception() - } finally { - field = "" - } - } - } - - var email: String = "" - set(value) { - val regex = Regex("[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\\.[a-zA-Z]{2,3}") - - if (!value.matches(regex)) { - throw IllegalArgumentException("INVALID EMAIL VALUE") - } else { - try { - field = value - } catch (e: Exception) { - // TODO: 회원 이메일 등록 시 예상하지 못한 예외 처리 - throw Exception() - } finally { - field = "" - } - } - } - - // 숫자, 문자, 특수문자 모두 포함 (8~15자) - var password: String = "" - set(value) { - val regex = Regex("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[\$@\$!%*#?&]).{8,15}.\$") - - if (!value.matches(regex)) { - throw IllegalArgumentException("INVALID PASSWORD VALUE") - } else { - try { - field = value - } catch (e: Exception) { - throw Exception() - } finally { - field = "" - } - } - } - - constructor(id: Int, name: String, email: String, password: String) : this() { - this.id = id - this.name = name - this.email = email - this.password = password - } -} - */ \ No newline at end of file +data class User ( + var id: Long = 0, + var name: String = "", + var email: String = "", + var password: String = "", +) \ No newline at end of file diff --git a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapper.kt b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapper.kt index 2f649a8..e4f59e9 100644 --- a/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapper.kt +++ b/src/main/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapper.kt @@ -2,10 +2,12 @@ package com.flabedu.blackpostoffice.parkyounghwan.mapper import com.flabedu.blackpostoffice.parkyounghwan.domain.User import org.apache.ibatis.annotations.Mapper -import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.stereotype.Component @Mapper -@Qualifier("pUserMapper") +@Component("parkyounghwan.UserMapper") interface UserMapper { - fun selectUserById(id: Int): User + fun selectUserByEmail(email: String): User + fun insertUser(user: User): Int + fun deleteAllUser(): Int } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 502b0a1..58668df 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,11 +1,4 @@ +# default profiles spring: - datasource: - hikari: - driver-class-name: org.h2.Driver - jdbc-url: jdbc:h2:mem:test - username: sa - password: - - h2: - console: - enabled: true \ No newline at end of file + profiles: + active: local \ No newline at end of file diff --git a/src/main/resources/parkyounghwan/application-parkyounghwan.properties b/src/main/resources/parkyounghwan/application-parkyounghwan.properties new file mode 100644 index 0000000..88e2b32 --- /dev/null +++ b/src/main/resources/parkyounghwan/application-parkyounghwan.properties @@ -0,0 +1,3 @@ +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/black-postoffice?serverTimeZone=UTC$CharacterEncoding=UTF-8 +spring.datasource.username=root \ No newline at end of file diff --git a/src/main/resources/parkyounghwan/application-parkyounghwan.yml b/src/main/resources/parkyounghwan/application-parkyounghwan.yml deleted file mode 100644 index a71d6a7..0000000 --- a/src/main/resources/parkyounghwan/application-parkyounghwan.yml +++ /dev/null @@ -1,14 +0,0 @@ -logging: - level: - root: WARN - sample: - mybatis: - xml: - mapper: TRACE - -spring: - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/black-postoffice?serverTimeZone=UTC$CharacterEncoding=UTF-8 - username: root - password: 123Zkfltmak@ \ No newline at end of file diff --git a/src/main/resources/parkyounghwan/mapper/UserMapper.xml b/src/main/resources/parkyounghwan/mapper/UserMapper.xml index abd77e7..40b0266 100644 --- a/src/main/resources/parkyounghwan/mapper/UserMapper.xml +++ b/src/main/resources/parkyounghwan/mapper/UserMapper.xml @@ -4,7 +4,16 @@ "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + SELECT * FROM USER WHERE email = #{email} + + + INSERT INTO USER (name, email, password) + VALUES (#{name}, #{email}, #{password}) + + + + DELETE FROM USER + \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt deleted file mode 100644 index 0d24416..0000000 --- a/src/test/kotlin/com/flabedu/blackpostoffice/domain/UserValidationCheckTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.flabedu.blackpostoffice.domain - -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test -import org.junit.runner.RunWith -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner -import java.lang.IllegalArgumentException - -@RunWith(SpringJUnit4ClassRunner::class) -@SpringBootTest -class UserValidationCheckTest { - - @Test - fun `User 'name' field 유효성 검사`() { - val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { - val user = User() - user.name = "" - } - - Assertions.assertEquals("INVALID NAME VALUE", e.message) - } - - @Test - fun `User 'email' field 유효성 검사`() { - val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { - val user = User() - user.email = "" - } - - Assertions.assertEquals("INVALID EMAIL VALUE", e.message) - } - - @Test - fun `User 'password' field 유효성 검사`() { - val e: Exception = Assertions.assertThrows(IllegalArgumentException::class.java) { - val user = User() - user.password = "" - } - - Assertions.assertEquals("INVALID PASSWORD VALUE", e.message) - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/dao/UserDaoTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/dao/UserDaoTest.kt deleted file mode 100644 index fecb5d9..0000000 --- a/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/dao/UserDaoTest.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.flabedu.blackpostoffice.parkyounghwan.dao - -import com.flabedu.blackpostoffice.domain.User -import junit.framework.Assert.assertEquals -import org.junit.jupiter.api.Test -import org.junit.runner.RunWith -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner - -@RunWith(SpringJUnit4ClassRunner::class) -@SpringBootTest(properties = ["classpath:application.yml"]) -class UserDaoTest { - - @Autowired - private lateinit var userDao: UserDao - - @Test - fun `유저 등록 테스드`() { - val user = User(1, "parkyounghwan", "dudrnxps@gmail.com", "1234") - - userDao.add(user) - - assertEquals(1, user.id) - assertEquals("parkyounghwan", user.name) - assertEquals("1234", user.password) - - var user2: User = userDao.get(1) - - assertEquals(user.id, user2.id) - assertEquals(user.name, user2.name) - assertEquals(user.password, user2.password) - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapperTest.kt b/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapperTest.kt index bc6a449..1c63e32 100644 --- a/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapperTest.kt +++ b/src/test/kotlin/com/flabedu/blackpostoffice/parkyounghwan/mapper/UserMapperTest.kt @@ -1,5 +1,7 @@ package com.flabedu.blackpostoffice.parkyounghwan.mapper +import com.flabedu.blackpostoffice.parkyounghwan.domain.User +import junit.framework.Assert.assertEquals import org.junit.jupiter.api.Test import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired @@ -12,16 +14,19 @@ import org.springframework.test.context.junit4.SpringRunner class UserMapperTest { @Autowired - @Qualifier("pUserMapper") + @Qualifier("parkyounghwan.UserMapper") lateinit var userMapper: UserMapper @Test - fun `UserMapperTest 클래스 호출 테스트`() { - println("hello `UserMapperTest`") - } + fun `회원 정보 저장 테스트`() { + val user = User(0, "홍길동", "honggildong@korea.com", "1234") - @Test - fun `UserMapper 저장 된 유저 ID 로 불러오기`() { - print(userMapper.selectUserById(1)) + assertEquals(0, userMapper.deleteAllUser()) + assertEquals(1, userMapper.insertUser(user)) + + val result: User = userMapper.selectUserByEmail("honggildong@korea.com") + + assertEquals("홍길동", result.name) + assertEquals(1, userMapper.deleteAllUser()) } } \ No newline at end of file