1
1
package net .chrisrichardson .ftgo .orderservice .domain ;
2
2
3
- import net .chrisrichardson .ftgo .orderservice .api .events .OrderState ;
4
3
import org .junit .Test ;
5
4
import org .junit .runner .RunWith ;
6
5
import org .springframework .beans .factory .annotation .Autowired ;
7
6
import org .springframework .boot .test .context .SpringBootTest ;
8
7
import org .springframework .test .context .junit4 .SpringRunner ;
9
8
import org .springframework .transaction .support .TransactionTemplate ;
10
9
11
- import static net .chrisrichardson .ftgo .orderservice .OrderDetailsMother .CONSUMER_ID ;
12
- import static net .chrisrichardson .ftgo .orderservice .OrderDetailsMother .chickenVindalooLineItems ;
13
- import static net .chrisrichardson .ftgo .orderservice .RestaurantMother .AJANTA_ID ;
14
- import static net .chrisrichardson .ftgo .orderservice .RestaurantMother .AJANTA_RESTAURANT_MENU_ITEMS ;
15
- import static net .chrisrichardson .ftgo .orderservice .RestaurantMother .AJANTA_RESTAURANT_NAME ;
10
+ import static net .chrisrichardson .ftgo .orderservice .RestaurantMother .*;
16
11
import static org .junit .Assert .assertEquals ;
17
- import static org .junit .Assert .assertNotNull ;
18
12
19
13
@ RunWith (SpringRunner .class )
20
14
@ SpringBootTest (classes = OrderJpaTestConfiguration .class )
@@ -27,21 +21,37 @@ public class RestaurantJpaTest {
27
21
private TransactionTemplate transactionTemplate ;
28
22
29
23
@ Test
30
- public void shouldSaveRestaurant () {
24
+ public void shouldSaveAndLoadRestaurant () {
25
+ long restaurantId = saveRestaurant ();
26
+ assertEquals (AJANTA_ID , restaurantId );
27
+ loadRestaurant (restaurantId );
28
+ }
31
29
32
- transactionTemplate .execute ((ts ) -> {
33
- Restaurant restaurant = new Restaurant (AJANTA_ID , AJANTA_RESTAURANT_NAME , AJANTA_RESTAURANT_MENU_ITEMS );
34
- restaurantRepository .save (restaurant );
35
- return null ;
36
- });
37
- transactionTemplate .execute ((ts ) -> {
38
- Restaurant restaurant = new Restaurant (AJANTA_ID , AJANTA_RESTAURANT_NAME , AJANTA_RESTAURANT_MENU_ITEMS );
39
- restaurantRepository .save (restaurant );
30
+ @ Test
31
+ public void shouldSaveRestaurantTwice () {
32
+ long restaurantId1 = saveRestaurant ();
33
+ long restaurantId2 = saveRestaurant ();
34
+ assertEquals (AJANTA_ID , restaurantId1 );
35
+ assertEquals (restaurantId1 , restaurantId2 );
36
+ loadRestaurant (restaurantId1 );
37
+ }
38
+
39
+ private void loadRestaurant (long restaurantId ) {
40
+ transactionTemplate .execute (ts -> {
41
+ Restaurant restaurant = restaurantRepository .findById (restaurantId ).get ();
42
+ assertEquals (AJANTA_RESTAURANT_NAME , restaurant .getName ());
43
+ assertEquals (AJANTA_RESTAURANT_MENU_ITEMS , restaurant .getMenuItems ());
40
44
return null ;
41
45
});
46
+ }
42
47
43
48
44
-
49
+ private long saveRestaurant () {
50
+ return transactionTemplate .execute ((ts ) -> {
51
+ Restaurant restaurant = new Restaurant (AJANTA_ID , AJANTA_RESTAURANT_NAME , AJANTA_RESTAURANT_MENU_ITEMS );
52
+ restaurantRepository .save (restaurant );
53
+ return restaurant .getId ();
54
+ });
45
55
}
46
56
47
57
}
0 commit comments