Junit Test Application-15-회원가입 컨트롤러 정규표현식과 Dto 적용

jaegeunsong97·2023년 8월 4일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

정규표현식 자료

dto에 정규표현식 적용

package shop.mtcoding.bank.dto.user;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import lombok.Getter;
import lombok.Setter;
import shop.mtcoding.bank.domain.user.User;
import shop.mtcoding.bank.domain.user.UserEnum;

public class UserRequestDto {

     @Getter
     @Setter
     public static class JoinRequestDto {

          @Pattern(regexp = "^[a-zA-Z0-9]{2,20}$", message = "영문/숫자 2~20자 이내로 작성해주세요. ")
          @NotEmpty // null, empty 불가능
          private String username;
          @Size(min = 4, max = 20)
          @NotEmpty
          private String password;
          @Pattern(regexp = "^[a-zA-Z0-9]{2,10}@[a-zA-Z0-9]{2,6}\\.[a-zA-Z]{2,3}$", message = "이메일 형식으로 작성해주세요. ")
          @NotEmpty
          private String email;
          @Pattern(regexp = "^[a-zA-Z가-힣]{1,20}$", message = "한글/영문 1~20자 이내로 작성해주세요. ")
          @NotEmpty
          private String fullname;

          public User toEntity(BCryptPasswordEncoder passwordEncoder) {
               return User.builder()
                         .username(username)
                         .password(passwordEncoder.encode(password))
                         .email(email)
                         .fullname(fullname)
                         .role(UserEnum.CUSTOMER)
                         .build();
          }
     }
}

정규표현식 학습

package shop.mtcoding.bank.temp;

import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

// java.util.regex.Pattern
public class RegexTest {

     @Test
     public void 한글만된다_test() throws Exception {
          String value = "한글";
          boolean result = Pattern.matches("^[가-힣]+$", value);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void 한글은안된다_test() throws Exception {
          String value = "abc";
          boolean result = Pattern.matches("^[^ㄱ-ㅎ가-힣]*$", value);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void 영어만된다_test() throws Exception {
          String value = "ssar";
          boolean result = Pattern.matches("^[a-zA-Z]+$", value);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void 영어는안된다_test() throws Exception {
          String value = "가22";
          boolean result = Pattern.matches("^[^a-zA-Z]*$", value);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void 영어와숫자만된다_test() throws Exception {
          String value = "ab12";
          boolean result = Pattern.matches("^[a-zA-Z0-9]+$", value);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void 영어만되고_길이는최소2최대4이다_test() throws Exception {
          String value = "ssar";
          boolean result = Pattern.matches("^[a-zA-Z]{2,4}$", value);
          System.out.println("테스트 : " + result);
     }

     // username, email, fullname
     @Test
     public void user_username_test() throws Exception {
          String username = "ssar";
          boolean result = Pattern.matches("^[a-zA-Z0-9]{2,20}$", username);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void user_fullname_test() throws Exception {
          String fullname = "메타코딩";
          boolean result = Pattern.matches("^[a-zA-Z가-힣]{1,20}$", fullname);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void user_email_test() throws Exception {
          String fullname = "ssaraa@nate.com"; // ac.kr co.kr or.kr
          boolean result = Pattern.matches("^[a-zA-Z0-9]{2,10}@[a-zA-Z0-9]{2,6}\\.[a-zA-Z]{2,3}$", fullname);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void account_gubun_test1() throws Exception {
          String gubun = "DEPOSIT"; // ac.kr co.kr or.kr
          boolean result = Pattern.matches("^(DEPOSIT)$", gubun);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void account_gubun_test2() throws Exception {
          String gubun = "TRANSFER"; // ac.kr co.kr or.kr
          boolean result = Pattern.matches("^(DEPOSIT|TRANSFER)$", gubun);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void account_tel_test1() throws Exception {
          String tel = "010-3333-7777"; // ac.kr co.kr or.kr
          boolean result = Pattern.matches("^[0-9]{3}-[0-9]{4}-[0-9]{4}", tel);
          System.out.println("테스트 : " + result);
     }

     @Test
     public void account_tel_test2() throws Exception {
          String tel = "01033337777"; // ac.kr co.kr or.kr
          boolean result = Pattern.matches("^[0-9]{11}", tel);
          System.out.println("테스트 : " + result);
     }
}
profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글