<commit_before>package se.lagrummet.rinfo.collector.atom

import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
import static org.junit.Assert.*

import org.apache.abdera.model.AtomDate
import org.apache.abdera.model.Feed
import org.apache.abdera.model.Entry
import org.apache.abdera.i18n.iri.IRI

import org.restlet.*
import org.restlet.data.Protocol


class FeedArchivePastToPresentReaderTest {

    private static component
    private static testHttpPort = 9991

    def baseUrl = "http://localhost:${testHttpPort}"
    def feedUrl =  new URL("${baseUrl}/index1.atom")

    @BeforeClass
    static void setupClass() {
        component = new Component()
        component.servers.add(Protocol.HTTP, testHttpPort)
        component.clients.add(Protocol.FILE)
        component.defaultHost.attach(new ChangedFeedApp())
        component.start()
    }

    @AfterClass
    static void tearDownClass() {
        component.stop()
    }

    @Test
    void shouldReadFeedInOrder() {
        def reader = new CollectReader()
        reader.readFeed(feedUrl)
        assertEquals(
                [new URL("${baseUrl}/arch1.atom"), feedUrl],
                reader.visitedPages )
        assertEquals( [
                    "http://example.org/entry/1 2000-01-01T00:00:01.000Z",
                    //"http://example.org/entry/2 2000-01-01T00:00:02.000Z",
                    "http://example.org/entry/3 2000-01-01T00:00:03.000Z",
                    "http://example.org/entry/2 2000-01-01T00:01:00.000Z",
                ],
                reader.visitedEntries.collect {
                    "${it.id} ${it.updatedElement.getString()}" as String
                }
            )
    }

    @Test
    void shouldStopOnProcessedEntry() {
        def reader = new CollectReader(
                knownEntry:[id: "http://example.org/entry/3",
                             updated:"2000-01-01T00:00:03.000Z"]
            )
        reader.readFeed(feedUrl)
        assertEquals 1, reader.visitedPages.size()
        assertEquals 1, reader.visitedEntries.size()
    }

    @Test
    void shouldStopOnVisitedArchive() {
        def reader = new CollectReader(knownArchive:"${baseUrl}/arch1.atom")
        reader.readFeed(feedUrl)
        assertEquals 1, reader.visitedPages.size()
    }

    @Test
    void shouldPutUriDateIfNewOrYoungest() {
        def map = [:]
        def iri = new IRI("http://example.org/1")

        FeedArchivePastToPresentReader.putUriDateIfNewOrYoungest(map,
                iri, new AtomDate("2000-01-01T00:01:00.000Z"))
        assertEquals new AtomDate("2000-01-01T00:01:00.000Z"), map[iri]

        FeedArchivePastToPresentReader.putUriDateIfNewOrYoungest(map,
                iri, new AtomDate("2002-01-01T00:01:00.000Z"))<commit_msg>added note for planned high-level specs<commit_after>package se.lagrummet.rinfo.collector.atom

import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
import static org.junit.Assert.*

import org.apache.abdera.model.AtomDate
import org.apache.abdera.model.Feed
import org.apache.abdera.model.Entry
import org.apache.abdera.i18n.iri.IRI

import org.restlet.*
import org.restlet.data.Protocol


class FeedArchivePastToPresentReaderTest {

    private static component
    private static testHttpPort = 9991

    def baseUrl = "http://localhost:${testHttpPort}"
    def feedUrl =  new URL("${baseUrl}/index1.atom")

    @BeforeClass
    static void setupClass() {
        component = new Component()
        component.servers.add(Protocol.HTTP, testHttpPort)
        component.clients.add(Protocol.FILE)
        component.defaultHost.attach(new ChangedFeedApp())
        component.start()
    }

    @AfterClass
    static void tearDownClass() {
        component.stop()
    }

    @Test
    void shouldReadFeedInOrder() {
        def reader = new CollectReader()
        reader.readFeed(feedUrl)
        assertEquals(
                [new URL("${baseUrl}/arch1.atom"), feedUrl],
                reader.visitedPages )
        assertEquals( [
                    "http://example.org/entry/1 2000-01-01T00:00:01.000Z",
                    //"http://example.org/entry/2 2000-01-01T00:00:02.000Z",
                    "http://example.org/entry/3 2000-01-01T00:00:03.000Z",
                    "http://example.org/entry/2 2000-01-01T00:01:00.000Z",
                ],
                reader.visitedEntries.collect {
                    "${it.id} ${it.updatedElement.getString()}" as String
                }
            )
    }

    @Test
    void shouldStopOnProcessedEntry() {
        def reader = new CollectReader(
                knownEntry:[id: "http://example.org/entry/3",
                             updated:"2000-01-01T00:00:03.000Z"]
            )
        reader.readFeed(feedUrl)
        assertEquals 1, reader.visitedPages.size()
        assertEquals 1, reader.visitedEntries.size()
    }

    @Test
    void shouldStopOnVisitedArchive() {
        def reader = new CollectReader(knownArchive:"${baseUrl}/arch1.atom")
        reader.readFeed(feedUrl)
        assertEquals 1, reader.visitedPages.size()
    }

    /* TODO: high-level meaningful specs(s) related to "putUriDateIfNewOrYoungest":
        - shouldReportResurrectedEntry:
            an older delete mustn't supress a younged updated
            - given continuous feed events:
                - Entry(id="123", published=1)
                - Entry(id="123", updated=2)
                - Entry(id="123", deleted=3)
                - Entry(id="123", updated=4)
            - should report:
                - Entry(id="123", deleted=3)
                - Entry(id="123", updated=4)

        * TODO: write runnable specs like the above for most tests!
            - robotframework or BDD unit-tests?
    */

    @Test
    void shouldPutUriDateIfNewOrYoungest() {
        def map = [:]
        def iri = new IRI("http://example.org/1")

        FeedArchivePastToPresentReader.putUriDateIfNewOrYoungest(map,
                iri, new AtomDate("2000-01-01T00:01:00.000Z"))
        assertEquals new AtomDate("2000-01-01T00:01:00.000Z"), map[iri]

        FeedArchivePastToPresentReader.putUriDateIfNewOrYoungest(map,
                iri, new AtomDate("2002-01-01T00:01:00.000Z"))
{'fraction_non_alphanumeric': 0.11718499519076628, 'fraction_numerical': 0.053222186598268675, 'mean_word_length': 2.219298245614035, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1226105', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1853, 'n_words': 318}