package com.docobo.contriumc3.APIConnection;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.cortrium.cortriumc3.ApiConnection.ApiConnectionManager;
import com.cortrium.cortriumc3.ApiConnection.models.Channels;
import com.cortrium.cortriumc3.ApiConnection.models.Device;
import com.cortrium.cortriumc3.ApiConnection.models.Recordings;
import com.cortrium.cortriumc3.CortriumC3Ecg;
import com.cortrium.cortriumc3.R;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by Kike Bodi on 10/07/2017.
 * Cortrium
 * bodi.inf@gmail.com
 */

@RunWith(AndroidJUnit4.class)
@SmallTest
public class ApiTest {

    private Context instrumentationContext;
    final CountDownLatch signal = new CountDownLatch(1);

    @Before
    public void setUp(){
        instrumentationContext = InstrumentationRegistry.getContext();
    }



    @Test
    public void testGET() throws InterruptedException {
        String url = InstrumentationRegistry.getTargetContext().getString(R.string.api_url);
        ApiConnectionManager apiManager = new ApiConnectionManager(url);
        apiManager.getRecordingFromAPI("5bf2eb60-655d-11e7-8c8c-d5743dc97a59");

        signal.await(3, TimeUnit.SECONDS);
        Assert.assertTrue(ApiConnectionManager.successfullGetRequest);
    }

    public void testGET(String id) throws InterruptedException {
        String url = InstrumentationRegistry.getTargetContext().getString(R.string.api_url);
        ApiConnectionManager apiManager = new ApiConnectionManager(url);
        apiManager.getRecordingFromAPI(id);

        //signal.await(3, TimeUnit.SECONDS);
        //Assert.assertTrue(ApiConnectionManager.successfullGetRequest);
    }

    @Test
    public void testPOST() throws InterruptedException {
        String recordingName = "recording_test"+System.currentTimeMillis();
        String url = InstrumentationRegistry.getTargetContext().getString(R.string.api_url);
        ApiConnectionManager connector = new ApiConnectionManager(url);

        File mFile = createFile(recordingName);
        Recordings recording = createRecording(recordingName);
        connector.postRecordingToAPI(recording,mFile);

        signal.await(10, TimeUnit.SECONDS);
        Assert.assertTrue(ApiConnectionManager.successfullPostRequest);
    }

    @Test
    public void testDELETE() throws InterruptedException {
        String url = InstrumentationRegistry.getTargetContext().getString(R.string.api_url);
        ApiConnectionManager connector = new ApiConnectionManager(url);
        connector.deleteRecording(ApiConnectionManager.lastRecordingId);
        //connector.deleteRecording("0a9dc650-6584-11e7-8c8c-d5743dc97a59");
        signal.await(3, TimeUnit.SECONDS);
        testGET(ApiConnectionManager.lastRecordingId);
        signal.await(3, TimeUnit.SECONDS);
        Assert.assertTrue(!ApiConnectionManager.successfullGetRequest);
    }

    private Recordings createRecording(String filename){
        Channels myChannels = new Channels(true, true, true, true);
        Device myDevice = new Device("C3-RetrofitTest",null,null,null,null,null);
        Recordings myRecording = new Recordings(null,filename,10,myDevice,myChannels,null,null);

        return myRecording;
    }

    private File createFile(String filename){

        File file = new File(instrumentationContext.getExternalFilesDir(null), filename);
        String path = instrumentationContext.getExternalFilesDir(null)+"/"+filename;
        try {
            FileOutputStream outputStream = instrumentationContext.openFileOutput(filename, Context.MODE_PRIVATE);
            outputStream.write("hjghjghghg".getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return file;
    }
}

@@ -71,16 +71,47 @@
 ontext;%0A
+import android.os.Environment;%0A
 import a
@@ -155,16 +155,16 @@
 gistry;%0A
-
 import a
@@ -1937,127 +1937,8 @@
 id);
-%0A%0A        //signal.await(3, TimeUnit.SECONDS);%0A        //Assert.assertTrue(ApiConnectionManager.successfullGetRequest);
 %0A   
@@ -2275,32 +2275,16 @@
 e = 
-createFile(recordingName
+getFile(
 );%0A 
@@ -2329,29 +2329,31 @@
 cording(
-recording
+mFile.get
 Name
+()
 );%0A     
@@ -2511,32 +2511,106 @@
 llPostRequest);%0A
+        Assert.assertTrue(ApiConnectionManager.successfullUploadRequest);%0A
     %7D%0A%0A    @Test
@@ -3019,32 +3019,108 @@
 eUnit.SECONDS);%0A
+        //It always return true so we need to do a GET request to check it.%0A
         testGET(
@@ -3629,49 +3629,92 @@
    p
-rivate File createFile(String filename)%7B%0A
+ublic File getFile()%7B%0A        //Change for a file in your phone. Check also the path
 %0A   
@@ -3723,17 +3723,20 @@
    File 
-f
+bleF
 ile = ne
@@ -3742,17 +3742,17 @@
 ew File(
-i
+I
 nstrumen
@@ -3761,98 +3761,35 @@
 tion
-Context.getExternalFilesDir(null), filename);%0A        String path = instrumentation
+Registry.getTarget
 Context
+()
 .get
@@ -3814,461 +3814,96 @@
 ull)
-+%22/%22+filename;%0A        try %7B%0A            FileOutputStream outputStream = ins
+,%22Cor
 tr
+i
 um
-entationContext.openFileOutput(filename, Context.MODE_PRIVATE);%0A            outputStream.write(%22hjghjghghg%22.getBytes());%0A            outputStream.close();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        try %7B%0A            file.createNewFile();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return f
+C3Data/2017-05-19-12-36-24-C3-849C7913F5D4/591EE6C8.BLE%22);%0A        return bleF
 ile;

Test for upload File