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<String, String>) doc.get(ActivityIndexDefinition.FIELD_DETAILS))
53 .containsOnly(MapEntry.entry("foo", "bar"), MapEntry.entry("profileKey", "PROFILE_KEY"));
54 assertThat(doc.get(ActivityIndexDefinition.FIELD_LOGIN)).isEqualTo("THE_AUTHOR");
56 assertThat(it.hasNext()).isTrue();
57 assertThat(it.next()).isNotNull();
58 assertThat(it.hasNext()).isFalse();
61 assertThat(formatLongDate(it.getMaxRowDate())).startsWith("2015-01-01");
65 public void traverse_after_date() {
66 dbTester.prepareDbUnit(getClass(), "traverse.xml");
67 ActivityResultSetIterator it = ActivityResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), DateUtils.parseDate("2014-12-01").getTime());
69 assertThat(it.hasNext()).isTrue();
70 UpdateRequest request = it.next();
71 assertThat(request).isNotNull();
72 Map<String, Object> doc = request.doc().sourceAsMap();
73 assertThat(doc.get(ActivityIndexDefinition.FIELD_KEY)).isEqualTo("UUID2");
75 assertThat(it.hasNext()).isFalse();
78 assertThat(formatLongDate(it.getMaxRowDate())).startsWith("2015-01-01");
82 public void nothing_to_traverse() {
83 dbTester.prepareDbUnit(getClass(), "traverse.xml");
84 ActivityResultSetIterator it = ActivityResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), DateUtils.parseDate("2030-01-01").getTime());
86 assertThat(it.hasNext()).isFalse();
89 assertThat(it.getMaxRowDate()).isEqualTo(0L);
92 private String formatLongDate(long dateInMs) {
93 return DateUtils.formatDateTime(DateUtils.longToDate(dateInMs));