mirror of https://github.com/apache/nifi.git
NIFI-6509 - Date related issue in unit test VolatileComponentStatusRepositoryTest
As NIFI-6508 has already solved the original issue, turned this PR into a refactor within in the affected Test class. This closes #3621. Signed-off-by: Koji Kawamura <ijokarumawak@apache.org>
This commit is contained in:
parent
cc88dd428f
commit
3de81d618e
|
@ -27,8 +27,8 @@ import org.junit.Test;
|
|||
import org.apache.nifi.util.NiFiProperties;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.apache.nifi.controller.status.history.VolatileComponentStatusRepository.DEFAULT_NUM_DATA_POINTS;
|
||||
import static org.apache.nifi.controller.status.history.VolatileComponentStatusRepository.NUM_DATA_POINTS_PROPERTY;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
|
@ -39,120 +39,121 @@ import static org.testng.AssertJUnit.assertEquals;
|
|||
*/
|
||||
public class VolatileComponentStatusRepositoryTest {
|
||||
|
||||
private static VolatileComponentStatusRepository repo1;
|
||||
private static VolatileComponentStatusRepository repo2;
|
||||
private static VolatileComponentStatusRepository repo3;
|
||||
private static VolatileComponentStatusRepository filledRepo;
|
||||
private static VolatileComponentStatusRepository partiallyFilledRepo;
|
||||
private static VolatileComponentStatusRepository emptyRepo;
|
||||
private static final int FIVE_MINUTES = 300000;
|
||||
private static int BUFSIZE3 = 10;
|
||||
|
||||
@BeforeClass
|
||||
public static void createBuffers() {
|
||||
NiFiProperties props1 = mock(NiFiProperties.class);
|
||||
int BUFSIZE1 = 1_000_000;
|
||||
when(props1.getIntegerProperty(anyString(), anyInt())).thenReturn(BUFSIZE1);
|
||||
repo1 = new VolatileComponentStatusRepository(props1);
|
||||
// Fill the repo1 buffer completely with Date objects at five-minute intervals
|
||||
// This provides dates up to around Jul 1979
|
||||
for (long i = 0; i < BUFSIZE1; i++) {
|
||||
repo1.timestamps.add(new Date(i * FIVE_MINUTES));
|
||||
}
|
||||
assertEquals(BUFSIZE1, repo1.timestamps.getSize());
|
||||
filledRepo = initRepo(1_000_000, 0);
|
||||
|
||||
NiFiProperties props2 = mock(NiFiProperties.class);
|
||||
int BUFSIZE2 = 1000;
|
||||
when(props2.getIntegerProperty(anyString(), anyInt())).thenReturn(BUFSIZE2);
|
||||
repo2 = new VolatileComponentStatusRepository(props2);
|
||||
int OFFSET = 10;
|
||||
// Verify partially filled buffers work as expected.
|
||||
for (long i = 0; i < BUFSIZE2 - OFFSET; i++) {
|
||||
repo2.timestamps.add(new Date(i * FIVE_MINUTES));
|
||||
}
|
||||
assertEquals(BUFSIZE2 - OFFSET, repo2.timestamps.getSize());
|
||||
partiallyFilledRepo = initRepo(1000, 10);
|
||||
|
||||
NiFiProperties props3 = mock(NiFiProperties.class);
|
||||
when(props3.getIntegerProperty(anyString(), anyInt())).thenReturn(BUFSIZE3);
|
||||
repo3 = new VolatileComponentStatusRepository(props3);
|
||||
emptyRepo = createRepo(BUFSIZE3);
|
||||
}
|
||||
|
||||
private Date asDate(LocalDateTime localDateTime) {
|
||||
return Date.from(localDateTime.toInstant(ZoneOffset.UTC));
|
||||
private static VolatileComponentStatusRepository initRepo(int bufferSize, int offset) {
|
||||
VolatileComponentStatusRepository repo = createRepo(bufferSize);
|
||||
|
||||
for (long i = 0; i < bufferSize - offset; i++) {
|
||||
repo.timestamps.add(new Date(i * FIVE_MINUTES));
|
||||
}
|
||||
assertEquals(bufferSize - offset, repo.timestamps.getSize());
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
private static VolatileComponentStatusRepository createRepo(int bufferSize) {
|
||||
NiFiProperties props = mock(NiFiProperties.class);
|
||||
|
||||
when(props.getIntegerProperty(NUM_DATA_POINTS_PROPERTY, DEFAULT_NUM_DATA_POINTS)).thenReturn(bufferSize);
|
||||
|
||||
VolatileComponentStatusRepository repo = new VolatileComponentStatusRepository(props);
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
private static Date asDate(LocalDateTime localDateTime) {
|
||||
return Date.from(localDateTime.toInstant(ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterDatesReturnAll() {
|
||||
List<Date> dates = repo1.filterDates(null, null, Integer.MAX_VALUE);
|
||||
assert repo1.timestamps != null;
|
||||
assertEquals(repo1.timestamps.getSize(), dates.size());
|
||||
assertTrue(dates.equals(repo1.timestamps.asList()));
|
||||
repo1.timestamps.add(new Date());
|
||||
testFilterDatesReturnAll(filledRepo);
|
||||
testFilterDatesReturnAll(partiallyFilledRepo);
|
||||
}
|
||||
|
||||
dates = repo2.filterDates(null, null, Integer.MAX_VALUE);
|
||||
assertEquals(repo2.timestamps.getSize(), dates.size());
|
||||
assertTrue(dates.equals(repo2.timestamps.asList()));
|
||||
repo1.timestamps.add(new Date());
|
||||
private void testFilterDatesReturnAll(VolatileComponentStatusRepository repo) {
|
||||
List<Date> dates = repo.filterDates(null, null, Integer.MAX_VALUE);
|
||||
assert repo.timestamps != null;
|
||||
assertEquals(repo.timestamps.getSize(), dates.size());
|
||||
assertTrue(dates.equals(repo.timestamps.asList()));
|
||||
repo.timestamps.add(new Date());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterDatesUsingPreferredDataPoints() {
|
||||
List<Date> dates = repo1.filterDates(null, null, 1);
|
||||
List<Date> dates = filledRepo.filterDates(null, null, 1);
|
||||
assertEquals(1, dates.size());
|
||||
assertEquals(repo1.timestamps.getNewestElement(), dates.get(0));
|
||||
assertEquals(filledRepo.timestamps.getNewestElement(), dates.get(0));
|
||||
|
||||
int numPoints = 14;
|
||||
dates = repo1.filterDates(null, null, numPoints);
|
||||
assertEquals(numPoints, dates.size());
|
||||
assertEquals(repo1.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
assertEquals(repo1.timestamps.asList().get(repo1.timestamps.getSize() - numPoints), dates.get(0));
|
||||
testFilterDatesUsingPreferredDataPoints(filledRepo, 14);
|
||||
testFilterDatesUsingPreferredDataPoints(partiallyFilledRepo, 22);
|
||||
}
|
||||
|
||||
numPoints = 22;
|
||||
dates = repo2.filterDates(null, null, numPoints);
|
||||
private void testFilterDatesUsingPreferredDataPoints(VolatileComponentStatusRepository repo, int numPoints) {
|
||||
List<Date> dates = repo.filterDates(null, null, numPoints);
|
||||
assertEquals(numPoints, dates.size());
|
||||
assertEquals(repo2.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
assertEquals(repo2.timestamps.asList().get(repo2.timestamps.getSize() - numPoints),
|
||||
dates.get(0));
|
||||
assertEquals(repo.timestamps.getNewestElement(), dates.get(dates.size() - 1));
|
||||
assertEquals(repo.timestamps.asList().get(repo.timestamps.getSize() - numPoints), dates.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterDatesUsingStartFilter() {
|
||||
// Filter with date that exactly matches an entry in timestamps buffer
|
||||
Date start = asDate(LocalDateTime.of(1978, 1, 1, 0, 45, 0));
|
||||
List<Date> dates = repo1.filterDates(start, null, Integer.MAX_VALUE);
|
||||
List<Date> dates = filledRepo.filterDates(start, null, Integer.MAX_VALUE);
|
||||
assertEquals(start, dates.get(0));
|
||||
assertEquals(repo1.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
assertEquals(filledRepo.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
|
||||
// filter using a date that does not exactly match the time, i.e., not on a five-minute mark
|
||||
start = asDate(LocalDateTime.of(1974, 1, 1, 3, 2, 0));
|
||||
dates = repo1.filterDates(start, null, Integer.MAX_VALUE);
|
||||
dates = filledRepo.filterDates(start, null, Integer.MAX_VALUE);
|
||||
assertTrue(start.getTime() < dates.get(0).getTime());
|
||||
assertTrue(dates.get(0).getTime() < (start.getTime() + FIVE_MINUTES));
|
||||
assertEquals(repo1.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
assertEquals(filledRepo.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
|
||||
start = asDate(LocalDateTime.of(1970, 1, 1, 0, 0, 0));
|
||||
dates = repo2.filterDates(start, null, Integer.MAX_VALUE);
|
||||
dates = partiallyFilledRepo.filterDates(start, null, Integer.MAX_VALUE);
|
||||
assertEquals(start, dates.get(0));
|
||||
assertEquals(repo2.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
assertEquals(partiallyFilledRepo.timestamps.getNewestElement(), dates.get(dates.size()-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterDatesUsingEndFilter() {
|
||||
// Filter with date that exactly matches an entry in timestamps buffer
|
||||
Date end = asDate(LocalDateTime.of(1970, 2, 1,1, 10, 0));
|
||||
List<Date> dates = repo1.filterDates(null, end, Integer.MAX_VALUE);
|
||||
List<Date> dates = filledRepo.filterDates(null, end, Integer.MAX_VALUE);
|
||||
assertEquals(end, dates.get(dates.size()-1));
|
||||
assertEquals(repo1.timestamps.getOldestElement(), dates.get(0));
|
||||
assertEquals(filledRepo.timestamps.getOldestElement(), dates.get(0));
|
||||
|
||||
// filter using a date that does not exactly match the times in buffer
|
||||
end = asDate(LocalDateTime.of(1970, 2, 1,1, 7, 0));
|
||||
dates = repo1.filterDates(null, end, Integer.MAX_VALUE);
|
||||
dates = filledRepo.filterDates(null, end, Integer.MAX_VALUE);
|
||||
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
|
||||
assertTrue((end.getTime() - FIVE_MINUTES) < dates.get(dates.size()-1).getTime());
|
||||
assertEquals(dates.get(0), repo1.timestamps.getOldestElement());
|
||||
assertEquals(dates.get(0), filledRepo.timestamps.getOldestElement());
|
||||
|
||||
end = asDate(LocalDateTime.of(1970, 1, 2,1, 7, 0));
|
||||
dates = repo2.filterDates(null, end, Integer.MAX_VALUE);
|
||||
dates = partiallyFilledRepo.filterDates(null, end, Integer.MAX_VALUE);
|
||||
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
|
||||
assertTrue((end.getTime() - FIVE_MINUTES) < dates.get(dates.size()-1).getTime());
|
||||
assertEquals(repo2.timestamps.asList().get(0), dates.get(0));
|
||||
assertEquals(partiallyFilledRepo.timestamps.asList().get(0), dates.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -160,14 +161,14 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
// Filter with dates that exactly matches entries in timestamps buffer
|
||||
Date start = asDate(LocalDateTime.of(1975, 3, 1, 3, 15, 0));
|
||||
Date end = asDate(LocalDateTime.of(1978, 4, 2,4, 25, 0));
|
||||
List<Date> dates = repo1.filterDates(start, end, Integer.MAX_VALUE);
|
||||
List<Date> dates = filledRepo.filterDates(start, end, Integer.MAX_VALUE);
|
||||
assertEquals(start, dates.get(0));
|
||||
assertEquals(end, dates.get(dates.size()-1));
|
||||
|
||||
// Filter with dates that do not exactly matches entries in timestamps buffer
|
||||
start = asDate(LocalDateTime.of(1975, 3, 1, 3, 3, 0));
|
||||
end = asDate(LocalDateTime.of(1977, 4, 2,4, 8, 0));
|
||||
dates = repo1.filterDates(start, end, Integer.MAX_VALUE);
|
||||
dates = filledRepo.filterDates(start, end, Integer.MAX_VALUE);
|
||||
assertTrue(start.getTime() < dates.get(0).getTime());
|
||||
assertTrue(dates.get(0).getTime() < (start.getTime() + FIVE_MINUTES));
|
||||
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
|
||||
|
@ -175,7 +176,7 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
|
||||
start = asDate(LocalDateTime.of(1970, 1, 1, 3, 15, 0));
|
||||
end = asDate(LocalDateTime.of(1970, 1, 2,4, 25, 0));
|
||||
dates = repo1.filterDates(start, end, Integer.MAX_VALUE);
|
||||
dates = filledRepo.filterDates(start, end, Integer.MAX_VALUE);
|
||||
assertEquals(start, dates.get(0));
|
||||
assertEquals(end, dates.get(dates.size()-1));
|
||||
}
|
||||
|
@ -186,7 +187,7 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
int numPoints = 5;
|
||||
Date start = asDate(LocalDateTime.of(1977, 1, 1, 0, 30, 0));
|
||||
Date end = asDate(LocalDateTime.of(1977, 2, 1,1, 0, 0));
|
||||
List<Date> dates = repo1.filterDates(start, end, numPoints);
|
||||
List<Date> dates = filledRepo.filterDates(start, end, numPoints);
|
||||
assertEquals(numPoints, dates.size());
|
||||
assertEquals(dates.get(dates.size()-1), end);
|
||||
assertEquals(dates.get(dates.size()-numPoints), new Date(end.getTime() - (numPoints-1)*FIVE_MINUTES));
|
||||
|
@ -194,7 +195,7 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
// Filter with dates that do not exactly matches entries in timestamps buffer
|
||||
start = asDate(LocalDateTime.of(1975, 1, 1, 0, 31, 0));
|
||||
end = asDate(LocalDateTime.of(1978, 2, 1,1, 59, 0));
|
||||
dates = repo1.filterDates(start, end, numPoints);
|
||||
dates = filledRepo.filterDates(start, end, numPoints);
|
||||
assertTrue(dates.get(0).getTime() < new Date(end.getTime() - (numPoints-1)*FIVE_MINUTES).getTime());
|
||||
assertTrue(new Date(end.getTime() - (numPoints * FIVE_MINUTES)).getTime() < dates.get(0).getTime());
|
||||
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
|
||||
|
@ -203,7 +204,7 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
|
||||
start = asDate(LocalDateTime.of(1970, 1, 1, 0, 31, 0));
|
||||
end = asDate(LocalDateTime.of(1970, 1, 1,1, 59, 0));
|
||||
dates = repo2.filterDates(start, end, numPoints);
|
||||
dates = partiallyFilledRepo.filterDates(start, end, numPoints);
|
||||
assertTrue(dates.get(0).getTime() < new Date(end.getTime() - (numPoints-1)*FIVE_MINUTES).getTime());
|
||||
assertTrue(new Date(end.getTime() - (numPoints * FIVE_MINUTES)).getTime() < dates.get(0).getTime());
|
||||
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
|
||||
|
@ -215,16 +216,16 @@ public class VolatileComponentStatusRepositoryTest {
|
|||
public void testFilterWorksWithCircularBuffer() {
|
||||
// Fill repo3 with Date objects at five-minute intervals
|
||||
// This repository is used to verify circular actions behave as expected.
|
||||
for (int i = 0; i < 25; i++) {
|
||||
repo3.timestamps.add(new Date(i * FIVE_MINUTES));
|
||||
List<Date> dates = repo3.filterDates(null, null, Integer.MAX_VALUE);
|
||||
for (int i = 0; i < BUFSIZE3 + 15; i++) {
|
||||
emptyRepo.timestamps.add(new Date(i * FIVE_MINUTES));
|
||||
List<Date> dates = emptyRepo.filterDates(null, null, Integer.MAX_VALUE);
|
||||
if (i < BUFSIZE3 - 1) {
|
||||
assertEquals(null, repo3.timestamps.getOldestElement());
|
||||
assertEquals(repo3.timestamps.asList().get(0), dates.get(0));
|
||||
assertEquals(null, emptyRepo.timestamps.getOldestElement());
|
||||
} else {
|
||||
assertEquals(repo3.timestamps.getOldestElement(), dates.get(0));
|
||||
assertEquals(emptyRepo.timestamps.getOldestElement(), dates.get(0));
|
||||
}
|
||||
assertEquals(repo3.timestamps.getNewestElement(), dates.get(dates.size() - 1));
|
||||
assertEquals(emptyRepo.timestamps.asList().get(0), dates.get(0));
|
||||
assertEquals(emptyRepo.timestamps.getNewestElement(), dates.get(dates.size() - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue