package com.izouma.zhirongip.repo;

import com.izouma.zhirongip.domain.User;
import com.izouma.zhirongip.enums.AuthorityName;
import com.izouma.zhirongip.security.Authority;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;

import javax.persistence.criteria.Predicate;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepoTest {
    @Autowired
    private UserRepo userRepo;

    @Test
    public void testUser() {
        User user = User.builder()
                .username("root")
                .password(new BCryptPasswordEncoder().encode("123456"))
                .avatar("")
                .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_ADMIN)))
                .build();
        userRepo.save(user);
    }

    @Test
    public void createUser() {

        User user = userRepo.findByUsernameAndDelFalse("root");
        user.setPassword(new BCryptPasswordEncoder().encode("123456"));
        userRepo.save(user);
    }

    @Test
    public void findAllByAuthoritiesContains() {
        List<User> list = userRepo.findAllByAuthoritiesContainsAndDelFalse(Authority.builder()
                .name("ROLE_ADMIN")
                .build());
        System.out.println(list);
    }

    @Test
    public void test() {

        System.out.println(userRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
            Predicate equal = criteriaBuilder.equal(root.get("authorities").get("name"), "ROLE_ADMIN");
            return criteriaBuilder.and(equal);
        })));
    }

    @Test
    public void test1() {
        String phone = "18011115555";
        String phone1 = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        System.out.println(phone1);
    }

    @Test
    public void  test2(){
        System.out.println(Pattern.matches("^\\d+,\\d+$", "10,100"));
    }
}
