Skip to content

Commit

Permalink
OGM-952 Re-organizing test, asserting actual persistent representation
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 11, 2016
1 parent 7e102ce commit f846d16
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 212 deletions.
@@ -0,0 +1,130 @@
/*
* 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.type.converter;

import static org.fest.assertions.Assertions.assertThat;

import java.util.Map;
import java.util.UUID;

import org.hibernate.Session;
import org.hibernate.ogm.OgmSessionFactory;
import org.hibernate.ogm.cfg.OgmConfiguration;
import org.hibernate.ogm.model.impl.DefaultEntityKeyMetadata;
import org.hibernate.ogm.model.key.spi.EntityKey;
import org.hibernate.ogm.utils.OgmTestCase;
import org.hibernate.ogm.utils.TestHelper;
import org.junit.Test;

/**
* Test the JPA @Convert logic in OGM
* TODO: should it be in the TCK, that's core code but it's nice to see it tested across all backends
*
* @author Emmanuel Bernard emmanuel@hibernate.org
*/
public class JpaAttributeConverterTest extends OgmTestCase {

/**
* String -> String
*/
@Test
public void jpaConverterIsApplied() throws Exception {
Session session = openSession();
session.getTransaction().begin();
Printer printer = new Printer();
printer.name = "somefoo";
assertThat( printer.name ).isEqualTo( "somefoo" );
session.persist( printer );
session.getTransaction().commit();
session.clear();

session.getTransaction().begin();
// Make sure the converter has actually been applied
Map<String, Object> persistedTuple = TestHelper.extractEntityTuple(
sessions,
getPrinterEntityKey( printer.id )
);
String persistedPrinterName = (String) persistedTuple.get( "name" );
assertThat( persistedPrinterName ).isEqualTo( "SOMEFOO" );
session.getTransaction().commit();
session.clear();

session.getTransaction().begin();
printer = session.get( Printer.class, printer.id );
assertThat( printer ).isNotNull();
assertThat( printer.name ).isEqualTo( "somefoo" );
session.delete( printer );
session.getTransaction().commit();

session.close();
}

/**
* MyString -> String
*/
@Test
public void jpaConverterIsAppliedToCustomType() throws Exception {
Session session = openSession();
session.getTransaction().begin();
Printer printer = new Printer();
printer.brand = new MyString( "printr inc." );
assertThat( printer.brand.toString() ).isEqualTo( "printr inc." );
session.persist( printer );
session.getTransaction().commit();
session.clear();

session.getTransaction().begin();
// Make sure the converter has actually been applied
Map<String, Object> persistedTuple = TestHelper.extractEntityTuple(
sessions,
getPrinterEntityKey( printer.id )
);
String persistedPrinterName = (String) persistedTuple.get( "brand" );
assertThat( persistedPrinterName ).isEqualTo( "PRINTR INC." );
session.getTransaction().commit();
session.clear();

session.getTransaction().begin();
printer = session.get( Printer.class, printer.id );
assertThat( printer ).isNotNull();
assertThat( printer.brand.toString() ).isEqualTo( "printr inc." );
session.delete( printer );
session.getTransaction().commit();

session.close();
}

/**
* String -> MyString: Exception expected
*/
@Test
public void testGridTypeForIntermediaryTypeNotSupported() throws Exception {
OgmConfiguration cfg = new OgmConfiguration();
cfg.addAnnotatedClass( OtherPrinter.class );

try {
OgmSessionFactory sf = cfg.buildSessionFactory();
sf.close();
assertThat( true == false ).as( "We should fail as the AttributeConverter is not supported" );
}
catch (Exception e) {
assertThat( e.getCause().getCause().getMessage() ).startsWith( "OGM000084" );
}
}

private EntityKey getPrinterEntityKey(UUID id) {
return new EntityKey(
new DefaultEntityKeyMetadata( "Printer", new String[] { "id" } ),
new Object[]{ id.toString() }
);
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Printer.class };
}
}
@@ -0,0 +1,57 @@
/*
* 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.type.converter;

/**
* @author Emmanuel Bernard emmanuel@hibernate.org
*/
public class MyString {

private final String string;

public MyString(String string) {
// note that the string is explicitly not lower case sanitized
// we let the converter do this job to see if it is called
this.string = string;
}

@Override
public String toString() {
return string;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( string == null ) ? 0 : string.hashCode() );
return result;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
MyString other = (MyString) obj;
if ( string == null ) {
if ( other.string != null ) {
return false;
}
}
else if ( !string.equals( other.string ) ) {
return false;
}
return true;
}
}
@@ -0,0 +1,25 @@
/*
* 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.type.converter;

import javax.persistence.AttributeConverter;

/**
* @author Gunnar Morling
*/
public class MyStringToUpperCaseStringConverter implements AttributeConverter<MyString, String> {

@Override
public String convertToDatabaseColumn(MyString attribute) {
return attribute != null ? attribute.toString().toUpperCase() : null;
}

@Override
public MyString convertToEntityAttribute(String dbData) {
return dbData != null ? new MyString( dbData.toLowerCase() ) : null;
}
}
Expand Up @@ -11,10 +11,10 @@
* 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.type.custom;
package org.hibernate.ogm.backendtck.type.converter;

import java.net.URL;
import java.util.UUID;

import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand All @@ -25,12 +25,12 @@
*/
@Entity
public class OtherPrinter {

@Id
@GeneratedValue
public UUID id;

// should crash and burn as MyString is not a supported type
@Convert(converter = JpaConvertCustomTypeTest.URLToMyStringConverter.class)
public URL url;

@Convert(converter = StringToMyStringConverter.class)
public String name;
}
Expand Up @@ -4,10 +4,10 @@
* 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.type.custom;
package org.hibernate.ogm.backendtck.type.converter;

import java.net.URL;
import java.util.UUID;

import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand All @@ -18,14 +18,21 @@
*/
@Entity
public class Printer {

@Id
@GeneratedValue
public UUID id;

@Convert(converter = JpaConvertCustomTypeTest.MyStringToUpperCaseAndBackConverter.class)
public JpaConvertCustomTypeTest.MyString name;
@Convert(converter = StringToUpperCaseConverter.class)
public String name;

@Convert(converter = MyStringToUpperCaseStringConverter.class)
public MyString brand;

@Convert(converter = StringToByteArrayConverter.class)
public String description;

@Convert(converter = JpaConvertCustomTypeTest.URLToURLConverter.class)
public URL url;
// @Convert(converter = JpaConvertCustomTypeTest.URLToURLConverter.class)
// public URL url;

}
@@ -0,0 +1,27 @@
/*
* 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.type.converter;

import java.nio.charset.StandardCharsets;

import javax.persistence.AttributeConverter;

/**
* @author Gunnar Morling
*/
public class StringToByteArrayConverter implements AttributeConverter<String, byte[]> {

@Override
public byte[] convertToDatabaseColumn(String attribute) {
return attribute != null ? attribute.getBytes( StandardCharsets.UTF_8 ) : null;
}

@Override
public String convertToEntityAttribute(byte[] dbData) {
return dbData != null ? new String( dbData, StandardCharsets.UTF_8 ) : null;
}
}
@@ -0,0 +1,25 @@
/*
* 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.type.converter;

import javax.persistence.AttributeConverter;

/**
* @author Gunnar Morling
*/
public class StringToMyStringConverter implements AttributeConverter<String, MyString> {

@Override
public MyString convertToDatabaseColumn(String attribute) {
return attribute != null ? new MyString( attribute.toUpperCase() ) : null;
}

@Override
public String convertToEntityAttribute(MyString dbData) {
return dbData != null ? dbData.toString().toLowerCase() : null;
}
}
@@ -0,0 +1,25 @@
/*
* 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.type.converter;

import javax.persistence.AttributeConverter;

/**
* @author Gunnar Morling
*/
public class StringToUpperCaseConverter implements AttributeConverter<String, String> {

@Override
public String convertToDatabaseColumn(String attribute) {
return attribute != null ? attribute.toUpperCase() : null;
}

@Override
public String convertToEntityAttribute(String dbData) {
return dbData != null ? dbData.toLowerCase() : null;
}
}

0 comments on commit f846d16

Please sign in to comment.