-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocietyApplicationTests.java
120 lines (91 loc) · 3.37 KB
/
SocietyApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package de.stanetz.society;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import de.stanetz.society.domain.Person;
import de.stanetz.society.domain.Sex;
import de.stanetz.society.repository.PersonRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SocietyApplicationTests {
@Autowired
private PersonRepository personRepository;
@Autowired
private Session springManagedSession;
private static final Logger LOG = LoggerFactory.getLogger(SocietyApplicationTests.class);
@Test
public void contextLoads() {
//Set up the Session
SessionFactory sessionFactory = new SessionFactory("de.stanetz.society");
Session session = sessionFactory.openSession();
Person child = new Person("Child", Sex.OTHER);
Person father = new Person("Father", Sex.MALE);
Person mother = new Person("Mother", Sex.FEMALE);
child.setParents(Arrays.asList(mother, father));
LOG.info("Save");
session.beginTransaction();
//Persist the movie. This persists the actors as well.
session.save(child);
session.getTransaction().commit();
LOG.info("Clear session");
session.clear();
//Load a movie
Person loadedChild = session.load(Person.class, child.getId());
Assert.assertEquals(2, loadedChild.getParents().size());
Assert.assertNotSame(child, loadedChild);
loadedChild.setParents(Arrays.asList(mother));
LOG.info("Save and Remove Relation to " + father.getId());
session.beginTransaction();
session.save(loadedChild);
session.getTransaction().commit();
LOG.info("Clear session");
session.clear();
Person loadedChild2 = session.load(Person.class, child.getId());
Assert.assertNotSame(loadedChild, loadedChild2);
loadedChild = null;
Assert.assertEquals(1, loadedChild2.getParents().size());
LOG.info("Delete it");
session.beginTransaction();
session.delete(mother);
session.delete(father);
session.delete(child);
session.getTransaction().commit();
}
@Test
public void repoTest() {
Person child = new Person("Child", Sex.OTHER);
Person father = new Person("Father", Sex.MALE);
Person mother = new Person("Mother", Sex.FEMALE);
child.setParents(Arrays.asList(mother, father));
LOG.info("Save");
//Persist the movie. This persists the actors as well.
personRepository.save(child);
LOG.info("Clear session");
springManagedSession.clear();
//Load a movie
Person loadedChild = personRepository.findOne(child.getId());
Assert.assertEquals(2, loadedChild.getParents().size());
Assert.assertNotSame(child, loadedChild);
loadedChild.setParents(Arrays.asList(mother));
LOG.info("Save and Remove Relation to " + father.getId());
personRepository.save(loadedChild);
LOG.info("Clear session");
springManagedSession.clear();
Person loadedChild2 = personRepository.findOne(child.getId());
Assert.assertNotSame(loadedChild, loadedChild2);
loadedChild = null;
Assert.assertEquals(1, loadedChild2.getParents().size());
LOG.info("Delete it");
personRepository.delete(mother);
personRepository.delete(father);
personRepository.delete(child);
}
}