package com.kryptnostic.client.tests;

import javax.inject.Inject;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.kryptnostic.kodex.v1.client.KryptnosticClient;
import com.kryptnostic.kodex.v1.exceptions.types.BadRequestException;
import com.kryptnostic.kodex.v1.exceptions.types.ResourceNotFoundException;
import com.kryptnostic.storage.v1.models.Document;

/**
 * Kryptnostic client tests. Primarily testing the functionality of KryptnosticConnection.
 * 
 * @author Nick Hewitt
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = TestConfiguration.class)
public class ClientTests {
    @Inject
    private KryptnosticClient kryptnosticClient;

    @Test
    public void uploadDocumentGetDocumentTest() {
        String document = "lo and behold.";
        String id = null;
        try {
            id = kryptnosticClient.uploadDocument(document);
        } catch (BadRequestException e) {
            e.printStackTrace();
        }
        Document retrieved = null;
        try {
            retrieved = kryptnosticClient.getDocument(id);
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(retrieved);
        Assert.assertEquals(retrieved.getBody(), document);
    }

    @Test
    public void updateDocumentTest() {
        String document = "what is best in life? the wind in your hair. falcons at your wrist.";
        String id = null;
        try {
            id = kryptnosticClient.uploadDocument(document);
        } catch (BadRequestException e) {
            e.printStackTrace();
        }
        String newDocument = "what is best in life? To crush your enemies, see them driven before you, and to hear the lamentation of the loved ones.";
        try {
            kryptnosticClient.updateDocument(id, newDocument);
        } catch (ResourceNotFoundException e1) {
            e1.printStackTrace();
        }

        Document retrieved = null;
        try {
            retrieved = kryptnosticClient.getDocument(id);
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(retrieved);
        Assert.assertEquals(retrieved, newDocument);
    }
}

@@ -1523,32 +1523,42 @@
 rt.assertEquals(
+document, 
 retrieved.getBod
@@ -1564,18 +1564,8 @@
 dy()
-, document
 );%0A 
@@ -2497,19 +2497,8 @@
 als(
-retrieved, 
 newD
@@ -2500,23 +2500,44 @@
 (newDocument
+, retrieved.getBody()
 );%0A    %7D%0A%7D%0A

fix failing document test