|
| 1 | +/* |
| 2 | + * Hibernate Search, full-text search for your domain model |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.search.test.bridge; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.hibernate.search.annotations.ContainedIn; |
| 15 | +import org.hibernate.search.annotations.DocumentId; |
| 16 | +import org.hibernate.search.annotations.Field; |
| 17 | +import org.hibernate.search.annotations.Indexed; |
| 18 | +import org.hibernate.search.annotations.IndexedEmbedded; |
| 19 | +import org.hibernate.search.query.dsl.QueryBuilder; |
| 20 | +import org.hibernate.search.testsupport.TestForIssue; |
| 21 | +import org.hibernate.search.testsupport.junit.SearchFactoryHolder; |
| 22 | +import org.hibernate.search.testsupport.junit.SearchITHelper; |
| 23 | + |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.apache.lucene.search.Query; |
| 28 | + |
| 29 | +@TestForIssue(jiraKey = "HSEARCH-3407") |
| 30 | +public class MultiValuedBooleanBridgeTest { |
| 31 | + |
| 32 | + @Rule |
| 33 | + public final SearchFactoryHolder sfHolder = new SearchFactoryHolder( ParentEntity.class, ChildEntity.class ); |
| 34 | + |
| 35 | + private final SearchITHelper helper = new SearchITHelper( sfHolder ); |
| 36 | + |
| 37 | + @Test |
| 38 | + public void test() { |
| 39 | + ChildEntity childEntity1 = new ChildEntity( 1L, true ); |
| 40 | + ChildEntity childEntity2 = new ChildEntity( 2L, false ); |
| 41 | + ParentEntity yourEntity1 = new ParentEntity( 1L, childEntity1, childEntity2 ); |
| 42 | + |
| 43 | + helper.add( yourEntity1 ); |
| 44 | + |
| 45 | + QueryBuilder qb = helper.queryBuilder( ParentEntity.class ); |
| 46 | + |
| 47 | + // finding ParentEntities with a children having boolean property "true" should match 1L |
| 48 | + Query query = qb.keyword().onField( "children.booleanProperty" ).matching( true ).createQuery(); |
| 49 | + |
| 50 | + helper.assertThat( query ) |
| 51 | + .matchesExactlyIds( 1L ); |
| 52 | + |
| 53 | + // finding ParentEntities with a children having boolean property "false" should also match 1L |
| 54 | + query = qb.keyword().onField( "children.booleanProperty" ).matching( false ).createQuery(); |
| 55 | + helper.assertThat( query ) |
| 56 | + .matchesExactlyIds( 1L ); |
| 57 | + } |
| 58 | + |
| 59 | + @Indexed |
| 60 | + public static class ParentEntity { |
| 61 | + |
| 62 | + @DocumentId |
| 63 | + private Long id; |
| 64 | + |
| 65 | + @IndexedEmbedded |
| 66 | + private List<ChildEntity> children = new ArrayList<>(); |
| 67 | + |
| 68 | + protected ParentEntity() { |
| 69 | + } |
| 70 | + |
| 71 | + public ParentEntity(Long id, ChildEntity... childEntities) { |
| 72 | + this.id = id; |
| 73 | + Arrays.stream( childEntities ).forEach( this::addChildEntity ); |
| 74 | + } |
| 75 | + |
| 76 | + public Long getId() { |
| 77 | + return id; |
| 78 | + } |
| 79 | + |
| 80 | + public List<ChildEntity> getChildren() { |
| 81 | + return Collections.unmodifiableList( children ); |
| 82 | + } |
| 83 | + |
| 84 | + public void addChildEntity(ChildEntity child) { |
| 85 | + child.setParent( this ); |
| 86 | + this.children.add( child ); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Indexed |
| 92 | + public static class ChildEntity { |
| 93 | + |
| 94 | + @DocumentId |
| 95 | + private Long id; |
| 96 | + |
| 97 | + @ContainedIn |
| 98 | + private ParentEntity parent; |
| 99 | + |
| 100 | + @Field |
| 101 | + private boolean booleanProperty; |
| 102 | + |
| 103 | + protected ChildEntity() { |
| 104 | + } |
| 105 | + |
| 106 | + public ChildEntity(Long id, boolean booleanProperty) { |
| 107 | + this.id = id; |
| 108 | + this.booleanProperty = booleanProperty; |
| 109 | + } |
| 110 | + |
| 111 | + public Long getId() { |
| 112 | + return id; |
| 113 | + } |
| 114 | + |
| 115 | + public boolean getBooleanProperty() { |
| 116 | + return booleanProperty; |
| 117 | + } |
| 118 | + |
| 119 | + public void setBooleanProperty(boolean booleanProperty) { |
| 120 | + this.booleanProperty = booleanProperty; |
| 121 | + } |
| 122 | + |
| 123 | + protected void setParent(ParentEntity parent) { |
| 124 | + this.parent = parent; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | +} |
0 commit comments