mirror of
https://github.com/corda/corda.git
synced 2024-12-19 04:57:58 +00:00
empty list checks (#6262)
This commit is contained in:
parent
350066d386
commit
cc8ce3ca99
@ -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"))
|
||||
|
||||
//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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user