package com.izouma.nineth.service;

import com.github.kevinsawicki.http.HttpRequest;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.service.storage.StorageService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class UserServiceTest extends ApplicationTests {

    @Autowired
    private UserService    userService;
    @Autowired
    private UserRepo       userRepo;
    @Autowired
    private StorageService storageService;

    @Test
    public void findByUsernameAndDelFalse1() {
        userService.clearCache();
        userRepo.findByUsernameAndDelFalse("admin");
        userRepo.findByUsernameAndDelFalse("admin");
    }

    @Test
    public void searchByPhone() {
        System.out.println(userService.searchByPhone("15077886171"));
    }

    @Test
    public void sss9() throws IOException {
        for (User user : userRepo.findAll()) {
            if (user.getAvatar().startsWith("https://cdn")) {
                HttpRequest request = HttpRequest.get(user.getAvatar());
                File file = File.createTempFile("gw1", "1sadga");
                request.receive(file);
                user.setAvatar(storageService.uploadFromInputStream(new FileInputStream(file), "image/" + RandomStringUtils.randomAlphabetic(12) + ".jpg"));
                userRepo.save(user);
            }
        }
    }
}