/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2019 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.exist.xquery.modules.persistentlogin;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.exist.http.RESTTest;
import org.exist.xmldb.EXistResource;
import org.exist.xmldb.UserManagementService;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.BinaryResource;

import javax.annotation.Nullable;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class LoginModuleTest extends RESTTest {

    private static String XQUERY = "import module namespace login=\"http://exist-db.org/xquery/login\" " +
            "at \"resource:org/exist/xquery/modules/persistentlogin/login.xql\";" +
            "login:set-user('org.exist.login', (), false())," +
            "sm:id()/(descendant::sm:effective,descendant::sm:real)[1]/sm:username/string()";

    private final static String XQUERY_FILENAME = "test-login.xql";

    private static Collection root;
    private static HttpClient client;

    @BeforeClass
    public static void beforeClass() throws XMLDBException {
        root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc/db", "admin", "");
        final BinaryResource res = (BinaryResource)root.createResource(XQUERY_FILENAME, "BinaryResource");
        ((EXistResource) res).setMimeType("application/xquery");
        res.setContent(XQUERY);
        root.storeResource(res);
        final UserManagementService ums = (UserManagementService)root.getService("UserManagementService", "1.0");
        ums.chmod(res, 0777);

        final BasicCookieStore store = new BasicCookieStore();
        client = HttpClientBuilder.create().setDefaultCookieStore(store).build();
    }

    @AfterClass
    public static void afterClass() throws XMLDBException {
        final BinaryResource res = (BinaryResource)root.getResource(XQUERY_FILENAME);
        root.removeResource(res);
    }

    @Test
    public void loginAndLogout() throws IOException {
        // not logged in
        doGet(null, "guest");

        // log in as admin
        doGet("user=admin&password=&duration=P1D", "admin");

        // second request should stay logged in
        doGet(null, "admin");

        // log off returns to guest user
        doGet("logout=true", "guest");
    }

    private void doGet(@Nullable String params, String expected) throws IOException {
        final HttpGet httpGet = new HttpGet(getCollectionRootUri() + "/" + XQUERY_FILENAME +
                (params == null ? "" : "?" + params));
        HttpResponse response = client.execute(httpGet);
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        assertEquals(expected, EntityUtils.toString(entity));
    }
}

@@ -1017,110 +1017,8 @@
 nt;%0A
-import org.apache.http.client.config.CookieSpecs;%0Aimport org.apache.http.client.config.RequestConfig;%0A
 impo
@@ -1060,16 +1060,16 @@
 ttpGet;%0A
+
 import o
@@ -1229,21 +1229,27 @@
 ist.
-http.RESTTest
+test.ExistWebServer
 ;%0Aim
@@ -1326,24 +1326,57 @@
 entService;%0A
+import org.exist.xmldb.XmldbURI;%0A
 import org.j
@@ -1418,24 +1418,52 @@
 eforeClass;%0A
+import org.junit.ClassRule;%0A
 import org.j
@@ -1779,25 +1779,8 @@
 est 
-extends RESTTest 
 %7B%0A%0A 
@@ -2029,24 +2029,24 @@
 alse()),%22 +%0A
-
             
@@ -2124,24 +2124,135 @@
 string()%22;%0A%0A
+    @ClassRule%0A    public static final ExistWebServer existWebServer = new ExistWebServer(true, false, true);%0A%0A
     private 
@@ -3836,36 +3836,87 @@
 Get(
-getCollectionRootUri() + %22/%22
+%22http://localhost:%22 + existWebServer.getPort() + XmldbURI.ROOT_COLLECTION + '/'
  + X

Fix build issue in test case