[+/-] Simplify Unit Tests, remove unnecessary ErrorCodes.

This commit is contained in:
VergeDX
2021-01-21 09:54:34 +08:00
parent c8ecf53ab5
commit 3286c982aa
3 changed files with 43 additions and 34 deletions
@@ -36,9 +36,9 @@ public class RegistryController {
// [!] @RequestHeader(required = false) makes no need make another error handler. // [!] @RequestHeader(required = false) makes no need make another error handler.
// [!] And also, ExceptionHandler of MissingRequestHeaderException cannot deal with all missing fields. // [!] And also, ExceptionHandler of MissingRequestHeaderException cannot deal with all missing fields.
@Pattern(regexp = User.RE_USERNAME, message = ErrorCode.USER_NAME_NOT_MATCH_REGEX) @Pattern(regexp = User.RE_USERNAME, message = ErrorCode.USER_NAME_NOT_MATCH_REGEX)
@NotNull(message = ErrorCode.USER_NAME_IS_NULL) @RequestHeader(required = false) String username, @RequestHeader String username,
@Pattern(regexp = User.RE_PASSWORD, message = ErrorCode.USER_PASSWORD_NOT_MATCH_REGEX) @Pattern(regexp = User.RE_PASSWORD, message = ErrorCode.USER_PASSWORD_NOT_MATCH_REGEX)
@NotNull(message = ErrorCode.USER_PASSWORD_IS_NULL) @RequestHeader(required = false) String password @RequestHeader String password
) { ) {
// First, spring will check args. If not pass there regex, raise ConstraintViolationException. // First, spring will check args. If not pass there regex, raise ConstraintViolationException.
// Then, the aspect will check username if already exists. // Then, the aspect will check username if already exists.
@@ -2,16 +2,12 @@ package org.hydev.clock_api.error;
public interface ErrorCode { public interface ErrorCode {
// Missing field in header.
String USER_NAME_IS_NULL = "A0101";
String USER_PASSWORD_IS_NULL = "A0102";
// Field not match regex. // Field not match regex.
String USER_NAME_NOT_MATCH_REGEX = "A0111"; String USER_NAME_NOT_MATCH_REGEX = "A0101";
String USER_PASSWORD_NOT_MATCH_REGEX = "A0112"; String USER_PASSWORD_NOT_MATCH_REGEX = "A0102";
// Field already exists. // Field already exists.
String USER_NAME_ALREADY_EXISTS = "A0121"; String USER_NAME_ALREADY_EXISTS = "A0111";
// Inner system field is null. // Inner system field is null.
String INNER_USERNAME_IS_NULL = "B0101"; String INNER_USERNAME_IS_NULL = "B0101";
@@ -4,14 +4,16 @@ import org.hamcrest.Matchers
import org.hydev.clock_api.entity.User import org.hydev.clock_api.entity.User
import org.hydev.clock_api.error.ErrorCode.* import org.hydev.clock_api.error.ErrorCode.*
import org.hydev.clock_api.repository.UserRepository import org.hydev.clock_api.repository.UserRepository
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.* import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpEntity import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.test.context.ActiveProfiles import org.springframework.test.context.ActiveProfiles
@@ -22,7 +24,6 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.util.DigestUtils import org.springframework.util.DigestUtils
import org.springframework.util.LinkedMultiValueMap import org.springframework.util.LinkedMultiValueMap
import java.util.*
import javax.validation.ConstraintViolationException import javax.validation.ConstraintViolationException
// https://stackoverflow.com/questions/59097035/springboottest-vs-webmvctest-datajpatest-service-unit-tests-what-is-the-b // https://stackoverflow.com/questions/59097035/springboottest-vs-webmvctest-datajpatest-service-unit-tests-what-is-the-b
@@ -63,39 +64,51 @@ class UserRegisterDeleteNodeTest {
@Autowired @Autowired
private lateinit var restTemplate: TestRestTemplate private lateinit var restTemplate: TestRestTemplate
private fun Map<String, String>.headersToHttpEntity(): HttpEntity<String> {
val tempMultiValueMap = LinkedMultiValueMap<String, String>()
this.forEach { tempMultiValueMap[it.key] = listOf(it.value) }
return HttpEntity<String>(tempMultiValueMap)
}
private fun Map<String, String>.toHttpHeaders(): HttpHeaders {
val linkedMultiValueMap = LinkedMultiValueMap<String, String>()
this.forEach { linkedMultiValueMap.add(it.key, it.value) }
return HttpHeaders(linkedMultiValueMap)
}
// Post to register node with headers, expect 406 and ErrorCodes. // Post to register node with headers, expect 406 and ErrorCodes.
// todo: Using List instead of Array. // todo: Using List instead of Array.
private fun pTRWHsE406AECs(headerMap: Map<String, String>, expectedECList: Array<String>) { private fun pTRWHsE406AECs(headerMap: Map<String, String>, expectedECList: List<String>) {
val tempMultiValueMap = LinkedMultiValueMap<String, String>() mockMvc.perform(post(REGISTER_NODE).headers(headerMap.toHttpHeaders()))
headerMap.forEach { tempMultiValueMap[it.key] = listOf(it.value) } .andExpect(status().isNotAcceptable)
val httpEntity = HttpEntity<String>(tempMultiValueMap) .andExpect(content().json(expectedECList.toString()))
}
// Using exchange to custom headers, etc. Args: (node, method, headers, forObject). private fun pTRWHsEHS(headerMap: Map<String, String>, expectedHttpStatus: HttpStatus) {
// https://stackoverflow.com/questions/16781680/http-get-with-headers-using-resttemplate
val responseEntity = val responseEntity =
restTemplate.exchange(REGISTER_NODE, HttpMethod.POST, httpEntity, Array<String>::class.java) restTemplate.exchange(
REGISTER_NODE, HttpMethod.POST,
// Expect http status is 406 NOT ACCEPTABLE, and ErrorCode array are same. headerMap.headersToHttpEntity(), String::class.java
assertEquals(HttpStatus.NOT_ACCEPTABLE, responseEntity.statusCode) )
assertArrayEquals(expectedECList, responseEntity.body) assertEquals(expectedHttpStatus, responseEntity.statusCode)
} }
@Test @Test
// [A0101, A0102, A0101 + A0102] M1 * 2 + M2. // [A0101, A0102, A0101 + A0102] M1 * 2 + M2.
fun testWhenMissingField() { fun testWhenMissingField() {
pTRWHsE406AECs(mapOf(H_PASSWORD to V_PASSWORD), arrayOf(USER_NAME_IS_NULL)) pTRWHsEHS(mapOf(H_PASSWORD to V_PASSWORD), HttpStatus.BAD_REQUEST)
pTRWHsE406AECs(mapOf(H_USERNAME to V_USERNAME), arrayOf(USER_PASSWORD_IS_NULL)) pTRWHsEHS(mapOf(H_USERNAME to V_USERNAME), HttpStatus.BAD_REQUEST)
pTRWHsE406AECs(mapOf(), arrayOf(USER_NAME_IS_NULL, USER_PASSWORD_IS_NULL)) pTRWHsEHS(mapOf(), HttpStatus.BAD_REQUEST)
} }
@Test @Test
// [A0111, A0112, A0111 + A0112] W1 * 2 + W2. // [A0111, A0112, A0111 + A0112] W1 * 2 + W2.
fun testWhenNotMatchRegex() { fun testWhenNotMatchRegex() {
pTRWHsE406AECs(mapOf(H_USERNAME to "", H_PASSWORD to V_PASSWORD), arrayOf(USER_NAME_NOT_MATCH_REGEX)) pTRWHsE406AECs(mapOf(H_USERNAME to "", H_PASSWORD to V_PASSWORD), listOf(USER_NAME_NOT_MATCH_REGEX))
pTRWHsE406AECs(mapOf(H_USERNAME to V_USERNAME, H_PASSWORD to ""), arrayOf(USER_PASSWORD_NOT_MATCH_REGEX)) pTRWHsE406AECs(mapOf(H_USERNAME to V_USERNAME, H_PASSWORD to ""), listOf(USER_PASSWORD_NOT_MATCH_REGEX))
pTRWHsE406AECs( pTRWHsE406AECs(
mapOf(H_USERNAME to "", H_PASSWORD to ""), mapOf(H_USERNAME to "", H_PASSWORD to ""),
arrayOf(USER_NAME_NOT_MATCH_REGEX, USER_PASSWORD_NOT_MATCH_REGEX) listOf(USER_NAME_NOT_MATCH_REGEX, USER_PASSWORD_NOT_MATCH_REGEX)
) )
} }
@@ -149,14 +162,14 @@ class UserRegisterDeleteNodeTest {
// Post to delete node with headers and expect HttpStatus. // Post to delete node with headers and expect HttpStatus.
private fun pTDWHsAEHS(headerMap: Map<String, String>, expectedHttpStatus: HttpStatus) { private fun pTDWHsAEHS(headerMap: Map<String, String>, expectedHttpStatus: HttpStatus) {
val tempMultiValueMap = LinkedMultiValueMap<String, String>() val responseEntity = restTemplate.exchange(
headerMap.forEach { tempMultiValueMap[it.key] = listOf(it.value) } DELETE_NODE, HttpMethod.POST,
val httpEntity = HttpEntity<String>(tempMultiValueMap) headerMap.headersToHttpEntity(), String::class.java
)
val responseEntity = restTemplate.exchange(DELETE_NODE, HttpMethod.POST, httpEntity, String::class.java)
assertEquals(expectedHttpStatus, responseEntity.statusCode) assertEquals(expectedHttpStatus, responseEntity.statusCode)
} }
// -- New node test.
@Test @Test
fun testDeleteUser() { fun testDeleteUser() {
mockMvc.perform(post(REGISTER_NODE).header(H_USERNAME, V_USERNAME).header(H_PASSWORD, V_PASSWORD)) mockMvc.perform(post(REGISTER_NODE).header(H_USERNAME, V_USERNAME).header(H_PASSWORD, V_PASSWORD))