package edu.harvard.iq.dataverse.locality;

import edu.harvard.iq.dataverse.util.json.JsonUtil;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import org.junit.Test;

public class StorageSiteUtilTest {

    @Test
    public void testParse() throws Exception {
        JsonObjectBuilder job = Json.createObjectBuilder();
        job.add(StorageSite.HOSTNAME, "myHostname");
        job.add(StorageSite.NAME, "myName");
        job.add(StorageSite.PRIMARY_STORAGE, true);
        job.add(StorageSite.TRANSFER_PROTOCOLS, "rsync");
        StorageSite result = StorageSiteUtil.parse(job.build());
        result.setId(42l);
        String output = JsonUtil.prettyPrint(result.toJsonObjectBuilder().build().toString());
        System.out.println("output: " + output);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testMissingHostname() throws Exception {
        JsonObjectBuilder job = Json.createObjectBuilder();
        job.add(StorageSite.NAME, "myName");
        job.add(StorageSite.PRIMARY_STORAGE, true);
        job.add(StorageSite.TRANSFER_PROTOCOLS, "rsync");
        StorageSiteUtil.parse(job.build());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBadProtocol() throws Exception {
        JsonObjectBuilder job = Json.createObjectBuilder();
        job.add(StorageSite.HOSTNAME, "myHostname");
        job.add(StorageSite.NAME, "myName");
        job.add(StorageSite.PRIMARY_STORAGE, true);
        job.add(StorageSite.TRANSFER_PROTOCOLS, "junk");
        StorageSiteUtil.parse(job.build());
    }

}

@@ -89,16 +89,67 @@
 onUtil;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -1629,10 +1629,1878 @@
 %0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void testNonBoolean() throws Exception %7B%0A        JsonObjectBuilder job = Json.createObjectBuilder();%0A        job.add(StorageSite.HOSTNAME, %22myHostname%22);%0A        job.add(StorageSite.NAME, %22myName%22);%0A        job.add(StorageSite.PRIMARY_STORAGE, %22not a boolean%22);%0A        job.add(StorageSite.TRANSFER_PROTOCOLS, %22rsync%22);%0A        StorageSiteUtil.parse(job.build());%0A    %7D%0A%0A    @Test(expected = Exception.class)%0A    public void testSecondPrimaryNotAllowed() throws Exception %7B%0A        StorageSite newStorageSite = new StorageSite();%0A        newStorageSite.setPrimaryStorage(true);%0A        List%3CStorageSite%3E exitingSites = new ArrayList%3C%3E();%0A        StorageSite existingSite1 = new StorageSite();%0A        existingSite1.setPrimaryStorage(true);%0A        exitingSites.add(existingSite1);%0A        StorageSiteUtil.ensureOnlyOnePrimary(newStorageSite, exitingSites);%0A    %7D%0A%0A    @Test%0A    public void testSecondNonPrimaryIsAllowed() throws Exception %7B%0A        StorageSite newStorageSite = new StorageSite();%0A        List%3CStorageSite%3E exitingSites = new ArrayList%3C%3E();%0A        StorageSite existingSite1 = new StorageSite();%0A        existingSite1.setPrimaryStorage(true);%0A        exitingSites.add(existingSite1);%0A        StorageSiteUtil.ensureOnlyOnePrimary(newStorageSite, exitingSites);%0A    %7D%0A%0A    @Test%0A    public void testCanAddPrimaryWhenNoExistingPrimary() throws Exception %7B%0A        StorageSite newStorageSite = new StorageSite();%0A        newStorageSite.setPrimaryStorage(true);%0A        List%3CStorageSite%3E exitingSites = new ArrayList%3C%3E();%0A        StorageSite existingSite1 = new StorageSite();%0A        exitingSites.add(existingSite1);%0A        StorageSiteUtil.ensureOnlyOnePrimary(newStorageSite, exitingSites);%0A    %7D%0A%0A    @Test%0A    public void testJustForCodeCoverage() %7B%0A        new StorageSiteUtil();%0A    %7D%0A%0A
 %7D%0A

increase code coverage to 100% for StorageSiteUtil #4396