package org.sagebionetworks.bridge.sdk.integration;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.bridge.rest.api.ForConsentedUsersApi;
import org.sagebionetworks.bridge.rest.model.Message;
import org.sagebionetworks.bridge.rest.model.ParticipantFile;
import org.sagebionetworks.bridge.rest.model.ParticipantFileList;
import org.sagebionetworks.bridge.user.TestUserHelper;
import org.sagebionetworks.bridge.user.TestUserHelper.TestUser;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class ParticipantFileTest {

    TestUser participant;

    ForConsentedUsersApi userApi;

    ParticipantFile file;

    @Before
    public void before() throws Exception {
        participant = TestUserHelper.createAndSignInUser(ParticipantFileTest.class, true);
        userApi = participant.getClient(ForConsentedUsersApi.class);
    }

    @After
    public void after() throws Exception {
        List<ParticipantFile> remainder = userApi.getParticipantFiles(null, 10).execute().body().getItems();
        while (!remainder.isEmpty()) {
            for (ParticipantFile file : remainder) {
                userApi.deleteParticipantFile(file.getFileId()).execute();
            }
            userApi.getParticipantFiles(null, 10).execute().body().getItems();
        }
        participant.signOutAndDeleteUser();
    }

    @Test
    public void crudParticipantFile() throws Exception {
        file = new ParticipantFile();
        file.setMimeType("text/plain");

        ForConsentedUsersApi userApi = participant.getClient(ForConsentedUsersApi.class);

        final ParticipantFile keys = userApi.createParticipantFile("file_id", file).execute().body();

        assertNotNull(keys);
        assertEquals(keys.getAppId(), "api");
        assertEquals(keys.getMimeType(), file.getMimeType());
        String uploadUrl = keys.getUploadUrl();

        URL url = new URL(uploadUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Content-Type", "text/plain");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text uploaded as an object via presigned URL.");
        out.close();
        assertEquals(connection.getResponseCode(), 200);
        connection.disconnect();

        ParticipantFileList results = userApi.getParticipantFiles(null, 5).execute().body();
        assertNotNull(results);
        List<ParticipantFile> resultList = results.getItems();
        assertEquals(resultList.size(), 1);
        ParticipantFile onlyFile = resultList.get(0);
        assertEquals(onlyFile.getMimeType(), "text/plain");
        assertEquals(onlyFile.getUserId(), participant.getUserId());
        assertEquals(onlyFile.getFileId(), "file_id");
        assertEquals(onlyFile.getAppId(), "api");

        for (int i = 0; i < 5; i++) {
            file = new ParticipantFile();
            file.setMimeType("text/plain");
            userApi.createParticipantFile(i + "file", file).execute();
        }

        results = userApi.getParticipantFiles(null, 5).execute().body();
        assertNotNull(results);
        resultList = results.getItems();
        assertEquals(resultList.size(), 5);
        String nextKey = results.getNextPageOffsetKey();
        results = userApi.getParticipantFiles(nextKey, 5).execute().body();
        assertNotNull(results);
        resultList = results.getItems();
        assertEquals(resultList.size(), 1);
        assertNull(results.getNextPageOffsetKey());

        Message deleteMessage = userApi.deleteParticipantFile("file_id").execute().body();
        assertNotNull(deleteMessage);
        assertEquals(deleteMessage.getMessage(), "Participant file deleted.");

        for (int i = 0; i < 5; i++) {
            userApi.deleteParticipantFile(i + "file").execute();
        }

        results = userApi.getParticipantFiles(null, 5).execute().body();
        assertNotNull(results);
        resultList = results.getItems();
        assertEquals(resultList.size(), 0);
    }
}

@@ -46,16 +46,45 @@
 ation;%0A%0A
+import okhttp3.ResponseBody;%0A
 import o
@@ -95,24 +95,24 @@
 unit.After;%0A
-
 import org.j
@@ -514,16 +514,44 @@
 tUser;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -655,16 +655,42 @@
 il.List;
+%0Aimport java.util.Scanner;
 %0A%0Aimport
@@ -854,16 +854,116 @@
 Test %7B%0A%0A
+    static final String TEST_UPLOAD_STRING = %22This text uploaded as an object via presigned URL.%22;%0A%0A
     Test
@@ -1613,32 +1613,44 @@
    %7D%0A           
+ remainder =
  userApi.getPart
@@ -2667,60 +2667,26 @@
 ite(
-%22This text uploaded as an object via presigned URL.%22
+TEST_UPLOAD_STRING
 );%0A 
@@ -3433,32 +3433,32 @@
 (%22text/plain%22);%0A
-
             user
@@ -3515,32 +3515,538 @@
 e();%0A        %7D%0A%0A
+%0A        ResponseBody body = userApi.getParticipantFile(%22file_id%22).execute().body();%0A        assertNotNull(body);%0A        assertNotNull(body.contentType());%0A        assertEquals(body.contentType().toString(), %22text/plain%22);%0A        assertEquals(body.contentLength(), TEST_UPLOAD_STRING.length());%0A        try (InputStream content = body.byteStream()) %7B%0A            Scanner sc = new Scanner(content);%0A            assertEquals(sc.nextLine(), %22This text uploaded as an object via presigned URL.%22);%0A        %7D%0A%0A
         results 

Fix getPFile bug