| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 4 | |
| 5 | import java.io.IOException; |
| 6 | import java.net.URI; |
| 7 | import java.net.URISyntaxException; |
| 8 | import java.nio.file.Files; |
| Marc Kupietz | e518c61 | 2021-03-02 16:17:40 +0100 | [diff] [blame] | 9 | import java.nio.file.LinkOption; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 10 | import java.nio.file.Path; |
| 11 | import java.nio.file.Paths; |
| 12 | |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 13 | import jakarta.ws.rs.client.Entity; |
| 14 | import jakarta.ws.rs.core.Form; |
| 15 | import jakarta.ws.rs.core.MediaType; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 16 | |
| 17 | import org.apache.http.HttpStatus; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 18 | import org.junit.jupiter.api.Test; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 19 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 21 | import jakarta.ws.rs.core.Response; |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 22 | |
| 23 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 24 | import de.ids_mannheim.korap.config.Attributes; |
| 25 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 26 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 27 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 28 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 29 | import de.ids_mannheim.korap.web.SearchKrill; |
| 30 | |
| 31 | /** |
| 32 | * @author margaretha |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 33 | */ |
| 34 | public class IndexControllerTest extends SpringJerseyTest { |
| 35 | |
| 36 | @Autowired |
| 37 | private SearchKrill searchKrill; |
| 38 | |
| 39 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 40 | public void testCloseIndex () throws IOException, KustvaktException, |
| 41 | URISyntaxException, InterruptedException { |
| 42 | URI uri = IndexControllerTest.class.getClassLoader() |
| 43 | .getResource("vc/named-vc1.jsonld").toURI(); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 44 | 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 Kupietz | e518c61 | 2021-03-02 16:17:40 +0100 | [diff] [blame] | 50 | if (Files.exists(symLink, LinkOption.NOFOLLOW_LINKS)) { |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 51 | Files.delete(symLink); |
| 52 | } |
| 53 | Files.createSymbolicLink(symLink, toLink); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 54 | searchKrill.getStatistics(null); |
| 55 | assertEquals(true, searchKrill.getIndex().isReaderOpen()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 56 | Form form = new Form(); |
| 57 | form.param("token", "secret"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 58 | Response response = target().path(API_VERSION).path("index") |
| 59 | .path("close").request().post(Entity.form(form)); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 60 | assertEquals(HttpStatus.SC_OK, response.getStatus()); |
| 61 | assertEquals(false, searchKrill.getIndex().isReaderOpen()); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 62 | // Cleaning database and cache |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 63 | Thread.sleep(200); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 64 | 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(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 71 | String entity = response.readEntity(String.class); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 72 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 73 | assertEquals(StatusCodes.NO_RESOURCE_FOUND, |
| 74 | node.at("/errors/0/0").asInt()); |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 75 | } |
| margaretha | 47a72a8 | 2019-07-03 16:00:54 +0200 | [diff] [blame] | 76 | } |