package dk.statsbiblioteket.medieplatform.autonomous;

import org.testng.Assert;
import org.testng.annotations.Test;

import dk.statsbiblioteket.doms.central.connectors.fedora.pidGenerator.PIDGeneratorException;

import javax.xml.bind.JAXBException;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Properties;

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

public class SBOIEventIndexTest {

    @Test(groups = {"externalTest"}, enabled = true)
    public void testGetBatches() throws Exception {
        Properties props = getProperties();

        SBOIEventIndex summa = getSboiClient(props);
        Iterator<Batch> batches = summa.findBatches(false, Arrays.asList("Data_Received"), new ArrayList<String>(),
                                                    Arrays.asList("Approved"));
        int count = 0;
        while (batches.hasNext()) {
            Batch next = batches.next();

            count++;
        }
        Assert.assertTrue(count > 0, "No batches Found");

    }

    @Test(groups = {"externalTest"}, enabled = true)
    public void testGetBatchFromList() throws Exception {
        Properties props = getProperties();

        SBOIEventIndex summa = getSboiClient(props);
        Iterator<Batch> batches = summa.findBatches(
                false, Arrays.asList("Data_Received"), new ArrayList<String>(), Arrays.asList("Approved"));
        Batch first = batches.next();
        Iterator<Batch> batches2 = summa.search(
                false, Arrays.asList("Data_Received"), new ArrayList<String>(), Arrays.asList("Approved"), Arrays.asList(first));

        assertEquals(
                batches2.next(),first);

    }


    @Test(groups = {"externalTest"}, enabled = true)
    public void testGetBatchesFromList() throws Exception {
        Properties props = getProperties();

        SBOIEventIndex summa = getSboiClient(props);
        Iterator<Batch> batches = summa.findBatches(
                false, Arrays.asList("Data_Received"), new ArrayList<String>(), Arrays.asList("Approved"));
        Batch first = batches.next();
        Batch second = batches.next();

        Iterator<Batch> batches2 = summa.search(
                false, Arrays.asList("Data_Received"), new ArrayList<String>(), Arrays.asList("Approved"), Arrays.asList(first,second));

        HashSet<Batch> results = new HashSet<Batch>();
        results.add(first);
        results.add(second);
        assertTrue(results.contains(batches2.next()));
        assertTrue(results.contains(batches2.next()));
        try {
            batches2.next();
            fail();
        } catch (NoSuchElementException e){

        }
    }


    private SBOIEventIndex getSboiClient(Properties props) throws
                                                           MalformedURLException,
                                                           JAXBException,
                                                           PIDGeneratorException {

        DomsEventStorageFactory factory = new DomsEventStorageFactory();
        factory.setFedoraLocation(props.getProperty(ConfigConstants.DOMS_URL));
        factory.setUsername(props.getProperty(ConfigConstants.DOMS_USERNAME));
        factory.setPassword(props.getProperty(ConfigConstants.DOMS_PASSWORD));
        DomsEventStorage domsEventStorage = factory.createDomsEventStorage();

        return new SBOIEventIndex(
                props.getProperty(ConfigConstants.AUTONOMOUS_SBOI_URL), new PremisManipulatorFactory(
                new NewspaperIDFormatter(), PremisManipulatorFactory.TYPE), domsEventStorage);
    }

    private Properties getProperties() throws IOException {
        String pathToProperties = System.getProperty("integration.test.newspaper.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(pathToProperties));
        return props;
    }
}

@@ -1061,32 +1061,42 @@
  Arrays.asList(%22
+Roundtrip_
 Approved%22));%0A   
@@ -1639,32 +1639,42 @@
  Arrays.asList(%22
+Roundtrip_
 Approved%22));%0A   
@@ -1844,32 +1844,42 @@
  Arrays.asList(%22
+Roundtrip_
 Approved%22), Arra
@@ -2320,32 +2320,42 @@
  Arrays.asList(%22
+Roundtrip_
 Approved%22));%0A   
@@ -2573,16 +2573,26 @@
 asList(%22
+Roundtrip_
 Approved

Replace "Approved" with "Roundtrip_Approved" where relevant