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:
Tamas Palfy 2019-08-21 19:25:16 +02:00 committed by Koji Kawamura
parent cc88dd428f
commit 3de81d618e
No known key found for this signature in database
GPG Key ID: 36136B0EC89E4758
1 changed files with 74 additions and 73 deletions

View File

@ -27,8 +27,8 @@ import org.junit.Test;
import org.apache.nifi.util.NiFiProperties; import org.apache.nifi.util.NiFiProperties;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt; import static org.apache.nifi.controller.status.history.VolatileComponentStatusRepository.DEFAULT_NUM_DATA_POINTS;
import static org.mockito.ArgumentMatchers.anyString; import static org.apache.nifi.controller.status.history.VolatileComponentStatusRepository.NUM_DATA_POINTS_PROPERTY;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
@ -39,120 +39,121 @@ import static org.testng.AssertJUnit.assertEquals;
*/ */
public class VolatileComponentStatusRepositoryTest { public class VolatileComponentStatusRepositoryTest {
private static VolatileComponentStatusRepository repo1; private static VolatileComponentStatusRepository filledRepo;
private static VolatileComponentStatusRepository repo2; private static VolatileComponentStatusRepository partiallyFilledRepo;
private static VolatileComponentStatusRepository repo3; private static VolatileComponentStatusRepository emptyRepo;
private static final int FIVE_MINUTES = 300000; private static final int FIVE_MINUTES = 300000;
private static int BUFSIZE3 = 10; private static int BUFSIZE3 = 10;
@BeforeClass @BeforeClass
public static void createBuffers() { 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 // Fill the repo1 buffer completely with Date objects at five-minute intervals
// This provides dates up to around Jul 1979 // This provides dates up to around Jul 1979
for (long i = 0; i < BUFSIZE1; i++) { filledRepo = initRepo(1_000_000, 0);
repo1.timestamps.add(new Date(i * FIVE_MINUTES));
}
assertEquals(BUFSIZE1, repo1.timestamps.getSize());
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. // Verify partially filled buffers work as expected.
for (long i = 0; i < BUFSIZE2 - OFFSET; i++) { partiallyFilledRepo = initRepo(1000, 10);
repo2.timestamps.add(new Date(i * FIVE_MINUTES));
}
assertEquals(BUFSIZE2 - OFFSET, repo2.timestamps.getSize());
NiFiProperties props3 = mock(NiFiProperties.class); emptyRepo = createRepo(BUFSIZE3);
when(props3.getIntegerProperty(anyString(), anyInt())).thenReturn(BUFSIZE3);
repo3 = new VolatileComponentStatusRepository(props3);
} }
private Date asDate(LocalDateTime localDateTime) { private static VolatileComponentStatusRepository initRepo(int bufferSize, int offset) {
return Date.from(localDateTime.toInstant(ZoneOffset.UTC)); 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 @Test
public void testFilterDatesReturnAll() { public void testFilterDatesReturnAll() {
List<Date> dates = repo1.filterDates(null, null, Integer.MAX_VALUE); testFilterDatesReturnAll(filledRepo);
assert repo1.timestamps != null; testFilterDatesReturnAll(partiallyFilledRepo);
assertEquals(repo1.timestamps.getSize(), dates.size()); }
assertTrue(dates.equals(repo1.timestamps.asList()));
repo1.timestamps.add(new Date());
dates = repo2.filterDates(null, null, Integer.MAX_VALUE); private void testFilterDatesReturnAll(VolatileComponentStatusRepository repo) {
assertEquals(repo2.timestamps.getSize(), dates.size()); List<Date> dates = repo.filterDates(null, null, Integer.MAX_VALUE);
assertTrue(dates.equals(repo2.timestamps.asList())); assert repo.timestamps != null;
repo1.timestamps.add(new Date()); assertEquals(repo.timestamps.getSize(), dates.size());
assertTrue(dates.equals(repo.timestamps.asList()));
repo.timestamps.add(new Date());
} }
@Test @Test
public void testFilterDatesUsingPreferredDataPoints() { public void testFilterDatesUsingPreferredDataPoints() {
List<Date> dates = repo1.filterDates(null, null, 1); List<Date> dates = filledRepo.filterDates(null, null, 1);
assertEquals(1, dates.size()); assertEquals(1, dates.size());
assertEquals(repo1.timestamps.getNewestElement(), dates.get(0)); assertEquals(filledRepo.timestamps.getNewestElement(), dates.get(0));
int numPoints = 14; testFilterDatesUsingPreferredDataPoints(filledRepo, 14);
dates = repo1.filterDates(null, null, numPoints); testFilterDatesUsingPreferredDataPoints(partiallyFilledRepo, 22);
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));
numPoints = 22; private void testFilterDatesUsingPreferredDataPoints(VolatileComponentStatusRepository repo, int numPoints) {
dates = repo2.filterDates(null, null, numPoints); List<Date> dates = repo.filterDates(null, null, numPoints);
assertEquals(numPoints, dates.size()); assertEquals(numPoints, dates.size());
assertEquals(repo2.timestamps.getNewestElement(), dates.get(dates.size()-1)); assertEquals(repo.timestamps.getNewestElement(), dates.get(dates.size() - 1));
assertEquals(repo2.timestamps.asList().get(repo2.timestamps.getSize() - numPoints), assertEquals(repo.timestamps.asList().get(repo.timestamps.getSize() - numPoints), dates.get(0));
dates.get(0));
} }
@Test @Test
public void testFilterDatesUsingStartFilter() { public void testFilterDatesUsingStartFilter() {
// Filter with date that exactly matches an entry in timestamps buffer // Filter with date that exactly matches an entry in timestamps buffer
Date start = asDate(LocalDateTime.of(1978, 1, 1, 0, 45, 0)); 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(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 // 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)); 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(start.getTime() < dates.get(0).getTime());
assertTrue(dates.get(0).getTime() < (start.getTime() + FIVE_MINUTES)); 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)); 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(start, dates.get(0));
assertEquals(repo2.timestamps.getNewestElement(), dates.get(dates.size()-1)); assertEquals(partiallyFilledRepo.timestamps.getNewestElement(), dates.get(dates.size()-1));
} }
@Test @Test
public void testFilterDatesUsingEndFilter() { public void testFilterDatesUsingEndFilter() {
// Filter with date that exactly matches an entry in timestamps buffer // Filter with date that exactly matches an entry in timestamps buffer
Date end = asDate(LocalDateTime.of(1970, 2, 1,1, 10, 0)); 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(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 // filter using a date that does not exactly match the times in buffer
end = asDate(LocalDateTime.of(1970, 2, 1,1, 7, 0)); 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(dates.get(dates.size()-1).getTime() < end.getTime());
assertTrue((end.getTime() - FIVE_MINUTES) < dates.get(dates.size()-1).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)); 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(dates.get(dates.size()-1).getTime() < end.getTime());
assertTrue((end.getTime() - FIVE_MINUTES) < dates.get(dates.size()-1).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 @Test
@ -160,14 +161,14 @@ public class VolatileComponentStatusRepositoryTest {
// Filter with dates that exactly matches entries in timestamps buffer // Filter with dates that exactly matches entries in timestamps buffer
Date start = asDate(LocalDateTime.of(1975, 3, 1, 3, 15, 0)); Date start = asDate(LocalDateTime.of(1975, 3, 1, 3, 15, 0));
Date end = asDate(LocalDateTime.of(1978, 4, 2,4, 25, 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(start, dates.get(0));
assertEquals(end, dates.get(dates.size()-1)); assertEquals(end, dates.get(dates.size()-1));
// Filter with dates that do not exactly matches entries in timestamps buffer // Filter with dates that do not exactly matches entries in timestamps buffer
start = asDate(LocalDateTime.of(1975, 3, 1, 3, 3, 0)); start = asDate(LocalDateTime.of(1975, 3, 1, 3, 3, 0));
end = asDate(LocalDateTime.of(1977, 4, 2,4, 8, 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(start.getTime() < dates.get(0).getTime());
assertTrue(dates.get(0).getTime() < (start.getTime() + FIVE_MINUTES)); assertTrue(dates.get(0).getTime() < (start.getTime() + FIVE_MINUTES));
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime()); 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)); start = asDate(LocalDateTime.of(1970, 1, 1, 3, 15, 0));
end = asDate(LocalDateTime.of(1970, 1, 2,4, 25, 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(start, dates.get(0));
assertEquals(end, dates.get(dates.size()-1)); assertEquals(end, dates.get(dates.size()-1));
} }
@ -186,7 +187,7 @@ public class VolatileComponentStatusRepositoryTest {
int numPoints = 5; int numPoints = 5;
Date start = asDate(LocalDateTime.of(1977, 1, 1, 0, 30, 0)); Date start = asDate(LocalDateTime.of(1977, 1, 1, 0, 30, 0));
Date end = asDate(LocalDateTime.of(1977, 2, 1,1, 0, 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(numPoints, dates.size());
assertEquals(dates.get(dates.size()-1), end); assertEquals(dates.get(dates.size()-1), end);
assertEquals(dates.get(dates.size()-numPoints), new Date(end.getTime() - (numPoints-1)*FIVE_MINUTES)); 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 // Filter with dates that do not exactly matches entries in timestamps buffer
start = asDate(LocalDateTime.of(1975, 1, 1, 0, 31, 0)); start = asDate(LocalDateTime.of(1975, 1, 1, 0, 31, 0));
end = asDate(LocalDateTime.of(1978, 2, 1,1, 59, 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(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(new Date(end.getTime() - (numPoints * FIVE_MINUTES)).getTime() < dates.get(0).getTime());
assertTrue(dates.get(dates.size()-1).getTime() < end.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)); start = asDate(LocalDateTime.of(1970, 1, 1, 0, 31, 0));
end = asDate(LocalDateTime.of(1970, 1, 1,1, 59, 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(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(new Date(end.getTime() - (numPoints * FIVE_MINUTES)).getTime() < dates.get(0).getTime());
assertTrue(dates.get(dates.size()-1).getTime() < end.getTime()); assertTrue(dates.get(dates.size()-1).getTime() < end.getTime());
@ -215,16 +216,16 @@ public class VolatileComponentStatusRepositoryTest {
public void testFilterWorksWithCircularBuffer() { public void testFilterWorksWithCircularBuffer() {
// Fill repo3 with Date objects at five-minute intervals // Fill repo3 with Date objects at five-minute intervals
// This repository is used to verify circular actions behave as expected. // This repository is used to verify circular actions behave as expected.
for (int i = 0; i < 25; i++) { for (int i = 0; i < BUFSIZE3 + 15; i++) {
repo3.timestamps.add(new Date(i * FIVE_MINUTES)); emptyRepo.timestamps.add(new Date(i * FIVE_MINUTES));
List<Date> dates = repo3.filterDates(null, null, Integer.MAX_VALUE); List<Date> dates = emptyRepo.filterDates(null, null, Integer.MAX_VALUE);
if (i < BUFSIZE3 - 1) { if (i < BUFSIZE3 - 1) {
assertEquals(null, repo3.timestamps.getOldestElement()); assertEquals(null, emptyRepo.timestamps.getOldestElement());
assertEquals(repo3.timestamps.asList().get(0), dates.get(0));
} else { } 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));
} }
} }
} }