[BAEL-4635] Fixed tests and formatted per feedback
This commit is contained in:
parent
400cbb845d
commit
3686fa1cfb
|
@ -13,27 +13,24 @@ public class HibernateConfig {
|
|||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
if (sessionFactory == null) {
|
||||
try {
|
||||
Configuration configuration = new Configuration();
|
||||
Configuration configuration = new Configuration();
|
||||
|
||||
Properties settings = new Properties();
|
||||
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
||||
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
||||
settings.put(Environment.USER, "root");
|
||||
settings.put(Environment.PASS, "password");
|
||||
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
||||
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
configuration.setProperties(settings);
|
||||
Properties settings = new Properties();
|
||||
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
||||
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
||||
settings.put(Environment.USER, "root");
|
||||
settings.put(Environment.PASS, "password");
|
||||
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
||||
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
configuration.setProperties(settings);
|
||||
|
||||
configuration.addAnnotatedClass(User.class);
|
||||
configuration.addAnnotatedClass(User.class);
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(configuration.getProperties())
|
||||
.build();
|
||||
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
}
|
||||
return sessionFactory;
|
||||
}
|
||||
|
|
|
@ -62,10 +62,10 @@ public class User implements Serializable {
|
|||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", User.class.getSimpleName() + "[", "]").add("id=" + id)
|
||||
.add("email='" + email + "'")
|
||||
.add("password='" + password + "'")
|
||||
.add("currentDevice='" + currentDevice + "'")
|
||||
.toString();
|
||||
.add("email='" + email + "'")
|
||||
.add("password='" + password + "'")
|
||||
.add("currentDevice='" + currentDevice + "'")
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,13 +9,11 @@ public class UserDao {
|
|||
|
||||
public void saveUser(User user) {
|
||||
Transaction transaction = null;
|
||||
try (Session session = HibernateConfig.getSessionFactory()
|
||||
.openSession()) {
|
||||
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
|
||||
transaction = session.beginTransaction();
|
||||
session.save(user);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
|
@ -23,10 +21,8 @@ public class UserDao {
|
|||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
try (Session session = HibernateConfig.getSessionFactory()
|
||||
.openSession()) {
|
||||
return session.createQuery("from User", User.class)
|
||||
.list();
|
||||
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
|
||||
return session.createQuery("from User", User.class).list();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import com.fasterxml.jackson.databind.MapperFeature;
|
|||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
||||
|
||||
public class TransientFieldTest {
|
||||
public class TransientFieldUnitTest {
|
||||
|
||||
private final UserDao userDao = new UserDao();
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class TransientFieldTest {
|
|||
private final User user = new User("user" + randInt + "@bar.com", "hunter2", "MacOSX");
|
||||
|
||||
@Test
|
||||
public void JPA_UserWithTransientAnnotation_FieldNotPersisted() {
|
||||
public void givenFieldWithTransientAnnotation_whenSavingViaJPA_thenFieldIgnored() {
|
||||
userDao.saveUser(user);
|
||||
List<User> allUsers = userDao.getUsers();
|
||||
User savedUser = allUsers.get(allUsers.indexOf(user));
|
||||
|
@ -32,7 +32,7 @@ public class TransientFieldTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void JavaSerialization_UserWithTransientAnnotation_FieldSerialized() throws IOException, ClassNotFoundException {
|
||||
public void givenFieldWithTransientAnnotation_whenSerializingObject_thenFieldSerialized() throws IOException, ClassNotFoundException {
|
||||
|
||||
FileOutputStream fout = new FileOutputStream("test.obj");
|
||||
ObjectOutputStream out = new ObjectOutputStream(fout);
|
||||
|
@ -49,7 +49,7 @@ public class TransientFieldTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void JSONSerialization_UserWithTransientAnnotation_FieldSerialized() throws JsonProcessingException {
|
||||
public void givenFieldWithTransientAnnotation_whenSerializingToJSON_thenFieldSerialized() throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String json = objectMapper.writeValueAsString(user);
|
||||
User savedUser = objectMapper.readValue(json, User.class);
|
||||
|
@ -58,7 +58,7 @@ public class TransientFieldTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void JSONSerialization_JacksonUsingHibernate5Module_FieldNotSerialized() throws JsonProcessingException {
|
||||
public void givenJacksonHibernate5Module_whenSerializingTransientAnnotation_thenFieldIgnored() throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModule(new Hibernate5Module());
|
||||
String json = objectMapper.writeValueAsString(user);
|
||||
|
@ -68,7 +68,7 @@ public class TransientFieldTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void JSONSerialization_MapperPropagatesTransientMarker_FieldSerialized() throws JsonProcessingException {
|
||||
public void givenPropagateTransientFieldFlag_whenSerializingTransientAnnotation_thenFieldSerialized() throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
|
||||
String json = objectMapper.writeValueAsString(user);
|
Loading…
Reference in New Issue