Skip to content

Commit 6ac7593

Browse files
committed
create cart document and cart repository
1 parent dba93e5 commit 6ac7593

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.idspring.commandpattern.entity;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.mongodb.core.mapping.Document;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* @author Eko Kurniawan Khannedy
13+
* @since 30/06/17
14+
*/
15+
@Data
16+
@Builder
17+
@Document
18+
public class Cart {
19+
20+
@Id
21+
private String id;
22+
23+
private List<CartItem> items = new ArrayList<>();
24+
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.idspring.commandpattern.entity;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
/**
7+
* @author Eko Kurniawan Khannedy
8+
* @since 30/06/17
9+
*/
10+
@Data
11+
@Builder
12+
public class CartItem {
13+
14+
private String id;
15+
16+
private String name;
17+
18+
private Long price;
19+
20+
private Integer quantity;
21+
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.idspring.commandpattern.repository;
2+
3+
import com.idspring.commandpattern.entity.Cart;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
6+
/**
7+
* @author Eko Kurniawan Khannedy
8+
* @since 30/06/17
9+
*/
10+
public interface CartRepository extends ReactiveMongoRepository<Cart, String> {
11+
12+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.idspring.commandpattern.repository;
2+
3+
import com.idspring.commandpattern.entity.Cart;
4+
import com.idspring.commandpattern.entity.CartItem;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.util.Arrays;
12+
13+
import static org.junit.Assert.*;
14+
15+
/**
16+
* @author Eko Kurniawan Khannedy
17+
* @since 30/06/17
18+
*/
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest
21+
public class CartRepositoryTest {
22+
23+
@Autowired
24+
private CartRepository cartRepository;
25+
26+
@Test
27+
public void testSaveCart() throws Exception {
28+
Cart cart = Cart.builder()
29+
.id("id")
30+
.items(Arrays.asList(
31+
CartItem.builder()
32+
.id("1")
33+
.name("Mie Ayam Goreng")
34+
.price(1000L)
35+
.quantity(10)
36+
.build(),
37+
CartItem.builder()
38+
.id("1")
39+
.name("Mie Ayam Rebus")
40+
.price(500L)
41+
.quantity(5)
42+
.build()
43+
))
44+
.build();
45+
46+
Cart result = cartRepository.save(cart).block();
47+
48+
assertEquals(cart, result);
49+
}
50+
51+
@Test
52+
public void testSaveAndGetCart() throws Exception {
53+
Cart cart = Cart.builder()
54+
.id("id")
55+
.items(Arrays.asList(
56+
CartItem.builder()
57+
.id("1")
58+
.name("Mie Ayam Goreng")
59+
.price(1000L)
60+
.quantity(10)
61+
.build(),
62+
CartItem.builder()
63+
.id("1")
64+
.name("Mie Ayam Rebus")
65+
.price(500L)
66+
.quantity(5)
67+
.build()
68+
))
69+
.build();
70+
71+
cartRepository.save(cart).block();
72+
Cart result = cartRepository.findById(cart.getId()).block();
73+
74+
assertEquals(cart, result);
75+
}
76+
}

0 commit comments

Comments
 (0)