empty list checks (#6262)

This commit is contained in:
nikinagy 2020-05-19 14:04:19 +01:00 committed by GitHub
parent 350066d386
commit cc8ce3ca99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 8 deletions

View File

@ -766,8 +766,14 @@ class HibernateQueryCriteriaParser(val contractStateType: Class<out ContractStat
val existingParticipants = ((((commonPredicates[predicateID]) as CompoundPredicate).expressions[0]) as InPredicate<*>)
.values.map { participant -> (participant as LiteralExpression<*>).literal }
log.warn("Adding new participants: $participants to existing participants: $existingParticipants")
commonPredicates.replace(predicateID, criteriaBuilder.and(
getPersistentPartyRoot().get<VaultSchemaV1.PersistentParty>("x500Name").`in`(existingParticipants + participants)))
commonPredicates.replace(
predicateID,
checkIfListIsEmpty(
args = existingParticipants + participants,
criteriaBuilder = criteriaBuilder,
predicate = criteriaBuilder.and(getPersistentPartyRoot().get<VaultSchemaV1.PersistentParty>("x500Name").`in`(existingParticipants + participants))
)
)
}
else {
// Get the persistent party entity.
@ -796,12 +802,18 @@ class HibernateQueryCriteriaParser(val contractStateType: Class<out ContractStat
val subQueryNotExists = criteriaQuery.subquery(Tuple::class.java)
val subRoot = subQueryNotExists.from(VaultSchemaV1.PersistentParty::class.java)
subQueryNotExists.select(subRoot.get("x500Name"))
subQueryNotExists.where(criteriaBuilder.and(
criteriaBuilder.equal(vaultStates.get<VaultSchemaV1.VaultStates>("stateRef"),
subRoot.get<VaultSchemaV1.PersistentParty>("compositeKey").get<PersistentStateRef>("stateRef"))),
criteriaBuilder.not(subRoot.get<VaultSchemaV1.PersistentParty>("x500Name").`in`(exactParticipants)))
val subQueryNotExistsPredicate = criteriaBuilder.and(criteriaBuilder.not(criteriaBuilder.exists(subQueryNotExists)))
constraintPredicates.add(subQueryNotExistsPredicate)
//if the list of exact participants is empty, we return nothing with 1=0
if (exactParticipants.isEmpty()) {
constraintPredicates.add(criteriaBuilder.and(criteriaBuilder.equal(criteriaBuilder.literal(1), 0)))
} else {
subQueryNotExists.where(criteriaBuilder.and(
criteriaBuilder.equal(vaultStates.get<VaultSchemaV1.VaultStates>("stateRef"),
subRoot.get<VaultSchemaV1.PersistentParty>("compositeKey").get<PersistentStateRef>("stateRef"))),
criteriaBuilder.not(subRoot.get<VaultSchemaV1.PersistentParty>("x500Name").`in`(exactParticipants)))
val subQueryNotExistsPredicate = criteriaBuilder.and(criteriaBuilder.not(criteriaBuilder.exists(subQueryNotExists)))
constraintPredicates.add(subQueryNotExistsPredicate)
}
// join with transactions for each matching participant (only required where more than one)
if (exactParticipants.size > 1)

View File

@ -249,6 +249,27 @@ abstract class VaultQueryTestsBase : VaultQueryParties {
}
}
@Test(timeout=300_000)
fun `returns zero states when exact participants list is empty`() {
database.transaction {
identitySvc.verifyAndRegisterIdentity(BIG_CORP_IDENTITY)
vaultFiller.fillWithDummyState(participants = listOf(MEGA_CORP))
vaultFiller.fillWithDummyState(participants = listOf(MEGA_CORP, BIG_CORP))
val criteria = VaultQueryCriteria(exactParticipants = emptyList())
val results = vaultService.queryBy<ContractState>(criteria)
assertThat(results.states).hasSize(0)
val criteriaWithOneExactParticipant = VaultQueryCriteria(exactParticipants = listOf(MEGA_CORP))
val resultsWithOneExactParticipant = vaultService.queryBy<ContractState>(criteriaWithOneExactParticipant)
assertThat(resultsWithOneExactParticipant.states).hasSize(1)
val criteriaWithMoreExactParticipants = VaultQueryCriteria(exactParticipants = listOf(MEGA_CORP, BIG_CORP))
val resultsWithMoreExactParticipants = vaultService.queryBy<ContractState>(criteriaWithMoreExactParticipants)
assertThat(resultsWithMoreExactParticipants.states).hasSize(1)
}
}
@Test(timeout=300_000)
fun `unconsumed base contract states for two participants`() {
database.transaction {