Skip to content

Commit

Permalink
[TH2-5165] added BookWithoutPageTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Mar 6, 2024
1 parent 7f2926b commit a578802
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.exactpro.cradle.cassandra.iterators.PagedIterator;
import com.exactpro.cradle.cassandra.resultset.IteratorProvider;
import com.exactpro.cradle.cassandra.retries.SelectQueryExecutor;
import com.exactpro.cradle.cassandra.utils.StorageUtils;
import com.exactpro.cradle.cassandra.workers.MessagesWorker;
import com.exactpro.cradle.filters.FilterForAny;
import com.exactpro.cradle.filters.FilterForGreater;
Expand Down Expand Up @@ -141,7 +142,7 @@ protected FilterForGreater<Instant> createLeftBoundFilter(MessageFilter filter)

// If the page wasn't specified in the filter, we should find a batch with a lower date,
// which may contain messages that satisfy the original condition
LocalDateTime leftBoundLocalDate = TimeUtils.toLocalTimestamp(result.getValue());
LocalDateTime leftBoundLocalDate = StorageUtils.toLocalDateTime(result.getValue());
LocalTime nearestBatchTime = getNearestBatchTime(leftBoundFromPage, filter.getSessionAlias(),
filter.getDirection().getLabel(), leftBoundLocalDate.toLocalDate(),
leftBoundLocalDate.toLocalTime());
Expand Down Expand Up @@ -182,7 +183,7 @@ private LocalTime getNearestBatchTime(Instant timestamp,
} catch (Exception e) {
throw new CradleStorageException("Error while getting left bound ", e);
}
if (TimeUtils.toLocalTimestamp(page.getStarted()).toLocalDate().isBefore(messageDate)) {
if (StorageUtils.toLocalDateTime(page.getStarted()).toLocalDate().isBefore(messageDate)) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.exactpro.cradle.cassandra.iterators.PagedIterator;
import com.exactpro.cradle.cassandra.resultset.IteratorProvider;
import com.exactpro.cradle.cassandra.retries.SelectQueryExecutor;
import com.exactpro.cradle.cassandra.utils.StorageUtils;
import com.exactpro.cradle.cassandra.workers.MessagesWorker;
import com.exactpro.cradle.filters.FilterForGreater;
import com.exactpro.cradle.filters.FilterForLess;
Expand Down Expand Up @@ -119,7 +120,7 @@ private FilterForGreater<Instant> createLeftBoundFilter(GroupedMessageFilter fil

// If the page wasn't specified in the filter, we should find a batch with a lower date,
// which may contain messages that satisfy the original condition
LocalDateTime leftBoundLocalDate = TimeUtils.toLocalTimestamp(result.getValue());
LocalDateTime leftBoundLocalDate = StorageUtils.toLocalDateTime(result.getValue());
LocalTime nearestBatchTime = getNearestBatchTime(
leftBoundFromPage,
filter.getGroupName(),
Expand Down Expand Up @@ -161,7 +162,7 @@ private LocalTime getNearestBatchTime(Instant timestamp,
} catch (Exception e) {
throw new CradleStorageException("Error while getting left bound ", e);
}
if (TimeUtils.toLocalTimestamp(page.getStarted()).toLocalDate().isBefore(messageDate)) {
if (StorageUtils.toLocalDateTime(page.getStarted()).toLocalDate().isBefore(messageDate)) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ private static FilterForGreater<Instant> createLeftBoundFilter(BookInfo bookInfo
since `createNextFilter` just passes timestamps from previous to next filters
*/
PageInfo firstPage = findFirstPage(filter.getPageId(), filter.getStartTimestampFrom(), bookInfo);
long duration = eventBatchDurationWorker.getMaxDuration(filter.getBookId().getName(), firstPage.getName(), filter.getScope(), readAttrs);
long duration = 0;
if (firstPage != null) {
duration = eventBatchDurationWorker.getMaxDuration(filter.getBookId().getName(), firstPage.getName(), filter.getScope(), readAttrs);
}

ComparisonOperation operation = filter.getStartTimestampFrom() == null ? ComparisonOperation.GREATER : filter.getStartTimestampFrom().getOperation();
ComparisonOperation operation = filter.getStartTimestampFrom() == null ? ComparisonOperation.GREATER : filter.getStartTimestampFrom().getOperation();
if (operation.equals(ComparisonOperation.GREATER)) {
return FilterForGreater.forGreater(actualFrom.minusMillis(duration));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -143,4 +144,16 @@ public static LocalTime toLocalTime(Instant instant) {
}
return LocalTime.ofInstant(instant, TIMEZONE_OFFSET);
}

public static LocalDateTime toLocalDateTime(Instant instant) {
if (instant.isBefore(MIN_EPOCH_INSTANT)) {
LOGGER.trace("Replaces {} by {}", instant, MIN_EPOCH_INSTANT);
return LocalDateTime.MIN;
}
if (instant.isAfter(MAX_EPOCH_INSTANT)) {
LOGGER.trace("Replaces {} by {}", instant, MAX_EPOCH_INSTANT);
return LocalDateTime.MAX;
}
return LocalDateTime.ofInstant(instant, TIMEZONE_OFFSET);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* use tests with embedded cassandra without
* actually calling any of init or utility methods
*/
// TODO: implement tests for empty storage
public abstract class BaseCradleCassandraTest {
protected static final String DEFAULT_PAGE_PREFIX = "test_page_";
protected static final BookId DEFAULT_BOOK_ID = new BookId("test_book");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright 2023-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.exactpro.cradle.cassandra.integration;

import com.exactpro.cradle.BookToAdd;
import com.exactpro.cradle.Direction;
import com.exactpro.cradle.counters.Interval;
import com.exactpro.cradle.messages.GroupedMessageFilter;
import com.exactpro.cradle.messages.GroupedMessageFilterBuilder;
import com.exactpro.cradle.messages.MessageFilter;
import com.exactpro.cradle.messages.MessageFilterBuilder;
import com.exactpro.cradle.testevents.TestEventFilter;
import com.exactpro.cradle.testevents.TestEventFilterBuilder;
import com.exactpro.cradle.utils.CradleStorageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.time.Instant;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.emptyList;
import static org.testng.Assert.assertEquals;

public class BookWithoutPageTest extends BaseCradleCassandraTest {
private static final Logger LOGGER = LoggerFactory.getLogger(BookWithoutPageTest.class);

@BeforeClass
public void startUp() throws IOException, InterruptedException, CradleStorageException {
super.startUp(false);
generateData();
}

@Override
protected void generateData() throws CradleStorageException, IOException {
try {
storage.addBook(new BookToAdd(bookId.getName()));
} catch (CradleStorageException | IOException e) {
LOGGER.error("Error while generating data:", e);
throw e;
}
}

@Test
public void testGetAllPages() throws CradleStorageException {
try {
assertEquals(storage.getAllPages(bookId), emptyList());
} catch (CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetMessages() throws CradleStorageException, IOException {
try {
MessageFilter filter = new MessageFilterBuilder()
.bookId(bookId)
.sessionAlias("test-session-alias")
.direction(Direction.FIRST)
.timestampFrom().isGreaterThan(Instant.MIN)
.timestampTo().isLessThan(Instant.MAX)
.build();
assertEquals(newArrayList(storage.getMessages(filter)), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetMessageBatches() throws CradleStorageException, IOException {
try {
MessageFilter filter = new MessageFilterBuilder()
.bookId(bookId)
.sessionAlias("test-session-alias")
.direction(Direction.FIRST)
.timestampFrom().isGreaterThan(Instant.MIN)
.timestampTo().isLessThan(Instant.MAX)
.build();
assertEquals(newArrayList(storage.getMessageBatches(filter)), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetGroupedMessageBatches() throws CradleStorageException, IOException {
try {
GroupedMessageFilter filter = new GroupedMessageFilterBuilder()
.bookId(bookId)
.groupName("test-group")
.timestampFrom().isGreaterThan(Instant.MIN)
.timestampTo().isLessThan(Instant.MAX)
.build();
assertEquals(newArrayList(storage.getGroupedMessageBatches(filter)), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetLastSequence() throws CradleStorageException, IOException {
try {
assertEquals(storage.getLastSequence("test-session-alias", Direction.FIRST, bookId), -1L);
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetFirstSequence() throws CradleStorageException, IOException {
try {
assertEquals(storage.getFirstSequence("test-session-alias", Direction.FIRST, bookId), -1L);
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetSessionAliases1() throws CradleStorageException, IOException {
try {
assertEquals(storage.getSessionAliases(bookId), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetSessionAliases2() throws CradleStorageException {
try {
assertEquals(newArrayList(storage.getSessionAliases(bookId, new Interval(Instant.MIN, Instant.MAX))),
emptyList());
} catch (CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetGroups() throws CradleStorageException, IOException {
try {
assertEquals(storage.getGroups(bookId), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetTestEvents() throws CradleStorageException, IOException {
try {
TestEventFilter filter = new TestEventFilterBuilder()
.bookId(bookId)
.scope("test-scope")
.timestampFrom().isGreaterThan(Instant.MIN)
.timestampTo().isLessThan(Instant.MAX)
.build();
assertEquals(newArrayList(storage.getTestEvents(filter)), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetScopes1() throws CradleStorageException, IOException {
try {
assertEquals(storage.getScopes(bookId), emptyList());
} catch (CradleStorageException | IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetScopes2() throws CradleStorageException {
try {
assertEquals(newArrayList(storage.getScopes(bookId, new Interval(Instant.MIN, Instant.MAX))),
emptyList());
} catch (CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetSessionGroups() throws CradleStorageException {
try {
assertEquals(newArrayList(storage.getSessionGroups(bookId, new Interval(Instant.MIN, Instant.MAX))),
emptyList());
} catch (CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}

@Test
public void testGetPages() throws CradleStorageException {
try {
assertEquals(newArrayList(storage.getPages(bookId, new Interval(Instant.MIN, Instant.MAX))),
emptyList());
} catch (CradleStorageException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}
}

0 comments on commit a578802

Please sign in to comment.