desktop/source/lib/init.cxx                       |   11 -
 linguistic/CppunitTest_linguistic_restprotocol.mk |   39 ++++
 linguistic/Module_linguistic.mk                   |    7 
 linguistic/qa/restprotocol.cxx                    |  189 ++++++++++++++++++++++
 4 files changed, 243 insertions(+), 3 deletions(-)

New commits:
commit 1be7d0380d0b6f2e344f6f5e0085bba4f045d4b2
Author:     Henry Castro <[email protected]>
AuthorDate: Thu Nov 24 18:35:08 2022 -0400
Commit:     Andras Timar <[email protected]>
CommitDate: Sat Jan 28 09:24:02 2023 +0000

    linguistic: add REST API protocol unit test
    
    Signed-off-by: Henry Castro <[email protected]>
    Change-Id: I4768f8bb5bfa572d222fa5610f95c99169e6e390
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143249
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145610
    Reviewed-by: Andras Timar <[email protected]>
    Tested-by: Andras Timar <[email protected]>

diff --git a/linguistic/CppunitTest_linguistic_restprotocol.mk 
b/linguistic/CppunitTest_linguistic_restprotocol.mk
new file mode 100644
index 000000000000..4e56286efe0d
--- /dev/null
+++ b/linguistic/CppunitTest_linguistic_restprotocol.mk
@@ -0,0 +1,39 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# 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/.
+#
+
+$(eval $(call gb_CppunitTest_CppunitTest,linguistic_restprotocol))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,linguistic_restprotocol, \
+    linguistic/qa/restprotocol \
+))
+
+$(eval $(call gb_CppunitTest_use_libraries,linguistic_restprotocol, \
+    comphelper \
+    cppu \
+    cppuhelper \
+    sal \
+    svt \
+    utl \
+    test \
+    unotest \
+))
+
+$(eval $(call gb_CppunitTest_use_api,linguistic_restprotocol,\
+    udkapi \
+    offapi \
+    oovbaapi \
+))
+
+$(eval $(call gb_CppunitTest_use_configuration,linguistic_restprotocol))
+
+$(eval $(call gb_CppunitTest_use_ure,linguistic_restprotocol))
+
+$(eval $(call gb_CppunitTest_use_rdb,linguistic_restprotocol,services))
+
+# vim: set noet sw=4 ts=4:
diff --git a/linguistic/Module_linguistic.mk b/linguistic/Module_linguistic.mk
index 956c1bbc2e80..b5366b97e611 100644
--- a/linguistic/Module_linguistic.mk
+++ b/linguistic/Module_linguistic.mk
@@ -16,9 +16,16 @@ $(eval $(call gb_Module_add_targets,linguistic,\
 #$(eval $(call gb_Module_add_check_targets,linguistic,\
 #))
 
+ifeq ($(OS),LINUX)
 $(eval $(call gb_Module_add_subsequentcheck_targets,linguistic,\
     JunitTest_linguistic_unoapi \
+    CppunitTest_linguistic_restprotocol \
 ))
+else
+$(eval $(call gb_Module_add_subsequentcheck_targets,linguistic,\
+    JunitTest_linguistic_unoapi \
+))
+endif
 
 # was disabled in old build system
 # JunitTest_linguistic_complex \
diff --git a/linguistic/qa/restprotocol.cxx b/linguistic/qa/restprotocol.cxx
new file mode 100644
index 000000000000..f0557be83dc8
--- /dev/null
+++ b/linguistic/qa/restprotocol.cxx
@@ -0,0 +1,189 @@
+/* -*- 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 <svtools/languagetoolcfg.hxx>
+#include <unotest/bootstrapfixturebase.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;
+
+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()
+    {
+        OStringBuffer aResponse;
+
+        aResponse.append("HTTP/1.1 200 OK\r\n");
+        aResponse.append("Server: MockServer\r\n");
+        aResponse.append("Cache-Control: no-cache\r\n");
+        aResponse.append("Content-Type: application/json\r\n");
+
+        aResponse.append("\r\n");
+        
aResponse.append("{\"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}]}");
+
+        m_aStreamSocket.write(aResponse.getStr(), aResponse.getLength());
+        m_aStreamSocket.close();
+    }
+
+    void NotFound()
+    {
+        OStringBuffer aResponse;
+
+        aResponse.append("HTTP/1.1 404 Not Found\r\n");
+        aResponse.append("Connection: Closed\r\n");
+        aResponse.append("\r\n");
+
+        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", "");
+    Sequence<::com::sun::star::beans::PropertyValue> aProperties;
+    SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get();
+    rLanguageOpts.setBaseURL("http://127.0.0.1:2022/api";);
+    rLanguageOpts.setUsername("hcastro");
+    rLanguageOpts.setApiKey("hcvhcvhcv");
+    rLanguageOpts.setEnabled(true);
+    rLanguageOpts.setSSLVerification(false);
+    rLanguageOpts.setRestProtocol("duden");
+    CPPUNIT_ASSERT_EQUAL(OUString("duden"), rLanguageOpts.getRestProtocol());
+
+    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(OUString("id"),
+                                     OUString("ths is a tst"),
+                                     aLocale,
+                                     0,
+                                     0,
+                                     aProperties);
+
+    CPPUNIT_ASSERT_EQUAL(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: */
commit 9a93440dadc3d9a397e75fadce6fd8c337cb62a7
Author:     Henry Castro <[email protected]>
AuthorDate: Tue Nov 22 11:39:45 2022 -0400
Commit:     Andras Timar <[email protected]>
CommitDate: Sat Jan 28 09:23:49 2023 +0000

    lok: add Rest Protocol property
    
    Signed-off-by: Henry Castro <[email protected]>
    Change-Id: I7353e16dbf835684e5ce5963387359924352937b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143124
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145609
    Reviewed-by: Andras Timar <[email protected]>
    Tested-by: Andras Timar <[email protected]>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index a70385a47e17..ab97df3f7c23 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -7021,22 +7021,27 @@ void setLanguageToolConfig()
 {
     const char* pEnabled = ::getenv("LANGUAGETOOL_ENABLED");
     const char* pBaseUrlString = ::getenv("LANGUAGETOOL_BASEURL");
-    const char* pUsername = ::getenv("LANGUAGETOOL_USERNAME");
-    const char* pApikey = ::getenv("LANGUAGETOOL_APIKEY");
-    const char* pSSLVerification = ::getenv("LANGUAGETOOL_SSL_VERIFICATION");
+
     if (pEnabled && pBaseUrlString)
     {
+        const char* pUsername = ::getenv("LANGUAGETOOL_USERNAME");
+        const char* pApikey = ::getenv("LANGUAGETOOL_APIKEY");
+        const char* pSSLVerification = 
::getenv("LANGUAGETOOL_SSL_VERIFICATION");
+        const char* pRestProtocol = ::getenv("LANGUAGETOOL_RESTPROTOCOL");
+
         OUString aEnabled = OStringToOUString(pEnabled, RTL_TEXTENCODING_UTF8);
         OUString aSSLVerification = OStringToOUString(pSSLVerification, 
RTL_TEXTENCODING_UTF8);
         if (aEnabled != "true")
             return;
         OUString aBaseUrl = OStringToOUString(pBaseUrlString, 
RTL_TEXTENCODING_UTF8);
+        OUString aRestProtocol = OStringToOUString(pRestProtocol, 
RTL_TEXTENCODING_UTF8);
         try
         {
             SvxLanguageToolOptions& rLanguageOpts = 
SvxLanguageToolOptions::Get();
             rLanguageOpts.setBaseURL(aBaseUrl);
             rLanguageOpts.setEnabled(true);
             rLanguageOpts.setSSLVerification(aSSLVerification == "true");
+            rLanguageOpts.setRestProtocol(aRestProtocol);
             if (pUsername && pApikey)
             {
                 OUString aUsername = OStringToOUString(pUsername, 
RTL_TEXTENCODING_UTF8);

Reply via email to