package edu.mayo.ve.FunctionalTests;

import edu.mayo.concurrency.workerQueue.WorkerPool;
import edu.mayo.ve.SecurityUserAppHelper;
import edu.mayo.ve.VCFLoaderPool;
import edu.mayo.ve.VCFParser.LoadWorker;
import edu.mayo.ve.VCFParser.VCFParser;
import edu.mayo.ve.resources.VCFUploadResource;
import edu.mayo.ve.resources.WorkerPoolManager;
import edu.mayo.ve.util.Tokens;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Random;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

public class VCFUploadResourceITCase {

    private final static long   RANDOM_LONG         = (new Random(System.currentTimeMillis()).nextLong());
    private final static String DUMMY_USER_ID       = "user_id_" + RANDOM_LONG;
    private final static String DUMMY_USER_TOKEN    = "user_token_" + RANDOM_LONG;
    private final static String DUMMY_ALIAS         = "alias_" + RANDOM_LONG;

    private SecurityUserAppHelper mockHelper;

    private static WorkerPool wp;

    @BeforeClass
    public static void setupLoaderPool() {
        LoadWorker logic = new LoadWorker(new VCFParser(), 50000);
        wp = new WorkerPool(logic, 1);
        VCFLoaderPool.setWp(wp);
        WorkerPoolManager.registerWorkerPool(Tokens.VCF_WORKERS, wp);
    }

    @AfterClass
    public static void tearDownLoaderPool() throws InterruptedException {
        wp.shutdown(1);
    }

    @Before
    public void setupMocks() throws Exception {
        mockHelper = mock(SecurityUserAppHelper.class);

        // mockito's way of mocking a method (e.g. registerWorkspace()) that returns void
        doNothing().when(mockHelper).registerWorkspace(eq(DUMMY_USER_ID), eq(DUMMY_USER_TOKEN), anyString(), eq(DUMMY_ALIAS));
    }


    @Test
    public void uploadFile() throws Exception {

        VCFUploadResource uploadResource = new VCFUploadResource(mockHelper);

        File vcf = new File("src/test/resources/testData/VCFUploadResourceITCase.vcf");
        InputStream inStream = new FileInputStream(vcf);

        String reporting = "FALSE";
        String compression = vcf.getName();
        uploadResource.uploadFile(DUMMY_USER_ID, DUMMY_ALIAS, reporting, compression, DUMMY_USER_TOKEN, inStream);

        verify(mockHelper).registerWorkspace(eq(DUMMY_USER_ID), eq(DUMMY_USER_TOKEN), anyString(), eq(DUMMY_ALIAS));
    }
}

@@ -31,16 +31,212 @@
 Tests;%0A%0A
+import com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParser;%0Aimport com.mongodb.BasicDBObject;%0Aimport com.mongodb.DB;%0Aimport com.mongodb.DBCollection;%0A
 import e
@@ -271,32 +271,70 @@
 eue.WorkerPool;%0A
+import edu.mayo.util.MongoConnection;%0A
 import edu.mayo.
@@ -470,24 +470,63 @@
 .VCFParser;%0A
+import edu.mayo.ve.resources.ExeQuery;%0A
 import edu.m
@@ -749,16 +749,50 @@
 .Test;%0A%0A
+import javax.ws.rs.core.Response;%0A
 import j
@@ -890,16 +890,67 @@
 andom;%0A%0A
+import static junit.framework.Assert.assertEquals;%0A
 import s
@@ -2645,16 +2645,29 @@
 %0A       
+ Response r =
  uploadR
@@ -2879,21 +2879,750 @@
 MMY_ALIAS));
+%0A%0A        //get the workspace id from the raw response and check there is one variant in there%0A        String json = r.getEntity().toString().replaceAll(%22File uploaded and workspace constructed :%22,%22%22);%0A        JsonElement jelement = new JsonParser().parse(json);%0A        JsonObject jobject = jelement.getAsJsonObject();%0A        String key = jobject.getAsJsonPrimitive(%22key%22).getAsString();%0A        assertEquals(1, count(key));%0A%0A        System.out.println(key);%0A%0A    %7D%0A%0A    private long count(String workspace)%7B%0A        ExeQuery q = new ExeQuery();%0A        DB db = MongoConnection.getDB();%0A        DBCollection col = db.getCollection(workspace);%0A        long count = q.countResults(col, new BasicDBObject());%0A        return count;
 %0A    %7D%0A%7D%0A

version checks file is actually uploaded!