xref: /OpenGrok/opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/AnnotationControllerTest.java (revision 9a303f1948b99409943e5b2e00a139a726dbc8d4)
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.core.Application;
27 import jakarta.ws.rs.core.GenericType;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.junit.jupiter.api.AfterEach;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.opengrok.indexer.configuration.RuntimeEnvironment;
33 import org.opengrok.indexer.history.HistoryGuru;
34 import org.opengrok.indexer.history.RepositoryFactory;
35 import org.opengrok.indexer.index.Indexer;
36 import org.opengrok.indexer.util.TestRepository;
37 
38 import java.io.BufferedReader;
39 import java.io.File;
40 import java.io.FileReader;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Set;
47 import java.util.concurrent.ConcurrentHashMap;
48 import java.util.stream.Collectors;
49 
50 import static org.junit.jupiter.api.Assertions.assertEquals;
51 import static org.junit.jupiter.api.Assertions.assertTrue;
52 
53 public class AnnotationControllerTest extends OGKJerseyTest {
54 
55     private final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
56 
57     private TestRepository repository;
58 
59     @Override
configure()60     protected Application configure() {
61         return new ResourceConfig(AnnotationController.class);
62     }
63 
64     @BeforeEach
65     @Override
setUp()66     public void setUp() throws Exception {
67         super.setUp();
68         repository = new TestRepository();
69         repository.create(HistoryGuru.class.getResource("/repositories"));
70 
71         env.setSourceRoot(repository.getSourceRoot());
72         env.setDataRoot(repository.getDataRoot());
73         env.setProjectsEnabled(true);
74         env.setHistoryEnabled(true);
75         RepositoryFactory.initializeIgnoredNames(env);
76 
77         Indexer.getInstance().prepareIndexer(
78                 env,
79                 true, // search for repositories
80                 true, // scan and add projects
81                 false, // don't create dictionary
82                 null, // subFiles - needed when refreshing history partially
83                 null); // repositories - needed when refreshing history partially
84     }
85 
86     @AfterEach
87     @Override
tearDown()88     public void tearDown() throws Exception {
89         super.tearDown();
90 
91         // This should match Configuration constructor.
92         env.setProjects(new ConcurrentHashMap<>());
93         env.setRepositories(new ArrayList<>());
94         env.getProjectRepositoriesMap().clear();
95 
96         repository.destroy();
97     }
98 
getNumLines(File file)99     private static int getNumLines(File file) throws IOException {
100         int lines = 0;
101 
102         try (BufferedReader in = new BufferedReader(new FileReader(file))) {
103             while (in.readLine() != null) {
104                 lines++;
105             }
106         }
107 
108         return lines;
109     }
110 
111     @Test
testAnnotationAPI()112     public void testAnnotationAPI() throws IOException {
113         final String path = "git/Makefile";
114         List<AnnotationController.AnnotationDTO> annotations = target("annotation")
115                 .queryParam("path", path)
116                 .request()
117                 .get(new GenericType<>() {
118                 });
119         assertEquals(getNumLines(new File(env.getSourceRootFile(), path)), annotations.size());
120         assertEquals("Trond Norbye", annotations.get(0).getAuthor());
121         List<String> ids = annotations.stream().
122                 map(AnnotationController.AnnotationDTO::getRevision).
123                 collect(Collectors.toList());
124         assertEquals(Arrays.asList("bb74b7e8", "bb74b7e8", "bb74b7e8", "bb74b7e8", "bb74b7e8",
125                 "bb74b7e8", "bb74b7e8", "bb74b7e8", "aa35c258", "aa35c258", "aa35c258"), ids);
126         List<String> versions = annotations.stream().
127                 map(AnnotationController.AnnotationDTO::getVersion).
128                 collect(Collectors.toList());
129         assertEquals(Arrays.asList("1/2", "1/2", "1/2", "1/2", "1/2", "1/2", "1/2", "1/2", "2/2", "2/2", "2/2"),
130                 versions);
131         assertTrue(annotations.get(0).getDescription().contains("sunray"));
132     }
133 
134     @Test
testAnnotationAPIWithRevision()135     public void testAnnotationAPIWithRevision() {
136         final String path = "git/Makefile";
137         List<AnnotationController.AnnotationDTO> annotations = target("annotation")
138                 .queryParam("path", path)
139                 .queryParam("revision", "bb74b7e8")
140                 .request()
141                 .get(new GenericType<>() {
142                 });
143         assertEquals(8, annotations.size());
144         assertEquals("Trond Norbye", annotations.get(0).getAuthor());
145         Set<String> ids = annotations.stream().
146                 map(AnnotationController.AnnotationDTO::getRevision).
147                 collect(Collectors.toSet());
148         List<String> versions = annotations.stream().
149                 map(AnnotationController.AnnotationDTO::getVersion).
150                 collect(Collectors.toList());
151         assertEquals(Arrays.asList("1/1", "1/1", "1/1", "1/1", "1/1", "1/1", "1/1", "1/1"),
152                 versions);
153         assertEquals(Collections.singleton("bb74b7e8"), ids);
154     }
155 }
156