• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Talathussain110 / ProductOrderManagement / #14

16 Dec 2025 09:45PM UTC coverage: 69.355% (-5.6%) from 75.0%
#14

push

Talathussain110
Added HtmlUnit tests for all WebControllers

13 of 20 new or added lines in 2 files covered. (65.0%)

43 of 62 relevant lines covered (69.35%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.42
/src/main/java/com/example/demo/controller/OrderWebController.java
1
package com.example.demo.controller;
2

3
import java.util.LinkedHashSet;
4
import java.util.List;
5
import java.util.Set;
6

7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Controller;
9
import org.springframework.ui.Model;
10
import org.springframework.web.bind.WebDataBinder;
11
import org.springframework.web.bind.annotation.GetMapping;
12
import org.springframework.web.bind.annotation.InitBinder;
13
import org.springframework.web.bind.annotation.ModelAttribute;
14
import org.springframework.web.bind.annotation.PathVariable;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18

19
import com.example.demo.model.Order;
20
import com.example.demo.model.Product;
21
import com.example.demo.services.OrderService;
22
import com.example.demo.services.ProductService;
23

24
@Controller
25
@RequestMapping("/orders")
26
public class OrderWebController {
1✔
27

28
        private static final String MESSAGE_ATTRIBUTE = "message";
29
        private static final String ORDER_ATTRIBUTE = "order";
30
        private static final String ORDERS_ATTRIBUTE = "orders";
31
        private static final String ALL_PRODUCTS_ATTRIBUTE = "allProducts";
32

33
        @Autowired
34
        private OrderService orderService;
35

36
        @Autowired
37
        private ProductService productService;
38

39
        @InitBinder
40
        public void initBinder(WebDataBinder binder) {
41
                binder.setDisallowedFields("products");
1✔
42
        }
1✔
43

44
        @GetMapping
45
        public String listOrders(Model model) {
46
                List<Order> allOrders = orderService.getAllOrders();
1✔
47
                model.addAttribute(ORDERS_ATTRIBUTE, allOrders);
1✔
48
                model.addAttribute(MESSAGE_ATTRIBUTE, allOrders.isEmpty() ? "No order" : "");
1✔
49
                return "order";
1✔
50
        }
51

52
        @GetMapping("/new")
53
        public String newOrder(Model model) {
54
                model.addAttribute(ORDER_ATTRIBUTE, new Order());
1✔
55
                model.addAttribute(MESSAGE_ATTRIBUTE, "");
1✔
56
                model.addAttribute(ALL_PRODUCTS_ATTRIBUTE, productService.getAllProducts());
1✔
57
                return "edit_order";
1✔
58
        }
59

60
        @GetMapping("/edit/{id}")
61
        public String editOrder(@PathVariable long id, Model model) {
62
                Order order = orderService.getOrderById(id);
1✔
63
                model.addAttribute(ORDER_ATTRIBUTE, order);
1✔
64
                model.addAttribute(MESSAGE_ATTRIBUTE, order == null ? "No order found with id: " + id : "");
1✔
65
                model.addAttribute(ALL_PRODUCTS_ATTRIBUTE, productService.getAllProducts());
1✔
66
                return "edit_order";
1✔
67
        }
68

69
        @PostMapping("/save")
70
        public String saveOrder(@ModelAttribute Order order,
71
                        @RequestParam(value = "products", required = false) List<Long> productIds) {
72

73
                if (productIds != null) {
1✔
NEW
74
                        Set<Product> selectedProducts = new LinkedHashSet<>();
×
NEW
75
                        for (Long id : productIds) {
×
NEW
76
                                Product product = productService.getProductById(id);
×
NEW
77
                                if (product != null) {
×
NEW
78
                                        selectedProducts.add(product);
×
79
                                }
NEW
80
                        }
×
NEW
81
                        order.setProducts(selectedProducts);
×
82
                }
83

84
                if (order.getId() == null) {
1✔
85
                        orderService.insertNewOrder(order);
1✔
86
                } else {
87
                        orderService.updateOrderById(order.getId(), order);
1✔
88
                }
89

90
                return "redirect:/orders";
1✔
91
        }
92

93
        @GetMapping("/delete/{id}")
94
        public String deleteOrder(@PathVariable long id, Model model) {
95
                orderService.deleteOrderById(id);
1✔
96
                model.addAttribute("deletedId", id);
1✔
97
                return "delete_order";
1✔
98
        }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc