package com.izouma.immall.repo;

import com.izouma.immall.domain.BackUser;
import com.izouma.immall.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 java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BackUserRepoTest {
    @Autowired
    private BackUserRepo backUserRepo;

    @Test
    public void testUser() {
        BackUser user = BackUser.builder()
                .username("testuser")
                .password("123")
                .avatar("")
                .enabled(true)
                .build();
        backUserRepo.save(user);
    }

    @Test
    public void createUser() {

        BackUser user = backUserRepo.findByUsername("root");
        user.setPassword(new BCryptPasswordEncoder().encode("123456"));
        backUserRepo.save(user);
    }

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

    @Test
    public void deleteUser(){
        backUserRepo.deleteByBackUserId(2L);
    }

    @Test
    public void findAllUser(){
        List<BackUser> b = backUserRepo.findAll();
        System.out.println(b);
    }
}
