package models;

import static org.fest.assertions.Assertions.assertThat;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.junit.Ignore;
import org.junit.Test;

import playRepository.Commit;
import playRepository.GitRepository;

/**
 * @author "Hwi Ahn"
 *
 */
public class ProjectTest extends ModelTest<Project> {

    @Test
    public void create() throws Exception {
        // Given
        Project project = new Project();
        project.name = "prj_test";
        project.overview = "Overview for prj_test";
        project.share_option = false;
        project.vcs = "GIT";
        // When
        Project.create(project);
        // Then
        Project actualProject = Project.find.byId(project.id);
        
        assertThat(actualProject).isNotNull();
        assertThat(actualProject.name).isEqualTo("prj_test");
        assertThat(actualProject.siteurl).isEqualTo("http://localhost:9000/prj_test");
    }
    
    @Test
    public void findMilestonesById() throws Exception {
        // Given
        // When
        Project sut = Project.find.byId(1l);
        // Then
        assertThat(sut.milestones.size()).isEqualTo(2);
    }
    
    @Test
    public void findIssueById() throws Exception {
        // Given
        // When
        Project sut = Project.find.byId(1l);
        // Then
        assertThat(sut.issues.size()).isEqualTo(7);
    }

    @Test
    public void delete() throws Exception {
        // Given
        // When
        Project.delete(1l);
        flush();
        // Then
        assertThat(Project.find.byId(1l)).isNull();
        assertThat(ProjectUser.findByIds(1l, 1l)).isNull();
        assertThat(Issue.findById(1l)).isNull();
        assertThat(Milestone.findById(1l)).isNull();
    }
    
    @Test
    public void findById() throws Exception {
        // Given
        // When
        Project project = Project.find.byId(1l);
        // Then
        assertThat(project.name).isEqualTo("nForge4java");
        assertThat(project.overview).isEqualTo("nFORGE는 소프트웨어 개발에 필요한 기능들을 사용하기 편리하게 웹으로 묶은 협업 개발 플랫폼입니다.");
        assertThat(project.share_option).isEqualTo(true);
        assertThat(project.vcs).isEqualTo("GIT");
        assertThat(project.siteurl).isEqualTo("http://localhost:9000/nForge4java");
      
    }
    
    @Test
    public void isOnlyManager() throws Exception {
        // Given
        // When
        List<Project> projectsHobi = Project.isOnlyManager(2l);
        List<Project> projectsEungjun = Project.isOnlyManager(5l);
        // Then
        assertThat(projectsHobi.size()).isEqualTo(1);
        assertThat(projectsEungjun.size()).isEqualTo(0);
    }

    @Test
    public void findProjectsByMember() throws Exception {
        // Given
        // When
        List<Project> projects = Project.findProjectsByMember(2l);
        // Then
        assertThat(projects.size()).isEqualTo(2);
    }

    @Test
    public void findByNameAndOwner() throws Exception {
        // Given
        String userName = "hobi";
        String projectName = "nForge4java";
        // When
        Project project = Project.findByNameAndOwner(userName, projectName);
        // Then
        assertThat(project.id).isEqualTo(1l);
    }

    @Test
    public void isProject() throws Exception {
        // Given
        String userName = "hobi";
        String projectName = "nForge4java";
        String newProjectName = "NanumFont";
        // When
        boolean result1 = Project.isProject(userName, projectName);
        boolean result2 = Project.isProject(userName, newProjectName);
        // Then
        assertThat(result1).isEqualTo(true);
        assertThat(result2).isEqualTo(false);
    }

    @Test
    public void projectNameChangeable() throws Exception {
        // Given
        String userName = "hobi";
        Long projectId = 1l;
        String newProjectName1 = "HelloSocialApp";
        String newProjectName2 = "NanumFont";
        // When
        boolean result1 = Project.projectNameChangeable(projectId, userName, newProjectName1);
        boolean result2 = Project.projectNameChangeable(projectId, userName, newProjectName2);
        // Then
        assertThat(result1).isEqualTo(false);
        assertThat(result2).isEqualTo(true);
    }

    @Test
    public void lastUpdate() throws Exception {
        // given
        String userName = "hobi";
        String projectName = "testProject";
        String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName;

        String repoPath = wcPath + "/.git";
        File repoDir = new File(repoPath);
        Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();
        repo.create(false);

        Git git = new Git(repo);
        String testFilePath = wcPath + "/readme.txt";
        BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));

        out.write("hello 1");
        out.flush();
        git.add().addFilepattern("readme.txt").call();
        git.commit().setMessage("commit 1").call();

        GitRepository gitRepo = new GitRepository(userName, projectName + "/");

//    	Project project = Project.findByNameAndOwner(userName, projectName);
    	// When
    	List<Commit> history = gitRepo.getHistory(0, 2, "HEAD");
    	Commit commit = history.get(0);
    	// Then
    	assertThat(commit.getAuthorDate()).isNotNull();
    }
}

@@ -2853,17 +2853,17 @@
 EqualTo(
-1
+0
 );%0A     
@@ -4519,1109 +4519,7 @@
    %7D
-%0A%0A    @Test%0A    public void lastUpdate() throws Exception %7B%0A        // given%0A        String userName = %22hobi%22;%0A        String projectName = %22testProject%22;%0A        String wcPath = GitRepository.getRepoPrefix() + userName + %22/%22 + projectName;%0A%0A        String repoPath = wcPath + %22/.git%22;%0A        File repoDir = new File(repoPath);%0A        Repository repo = new RepositoryBuilder().setGitDir(repoDir).build();%0A        repo.create(false);%0A%0A        Git git = new Git(repo);%0A        String testFilePath = wcPath + %22/readme.txt%22;%0A        BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath));%0A%0A        out.write(%22hello 1%22);%0A        out.flush();%0A        git.add().addFilepattern(%22readme.txt%22).call();%0A        git.commit().setMessage(%22commit 1%22).call();%0A%0A        GitRepository gitRepo = new GitRepository(userName, projectName + %22/%22);%0A%0A//    %09Project project = Project.findByNameAndOwner(userName, projectName);%0A    %09// When%0A    %09List%3CCommit%3E history = gitRepo.getHistory(0, 2, %22HEAD%22);%0A    %09Commit commit = history.get(0);%0A    %09// Then%0A    %09assertThat(commit.getAuthorDate()).isNotNull();%0A    %7D
 %0A%7D%0A

remove and fix test case