summaryrefslogtreecommitdiffstats
path: root/sw/qa/python/check_xnamedgraph.py
blob: 84c774bddfa3a3fd0c88ef1e569ac32cf25d7dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/env python
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import unittest
from org.libreoffice.unotest import UnoInProcess


class XNamedGraph(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls._uno = UnoInProcess()
        cls._uno.setUp()

    @classmethod
    def tearDownClass(cls):
        cls._uno.tearDown()

    def test_getStatements(self):
        xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
        xRepo = xDMA.getRDFRepository()
        xGraphs = xRepo.getGraphNames()

        all = self.getStatementsCount(xGraphs, xDMA, None, None)
        self.assertTrue(all > 0)

        # check that we have some node from the second RDF graph
        # (but not any from the list of LO standard nodes)
        DATE_URI_STR = "http://www.wollmux.org/WollMuxMetadata#WollMuxVersion"
        xUri = self.nameToUri(DATE_URI_STR)

        onlyWollMux = self.getStatementsCount(xGraphs, xDMA, xUri, None)
        self.assertTrue(onlyWollMux > 0)
        self.assertTrue(onlyWollMux < all)

        xDMA.close(True)

    def test_Statements_AddRemove(self):
        # take any first graph
        xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
        xGraph = self.getAnyGraph(xDMA)

        DATE_URI_STR = "http://www.example.com/Metadata#Version"
        OBJECT_STR = "foo"

        # not exist => add => remove
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
        self.addStatement(xDMA, xGraph, DATE_URI_STR, OBJECT_STR)
        self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
        self.removeStatement(xDMA, xGraph, DATE_URI_STR, None)
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))

        xDMA.close(True)

    def test_Statements_RemoveByObject(self):
        # take any first graph
        xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
        xGraph = self.getAnyGraph(xDMA)

        DATE_URI_STR = "http://www.example.com/Metadata#Version"
        OBJECT_STR = "foo"

        # remove by object
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
        self.addStatement(xDMA, xGraph, DATE_URI_STR, OBJECT_STR)
        self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR))
        self.removeStatement(xDMA, xGraph, None, OBJECT_STR)
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR))

        xDMA.close(True)

    def test_Statements_RemoveByObject(self):
        # take any first graph
        xDMA = self._uno.openTemplateFromTDOC("XNamedGraph.ott")
        xGraph = self.getAnyGraph(xDMA)

        DATE_URI_STR_1 = "http://www.example.com/Metadata#Version"
        DATE_URI_STR_2 = "http://www.example.com/Metadata#Second"
        OBJECT_STR = "foo"

        # remove several with one call
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))
        self.addStatement(xDMA, xGraph, DATE_URI_STR_1, OBJECT_STR)
        self.addStatement(xDMA, xGraph, DATE_URI_STR_2, OBJECT_STR)
        self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
        self.assertTrue(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))
        self.removeStatement(xDMA, xGraph, None, OBJECT_STR)
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_1))
        self.assertFalse(self.hasStatement(xDMA, xGraph, DATE_URI_STR_2))

        xDMA.close(True)

    def getAnyGraph(self, xDMA):
        xRepo = xDMA.getRDFRepository()
        xGraphs = xRepo.getGraphNames()
        self.assertTrue(len(xGraphs) > 0)
        xGraph = xRepo.getGraph(xGraphs[0])
        self.assertIsNotNone(xGraph)
        return xGraph

    def getStatementsCount(self, xGraphs, xDMA, xPredicate, xObject):
        count = 0
        xRepo = xDMA.getRDFRepository()

        for xGraphUri in xGraphs:
            xGraph = xRepo.getGraph(xGraphUri)

            xStatements = xGraph.getStatements(xDMA, xPredicate, xObject)
            self.assertIsNotNone(xStatements)

            if xPredicate is None:
                self.assertTrue(xStatements.hasMoreElements())

            for xStatement in xStatements:
                count += 1

        return count

    def hasStatement(self, xDMA, xGraph, xPredicateName):
        xPredicate = self.nameToUri(xPredicateName)
        self.assertIsNotNone(xPredicate)

        xStatements = xGraph.getStatements(xDMA, xPredicate, None)
        self.assertIsNotNone(xStatements)
        return xStatements.hasMoreElements()

    def nameToUri(self, xPredicateName):
        xServiceManager = self.__class__._uno.xContext.ServiceManager
        xUri = xServiceManager.createInstance("com.sun.star.rdf.URI")
        xUri.initialize((xPredicateName,))
        return xUri

    def stringToLiteral(self, xString):
        xServiceManager = self.__class__._uno.xContext.ServiceManager
        xLiteral = xServiceManager.createInstance("com.sun.star.rdf.Literal")
        xLiteral.initialize((xString,))
        return xLiteral

    def addStatement(self, xDMA, xGraph, xPredicateName, xObjectStr):
        xPredicate = self.nameToUri(xPredicateName)
        self.assertIsNotNone(xPredicate)

        xObject = self.stringToLiteral(xObjectStr)
        self.assertIsNotNone(xObject)

        xGraph.addStatement(xDMA, xPredicate, xObject)

    def removeStatement(self, xDMA, xGraph, xPredicateName, xObjectStr):
        xPredicate = None
        if xPredicateName is not None:
            xPredicate = self.nameToUri(xPredicateName)

        xObject = None
        if xObjectStr is not None:
            xObject = self.stringToLiteral(xObjectStr)

        xGraph.removeStatements(xDMA, xPredicate, xObject)


if __name__ == '__main__':
    unittest.main()

# vim: set shiftwidth=4 softtabstop=4 expandtab: