3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.activity.index;
23 import org.assertj.core.data.MapEntry;
24 import org.elasticsearch.action.update.UpdateRequest;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.System2;
29 import org.sonar.db.DbTester;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class ActivityResultSetIteratorTest {
36 public DbTester dbTester = DbTester.create(System2.INSTANCE);
39 * Iterate over two rows in table.
42 public void traverse() {
43 dbTester.prepareDbUnit(getClass(), "traverse.xml");
44 ActivityResultSetIterator it = ActivityResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L);
46 assertThat(it.hasNext()).isTrue();
47 UpdateRequest request = it.next();
48 Map<String, Object> doc = request.doc().sourceAsMap();
49 assertThat(doc.get(ActivityIndexDefinition.FIELD_KEY)).isEqualTo("UUID1");
50 assertThat(doc.get(ActivityIndexDefinition.FIELD_ACTION)).isEqualTo("THE_ACTION");
51 assertThat(doc.get(ActivityIndexDefinition.FIELD_MESSAGE)).isEqualTo("THE_MSG");
52 assertThat((Map) doc.get(ActivityIndexDefinition.FIELD_DETAILS)).containsOnly(MapEntry.entry("foo", "bar"));
53 assertThat(doc.get(ActivityIndexDefinition.FIELD_LOGIN)).isEqualTo("THE_AUTHOR");
55 assertThat(it.hasNext()).isTrue();
56 assertThat(it.next()).isNotNull();
57 assertThat(it.hasNext()).isFalse();
60 assertThat(formatLongDate(it.getMaxRowDate())).startsWith("2015-01-01");
64 public void traverse_after_date() {
65 dbTester.prepareDbUnit(getClass(), "traverse.xml");
66 ActivityResultSetIterator it = ActivityResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), DateUtils.parseDate("2014-12-01").getTime());
68 assertThat(it.hasNext()).isTrue();
69 UpdateRequest request = it.next();
70 assertThat(request).isNotNull();
71 Map<String, Object> doc = request.doc().sourceAsMap();
72 assertThat(doc.get(ActivityIndexDefinition.FIELD_KEY)).isEqualTo("UUID2");
74 assertThat(it.hasNext()).isFalse();
77 assertThat(formatLongDate(it.getMaxRowDate())).startsWith("2015-01-01");
81 public void nothing_to_traverse() {
82 dbTester.prepareDbUnit(getClass(), "traverse.xml");
83 ActivityResultSetIterator it = ActivityResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), DateUtils.parseDate("2030-01-01").getTime());
85 assertThat(it.hasNext()).isFalse();
88 assertThat(it.getMaxRowDate()).isEqualTo(0L);
91 private String formatLongDate(long dateInMs) {
92 return DateUtils.formatDateTime(DateUtils.longToDate(dateInMs));