summaryrefslogtreecommitdiffstats
path: root/linguistic/qa/restprotocol.cxx
blob: d5783d0a90a34d1b598e0e4923902fc78fe3b136 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-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/.
 */

#include <sal/config.h>

#include <algorithm>
#include <cassert>
#include <cstring>

#include <sal/log.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/strbuf.hxx>
#include <osl/socket.hxx>
#include <osl/thread.hxx>
#include <unotest/bootstrapfixturebase.hxx>
#include <officecfg/Office/Linguistic.hxx>

#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/connection/XAcceptor.hpp>
#include <com/sun/star/connection/XConnector.hpp>
#include <com/sun/star/linguistic2/XProofreader.hpp>
#include <com/sun/star/linguistic2/ProofreadingResult.hpp>

using namespace ::com::sun::star::uno;

namespace
{
class MockServerThread : public ::osl::Thread
{
public:
    MockServerThread()
        : m_aSocketAddr("localhost", 2022)
    {
    }

    virtual void SAL_CALL run()
    {
        if (m_aAcceptorSocket.acceptConnection(m_aStreamSocket) != osl_Socket_Ok)
        {
            return;
        }

        sal_Int32 nReadBytes;
        Sequence<sal_Int8> aBuffer(512);
        sal_Int32 nTcpNoDelay = sal_Int32(true);
        m_aStreamSocket.setOption(osl_Socket_OptionTcpNoDelay, &nTcpNoDelay, sizeof(nTcpNoDelay),
                                  osl_Socket_LevelTcp);

        nReadBytes = m_aStreamSocket.recv(aBuffer.getArray(), aBuffer.getLength());
        if (nReadBytes)
        {
            std::string aText(reinterpret_cast<const char*>(aBuffer.getConstArray()), nReadBytes);

            if (aText.find("POST /api/check") == std::string::npos)
            {
                NotFound();
            }
            else if (aText.find("Content-Type: application/json") == std::string::npos)
            {
                NotFound();
            }
            else
            {
                ResponseOK();
            }
        }
    }

    void ResponseOK()
    {
        OString aResponse(
            "HTTP/1.1 200 OK\r\n"
            "Server: MockServer\r\n"
            "Cache-Control: no-cache\r\n"
            "Content-Type: application/json\r\n"
            "\r\n"
            "{\"check-positions\":[{\"offset\":15,\"length\":6,\"errorcode\":4711,\"type\":"
            "\"orth\","
            "\"severity\":1,\"proposals\":[\"Entwurf\",\"Entw\u00fcrfe\"]},"
            "{\"offset\":22,\"length\":3,\"errorcode\":8221,\"type\":\"orth\",\"severity\":1}]}"_ostr);

        m_aStreamSocket.write(aResponse.getStr(), aResponse.getLength());
        m_aStreamSocket.close();
    }

    void NotFound()
    {
        OString aResponse("HTTP/1.1 404 Not Found\r\n"
                          "Connection: Closed\r\n"
                          "\r\n"_ostr);

        m_aStreamSocket.write(aResponse.getStr(), aResponse.getLength());
        m_aStreamSocket.close();
    }

    void stop()
    {
        m_aAcceptorSocket.close();
        join();
    }

    void init()
    {
        m_aAcceptorSocket.setOption(osl_Socket_OptionReuseAddr, 1);
        CPPUNIT_ASSERT(m_aAcceptorSocket.bind(m_aSocketAddr));
        CPPUNIT_ASSERT(m_aAcceptorSocket.listen());
    }

private:
    ::osl::SocketAddr m_aSocketAddr;
    ::osl::AcceptorSocket m_aAcceptorSocket;
    ::osl::StreamSocket m_aStreamSocket;
};
}

MockServerThread aMockServer;

class TestRestProtocol : public test::BootstrapFixtureBase
{
public:
    virtual void setUp() override;
    virtual void tearDown() override;

private:
    CPPUNIT_TEST_SUITE(TestRestProtocol);
    CPPUNIT_TEST(testProofreading);
    CPPUNIT_TEST_SUITE_END();

    void testProofreading();
};

void TestRestProtocol::testProofreading()
{
    css::lang::Locale aLocale("en", "US", "");
    using LanguageToolCfg = officecfg::Office::Linguistic::GrammarChecking::LanguageTool;
    auto batch(comphelper::ConfigurationChanges::create());

    LanguageToolCfg::BaseURL::set("http://127.0.0.1:2022/api", batch);
    LanguageToolCfg::Username::set("hcastro", batch);
    LanguageToolCfg::ApiKey::set("hcvhcvhcv", batch);
    LanguageToolCfg::IsEnabled::set(true, batch);
    LanguageToolCfg::SSLCertVerify::set(false, batch);
    LanguageToolCfg::RestProtocol::set("duden", batch);

    batch->commit();

    CPPUNIT_ASSERT_EQUAL(OUString("duden"), *LanguageToolCfg::RestProtocol::get());

    Reference<::com::sun::star::linguistic2::XProofreader> xProofreader(
        m_xSFactory->createInstance("com.sun.star.linguistic2.Proofreader"), UNO_QUERY);
    CPPUNIT_ASSERT(xProofreader.is());

    com::sun::star::linguistic2::ProofreadingResult aResult
        = xProofreader->doProofreading("id", "ths is a tst", aLocale, 0, 0, {});

    CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aResult.aErrors.getLength());
}

void TestRestProtocol::setUp()
{
    test::BootstrapFixtureBase::setUp();

    aMockServer.init();
    aMockServer.create();
    osl::Thread::wait(std::chrono::seconds(1));
}

void TestRestProtocol::tearDown()
{
    aMockServer.stop();

    test::BootstrapFixtureBase::tearDown();
}

CPPUNIT_TEST_SUITE_REGISTRATION(TestRestProtocol);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */