|
| 1 | +package com.baeldung.hibernate.persistjson; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Properties; |
| 10 | + |
| 11 | +import org.hibernate.HibernateException; |
| 12 | +import org.hibernate.Session; |
| 13 | +import org.hibernate.SessionFactory; |
| 14 | +import org.hibernate.boot.MetadataSources; |
| 15 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 16 | +import org.hibernate.cfg.Configuration; |
| 17 | +import org.hibernate.service.ServiceRegistry; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class PersistJSONUnitTest { |
| 23 | + |
| 24 | + private Session session; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void init() { |
| 28 | + try { |
| 29 | + Configuration configuration = new Configuration(); |
| 30 | + |
| 31 | + Properties properties = new Properties(); |
| 32 | + properties.load(Thread.currentThread() |
| 33 | + .getContextClassLoader() |
| 34 | + .getResourceAsStream("hibernate-persistjson.properties")); |
| 35 | + |
| 36 | + configuration.setProperties(properties); |
| 37 | + |
| 38 | + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()) |
| 39 | + .build(); |
| 40 | + MetadataSources metadataSources = new MetadataSources(serviceRegistry); |
| 41 | + metadataSources.addAnnotatedClass(Customer.class); |
| 42 | + |
| 43 | + SessionFactory factory = metadataSources.buildMetadata() |
| 44 | + .buildSessionFactory(); |
| 45 | + |
| 46 | + session = factory.openSession(); |
| 47 | + } catch (HibernateException | IOException e) { |
| 48 | + fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void close() { |
| 54 | + if (session != null) |
| 55 | + session.close(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenCustomer_whenCallingSerializeCustomerAttributes_thenAttributesAreConverted() throws IOException { |
| 60 | + |
| 61 | + Customer customer = new Customer(); |
| 62 | + customer.setFirstName("first name"); |
| 63 | + customer.setLastName("last name"); |
| 64 | + |
| 65 | + Map<String, Object> attributes = new HashMap<>(); |
| 66 | + attributes.put("address", "123 Main Street"); |
| 67 | + attributes.put("zipcode", 12345); |
| 68 | + |
| 69 | + customer.setCustomerAttributes(attributes); |
| 70 | + |
| 71 | + customer.serializeCustomerAttributes(); |
| 72 | + |
| 73 | + String serialized = customer.getCustomerAttributeJSON(); |
| 74 | + |
| 75 | + customer.setCustomerAttributeJSON(serialized); |
| 76 | + customer.deserializeCustomerAttributes(); |
| 77 | + |
| 78 | + Map<String, Object> deserialized = customer.getCustomerAttributes(); |
| 79 | + |
| 80 | + assertEquals("123 Main Street", deserialized.get("address")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenCustomer_whenSaving_thenAttributesAreConverted() { |
| 85 | + |
| 86 | + Customer customer = new Customer(); |
| 87 | + customer.setFirstName("first name"); |
| 88 | + customer.setLastName("last name"); |
| 89 | + |
| 90 | + Map<String, Object> attributes = new HashMap<>(); |
| 91 | + attributes.put("address", "123 Main Street"); |
| 92 | + attributes.put("zipcode", 12345); |
| 93 | + |
| 94 | + customer.setCustomerAttributes(attributes); |
| 95 | + |
| 96 | + session.beginTransaction(); |
| 97 | + |
| 98 | + int id = (int) session.save(customer); |
| 99 | + |
| 100 | + session.flush(); |
| 101 | + session.clear(); |
| 102 | + |
| 103 | + Customer result = session.createNativeQuery("select * from Customers where Customers.id = :id", Customer.class) |
| 104 | + .setParameter("id", id) |
| 105 | + .getSingleResult(); |
| 106 | + |
| 107 | + assertEquals(2, result.getCustomerAttributes() |
| 108 | + .size()); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments