<commit_before>package whelk.util

import org.codehaus.jackson.map.ObjectMapper
import spock.lang.Ignore
import spock.lang.Specification
import spock.lang.Unroll
import whelk.Document
import whelk.JsonLd
import whelk.exception.FramingException

@Unroll
class JsonLdSpec extends Specification {

    static final ObjectMapper mapper = new ObjectMapper()

    static final Map CONTEXT_DATA = [
        "@context": [
            "@vocab": "http://example.org/ns/",
            "pfx": "http://example.org/pfx/"
        ]
    ]

    static final Map VOCAB_DATA = [
        "@graph": [
            ["@id": "http://example.org/ns/ProvisionActivity",
             "subClassOf": [ ["@id": "http://example.org/ns/Event"] ]],
            ["@id": "http://example.org/ns/Publication",
             "subClassOf": ["@id": "http://example.org/ns/ProvisionActivity"]],
            ["@id": "http://example.org/pfx/SpecialPublication",
             "subClassOf": ["@id": "http://example.org/ns/Publication"]],

            ["@id": "http://example.org/ns/label", "@type": ["@id": "DatatypeProperty" ]],
            ["@id": "http://example.org/ns/prefLabel",
             "subPropertyOf": [ ["@id": "http://example.org/ns/label"] ]],
            ["@id": "http://example.org/ns/preferredLabel",
             "subPropertyOf": ["@id": "http://example.org/ns/prefLabel"]],
            ["@id": "http://example.org/ns/name",
             "subPropertyOf": ["@id": "http://example.org/ns/label"]]
        ]
    ]

    def "should find external references"() {
        given:
        def graph = ['@graph': [
                ['@id': '/foo',
                    'sameAs': [['@id': '/bar'], ['@id': '/baz']],
                    'third': ['@id': '/third']
                ],
                ['@id': '/second',
                    'foo': '/foo',
                    'some': 'value'],
                ['@id': '/third',
                    'external': ['@id': '/external']],
                ['@id': '/fourth',
                    'internal': ['@id': '/some_id']],
                ['@graph': [['@id': '/some_id']]],
                ['@graph': ['@id': '/some_other_id']]
            ],
            '@context': 'base.jsonld']
        def expected = ['/external']

        expect:
        assert JsonLd.getExternalReferences(graph) == expected
    }

    def "should get local objects"() {
        given:
        def input = ['@graph': [['@id': '/foo',
                                'sameAs': [['@id': '/baz']],
                                'bar': ['@id': '/bar']],
                                ['@graph': [['@id': '/quux',
                                             'some': 'value']]],
                                ['@graph': ['@id': '/some_id']]]]
        Set expected = ['/foo', '/quux', '/some_id']

        expect:
        assert JsonLd.getLocalObjects(input) == expected
    }

    def "should handle malformed sameAs when getting local objects"() {
        given:
        def input = ['@graph': [['@id': '/foo',
                                'sameAs': [['bad_key': '/baz']],
                                'bar': ['@id': '/bar']],
                                ['@graph': [['@id': '/quux',
                                             'some': 'value']]]]]
        Set expected = ['/foo', '/quux']

        expect:
        assert JsonLd.getLocalObjects(input) == expected
    }

   def "should get all references"() {
       given:
       def input = ['@graph': [['@id': '/foo',
                                'bar': ['@id': '/bar'],
                                'extra': ['baz': ['@id': '/baz']],
                                'aList': [['quux': ['@id': '/quux']]],
                                'quux': ['@id': '/quux']],
                               ['@id': '/bar',
                                'someValue': 1],
                               ['@id': '/baz/',
                                'someOtherValue': 2]]]
       Set expected = ['/bar', '/baz', '/quux']
       expect:
       assert JsonLd.getAllReferences(input) == expected
   }

   def "should not get references if @graph is missing"() {
       given:
       def input = ['@id': '/foo']
       when:
       JsonLd.getAllReferences(input)
       then:
       thrown FramingException
   }

   def "should frame flat jsonld"() {
        given:
        def id = "1234"
        def input = ["@graph": [["@id": id, "foo": "bar"]]]
        def expected = ["@id": id, "foo": "bar"]
        expect:
        assert JsonLd.frame(id, input) == expected
    }

    def "framing framed jsonld should preserve input"() {
        given:
        def id = "1234"
        def input = ["@id": id, "foo": "bar"]
        expect:<commit_msg>Add spec for linking record as meta when framing<commit_after>package whelk.util

import org.codehaus.jackson.map.ObjectMapper
import spock.lang.Ignore
import spock.lang.Specification
import spock.lang.Unroll
import whelk.Document
import whelk.JsonLd
import whelk.exception.FramingException

@Unroll
class JsonLdSpec extends Specification {

    static final ObjectMapper mapper = new ObjectMapper()

    static final Map CONTEXT_DATA = [
        "@context": [
            "@vocab": "http://example.org/ns/",
            "pfx": "http://example.org/pfx/"
        ]
    ]

    static final Map VOCAB_DATA = [
        "@graph": [
            ["@id": "http://example.org/ns/ProvisionActivity",
             "subClassOf": [ ["@id": "http://example.org/ns/Event"] ]],
            ["@id": "http://example.org/ns/Publication",
             "subClassOf": ["@id": "http://example.org/ns/ProvisionActivity"]],
            ["@id": "http://example.org/pfx/SpecialPublication",
             "subClassOf": ["@id": "http://example.org/ns/Publication"]],

            ["@id": "http://example.org/ns/label", "@type": ["@id": "DatatypeProperty" ]],
            ["@id": "http://example.org/ns/prefLabel",
             "subPropertyOf": [ ["@id": "http://example.org/ns/label"] ]],
            ["@id": "http://example.org/ns/preferredLabel",
             "subPropertyOf": ["@id": "http://example.org/ns/prefLabel"]],
            ["@id": "http://example.org/ns/name",
             "subPropertyOf": ["@id": "http://example.org/ns/label"]]
        ]
    ]

    def "should find external references"() {
        given:
        def graph = ['@graph': [
                ['@id': '/foo',
                    'sameAs': [['@id': '/bar'], ['@id': '/baz']],
                    'third': ['@id': '/third']
                ],
                ['@id': '/second',
                    'foo': '/foo',
                    'some': 'value'],
                ['@id': '/third',
                    'external': ['@id': '/external']],
                ['@id': '/fourth',
                    'internal': ['@id': '/some_id']],
                ['@graph': [['@id': '/some_id']]],
                ['@graph': ['@id': '/some_other_id']]
            ],
            '@context': 'base.jsonld']
        def expected = ['/external']

        expect:
        assert JsonLd.getExternalReferences(graph) == expected
    }

    def "should get local objects"() {
        given:
        def input = ['@graph': [['@id': '/foo',
                                'sameAs': [['@id': '/baz']],
                                'bar': ['@id': '/bar']],
                                ['@graph': [['@id': '/quux',
                                             'some': 'value']]],
                                ['@graph': ['@id': '/some_id']]]]
        Set expected = ['/foo', '/quux', '/some_id']

        expect:
        assert JsonLd.getLocalObjects(input) == expected
    }

    def "should handle malformed sameAs when getting local objects"() {
        given:
        def input = ['@graph': [['@id': '/foo',
                                'sameAs': [['bad_key': '/baz']],
                                'bar': ['@id': '/bar']],
                                ['@graph': [['@id': '/quux',
                                             'some': 'value']]]]]
        Set expected = ['/foo', '/quux']

        expect:
        assert JsonLd.getLocalObjects(input) == expected
    }

   def "should get all references"() {
       given:
       def input = ['@graph': [['@id': '/foo',
                                'bar': ['@id': '/bar'],
                                'extra': ['baz': ['@id': '/baz']],
                                'aList': [['quux': ['@id': '/quux']]],
                                'quux': ['@id': '/quux']],
                               ['@id': '/bar',
                                'someValue': 1],
                               ['@id': '/baz/',
                                'someOtherValue': 2]]]
       Set expected = ['/bar', '/baz', '/quux']
       expect:
       assert JsonLd.getAllReferences(input) == expected
   }

   def "should not get references if @graph is missing"() {
       given:
       def input = ['@id': '/foo']
       when:
       JsonLd.getAllReferences(input)
       then:
       thrown FramingException
   }

   def "should frame flat jsonld"() {
        given:
        def id = "1234"
        def input = ["@graph": [["@id": id, "foo": "bar"]]]
        def expected = ["@id": id, "foo": "bar"]
        expect:
        assert JsonLd.frame(id, input) == expected
    }

   def "should link record as meta when framing jsonld"() {
        given:
        def input = ["@graph": [
            ["@id": "1", "mainEntity": ["@id": "1#it"]],
            ["@id": "1#it", "foo": "bar"]
        ]]
        def expected = [
            "@id": "1#it",
            "foo": "bar",
            "meta": ["@id": "1", "mainEntity": ["@id": "1#it"]]
        ]
        expect:
        assert JsonLd.frame("1#it", input) == expected
    }

    def "framing framed jsonld should preserve input"() {
        given:
        def id = "1234"
        def input = ["@id": id, "foo": "bar"]
        expect:
{'fraction_non_alphanumeric': 0.19742053417284453, 'fraction_numerical': 0.0027419518635117294, 'mean_word_length': 1.486868686868687, 'pattern_counts': {'":': 76, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968237', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2857, 'n_words': 646}