package org.sagebionetworks.bridge.models;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.sagebionetworks.bridge.dao.ParticipantOption.SharingScope;
import org.sagebionetworks.bridge.exceptions.InvalidEntityException;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class SharingOptionTest {

    @Test
    public void sharingOptionUsesCorrectDefaults() {
        ObjectNode node = JsonNodeFactory.instance.objectNode();
        node.put("scope", SharingScope.SPONSORS_AND_PARTNERS.name().toLowerCase());
        
        SharingOption option = SharingOption.fromJson(node, 1);
        assertEquals(SharingScope.NO_SHARING, option.getSharingScope());
        
        option = SharingOption.fromJson(node, 2);
        assertEquals(SharingScope.SPONSORS_AND_PARTNERS, option.getSharingScope());
        
        try {
            node = JsonNodeFactory.instance.objectNode();
            option = SharingOption.fromJson(node, 2);
            fail("Should have thrown an invalid entity exception");
        } catch(InvalidEntityException e) {
            assertTrue(e.getMessage().contains("scope is required"));
        }
    }
    
    @Test
    public void sharingOptionFailsGracefully() {
        SharingOption option = SharingOption.fromJson(null, 1);
        assertEquals(SharingScope.NO_SHARING, option.getSharingScope());
        
        try {
            option = SharingOption.fromJson(null, 11);
            fail("Should have thrown an invalid entity exception");
        } catch(InvalidEntityException e) {
            assertTrue(e.getMessage().contains("scope is required"));
        }        
    }
    
}

@@ -535,24 +535,25 @@
 efaults() %7B%0A
+%0A
         Obje
@@ -617,92 +617,170 @@
     
-node.put(%22scope%22, SharingScope.SPONSORS_AND_PARTNERS.name().toLowerCase());%0A        
+// These JSON strings are the expected API to the client%0A        // A test failure wound indicate broken APIs%0A        node.put(%22scope%22, %22sponsors_and_partners%22);%0A
 %0A   
@@ -909,33 +909,25 @@
 ngScope());%0A
-        
 %0A
+
         opti
@@ -1044,33 +1044,25 @@
 ngScope());%0A
-        
 %0A
+
         try 
@@ -1366,27 +1366,398 @@
        %7D
+%0A%0A        node = JsonNodeFactory.instance.objectNode();%0A        // The following JSON strings are the expected API to the client%0A        // A test failure wound indicate broken APIs%0A        node.put(%22scope%22, %22all_qualified_researchers%22);%0A        option = SharingOption.fromJson(node, 2);%0A        assertEquals(SharingScope.ALL_QUALIFIED_RESEARCHERS, option.getSharingScope());
 %0A    %7D%0A
-    
 %0A    @Te

Test sharing scope names as JSON strings