use of org.orcid.persistence.jpa.entities.InvalidRecordDataChangeEntity in project ORCID-Source by ORCID.
the class InvalidRecordDataChangeDaoTest method assertAllPagesAscendingOrder.
private void assertAllPagesAscendingOrder(int pageSize, int expectedNumberOfPages, int expectedNumberOfElementsInLastPage) {
int firstElementIdOnDB = 1000;
int lastElementIdOnDB = 1015;
Long lastSequence = null;
List<InvalidRecordDataChangeEntity> page = dao.getByDateCreated(lastSequence, Long.valueOf(pageSize), false);
Long firstElementId = Long.valueOf(firstElementIdOnDB);
Long lastElementId = Long.valueOf(firstElementIdOnDB + (pageSize - 1));
int pageCount = 0;
do {
pageCount++;
// Assert the page is not null
assertNotNull(page);
// Check the first element is the one we expect
assertEquals(Long.valueOf(firstElementId), page.get(0).getId());
// Iterate over each element and verify the id is decreasing
for (InvalidRecordDataChangeEntity element : page) {
if (lastSequence == null) {
assertEquals(Long.valueOf(firstElementIdOnDB), element.getId());
lastSequence = element.getId();
} else {
assertTrue(element.getId() == (lastSequence + 1));
lastSequence = element.getId();
}
}
// pages
if (expectedNumberOfElementsInLastPage != 0 && pageCount == expectedNumberOfPages) {
assertEquals(expectedNumberOfElementsInLastPage, page.size());
assertEquals(Long.valueOf(lastElementIdOnDB), page.get(expectedNumberOfElementsInLastPage - 1).getId());
} else {
assertEquals(pageSize, page.size());
assertEquals(Long.valueOf(lastElementId), page.get(pageSize - 1).getId());
}
assertEquals(lastElementId, lastSequence);
firstElementId = lastElementId + 1;
lastElementId = (lastElementId + pageSize) < lastElementIdOnDB ? (lastElementId + pageSize) : lastElementIdOnDB;
page = dao.getByDateCreated(lastSequence, Long.valueOf(pageSize), false);
} while (!page.isEmpty());
assertEquals(expectedNumberOfPages, pageCount);
}
use of org.orcid.persistence.jpa.entities.InvalidRecordDataChangeEntity in project ORCID-Source by ORCID.
the class RecordCorrectionsManagerReadOnlyTest method before.
/**
* Simulates a list of 10 record corrections from 1 to 10.
* */
@Before
public void before() {
MockitoAnnotations.initMocks(this);
TargetProxyHelper.injectIntoProxy(manager, "dao", dao);
when(dao.getByDateCreated(ArgumentMatchers.anyLong(), ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).then(new Answer<List<InvalidRecordDataChangeEntity>>() {
@Override
public List<InvalidRecordDataChangeEntity> answer(InvocationOnMock invocation) throws Throwable {
Long first = (Long) invocation.getArgument(0);
Long size = (Long) invocation.getArgument(1);
Boolean order = (Boolean) invocation.getArgument(2);
List<InvalidRecordDataChangeEntity> elements = new ArrayList<InvalidRecordDataChangeEntity>();
if (order) {
for (long i = first; i > (first - size); i--) {
if (i < 1) {
break;
}
InvalidRecordDataChangeEntity element = new InvalidRecordDataChangeEntity();
element.setDateCreated(new Date());
element.setDescription("description " + i);
element.setId(Long.valueOf(i));
element.setLastModified(new Date());
element.setNumChanged(Long.valueOf(i));
element.setSqlUsedToUpdate("select * from table");
element.setType("type " + i);
elements.add(element);
}
} else {
for (long i = first; i < (first + size); i++) {
if (i > 10) {
break;
}
InvalidRecordDataChangeEntity element = new InvalidRecordDataChangeEntity();
element.setDateCreated(new Date());
element.setDescription("description " + i);
element.setId(Long.valueOf(i));
element.setLastModified(new Date());
element.setNumChanged(Long.valueOf(i));
element.setSqlUsedToUpdate("select * from table");
element.setType("type " + i);
elements.add(element);
}
}
return elements;
}
});
when(dao.haveNext(ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Long sequence = (Long) invocation.getArgument(0);
Boolean order = (Boolean) invocation.getArgument(1);
if (order) {
if (sequence <= 1L) {
return false;
}
} else {
if (sequence >= 10) {
return false;
}
}
return true;
}
});
when(dao.havePrevious(ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Long sequence = (Long) invocation.getArgument(0);
Boolean order = (Boolean) invocation.getArgument(1);
if (order) {
if (sequence >= 10L) {
return false;
}
} else {
if (sequence <= 1L) {
return false;
}
}
return true;
}
});
manager.cacheEvict();
}
use of org.orcid.persistence.jpa.entities.InvalidRecordDataChangeEntity in project ORCID-Source by ORCID.
the class JpaJaxbInvalidRecordDataChangeAdapterTest method fromEntityTest.
@Test
public void fromEntityTest() {
InvalidRecordDataChangeEntity entity = getEntity();
RecordCorrection element = adapter.toInvalidRecordDataChange(entity);
assertNotNull(element);
assertEquals(element.getSequence(), entity.getId());
assertEquals(element.getDateCreated(), entity.getDateCreated());
assertEquals(element.getDescription(), entity.getDescription());
assertEquals(element.getLastModified(), entity.getLastModified());
assertEquals(element.getNumChanged(), entity.getNumChanged());
assertEquals(element.getSqlUsedToUpdate(), entity.getSqlUsedToUpdate());
assertEquals(element.getType(), entity.getType());
}
use of org.orcid.persistence.jpa.entities.InvalidRecordDataChangeEntity in project ORCID-Source by ORCID.
the class InvalidRecordDataChangeDaoTest method fetchAllAscendantTest.
@Test
public void fetchAllAscendantTest() {
List<InvalidRecordDataChangeEntity> onePage = dao.getByDateCreated(null, 16L, false);
assertNotNull(onePage);
assertEquals(16, onePage.size());
// Assert the first one
assertEquals(Long.valueOf(1000), onePage.get(0).getId());
// Assert the last one
assertEquals(Long.valueOf(1015), onePage.get(15).getId());
Iterator<InvalidRecordDataChangeEntity> it = onePage.iterator();
Long initial = null;
Long next = null;
// Verify they respect the descendant ordered
do {
InvalidRecordDataChangeEntity current = it.next();
if (initial == null) {
initial = current.getId();
continue;
}
next = current.getId();
assertTrue((initial + 1L) == next);
initial = next;
next = null;
} while (it.hasNext());
}
use of org.orcid.persistence.jpa.entities.InvalidRecordDataChangeEntity in project ORCID-Source by ORCID.
the class RecordCorrectionsManagerReadOnlyImpl method getInvalidRecordDataChangesAscending.
@Override
@Cacheable(value = "invalid-record-data-change-page-asc", key = "(#lastElement == null ? 'none' : #lastElement.toString()).concat('-').concat(#pageSize.toString())")
public RecordCorrectionsPage getInvalidRecordDataChangesAscending(Long lastElement, Long pageSize) {
List<InvalidRecordDataChangeEntity> entities = dao.getByDateCreated(lastElement, pageSize, ASCENDING);
if (entities == null || entities.isEmpty()) {
throw new IllegalArgumentException("Unable to find a page with the following params: lastElement=" + lastElement + " pageSize: " + pageSize + " ascending order");
}
List<RecordCorrection> elements = adapter.toInvalidRecordDataChanges(entities);
Long first = null;
Long last = null;
for (RecordCorrection element : elements) {
if (first == null || element.getSequence() < first) {
first = element.getSequence();
}
if (last == null || element.getSequence() > last) {
last = element.getSequence();
}
}
Boolean haveNext = dao.haveNext(last, ASCENDING);
Boolean havePrevious = dao.havePrevious(first, ASCENDING);
RecordCorrectionsPage page = new RecordCorrectionsPage();
page.setFirstElementId(first);
page.setLastElementId(last);
page.setHaveNext(haveNext);
page.setHavePrevious(havePrevious);
page.setRecordCorrections(elements);
return page;
}
Aggregations