|
| 1 | +/* |
| 2 | + * Hibernate OGM, Domain model persistence for NoSQL datastores |
| 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 | + |
| 8 | +/* |
| 9 | + * Hibernate OGM, Domain model persistence for NoSQL datastores |
| 10 | + * |
| 11 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 12 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 13 | + */ |
| 14 | +package org.hibernate.ogm.backendtck.embeddable; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import org.hibernate.Session; |
| 23 | +import org.hibernate.Transaction; |
| 24 | +import org.hibernate.ogm.backendtck.associations.collection.types.GrandChild; |
| 25 | +import org.hibernate.ogm.backendtck.associations.collection.types.GrandMother; |
| 26 | +import org.hibernate.ogm.cfg.OgmProperties; |
| 27 | +import org.hibernate.ogm.dialect.impl.GridDialects; |
| 28 | +import org.hibernate.ogm.dialect.spi.GridDialect; |
| 29 | +import org.hibernate.ogm.utils.InvokedOperationsLoggingDialect; |
| 30 | +import org.hibernate.ogm.utils.OgmTestCase; |
| 31 | + |
| 32 | +import static org.fest.assertions.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Emmanuel Bernard emmanuel@hibernate.org |
| 36 | + */ |
| 37 | +public class GridDialectOperationInvocationForElementCollectionTest extends OgmTestCase { |
| 38 | + |
| 39 | + @Before |
| 40 | + public void resetOperationsLog() { |
| 41 | + getOperationsLogger().reset(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testEmbeddableCollectionAsList() throws Exception { |
| 46 | + //insert entity with embedded collection |
| 47 | + Session session = openSession(); |
| 48 | + Transaction tx = session.beginTransaction(); |
| 49 | + GrandChild luke = new GrandChild(); |
| 50 | + luke.setName( "Luke" ); |
| 51 | + GrandChild leia = new GrandChild(); |
| 52 | + leia.setName( "Leia" ); |
| 53 | + GrandMother grandMother = new GrandMother(); |
| 54 | + grandMother.getGrandChildren().add( luke ); |
| 55 | + grandMother.getGrandChildren().add( leia ); |
| 56 | + session.persist( grandMother ); |
| 57 | + tx.commit(); |
| 58 | + |
| 59 | + assertThat( getOperations() ).containsExactly( |
| 60 | + "getTuple", |
| 61 | + "createTuple", |
| 62 | + "insertOrUpdateTuple", |
| 63 | + "getAssociation", |
| 64 | + "createAssociation", |
| 65 | + "insertOrUpdateAssociation" |
| 66 | + ); |
| 67 | + session.clear(); |
| 68 | + |
| 69 | + resetOperationsLog(); |
| 70 | + |
| 71 | + //remove one of the elements and add a new one |
| 72 | + tx = session.beginTransaction(); |
| 73 | + grandMother = (GrandMother) session.get( GrandMother.class, grandMother.getId() ); |
| 74 | + grandMother.getGrandChildren().remove( 0 ); |
| 75 | + tx.commit(); |
| 76 | + session.clear(); |
| 77 | + |
| 78 | + assertThat( getOperations() ).containsExactly( |
| 79 | + "getTuple", // load GrandMother |
| 80 | + "getAssociation", // collection is loaded by gdMother.getGrandChildren() |
| 81 | + "getAssociation", // association loaded to delete rows |
| 82 | + "insertOrUpdateAssociation", //remove 1,leia row from association |
| 83 | + "getAssociation", // load association to update row |
| 84 | + "insertOrUpdateAssociation", // put 0,leia (essentially removing luke) |
| 85 | + "getAssociation", // load association to insert rows |
| 86 | + "insertOrUpdateAssociation" // a no-op in this case since there is no line to update |
| 87 | + ); |
| 88 | + |
| 89 | + resetOperationsLog(); |
| 90 | + |
| 91 | + //assert removal has been propagated |
| 92 | + tx = session.beginTransaction(); |
| 93 | + grandMother = (GrandMother) session.get( GrandMother.class, grandMother.getId() ); |
| 94 | + assertThat( grandMother.getGrandChildren() ).onProperty( "name" ).containsExactly( "Leia" ); |
| 95 | + |
| 96 | + session.delete( grandMother ); |
| 97 | + tx.commit(); |
| 98 | + |
| 99 | + assertThat( getOperations() ).containsExactly( |
| 100 | + "getTuple", // load grand mother |
| 101 | + "getAssociation", // load collection |
| 102 | + "getAssociation", // load collection for removal |
| 103 | + "removeAssociation", // actual collection removal |
| 104 | + "removeTuple" // remove tuple |
| 105 | + ); |
| 106 | + session.close(); |
| 107 | + |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected Class<?>[] getAnnotatedClasses() { |
| 113 | + return new Class<?>[] { GrandMother.class, GrandChild.class }; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void configure(Map<String, Object> settings) { |
| 118 | + settings.put( OgmProperties.GRID_DIALECT, InvokedOperationsLoggingDialect.class ); |
| 119 | + } |
| 120 | + |
| 121 | + private InvokedOperationsLoggingDialect getOperationsLogger() { |
| 122 | + GridDialect gridDialect = getSessionFactory().getServiceRegistry().getService( GridDialect.class ); |
| 123 | + InvokedOperationsLoggingDialect invocationLogger = GridDialects.getDelegateOrNull( |
| 124 | + gridDialect, |
| 125 | + InvokedOperationsLoggingDialect.class |
| 126 | + ); |
| 127 | + |
| 128 | + return invocationLogger; |
| 129 | + } |
| 130 | + |
| 131 | + private List<String> getOperations() { |
| 132 | + return getOperationsLogger().getOperations(); |
| 133 | + } |
| 134 | +} |
0 commit comments