-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGM-693 Add test cases for queries on embeddables
- Loading branch information
Showing
4 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/src/test/java/org/hibernate/ogm/backendtck/queries/EmbeddedCollectionItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.queries; | ||
|
||
import javax.persistence.Embeddable; | ||
|
||
import org.hibernate.search.annotations.Analyze; | ||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.IndexedEmbedded; | ||
import org.hibernate.search.annotations.Store; | ||
|
||
@Embeddable | ||
public class EmbeddedCollectionItem { | ||
|
||
// Store.YES for filtering in query | ||
// Analyze.NO for projection in query | ||
@Field(store = Store.YES, analyze = Analyze.NO) | ||
private String item; | ||
|
||
@Field(store = Store.YES, analyze = Analyze.NO) | ||
private String anotherItem; | ||
|
||
@IndexedEmbedded | ||
private AnotherEmbeddable embedded; | ||
|
||
public EmbeddedCollectionItem() { | ||
} | ||
|
||
public EmbeddedCollectionItem(String embeddedString, String anotherItem, AnotherEmbeddable anotherEmbeddable) { | ||
this.item = embeddedString; | ||
this.anotherItem = anotherItem; | ||
this.embedded = anotherEmbeddable; | ||
} | ||
|
||
public String getItem() { | ||
return item; | ||
} | ||
|
||
public void setItem(String embeddedString) { | ||
this.item = embeddedString; | ||
} | ||
|
||
public AnotherEmbeddable getEmbedded() { | ||
return embedded; | ||
} | ||
|
||
public void setEmbedded(AnotherEmbeddable anotherEmbeddable) { | ||
this.embedded = anotherEmbeddable; | ||
} | ||
|
||
public String getAnotherItem() { | ||
return anotherItem; | ||
} | ||
|
||
public void setAnotherItem(String anotherItem) { | ||
this.anotherItem = anotherItem; | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
...src/test/java/org/hibernate/ogm/backendtck/queries/QueriesWithEmbeddedCollectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.backendtck.queries; | ||
|
||
import static org.hibernate.ogm.utils.OgmAssertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.ogm.utils.OgmTestCase; | ||
import org.hibernate.ogm.utils.TestSessionFactory; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
/** | ||
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc. | ||
* @author Gunnar Morling | ||
* @author Davide D'Alto | ||
*/ | ||
public class QueriesWithEmbeddedCollectionTest extends OgmTestCase { | ||
|
||
@TestSessionFactory | ||
public static SessionFactory sessions; | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
private Session session; | ||
|
||
private Transaction tx; | ||
|
||
@Before | ||
public void createSession() { | ||
closeSession(); | ||
session = sessions.openSession(); | ||
tx = session.beginTransaction(); | ||
} | ||
|
||
@After | ||
public void closeSession() { | ||
if ( tx != null && tx.isActive() ) { | ||
tx.commit(); | ||
tx = null; | ||
} | ||
if ( session != null ) { | ||
session.close(); | ||
session = null; | ||
} | ||
} | ||
|
||
@Test | ||
public void testEqualOpeartorWithEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item = 'item[0]'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testInOperatorWithEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "from WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item IN ( 'item[0]' )" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
|
||
@Test | ||
public void testBetweenOpeartorWithEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item BETWEEN 'aaaaaa' AND 'zzzzzzzzz'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testLikeOpeartorWithEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item LIKE 'item[1%'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testEqualEmbeddedCollectionWithEmbeddableInCollectionWhereClause() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item = 'item[1]'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testConjunctionOperatorWithEmbeddedInEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item = 'item[1]' AND c.anotherItem IN ('secondItem[0]')" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testBetweenOperatorWithEmbeddedInEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.embedded.embeddedInteger BETWEEN -100 AND 100" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testLikeOperatorWithEmbeddedInEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.embedded.embeddedString LIKE 'string[1%'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testEqualOperatorWithEmbeddedInEmbeddedCollectionForString() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.embedded.embeddedString = 'string[1][0]'" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testEqualOperatorWithEmbeddedInEmbeddedCollectionForInteger() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.embedded.embeddedInteger = 10" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testConjunctionOperatorEqualOperatorWithEmbeddedInEmbeddedCollection() throws Exception { | ||
List<?> result = session.createQuery( "FROM WithEmbedded e JOIN e.anEmbeddedCollection c WHERE c.item = 'item[1]' AND c.embedded.embeddedString IN ('string[1][0]')" ).list(); | ||
assertThat( result ).onProperty( "id" ).containsOnly( 1L ); | ||
} | ||
|
||
@Test | ||
public void testQueryReturningEmbeddedObject() { | ||
@SuppressWarnings("unchecked") | ||
List<WithEmbedded> list = session.createQuery( "from WithEmbedded we" ).list(); | ||
|
||
assertThat( list ) | ||
.onProperty( "anEmbeddable" ) | ||
.onProperty( "embeddedString" ) | ||
.containsExactly( "embedded 1" ); | ||
|
||
assertThat( list ) | ||
.onProperty( "anEmbeddable" ) | ||
.onProperty( "anotherEmbeddable" ) | ||
.onProperty( "embeddedString" ) | ||
.containsExactly( "string 1" ); | ||
|
||
assertThat( list ) | ||
.onProperty( "yetAnotherEmbeddable" ) | ||
.onProperty( "embeddedString" ) | ||
.containsExactly( "embedded 2" ); | ||
|
||
assertThat( list.get( 0 ).getAnEmbeddedCollection() ) | ||
.onProperty( "item" ) | ||
.containsOnly( "item[0]", "item[1]" ); | ||
|
||
assertThat( list.get( 0 ).getAnEmbeddedCollection() ) | ||
.onProperty( "anotherItem" ) | ||
.containsOnly( "secondItem[0]", null ); | ||
|
||
assertThat( list.get( 0 ).getAnotherEmbeddedCollection() ) | ||
.onProperty( "item" ) | ||
.containsOnly( "another[0]", "another[1]" ); | ||
|
||
assertThat( list.get( 0 ).getAnotherEmbeddedCollection() ) | ||
.onProperty( "anotherItem" ) | ||
.containsOnly( null, null ); | ||
} | ||
|
||
@BeforeClass | ||
public static void insertTestEntities() throws Exception { | ||
WithEmbedded with = new WithEmbedded( 1L, null ); | ||
with.setAnEmbeddable( new AnEmbeddable( "embedded 1", new AnotherEmbeddable( "string 1", 1 ) ) ); | ||
with.setYetAnotherEmbeddable( new AnEmbeddable( "embedded 2" ) ); | ||
with.setAnEmbeddedCollection( Arrays.asList( new EmbeddedCollectionItem( "item[0]", "secondItem[0]", null ), new EmbeddedCollectionItem( "item[1]", null, new AnotherEmbeddable( "string[1][0]", 10 ) ) ) ); | ||
with.setAnotherEmbeddedCollection( Arrays.asList( new EmbeddedCollectionItem( "another[0]", null, null ), new EmbeddedCollectionItem( "another[1]", null, null ) ) ); | ||
|
||
persist( with ); | ||
} | ||
|
||
|
||
@AfterClass | ||
public static void removeTestEntities() throws Exception { | ||
final Session session = sessions.openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
session.delete( session.load( WithEmbedded.class, 1L ) ); | ||
|
||
transaction.commit(); | ||
session.close(); | ||
} | ||
|
||
private static void persist(Object... entities) { | ||
final Session session = sessions.openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
for ( Object entity : entities ) { | ||
session.persist( entity ); | ||
} | ||
|
||
transaction.commit(); | ||
session.close(); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { WithEmbedded.class }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters