xref: /OpenGrok/suggester/src/test/java/org/opengrok/suggest/popular/impl/ChronicleMapAdapterTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
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  */
23 package org.opengrok.suggest.popular.impl;
24 
25 import org.apache.lucene.util.BytesRef;
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.opengrok.suggest.popular.impl.chronicle.ChronicleMapAdapter;
30 
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.util.AbstractMap.SimpleEntry;
35 import java.util.List;
36 import java.util.Map.Entry;
37 
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
40 import static org.junit.jupiter.api.Assertions.assertEquals;
41 
42 public class ChronicleMapAdapterTest {
43 
44     private static final String FIELD = "test";
45 
46     private ChronicleMapAdapter map;
47 
48     private Path tempFile;
49 
50     @BeforeEach
setUp()51     public void setUp() throws IOException {
52         tempFile = Files.createTempFile("opengrok", "test");
53 
54         map = new ChronicleMapAdapter(FIELD, 3, 10, tempFile.toFile());
55     }
56 
57     @AfterEach
tearDown()58     public void tearDown() throws IOException {
59         map.close();
60         Files.delete(tempFile);
61     }
62 
63     @Test
dataNotLostAfterResizeTest()64     public void dataNotLostAfterResizeTest() throws IOException {
65         fillData(0, 10, map);
66 
67         map.resize(20, 20);
68 
69         checkData(10, map);
70     }
71 
fillData(int start, int end, ChronicleMapAdapter map)72     private void fillData(int start, int end, ChronicleMapAdapter map) {
73         for (int i = start; i < end; i++) {
74             map.increment(new BytesRef("" + i), i);
75         }
76     }
77 
checkData(int count, ChronicleMapAdapter map)78     private void checkData(int count, ChronicleMapAdapter map) {
79         for (int i = 0; i < count; i++) {
80             assertEquals(i, map.get(new BytesRef("" + i)));
81         }
82     }
83 
84     @Test
testResize()85     public void testResize() throws IOException {
86         fillData(0, 10, map);
87 
88         map.resize(500, 20);
89 
90         fillData(10, 500, map);
91 
92         checkData(500, map);
93     }
94 
95     @Test
96     @SuppressWarnings("unchecked") // for contains()
testGetPopularityData()97     public void testGetPopularityData() {
98         Entry<BytesRef, Integer> e1 = new SimpleEntry<>(new BytesRef("test"), 1);
99         Entry<BytesRef, Integer> e2 = new SimpleEntry<>(new BytesRef("test2"), 2);
100 
101         map.increment(e1.getKey(), e1.getValue());
102         map.increment(e2.getKey(), e2.getValue());
103 
104         List<Entry<BytesRef, Integer>> data = map.getPopularityData(0, 10);
105 
106         assertThat(data, contains(e2, e1));
107     }
108 
109     @Test
110     @SuppressWarnings("unchecked") // for contains()
testGetPopularityPaging()111     public void testGetPopularityPaging() {
112         Entry<BytesRef, Integer> e1 = new SimpleEntry<>(new BytesRef("test"), 1);
113         Entry<BytesRef, Integer> e2 = new SimpleEntry<>(new BytesRef("test2"), 2);
114         Entry<BytesRef, Integer> e3 = new SimpleEntry<>(new BytesRef("test3"), 3);
115 
116         map.increment(e1.getKey(), e1.getValue());
117         map.increment(e2.getKey(), e2.getValue());
118         map.increment(e3.getKey(), e3.getValue());
119 
120         List<Entry<BytesRef, Integer>> data = map.getPopularityData(0, 2);
121 
122         assertThat(data, contains(e3, e2));
123 
124         data = map.getPopularityData(1, 2);
125 
126         assertThat(data, contains(e1));
127     }
128 
129 }
130