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) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2019, 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 org.junit.jupiter.api.AfterAll;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.opengrok.suggest.Suggester;
32 import org.opengrok.indexer.configuration.RuntimeEnvironment;
33 import org.opengrok.indexer.configuration.SuggesterConfig;
34 import org.opengrok.indexer.index.Indexer;
35 import org.opengrok.indexer.search.QueryBuilder;
36 import org.opengrok.indexer.util.TestRepository;
37 import org.opengrok.web.api.v1.RestApp;
38 import org.opengrok.web.api.v1.suggester.provider.service.impl.SuggesterServiceImpl;
39 
40 import java.io.File;
41 import java.lang.reflect.Field;
42 import java.util.Collections;
43 import java.util.Map;
44 import java.util.concurrent.TimeUnit;
45 import java.util.stream.Collectors;
46 
47 import static org.awaitility.Awaitility.await;
48 import static org.hamcrest.MatcherAssert.assertThat;
49 import static org.hamcrest.Matchers.containsInAnyOrder;
50 
51 public class SuggesterControllerProjectsDisabledTest extends OGKJerseyTest {
52 
53     private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
54 
55     private static TestRepository repository;
56 
57     @Override
configure()58     protected Application configure() {
59         return new RestApp();
60     }
61 
62     @BeforeAll
setUpClass()63     public static void setUpClass() throws Exception {
64         repository = new TestRepository();
65         repository.create(SuggesterControllerTest.class.getClassLoader().getResource("sources"));
66 
67         env.setHistoryEnabled(false);
68         env.setProjectsEnabled(false);
69         env.setSourceRoot(repository.getSourceRoot() + File.separator + "java");
70         Indexer.getInstance().prepareIndexer(env, true, true,
71                 false, null, null);
72         env.setDefaultProjectsFromNames(Collections.singleton("__all__"));
73         Indexer.getInstance().doIndexerExecution(true, null, null);
74 
75         env.getSuggesterConfig().setRebuildCronConfig(null);
76     }
77 
78     @AfterAll
tearDownClass()79     public static void tearDownClass() {
80         repository.destroy();
81     }
82 
83     @BeforeEach
before()84     public void before() {
85         await().atMost(15, TimeUnit.SECONDS).until(() -> getSuggesterProjectDataSize() == 1);
86 
87         env.setSuggesterConfig(new SuggesterConfig());
88     }
89 
getSuggesterProjectDataSize()90     private static int getSuggesterProjectDataSize() throws Exception {
91         Field f = SuggesterServiceImpl.class.getDeclaredField("suggester");
92         f.setAccessible(true);
93         Suggester suggester = (Suggester) f.get(SuggesterServiceImpl.getInstance());
94 
95         Field f2 = Suggester.class.getDeclaredField("projectData");
96         f2.setAccessible(true);
97 
98         return ((Map) f2.get(suggester)).size();
99     }
100 
101     @Test
suggestionsSimpleTest()102     void suggestionsSimpleTest() {
103         SuggesterControllerTest.Result res = target(SuggesterController.PATH)
104                 .queryParam("field", QueryBuilder.FULL)
105                 .queryParam(QueryBuilder.FULL, "inner")
106                 .request()
107                 .get(SuggesterControllerTest.Result.class);
108 
109         assertThat(res.suggestions.stream().map(r -> r.phrase).collect(Collectors.toList()),
110                 containsInAnyOrder("innermethod", "innerclass"));
111     }
112 
113 }
114