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 * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>. 22 */ 23 package org.opengrok.web.api.v1.controller; 24 25 import jakarta.ws.rs.core.Application; 26 import org.glassfish.jersey.server.ResourceConfig; 27 import org.junit.jupiter.api.AfterEach; 28 import org.junit.jupiter.api.BeforeEach; 29 import org.junit.jupiter.api.Test; 30 import org.opengrok.indexer.configuration.RuntimeEnvironment; 31 import org.opengrok.indexer.history.HistoryGuru; 32 import org.opengrok.indexer.history.RepositoryFactory; 33 import org.opengrok.indexer.index.Indexer; 34 import org.opengrok.indexer.util.TestRepository; 35 36 import java.io.IOException; 37 import java.nio.file.Files; 38 import java.nio.file.Paths; 39 import java.util.ArrayList; 40 import java.util.Collections; 41 import java.util.concurrent.ConcurrentHashMap; 42 43 import static org.junit.jupiter.api.Assertions.assertEquals; 44 45 public class FileControllerTest extends OGKJerseyTest { 46 47 private final RuntimeEnvironment env = RuntimeEnvironment.getInstance(); 48 49 private TestRepository repository; 50 51 @Override configure()52 protected Application configure() { 53 return new ResourceConfig(FileController.class); 54 } 55 56 @BeforeEach 57 @Override setUp()58 public void setUp() throws Exception { 59 super.setUp(); 60 repository = new TestRepository(); 61 repository.create(HistoryGuru.class.getResource("/repositories")); 62 63 env.setSourceRoot(repository.getSourceRoot()); 64 env.setDataRoot(repository.getDataRoot()); 65 env.setProjectsEnabled(true); 66 env.setHistoryEnabled(true); 67 RepositoryFactory.initializeIgnoredNames(env); 68 69 Indexer.getInstance().prepareIndexer( 70 env, 71 true, // search for repositories 72 true, // scan and add projects 73 false, // don't create dictionary 74 null, // subFiles - needed when refreshing history partially 75 null); // repositories - needed when refreshing history partially 76 Indexer.getInstance().doIndexerExecution(true, Collections.singletonList("/git"), null); 77 } 78 79 @AfterEach 80 @Override tearDown()81 public void tearDown() throws Exception { 82 super.tearDown(); 83 84 // This should match Configuration constructor. 85 env.setProjects(new ConcurrentHashMap<>()); 86 env.setRepositories(new ArrayList<>()); 87 env.getProjectRepositoriesMap().clear(); 88 89 repository.destroy(); 90 } 91 92 @Test testFileContent()93 public void testFileContent() throws IOException { 94 final String path = "git/header.h"; 95 byte[] encoded = Files.readAllBytes(Paths.get(repository.getSourceRoot(), path)); 96 String contents = new String(encoded); 97 String output = target("file") 98 .path("content") 99 .queryParam("path", path) 100 .request() 101 .get(String.class); 102 assertEquals(contents, output); 103 } 104 105 @Test testFileGenre()106 public void testFileGenre() { 107 final String path = "git/main.c"; 108 String genre = target("file") 109 .path("genre") 110 .queryParam("path", path) 111 .request() 112 .get(String.class); 113 assertEquals("PLAIN", genre); 114 } 115 } 116