package com.izouma.nineth.repo;

import com.github.javafaker.Faker;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.security.Authority;
import org.apache.commons.lang3.RandomStringUtils;
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 java.util.*;

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

    @Test
    public void testUser() {
        User user = User.builder()
                .username("testuser")
                .password("123")
                .avatar("")
                .build();
        userRepo.save(user);

    }

    @Test
    public void createUser() {
        for (int i = 0; i < 30; i++) {
            Faker faker = new Faker(new Locale("zh-CN"));
            User user = User.builder()
                    .username("9th_" + RandomStringUtils.randomAlphabetic(8))
                    .nickname(faker.name().fullName())
                    .avatar(faker.avatar().image())
                    .authorities(new HashSet<>(Arrays.asList(Authority.get(AuthorityName.ROLE_USER), Authority.get(AuthorityName.ROLE_MINTER))))
                    .build();
            userRepo.save(user);
        }
    }

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