package org.openrefine.wikidata.testing;

import static org.mockito.Mockito.mock;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.refine.ProjectManager;
import com.google.refine.ProjectManagerStub;
import com.google.refine.ProjectMetadata;
import com.google.refine.RefineServlet;
import com.google.refine.RefineServletStub;
import com.google.refine.RefineTest;
import com.google.refine.importers.SeparatorBasedImporter;
import com.google.refine.importing.ImportingJob;
import com.google.refine.importing.ImportingManager;
import com.google.refine.model.Project;

public class WikidataRefineTest extends PowerMockTestCase {
	protected File workspaceDir;
    protected RefineServlet servlet;
    private List<Project> projects = new ArrayList<Project>();
    private List<ImportingJob> importingJobs = new ArrayList<ImportingJob>();
    
    public Project createCSVProject(String input) {
    	return createCSVProject("test project", input);
    }
    
    protected Project createCSVProject(String projectName, String input) {
        Project project = new Project();
        
        ProjectMetadata metadata = new ProjectMetadata();
        metadata.setName(projectName);
        
        ObjectNode options = mock(ObjectNode.class);
        RefineTest.prepareImportOptions(options, ",", -1, 0, 0, 1, false, false);
        
        ImportingJob job = ImportingManager.createJob();
        
        SeparatorBasedImporter importer = new SeparatorBasedImporter();
        
        List<Exception> exceptions = new ArrayList<Exception>();
        importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
        project.update();
        ProjectManager.singleton.registerProject(project, metadata);
        
        projects.add(project);
        importingJobs.add(job);
        return project;
    }
    
    @BeforeMethod(alwaysRun = true)
    public void initServlet() {
    	servlet = new RefineServletStub();
        ProjectManager.singleton = new ProjectManagerStub();
        ImportingManager.initialize(servlet);
    }
}
