package fi.helsinki.cs.tmc.cli.tmcstuff;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import fi.helsinki.cs.tmc.cli.Application;
import fi.helsinki.cs.tmc.core.domain.Course;

import org.apache.commons.io.FileUtils;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class TmcUtilTest {
    Application app;

    @Before
    public void setUp() {
        app = new Application();
        app.createTmcCore(new Settings());
    }

    @Test
    public void findCouseIfItExists() {
        Course course;
        app.createTmcCore(new Settings(true));
        course = TmcUtil.findCourse(app.getTmcCore(), "demo");
        assertNotNull(course);
    }

    @Test
    public void returnNullIfCourseWontExist() {
        Course course;
        course = TmcUtil.findCourse(app.getTmcCore(), "afuwhf");
        assertNull(course);
    }

    @Test
    public void findExerciseIfItExists() {
        Course course;
        course = TmcUtil.findCourse(app.getTmcCore(), "demo");
        assertNotNull(course);
        TmcUtil.findExercise(course, "viikko1-002.HeiMaailma");
    }

    @Test
    public void returnNullIfExerciseWontExist() {
        Course course;
        course = TmcUtil.findCourse(app.getTmcCore(), "demo");
        assertNotNull(course);
        TmcUtil.findExercise(course, "xhu4ew");
    }

    @Test
    public void downloadExercise() {
        Course course;
        course = TmcUtil.findCourse(app.getTmcCore(), "demo");
        assertNotNull(course);
        TmcUtil.findExercise(course, "xhu4ew");
    }

    @Test
    public void downloadFailsIfCourseDontExist() {
        Course course;
        String name = "urfhuw";
        course = TmcUtil.findCourse(app.getTmcCore(), name);
        assertNotNull(course);
        TmcUtil.downloadAllExercises(app.getTmcCore(), course);

        boolean exists = Files.exists(Paths.get("./" + name));
        assertFalse(exists);
        if (exists) {
            try {
                FileUtils.deleteDirectory(new File(name));
            } catch (IOException e) {
            }
        }
    }

    @Test
    public void downloadExercisesWorks() {
        Course course;
        String name = "demo";
        course = TmcUtil.findCourse(app.getTmcCore(), name);
        assertNotNull(course);
        TmcUtil.downloadAllExercises(app.getTmcCore(), course);

        boolean exists = Files.exists(Paths.get("./" + name));
        assertTrue(exists);
        try {
            FileUtils.deleteDirectory(new File(name));
        } catch (IOException e) {
        }
    }
}

@@ -657,20 +657,144 @@
 ettings(
+true
 ));%0A
+        try %7B%0A            FileUtils.deleteDirectory(new File(%22cert-test%22));%0A        %7D catch (IOException e) %7B%0A        %7D%0A
     %7D%0A%0A 
@@ -825,16 +825,17 @@
  findCou
+r
 seIfItEx
@@ -870,55 +870,8 @@
 se;%0A
-        app.createTmcCore(new Settings(true));%0A
     
@@ -1625,760 +1625,8 @@
  %7D%0A%0A
-    @Test%0A    public void downloadExercise() %7B%0A        Course course;%0A        course = TmcUtil.findCourse(app.getTmcCore(), %22demo%22);%0A        assertNotNull(course);%0A        TmcUtil.findExercise(course, %22xhu4ew%22);%0A    %7D%0A%0A    @Test%0A    public void downloadFailsIfCourseDontExist() %7B%0A        Course course;%0A        String name = %22urfhuw%22;%0A        course = TmcUtil.findCourse(app.getTmcCore(), name);%0A        assertNotNull(course);%0A        TmcUtil.downloadAllExercises(app.getTmcCore(), course);%0A%0A        boolean exists = Files.exists(Paths.get(%22./%22 + name));%0A        assertFalse(exists);%0A        if (exists) %7B%0A            try %7B%0A                FileUtils.deleteDirectory(new File(name));%0A            %7D catch (IOException e) %7B%0A            %7D%0A        %7D%0A    %7D%0A%0A
     
@@ -1720,20 +1720,25 @@
 name = %22
-demo
+cert-test
 %22;%0A     

Fix tmc util tests.