xref: /OpenGrok/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java (revision 712a1733f06b0cf36128c401023d28622cd162d7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.web.api.v1.controller;
25 
26 import jakarta.ws.rs.client.Entity;
27 import jakarta.ws.rs.core.Application;
28 import jakarta.ws.rs.core.Response;
29 import org.junit.jupiter.api.Test;
30 import org.opengrok.indexer.configuration.Configuration;
31 import org.opengrok.indexer.configuration.RuntimeEnvironment;
32 import org.opengrok.indexer.util.IOUtils;
33 import org.opengrok.indexer.web.EftarFileReader;
34 import org.opengrok.indexer.web.PathDescription;
35 import org.opengrok.web.api.v1.RestApp;
36 
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.PrintWriter;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.nio.file.attribute.FileTime;
44 import java.text.ParseException;
45 import java.text.SimpleDateFormat;
46 import java.util.Date;
47 
48 import static org.junit.jupiter.api.Assertions.assertEquals;
49 import static org.junit.jupiter.api.Assertions.assertNotEquals;
50 import static org.junit.jupiter.api.Assertions.assertTrue;
51 
52 public class SystemControllerTest extends OGKJerseyTest {
53 
54     private final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
55 
56     @Override
configure()57     protected Application configure() {
58         return new RestApp();
59     }
60 
61     /**
62      * This method tests include files reload by testing it for one specific file out of the whole set.
63      * @throws IOException
64      */
65     @Test
testIncludeReload()66     public void testIncludeReload() throws IOException {
67         // Set new include root directory.
68         Path includeRootPath = Files.createTempDirectory("systemControllerTest");
69         File includeRoot = includeRootPath.toFile();
70         env.setIncludeRoot(includeRoot.getAbsolutePath());
71         assertEquals(includeRoot.getCanonicalPath(), env.getIncludeRootPath());
72 
73         // Create footer include file.
74         File footerFile = new File(includeRoot, Configuration.FOOTER_INCLUDE_FILE);
75         String content = "foo";
76         try (PrintWriter out = new PrintWriter(footerFile)) {
77             out.println(content);
78         }
79 
80         // Sanity check that getFooterIncludeFileContent() works since the test depends on it.
81         String before = env.getIncludeFiles().getFooterIncludeFileContent(false);
82         assertEquals(content, before.trim());
83 
84         // Modify the contents of the file.
85         content = content + "bar";
86         try (PrintWriter out = new PrintWriter(footerFile)) {
87             out.println(content);
88         }
89 
90         // Reload the contents via API call.
91         Response r = target("system")
92                 .path("includes").path("reload")
93                 .request().put(Entity.text(""));
94         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus());
95 
96         // Check that the content was reloaded.
97         String after = env.getIncludeFiles().getFooterIncludeFileContent(false);
98         assertNotEquals(before, after);
99         assertEquals(content, after.trim());
100 
101         // Cleanup
102         IOUtils.removeRecursive(includeRootPath);
103     }
104 
105     @Test
testDtagsEftarReload()106     public void testDtagsEftarReload() throws IOException {
107         // The output file will be located in a directory under data root so create it first.
108         Path dataRoot = Files.createTempDirectory("api_dtags_test");
109         env.setDataRoot(dataRoot.toString());
110         Paths.get(dataRoot.toString(), "index").toFile().mkdir();
111 
112         // Create path descriptions string.
113         StringBuilder sb = new StringBuilder();
114         PathDescription[] descriptions = {
115                 new PathDescription("/path1", "foo foo"),
116                 new PathDescription("/path2", "bar bar")
117         };
118 
119         // Reload the contents via API call.
120         Response r = target("system")
121                 .path("pathdesc")
122                 .request().post(Entity.json(descriptions));
123         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus());
124 
125         // Check
126         Path eftarPath = env.getDtagsEftarPath();
127         assertTrue(eftarPath.toFile().exists());
128         try (EftarFileReader er = new EftarFileReader(eftarPath.toString())) {
129             for (PathDescription description : descriptions) {
130                 assertEquals(description.getDescription(), er.get(description.getPath()));
131             }
132         }
133 
134         // Cleanup
135         IOUtils.removeRecursive(dataRoot);
136     }
137 
138     @Test
testIndexTime()139     public void testIndexTime() throws IOException, ParseException {
140         Path dataRoot = Files.createTempDirectory("indexTimetest");
141         env.setDataRoot(dataRoot.toString());
142         Path indexTimeFile = dataRoot.resolve("timestamp");
143         Files.createFile(indexTimeFile);
144         assertTrue(Files.exists(indexTimeFile));
145         SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss+ZZ");
146         Date date = f.parse("2021-02-16_11:18:01+UTC");
147         Files.setLastModifiedTime(indexTimeFile, FileTime.fromMillis(date.getTime()));
148 
149         Response r = target("system")
150                 .path("indextime")
151                 .request().get();
152         String result = r.readEntity(String.class);
153 
154         assertEquals("\"2021-02-16T11:18:01.000+00:00\"", result);
155 
156         // Cleanup
157         IOUtils.removeRecursive(dataRoot);
158     }
159 }
160