<commit_before>/*
 * 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.datastore.redis.test.embeddable;

import static org.hibernate.ogm.datastore.redis.utils.RedisTestHelper.assertDbObject;

import java.util.Arrays;

import org.hibernate.Transaction;
import org.hibernate.ogm.OgmSession;
import org.hibernate.ogm.backendtck.embeddable.Account;
import org.hibernate.ogm.backendtck.embeddable.Address;
import org.hibernate.ogm.backendtck.embeddable.AddressType;
import org.hibernate.ogm.backendtck.queries.Ending;
import org.hibernate.ogm.backendtck.queries.OptionalStoryBranch;
import org.hibernate.ogm.backendtck.queries.StoryBranch;
import org.hibernate.ogm.backendtck.queries.StoryGame;
import org.hibernate.ogm.utils.OgmTestCase;
import org.junit.Test;

/**
 * Tests for {@code @Embeddable} types and {@code @ElementCollection}s there-of.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class EmbeddableMappingTest extends OgmTestCase {

	@Test
	public void testEmbeddable() throws Exception {
		OgmSession session = openSession();

		Transaction transaction = session.beginTransaction();

		// Given, When
		Account account = new Account();
		account.setLogin( "emmanuel" );
		account.setPassword( "<PASSWORD>" );
		account.setHomeAddress( new Address() );
		final Address address = account.getHomeAddress();
		address.setCity( "Paris" );
		address.setCountry( "France" );
		address.setStreet1( "1 avenue des Champs Elysees" );
		address.setZipCode( "75007" );
		address.setType( new AddressType( "main" ) );
		session.persist( account );
		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
						"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'country' : 'France', " +
						"'street1' : '1 avenue des Champs Elysees'," +
						"'type' : {" +
						"'name' : 'main'" +
						"}" +
						"}, " +
						"'postal_code' : '75007', " +
						"'password' : '<PASSWORD>', " +
						"'version': 0 " +
						"}"
		);

		transaction.commit();
		session.clear();

		transaction = session.beginTransaction();

		Account loadedAccount = session.get( Account.class, account.getLogin() );

		// When

		// set some values to null
		loadedAccount.getHomeAddress().setCountry( null );
		loadedAccount.setPassword( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
						"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'street1' : '1 avenue des Champs Elysees'," +
						"'type' : {" +
						"'name' : 'main'" +
						"}" +
						"}, " +
						"'postal_code' : '75007', " +
						"'version': 1 " +
						"}"
		);

		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		loadedAccount = session.get( Account.class, account.getLogin() );

		// When
		// set a nested embedded to null
		loadedAccount.getHomeAddress().setType( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
						"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'street1' : '1 avenue des Champs Elysees'" +
						"}, " +
						"'postal_code' : '75007', " +
						"'version': 2 " +
						"}"
		);

		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		loadedAccount = session.get( Account.class, account.getLogin() );

		// When
		// set all properties of an embedded to null
		loadedAccount.getHomeAddress().setCity( null );
		loadedAccount.getHomeAddress().setStreet1( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
						"'postal_code' : '75007', " +
						"'version': 3 " +
						"}"
		);

		transaction.commit();
		// Clean-Up<commit_msg>OGM-887 Making expected JSON structs better readable<commit_after>/*
 * 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.datastore.redis.test.embeddable;

import static org.hibernate.ogm.datastore.redis.utils.RedisTestHelper.assertDbObject;

import java.util.Arrays;

import org.hibernate.Transaction;
import org.hibernate.ogm.OgmSession;
import org.hibernate.ogm.backendtck.embeddable.Account;
import org.hibernate.ogm.backendtck.embeddable.Address;
import org.hibernate.ogm.backendtck.embeddable.AddressType;
import org.hibernate.ogm.backendtck.queries.Ending;
import org.hibernate.ogm.backendtck.queries.OptionalStoryBranch;
import org.hibernate.ogm.backendtck.queries.StoryBranch;
import org.hibernate.ogm.backendtck.queries.StoryGame;
import org.hibernate.ogm.utils.OgmTestCase;
import org.junit.Test;

/**
 * Tests for {@code @Embeddable} types and {@code @ElementCollection}s there-of.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class EmbeddableMappingTest extends OgmTestCase {

	@Test
	public void testEmbeddable() throws Exception {
		OgmSession session = openSession();

		Transaction transaction = session.beginTransaction();

		// Given, When
		Account account = new Account();
		account.setLogin( "emmanuel" );
		account.setPassword( "<PASSWORD>" );
		account.setHomeAddress( new Address() );
		final Address address = account.getHomeAddress();
		address.setCity( "Paris" );
		address.setCountry( "France" );
		address.setStreet1( "1 avenue des Champs Elysees" );
		address.setZipCode( "75007" );
		address.setType( new AddressType( "main" ) );
		session.persist( account );
		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
					"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'country' : 'France', " +
						"'street1' : '1 avenue des Champs Elysees'," +
						"'type' : {" +
							"'name' : 'main'" +
						"}" +
					"}, " +
					"'postal_code' : '75007', " +
					"'password' : '<PASSWORD>', " +
					"'version': 0 " +
				"}"
		);

		transaction.commit();
		session.clear();

		transaction = session.beginTransaction();

		Account loadedAccount = session.get( Account.class, account.getLogin() );

		// When

		// set some values to null
		loadedAccount.getHomeAddress().setCountry( null );
		loadedAccount.setPassword( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
					"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'street1' : '1 avenue des Champs Elysees'," +
						"'type' : {" +
							"'name' : 'main'" +
						"}" +
					"}, " +
					"'postal_code' : '75007', " +
					"'version': 1 " +
				"}"
		);

		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		loadedAccount = session.get( Account.class, account.getLogin() );

		// When
		// set a nested embedded to null
		loadedAccount.getHomeAddress().setType( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
					"'homeAddress' : {" +
						"'city' : 'Paris', " +
						"'street1' : '1 avenue des Champs Elysees'" +
					"}, " +
					"'postal_code' : '75007', " +
					"'version': 2 " +
				"}"
		);

		transaction.commit();

		session.clear();

		transaction = session.beginTransaction();

		loadedAccount = session.get( Account.class, account.getLogin() );

		// When
		// set all properties of an embedded to null
		loadedAccount.getHomeAddress().setCity( null );
		loadedAccount.getHomeAddress().setStreet1( null );
		session.merge( loadedAccount );

		transaction.commit();

		transaction = session.beginTransaction();

		// Then
		assertDbObject(
				session.getSessionFactory(),
				// collection
				"Account",
				// query
				"emmanuel",
				// expected
				"{ " +
					"'postal_code' : '75007', " +
					"'version': 3 " +
				"}"
		);

		transaction.commit();
		// Clean-Up
{'fraction_non_alphanumeric': 0.15463125548726953, 'fraction_numerical': 0.009547848990342406, 'mean_word_length': 3.125396106835672, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5532873', 'n_tokens_mistral': 3669, 'n_tokens_neox': 2826, 'n_words': 579}