blob: 3b9dfbc561cdadb86a769081bb2c9cd1490b237b [file] [log] [blame]
margaretha47a72a82019-07-03 16:00:54 +02001package de.ids_mannheim.korap.web.controller;
2
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
margaretha47a72a82019-07-03 16:00:54 +02004
5import java.io.IOException;
6import java.net.URI;
7import java.net.URISyntaxException;
8import java.nio.file.Files;
Marc Kupietze518c612021-03-02 16:17:40 +01009import java.nio.file.LinkOption;
margaretha47a72a82019-07-03 16:00:54 +020010import java.nio.file.Path;
11import java.nio.file.Paths;
12
margaretha96c309d2023-08-16 12:24:12 +020013import jakarta.ws.rs.client.Entity;
14import jakarta.ws.rs.core.Form;
15import jakarta.ws.rs.core.MediaType;
margaretha47a72a82019-07-03 16:00:54 +020016
17import org.apache.http.HttpStatus;
Marc Kupietzd43a98d2023-09-22 17:11:46 +020018import org.junit.jupiter.api.Test;
margaretha47a72a82019-07-03 16:00:54 +020019import org.springframework.beans.factory.annotation.Autowired;
margaretha47a72a82019-07-03 16:00:54 +020020import com.fasterxml.jackson.databind.JsonNode;
margaretha96c309d2023-08-16 12:24:12 +020021import jakarta.ws.rs.core.Response;
margaretha47a72a82019-07-03 16:00:54 +020022
23import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
24import de.ids_mannheim.korap.config.Attributes;
25import de.ids_mannheim.korap.config.SpringJerseyTest;
26import de.ids_mannheim.korap.exceptions.KustvaktException;
27import de.ids_mannheim.korap.exceptions.StatusCodes;
28import de.ids_mannheim.korap.utils.JsonUtils;
29import de.ids_mannheim.korap.web.SearchKrill;
30
31/**
32 * @author margaretha
margaretha47a72a82019-07-03 16:00:54 +020033 */
34public class IndexControllerTest extends SpringJerseyTest {
35
36 @Autowired
37 private SearchKrill searchKrill;
38
39 @Test
margaretha35e1ca22023-11-16 22:00:01 +010040 public void testCloseIndex () throws IOException, KustvaktException,
41 URISyntaxException, InterruptedException {
42 URI uri = IndexControllerTest.class.getClassLoader()
43 .getResource("vc/named-vc1.jsonld").toURI();
margaretha47a72a82019-07-03 16:00:54 +020044 Path toLink = Paths.get(uri);
45 Path symLink = Paths.get("vc/named-vc1.jsonld");
46 Path vcPath = Paths.get("vc");
47 if (!Files.exists(vcPath)) {
48 Files.createDirectory(vcPath);
49 }
Marc Kupietze518c612021-03-02 16:17:40 +010050 if (Files.exists(symLink, LinkOption.NOFOLLOW_LINKS)) {
margaretha47a72a82019-07-03 16:00:54 +020051 Files.delete(symLink);
52 }
53 Files.createSymbolicLink(symLink, toLink);
margaretha47a72a82019-07-03 16:00:54 +020054 searchKrill.getStatistics(null);
55 assertEquals(true, searchKrill.getIndex().isReaderOpen());
abcpro173fe8f22022-11-08 19:56:52 +000056 Form form = new Form();
57 form.param("token", "secret");
margaretha35e1ca22023-11-16 22:00:01 +010058 Response response = target().path(API_VERSION).path("index")
59 .path("close").request().post(Entity.form(form));
margaretha47a72a82019-07-03 16:00:54 +020060 assertEquals(HttpStatus.SC_OK, response.getStatus());
61 assertEquals(false, searchKrill.getIndex().isReaderOpen());
margaretha47a72a82019-07-03 16:00:54 +020062 // Cleaning database and cache
margaretha47a72a82019-07-03 16:00:54 +020063 Thread.sleep(200);
margaretha35e1ca22023-11-16 22:00:01 +010064 response = target().path(API_VERSION).path("vc").path("~system")
65 .path("named-vc1").request()
66 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
67 .createBasicAuthorizationHeaderValue("admin", "pass"))
68 .delete();
69 response = target().path(API_VERSION).path("vc").path("~system")
70 .path("named-vc1").request().get();
abcpro173fe8f22022-11-08 19:56:52 +000071 String entity = response.readEntity(String.class);
margaretha47a72a82019-07-03 16:00:54 +020072 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +010073 assertEquals(StatusCodes.NO_RESOURCE_FOUND,
74 node.at("/errors/0/0").asInt());
margaretha47a72a82019-07-03 16:00:54 +020075 }
margaretha47a72a82019-07-03 16:00:54 +020076}