@@ -34,7 +34,7 @@ public class PersistenceTests {
34
34
public void setupDb () {
35
35
repository .deleteAll ();
36
36
37
- ReviewEntity entity = new ReviewEntity (1 , 2 , "amazon" , "s" , "c" );
37
+ var entity = new ReviewEntity (1 , 2 , "amazon" , "s" , "c" );
38
38
savedEntity = repository .save (entity );
39
39
40
40
assertEquals (entity , savedEntity );
@@ -43,26 +43,23 @@ public void setupDb() {
43
43
@ Test
44
44
public void create () {
45
45
46
- ReviewEntity newEntity = new ReviewEntity (1 , 3 , "amazon 1" , "s" , "c" );
46
+ var newEntity = new ReviewEntity (1 , 3 , "amazon 1" , "s" , "c" );
47
47
repository .save (newEntity );
48
48
49
49
Optional <ReviewEntity > entity = repository .findById (newEntity .getId ());
50
- ReviewEntity foundEntity = new ReviewEntity ();
51
- if (entity .isPresent ()) foundEntity = entity .get ();
52
50
53
- assertEquals (newEntity , foundEntity );
51
+ assertEquals (newEntity , entity . orElse ( new ReviewEntity ()) );
54
52
55
53
assertEquals (2 , repository .count ());
56
54
}
57
55
58
56
@ Test
59
57
public void update () {
58
+
60
59
savedEntity .setAuthor ("amazon 2" );
61
60
repository .save (savedEntity );
62
61
63
- Optional <ReviewEntity > entity = repository .findById (savedEntity .getId ());
64
- ReviewEntity foundEntity = new ReviewEntity ();
65
- if (entity .isPresent ()) foundEntity = entity .get ();
62
+ var foundEntity = repository .findById (savedEntity .getId ()).orElse (new ReviewEntity ());
66
63
67
64
assertEquals (1 , (long ) foundEntity .getVersion ());
68
65
assertEquals ("amazon 2" , foundEntity .getAuthor ());
@@ -71,6 +68,7 @@ public void update() {
71
68
@ Test
72
69
public void delete () {
73
70
repository .delete (savedEntity );
71
+
74
72
assertFalse (repository .existsById (savedEntity .getId ()));
75
73
}
76
74
@@ -86,34 +84,28 @@ public void getByProductId() {
86
84
public void duplicateError () {
87
85
88
86
Assertions .assertThrows (
89
- DataIntegrityViolationException .class ,
90
- () -> {
91
- ReviewEntity entity = new ReviewEntity (1 , 2 , "amazon 1" , "s" , "c" );
92
- repository .save (entity );
93
- });
87
+ DataIntegrityViolationException .class ,
88
+ () -> repository .save (new ReviewEntity (
89
+ 1 , 2 , "amazon 1" ,
90
+ "s" , "c" )));
94
91
}
95
92
96
93
@ Test
97
94
public void optimisticLockError () {
98
95
99
- ReviewEntity entity1 = new ReviewEntity (),
100
- entity2 = new ReviewEntity ();
101
-
102
96
// Store the saved entity in two separate entity objects
103
- Optional <ReviewEntity > result = repository .findById (savedEntity .getId ());
104
- if (result .isPresent ()) entity1 = result .get ();
105
-
106
- Optional <ReviewEntity > result2 = repository .findById (savedEntity .getId ());
107
- if (result2 .isPresent ()) entity2 = result2 .get ();
108
-
97
+ ReviewEntity entity1 = repository .findById (savedEntity .getId ()).orElse (new ReviewEntity ()),
98
+ entity2 = repository .findById (savedEntity .getId ()).orElse (new ReviewEntity ());
109
99
110
100
// Update the entity using the first entity object
111
101
entity1 .setAuthor ("amazon 1" );
112
102
repository .save (entity1 );
113
103
114
- // Update the entity using the second entity object.
115
- // This should fail since the second entity now holds a old version number, i.e. a Optimistic
116
- // Lock Error
104
+ /*
105
+ Update the entity using the second entity object.
106
+ This should fail since the second entity now holds a old version number,
107
+ i.e. a Optimistic Lock Error
108
+ */
117
109
try {
118
110
entity2 .setAuthor ("amazon 2" );
119
111
repository .save (entity2 );
@@ -123,9 +115,7 @@ public void optimisticLockError() {
123
115
}
124
116
125
117
// Get the updated entity from the database and verify its new sate
126
- Optional <ReviewEntity > foundEntity = repository .findById (savedEntity .getId ());
127
- ReviewEntity updatedEntity = new ReviewEntity ();
128
- if (foundEntity .isPresent ()) updatedEntity = foundEntity .get ();
118
+ var updatedEntity = repository .findById (savedEntity .getId ()).orElse (new ReviewEntity ());
129
119
130
120
assertEquals (1 , updatedEntity .getVersion ());
131
121
assertEquals ("amazon 1" , updatedEntity .getAuthor ());
0 commit comments