Skip to content

Commit b4d406b

Browse files
committed
Unit Testing Layer Service
1 parent 50814aa commit b4d406b

32 files changed

Lines changed: 1752 additions & 57 deletions

pom.xml

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
</properties>
2222

2323
<dependencies>
24+
25+
2426
<dependency>
2527
<groupId>org.springframework</groupId>
2628
<artifactId>spring-core</artifactId>
@@ -129,18 +131,7 @@
129131
<version>2.3.1</version>
130132
</dependency>
131133

132-
<dependency>
133-
<groupId>junit</groupId>
134-
<artifactId>junit</artifactId>
135-
<version>4.11</version>
136-
<scope>test</scope>
137-
</dependency>
138-
<dependency>
139-
<groupId>org.easymock</groupId>
140-
<artifactId>easymock</artifactId>
141-
<version>3.1</version>
142-
<scope>test</scope>
143-
</dependency>
134+
144135
<dependency>
145136
<groupId>org.springframework</groupId>
146137
<artifactId>spring-test</artifactId>
@@ -178,6 +169,30 @@
178169
<artifactId>commons-codec</artifactId>
179170
<version>1.8</version>
180171
</dependency>
172+
<dependency>
173+
<groupId>org.hamcrest</groupId>
174+
<artifactId>hamcrest-all</artifactId>
175+
<version>1.3</version>
176+
<scope>test</scope>
177+
</dependency>
178+
<dependency>
179+
<groupId>org.mockito</groupId>
180+
<artifactId>mockito-core</artifactId>
181+
<version>1.8.4</version>
182+
<scope>test</scope>
183+
</dependency>
184+
<dependency>
185+
<groupId>junit</groupId>
186+
<artifactId>junit</artifactId>
187+
<version>4.11</version>
188+
<scope>test</scope>
189+
</dependency>
190+
<dependency>
191+
<groupId>org.easymock</groupId>
192+
<artifactId>easymock</artifactId>
193+
<version>3.1</version>
194+
<scope>test</scope>
195+
</dependency>
181196

182197
<!-- <dependency>
183198
<groupId>org.apache.tiles</groupId>

src/main/java/com/agungsetiawan/finalproject/config/WebAppConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.agungsetiawan.finalproject.config;
22

3+
import com.agungsetiawan.finalproject.interceptor.CommonDataInterceptor;
34
import com.agungsetiawan.finalproject.service.CartService;
45
import java.util.Properties;
56
import javax.annotation.Resource;
@@ -16,7 +17,9 @@
1617
import org.springframework.orm.hibernate4.HibernateTransactionManager;
1718
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
1819
import org.springframework.transaction.annotation.EnableTransactionManagement;
20+
import org.springframework.web.context.request.WebRequestInterceptor;
1921
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
22+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
2023
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
2124
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
2225
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@@ -94,6 +97,16 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
9497
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
9598
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
9699
}
100+
101+
// @Override
102+
// public void addInterceptors(InterceptorRegistry registry){
103+
// registry.addWebRequestInterceptor(commonDataInterceptor());
104+
// }
105+
//
106+
// @Bean
107+
// public WebRequestInterceptor commonDataInterceptor(){
108+
// return new CommonDataInterceptor();
109+
// }
97110

98111

99112
// @Bean

src/main/java/com/agungsetiawan/finalproject/controller/AllBookController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class AllBookController {
1919
@Autowired
2020
private BookService bookService;
2121

22+
public void setBookService(BookService bookService){
23+
this.bookService=bookService;
24+
}
25+
2226
@RequestMapping(value = "public/book/all",method = RequestMethod.GET)
2327
public String allBook(Model model){
2428
List<Book> books=bookService.findRandom();

src/main/java/com/agungsetiawan/finalproject/controller/BookDetailController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.agungsetiawan.finalproject.domain.Category;
55
import com.agungsetiawan.finalproject.service.BookService;
66
import com.agungsetiawan.finalproject.service.CartService;
7+
import com.agungsetiawan.finalproject.service.CartServiceInterface;
78
import com.agungsetiawan.finalproject.service.CategoryService;
89
import java.util.List;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,22 +22,32 @@
2122
@Controller
2223
public class BookDetailController {
2324
@Autowired
24-
private CartService cart;
25+
private CartServiceInterface cart;
2526

2627
@Autowired
2728
private CategoryService categoryService;
2829

2930
@Autowired
3031
private BookService bookService;
3132

33+
public BookDetailController(){
34+
35+
}
36+
37+
public BookDetailController(CartServiceInterface cart,CategoryService categoryService,BookService bookService){
38+
this.cart=cart;
39+
this.categoryService=categoryService;
40+
this.bookService=bookService;
41+
}
42+
3243
@ModelAttribute(value = "listCategory")
3344
public List<Category> listCategory(){
3445
return categoryService.findAll();
3546
}
3647

3748
@ModelAttribute(value = "randomBooks")
3849
public List<Book> listBook(){
39-
return bookService.findAll();
50+
return bookService.findRandom();
4051
}
4152

4253
@ModelAttribute(value = "cartSize")

src/main/java/com/agungsetiawan/finalproject/controller/CartController.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.agungsetiawan.finalproject.domain.Category;
55
import com.agungsetiawan.finalproject.service.BookService;
66
import com.agungsetiawan.finalproject.service.CartService;
7+
import com.agungsetiawan.finalproject.service.CartServiceInterface;
78
import com.agungsetiawan.finalproject.service.CategoryService;
89
import java.util.List;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,22 +23,32 @@
2223
@Controller
2324
public class CartController {
2425
@Autowired
25-
private CartService cart;
26+
private CartServiceInterface cart;
2627

2728
@Autowired
2829
private BookService bookService;
2930

3031
@Autowired
3132
private CategoryService categoryService;
3233

34+
public CartController(CartServiceInterface cart,BookService bookService,CategoryService categoryService){
35+
this.cart=cart;
36+
this.bookService=bookService;
37+
this.categoryService=categoryService;
38+
}
39+
40+
public CartController(){
41+
42+
}
43+
3344
@ModelAttribute(value = "listCategory")
3445
public List<Category> listCategory(){
3546
return categoryService.findAll();
3647
}
3748

3849
@ModelAttribute(value = "randomBooks")
3950
public List<Book> listBook(){
40-
return bookService.findAll();
51+
return bookService.findRandom();
4152
}
4253

4354
@ModelAttribute(value = "cartSize")
@@ -56,21 +67,24 @@ public String showCart(Model model){
5667
@RequestMapping(value = "public/cart/add/{bookId}",method = RequestMethod.POST)
5768
public String addBook(@PathVariable("bookId") Long bookId, Model model){
5869
Book book=bookService.findOne(bookId);
59-
cart.save(book);
70+
Book bookSaved=cart.save(book);
71+
model.addAttribute("id", bookSaved.getId());
6072
return "redirect:/public/cart";
6173
}
6274

6375
@RequestMapping(value = "public/cart/remove/{bookId}",method = RequestMethod.GET)
6476
public String removeBook(@PathVariable("bookId") Long bookId ,Model model){
6577
Book book=bookService.findOne(bookId);
66-
cart.delete(book);
78+
Book bookDeleted=cart.delete(book);
79+
model.addAttribute("id", bookDeleted.getId());
6780
return "redirect:/public/cart";
6881
}
6982

7083
@RequestMapping(value = "public/cart/update",method = RequestMethod.POST)
71-
public String update(@RequestParam(value = "bookId") Long bookId,@RequestParam(value = "quantity") Integer quantity){
84+
public String update(@RequestParam(value = "bookId") Long bookId,@RequestParam(value = "quantity") Integer quantity,Model model){
7285
Book book=bookService.findOne(bookId);
73-
cart.update(book, quantity);
86+
Book bookUpdated=cart.update(book, quantity);
87+
model.addAttribute("id", bookUpdated.getId());
7488
return "redirect:/public/cart";
7589
}
7690

src/main/java/com/agungsetiawan/finalproject/controller/CategoryController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.agungsetiawan.finalproject.domain.Category;
55
import com.agungsetiawan.finalproject.service.BookService;
66
import com.agungsetiawan.finalproject.service.CartService;
7+
import com.agungsetiawan.finalproject.service.CartServiceInterface;
78
import com.agungsetiawan.finalproject.service.CategoryService;
89
import java.util.List;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,22 +24,32 @@
2324
@Controller
2425
public class CategoryController {
2526
@Autowired
26-
private CartService cart;
27+
private CartServiceInterface cart;
2728

2829
@Autowired
2930
private CategoryService categoryService;
3031

3132
@Autowired
3233
private BookService bookService;
3334

35+
public CategoryController(){
36+
37+
}
38+
39+
public CategoryController(CartServiceInterface cart,CategoryService categoryService,BookService bookService){
40+
this.cart=cart;
41+
this.categoryService=categoryService;
42+
this.bookService=bookService;
43+
}
44+
3445
@ModelAttribute(value = "listCategory")
3546
public List<Category> listCategory(){
3647
return categoryService.findAll();
3748
}
3849

3950
@ModelAttribute(value = "randomBooks")
4051
public List<Book> listBook(){
41-
return bookService.findAll();
52+
return bookService.findRandom();
4253
}
4354

4455
@ModelAttribute(value = "cartSize")

src/main/java/com/agungsetiawan/finalproject/controller/CustomerOrderController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.agungsetiawan.finalproject.controller;
22

3-
import com.agungsetiawan.finalproject.domain.Customer;
43
import com.agungsetiawan.finalproject.domain.Order;
54
import com.agungsetiawan.finalproject.domain.OrderDetail;
65
import com.agungsetiawan.finalproject.service.OrderService;
76
import java.util.List;
87
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.security.access.annotation.Secured;
99
import org.springframework.security.core.context.SecurityContextHolder;
1010
import org.springframework.stereotype.Controller;
1111
import org.springframework.ui.Model;
@@ -23,6 +23,14 @@ public class CustomerOrderController {
2323
@Autowired
2424
private OrderService orderService;
2525

26+
public CustomerOrderController(){
27+
28+
}
29+
30+
public CustomerOrderController(OrderService orderService){
31+
this.orderService=orderService;
32+
}
33+
2634
@RequestMapping(value = "secured/my/order",method = RequestMethod.GET)
2735
public String allOrder(Model model){
2836
String customer= SecurityContextHolder.getContext().getAuthentication().getName();

src/main/java/com/agungsetiawan/finalproject/domain/Order.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ public class Order implements Serializable{
3232
private Long id;
3333
private BigDecimal total;
3434
private String status;
35+
36+
public Order() {
37+
}
38+
39+
public Order(Long id, BigDecimal total, String status, String shippingAddress, String receiver, String city, String province, String receiverEmail, String receiverPhone, Date date, Customer customer) {
40+
this.id = id;
41+
this.total = total;
42+
this.status = status;
43+
this.shippingAddress = shippingAddress;
44+
this.receiver = receiver;
45+
this.city = city;
46+
this.province = province;
47+
this.receiverEmail = receiverEmail;
48+
this.receiverPhone = receiverPhone;
49+
this.date = date;
50+
this.customer = customer;
51+
}
52+
53+
3554

3655
@NotEmpty(message="Alamat Pengiriman Harus diisi")
3756
@Column(name = "shipping_address")

src/main/java/com/agungsetiawan/finalproject/domain/OrderDetail.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ public class OrderDetail {
3434
@JoinColumn(name = "id_order")
3535
private Order order;
3636

37+
public OrderDetail() {
38+
}
39+
40+
public OrderDetail(Long id, Integer amount, BigDecimal subTotal, BigDecimal price, Book book, Order order) {
41+
this.id = id;
42+
this.amount = amount;
43+
this.subTotal = subTotal;
44+
this.price = price;
45+
this.book = book;
46+
this.order = order;
47+
}
48+
49+
50+
3751
public Long getId() {
3852
return id;
3953
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.agungsetiawan.finalproject.interceptor;
2+
3+
import com.agungsetiawan.finalproject.service.BookService;
4+
import com.agungsetiawan.finalproject.service.CartServiceInterface;
5+
import com.agungsetiawan.finalproject.service.CategoryService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.web.context.request.WebRequest;
9+
import org.springframework.web.context.request.WebRequestInterceptor;
10+
11+
/**
12+
*
13+
* @author awanlabs
14+
*/
15+
public class CommonDataInterceptor implements WebRequestInterceptor{
16+
17+
@Autowired
18+
private CartServiceInterface cartService;
19+
@Autowired
20+
private BookService bookService;
21+
@Autowired
22+
private CategoryService categoryService;
23+
24+
@Override
25+
public void preHandle(WebRequest wr) throws Exception {
26+
}
27+
28+
@Override
29+
public void postHandle(WebRequest wr, ModelMap model) throws Exception {
30+
model.addAttribute("listCategory", categoryService.findAll());
31+
model.addAttribute("randomBooks", bookService.findRandom());
32+
model.addAttribute("cartSize", cartService.size());
33+
}
34+
35+
@Override
36+
public void afterCompletion(WebRequest wr, Exception excptn) throws Exception {
37+
}
38+
39+
}

0 commit comments

Comments
 (0)