<commit_before>package dk.statsbiblioteket.doms.updatetracker.improved.database;

import dk.statsbiblioteket.doms.updatetracker.improved.fedora.FedoraForUpdateTracker;
import dk.statsbiblioteket.doms.updatetracker.improved.fedora.FedoraFailedException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import static dk.statsbiblioteket.doms.updatetracker.improved.database.UpdateTrackerDAO.asSet;
import static java.util.Collections.emptyList;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UpdateTrackerPersistentStoreTest {

    private final String collection = "doms:Root_Collection";
    UpdateTrackerPersistentStore db;
    FedoraForUpdateTracker fedora;
    private File configFile;

    @Before
    public void setUp() throws Exception {
        configFile = new File(Thread.currentThread()
                                    .getContextClassLoader()
                                    .getResource("hibernate.cfg.xml")
                                    .toURI());

        fedora = mock(FedoraForUpdateTracker.class);
        //Collections for everybody
        when(fedora.getCollections(anyString(), any(Date.class))).thenReturn(asSet(collection));
        //No entry objects or view stuff until initialised
        when(fedora.getEntryAngles(anyString(), any(Date.class))).thenReturn(Collections.<String>emptyList());
        final UpdateTrackerBackend updateTrackerBackend = new UpdateTrackerBackend(fedora);
        db = new UpdateTrackerPersistentStoreImpl(configFile, fedora, updateTrackerBackend);
    }


    @After
    public void tearDown() throws Exception {
        db.close();
    }

    @Test
    public void testObjectCreatedBasic() throws Exception {
        init();
        Date now = new Date();
        final String pid = "doms:test1";
        addEntry(pid);
        db.objectCreated(pid, now);
        List<Record> list = db.lookup(now, "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("To many objects, some should have been deleted", 1, list.size());

        list = db.lookup(new Date(), "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("To many objects, some should have been deleted", 0, list.size());
    }

    private void init() throws Exception {
        //tearDown();
        //setUp();
    }

    private void addEntry(String pid, String... contained) throws FedoraFailedException {
        final String viewAngle = "SummaVisible";
        when(fedora.getEntryAngles(eq(pid), any(Date.class))).thenReturn(Arrays.asList(viewAngle));
        List<String> objects = new ArrayList<String>(Arrays.asList(contained));
        objects.add(pid);
        when(fedora.calcViewBundle(eq(pid),eq(viewAngle),any(Date.class))).thenReturn(new ViewBundle(pid,
                                                                                                               viewAngle,
                                                                                                               objects));
    }

    private void removeEntry(String pid) throws FedoraFailedException {
        final String viewAngle = "SummaVisible";
        when(fedora.getEntryAngles(eq(pid), any(Date.class))).thenThrow(new FedoraFailedException("Object not found"));
        when(fedora.calcViewBundle(eq(pid), eq(viewAngle), any(Date.class))).thenThrow(new FedoraFailedException("Object not found"));
    }


    @Test
    public void testObjectCreatedBeforeAndAfter() throws Exception {
        init();
        Date start = new Date();
        addEntry("doms:test1");
        db.objectCreated("doms:test1", start);<commit_msg>First idempotencity test<commit_after>package dk.statsbiblioteket.doms.updatetracker.improved.database;

import dk.statsbiblioteket.doms.updatetracker.improved.fedora.FedoraForUpdateTracker;
import dk.statsbiblioteket.doms.updatetracker.improved.fedora.FedoraFailedException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import static dk.statsbiblioteket.doms.updatetracker.improved.database.UpdateTrackerDAO.asSet;
import static java.util.Collections.emptyList;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UpdateTrackerPersistentStoreTest {

    private final String collection = "doms:Root_Collection";
    UpdateTrackerPersistentStore db;
    FedoraForUpdateTracker fedora;
    private File configFile;

    @Before
    public void setUp() throws Exception {
        configFile = new File(Thread.currentThread()
                                    .getContextClassLoader()
                                    .getResource("hibernate.cfg.xml")
                                    .toURI());

        fedora = mock(FedoraForUpdateTracker.class);
        //Collections for everybody
        when(fedora.getCollections(anyString(), any(Date.class))).thenReturn(asSet(collection));
        //No entry objects or view stuff until initialised
        when(fedora.getEntryAngles(anyString(), any(Date.class))).thenReturn(Collections.<String>emptyList());
        final UpdateTrackerBackend updateTrackerBackend = new UpdateTrackerBackend(fedora);
        db = new UpdateTrackerPersistentStoreImpl(configFile, fedora, updateTrackerBackend);
    }


    @After
    public void tearDown() throws Exception {
        db.close();
    }

    @Test
    public void testObjectCreatedBasic() throws Exception {
        init();
        Date now = new Date();
        final String pid = "doms:test1";
        addEntry(pid);
        db.objectCreated(pid, now);
        List<Record> list = db.lookup(now, "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("To many objects, some should have been deleted", 1, list.size());

        list = db.lookup(new Date(), "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("To many objects, some should have been deleted", 0, list.size());
    }

    private void init() throws Exception {
        //tearDown();
        //setUp();
    }

    @Test
    public void testIdempotense() throws Exception {
        init();
        Date test1Create = new Date(0);
        addEntry("doms:test1");
        db.objectCreated("doms:test1", test1Create);

        List<Record> list;

        final Date test1Published = new Date();
        db.objectStateChanged("doms:test1", test1Published, "A");

        list = db.lookup(test1Create, "SummaVisible", 0, 100, "A", "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        assertEquals(test1Published.getTime(), list.get(0).getActive().getTime());

        //After publish, the object still exist as Inactive
        list = db.lookup(test1Create, "SummaVisible", 0, 100, "I", "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        //And with the published timestamp, even
        assertEquals(test1Published.getTime(), list.get(0).getInactive().getTime());

        final Date test1Deleted = new Date();
        db.objectStateChanged("doms:test1", test1Deleted, "D");

        //After delete, the object is no longer published
        list = db.lookup(test1Create, "SummaVisible", 0, 100, "A", "doms:Root_Collection");
        assertEquals("Wrong number of objects", 1, list.size());
        assertEquals(list.get(0).getState(), Record.State.DELETED);

        list = db.lookup(test1Create, "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        assertEquals(test1Deleted.getTime(), list.get(0).getDeleted().getTime());

        db.objectCreated("doms:test1", test1Create);

        db.objectStateChanged("doms:test1", test1Published, "A");

        list = db.lookup(test1Create, "SummaVisible", 0, 100, "A", "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        assertEquals(test1Published.getTime(), list.get(0).getActive().getTime());

        //After publish, the object still exist as Inactive
        list = db.lookup(test1Create, "SummaVisible", 0, 100, "I", "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        //And with the published timestamp, even
        assertEquals(test1Published.getTime(), list.get(0).getInactive().getTime());

        db.objectStateChanged("doms:test1", test1Deleted, "D");

        //After delete, the object is no longer published
        list = db.lookup(test1Create, "SummaVisible", 0, 100, "A", "doms:Root_Collection");
        assertEquals("Wrong number of objects", 1, list.size());
        assertEquals(list.get(0).getState(), Record.State.DELETED);

        list = db.lookup(test1Create, "SummaVisible", 0, 100, null, "doms:Root_Collection");
        assertEquals("Wrong number of objects", list.size(), 1);
        assertEquals(test1Deleted.getTime(), list.get(0).getDeleted().getTime());
    }

    private void addEntry(String pid, String... contained) throws FedoraFailedException {
        final String viewAngle = "SummaVisible";
        when(fedora.getEntryAngles(eq(pid), any(Date.class))).thenReturn(Arrays.asList(viewAngle));
        List<String> objects = new ArrayList<String>(Arrays.asList(contained));
        objects.add(pid);
        when(fedora.calcViewBundle(eq(pid),eq(viewAngle),any(Date.class))).thenReturn(new ViewBundle(pid,
                                                                                                               viewAngle,
                                                                                                               objects));
    }

    private void removeEntry(String pid) throws FedoraFailedException {
        final String viewAngle = "SummaVisible";
        when(fedora.getEntryAngles(eq(pid), any(Date.class))).thenThrow(new FedoraFailedException("Object not found"));
        when(fedora.calcViewBundle(eq(pid), eq(viewAngle), any(Date.class))).thenThrow(new FedoraFailedException("Object not found"));
    }


    @Test
    public void testObjectCreatedBeforeAndAfter() throws Exception {
        init();
        Date start = new Date();
        addEntry("doms:test1");
        db.objectCreated("doms:test1", start);
{'fraction_non_alphanumeric': 0.11687279151943462, 'fraction_numerical': 0.00980565371024735, 'mean_word_length': 3.041770796144234, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3640085', 'n_tokens_mistral': 3200, 'n_tokens_neox': 3043, 'n_words': 681}