|
| 1 | +package org.opengrok.web.api.v1.controller; |
| 2 | + |
| 3 | + |
| 4 | +import org.glassfish.jersey.test.JerseyTest; |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | +import org.opengrok.indexer.configuration.Configuration; |
| 9 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 10 | +import org.opengrok.indexer.util.IOUtils; |
| 11 | +import org.opengrok.web.api.v1.RestApp; |
| 12 | + |
| 13 | +import javax.ws.rs.client.Entity; |
| 14 | +import javax.ws.rs.core.Application; |
| 15 | +import javax.ws.rs.core.Response; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.PrintWriter; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertNotEquals; |
| 25 | + |
| 26 | +public class SystemControllerTest extends JerseyTest { |
| 27 | + |
| 28 | + private RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 29 | + |
| 30 | + @Override |
| 31 | + protected Application configure() { |
| 32 | + return new RestApp(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * This method tests include files reload by testing it for one specific file out of the whole set. |
| 37 | + * @throws IOException |
| 38 | + */ |
| 39 | + @Test |
| 40 | + public void testIncludeReload() throws IOException { |
| 41 | + // Set new include root directory. |
| 42 | + Path includeRootPath = Files.createTempDirectory("systemControllerTest"); |
| 43 | + File includeRoot = includeRootPath.toFile(); |
| 44 | + env.setIncludeRoot(includeRoot.getAbsolutePath()); |
| 45 | + assertEquals(includeRoot.getCanonicalPath(), env.getIncludeRootPath()); |
| 46 | + |
| 47 | + // Create footer include file. |
| 48 | + File footerFile = new File(includeRoot, Configuration.FOOTER_INCLUDE_FILE); |
| 49 | + String content = "foo"; |
| 50 | + try (PrintWriter out = new PrintWriter(footerFile)) { |
| 51 | + out.println(content); |
| 52 | + } |
| 53 | + |
| 54 | + // Sanity check that getFooterIncludeFileContent() works since the test depends on it. |
| 55 | + String before = env.getConfiguration().getFooterIncludeFileContent(false); |
| 56 | + assertEquals(content, before.trim()); |
| 57 | + |
| 58 | + // Modify the contents of the file. |
| 59 | + content = content + "bar"; |
| 60 | + try (PrintWriter out = new PrintWriter(footerFile)) { |
| 61 | + out.println(content); |
| 62 | + } |
| 63 | + |
| 64 | + // Reload the contents via API call. |
| 65 | + Response r = target("system") |
| 66 | + .path("includes").path("reload") |
| 67 | + .request().put(Entity.text("")); |
| 68 | + assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus()); |
| 69 | + |
| 70 | + // Check that the content was reloaded. |
| 71 | + String after = env.getConfiguration().getFooterIncludeFileContent(false); |
| 72 | + assertNotEquals(before, after); |
| 73 | + assertEquals(content, after.trim()); |
| 74 | + |
| 75 | + // Cleanup |
| 76 | + IOUtils.removeRecursive(includeRootPath); |
| 77 | + } |
| 78 | +} |
0 commit comments