EntityQueryMatcher implements Predicate<@Nullable EntityRecordWithColumns> { @Override public boolean test(@Nullable EntityRecordWithColumns input) { if (input == null) { return false; } boolean result = idMatches(input) && columnValuesMatch(input); return result; } EntityQueryMatcher(EntityQuery<I> query); @Override boolean test(@Nullable EntityRecordWithColumns input);  }
@Test @DisplayName("match everything except null to empty query") void matchEverythingToEmpty() { Collection<Object> idFilter = Collections.emptyList(); EntityQuery<?> query = createQuery(idFilter, defaultQueryParameters()); EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query); assertFalse(matcher.test(null)); assertTrue(matcher.test(EntityRecordWithColumns.of(EntityRecord.getDefaultInstance(), Collections.emptyMap()))); }
@Test @DisplayName("match IDs") void matchIds() { Message genericId = Sample.messageOfType(ProjectId.class); Collection<Object> idFilter = singleton(genericId); Any entityId = AnyPacker.pack(genericId); EntityQuery<?> query = createQuery(idFilter, defaultQueryParameters()); EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query); EntityRecord matching = EntityRecord.newBuilder() .setEntityId(entityId) .build(); Any otherEntityId = AnyPacker.pack(Sample.messageOfType(ProjectId.class)); EntityRecord nonMatching = EntityRecord.newBuilder() .setEntityId(otherEntityId) .build(); EntityRecordWithColumns matchingRecord = EntityRecordWithColumns.of(matching, Collections.emptyMap()); EntityRecordWithColumns nonMatchingRecord = EntityRecordWithColumns.of(nonMatching, Collections.emptyMap()); assertTrue(matcher.test(matchingRecord)); assertFalse(matcher.test(nonMatchingRecord)); }
@Test @DisplayName("match columns") void matchColumns() { Column column = booleanColumn(); boolean actualValue = false; ColumnName columnName = column.name(); Collection<Object> ids = Collections.emptyList(); Multimap<Column, Filter> filters = ImmutableMultimap.of(column, eq(columnName.value(), actualValue)); CompositeQueryParameter parameter = createParams(filters, ALL); QueryParameters params = QueryParameters.newBuilder() .add(parameter) .build(); EntityQuery<?> query = createQuery(ids, params); Any matchingId = AnyPacker.pack(Sample.messageOfType(TaskId.class)); Any nonMatchingId = AnyPacker.pack(Sample.messageOfType(TaskId.class)); EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query); EntityRecord matching = EntityRecord.newBuilder() .setEntityId(matchingId) .build(); EntityRecord nonMatching = EntityRecord.newBuilder() .setEntityId(nonMatchingId) .build(); Map<ColumnName, Object> matchingColumns = ImmutableMap.of(columnName, actualValue); EntityRecordWithColumns nonMatchingRecord = EntityRecordWithColumns.of(nonMatching, Collections.emptyMap()); EntityRecordWithColumns matchingRecord = createRecord(matching, matchingColumns); assertTrue(matcher.test(matchingRecord)); assertFalse(matcher.test(nonMatchingRecord)); }
@Test @DisplayName("match Any instances") void matchAnyInstances() { Column column = anyColumn(); Any actualValue = anyValue(); ColumnName columnName = column.name(); EntityRecord record = Sample.messageOfType(EntityRecord.class); Map<ColumnName, Object> columns = singletonMap(columnName, actualValue); EntityRecordWithColumns recordWithColumns = createRecord(record, columns); Multimap<Column, Filter> filters = ImmutableMultimap.of(column, eq(columnName.value(), actualValue)); CompositeQueryParameter parameter = createParams(filters, ALL); QueryParameters parameters = QueryParameters.newBuilder() .add(parameter) .build(); EntityQuery<?> query = createQuery(emptySet(), parameters); EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query); assertTrue(matcher.test(recordWithColumns)); }
@Test @DisplayName("not match by wrong field name") void notMatchByWrongField() { String wrongName = "wrong"; Column target = booleanColumn(); Multimap<Column, Filter> filters = ImmutableMultimap.of(target, eq(wrongName, "any")); CompositeQueryParameter parameter = createParams(filters, EITHER); QueryParameters params = QueryParameters.newBuilder() .add(parameter) .build(); EntityQuery<?> query = createQuery(Collections.emptyList(), params); EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query); EntityRecord record = EntityRecord.newBuilder() .setEntityId(Any.getDefaultInstance()) .build(); EntityRecordWithColumns recordWithColumns = EntityRecordWithColumns.of(record, Collections.emptyMap()); assertFalse(matcher.test(recordWithColumns)); }