##### Coverage Report - com.ctrip.apollo.adminservice.controller.ClusterController

Classes in this File | Line Coverage | Branch Coverage | Complexity
ClusterController |
14% | 3/21
0% | 0/6
| 2.2;2.2

1 |  |
     package com.ctrip.apollo.adminservice.controller;
2 |  |
3 |  |
     import java.util.List;
4 |  |
5 |  |
     import org.springframework.beans.factory.annotation.Autowired;
6 |  |
     import org.springframework.http.HttpStatus;
7 |  |
     import org.springframework.http.ResponseEntity;
8 |  |
     import org.springframework.web.bind.annotation.PathVariable;
9 |  |
     import org.springframework.web.bind.annotation.RequestBody;
10 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
11 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
12 |  |
     import org.springframework.web.bind.annotation.RestController;
13 |  |
14 |  |
     import com.ctrip.apollo.biz.entity.Cluster;
15 |  |
     import com.ctrip.apollo.biz.service.ClusterService;
16 |  |
     import com.ctrip.apollo.biz.service.ViewService;
17 |  |
     import com.ctrip.apollo.common.utils.BeanUtils;
18 |  |
     import com.ctrip.apollo.core.dto.ClusterDTO;
19 |  |
     import com.ctrip.apollo.core.exception.NotFoundException;
20 |  |
21 |  |
     @RestController
22 | 1 |
     public class ClusterController {
23 |  |
24 |  |
       @Autowired
25 |  |
       private ViewService viewService;
26 |  |
27 |  |
       @Autowired
28 |  |
       private ClusterService clusterService;
29 |  |
30 |  |
       @RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST)
31 |  |
       public ResponseEntity<ClusterDTO> create(@PathVariable("appId") String appId,
32 |  |
           @RequestBody ClusterDTO dto) {
33 | 0 |
         Cluster entity = BeanUtils.transfrom(Cluster.class, dto);
34 | 0 |
         entity = clusterService.save(entity);
35 | 0 |
         dto = BeanUtils.transfrom(ClusterDTO.class, entity);
36 | 0 |
         return ResponseEntity.status(HttpStatus.CREATED).body(dto);
37 |  |
       }
38 |  |
39 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}", method = RequestMethod.DELETE)
40 |  |
       public void delete(@PathVariable("appId") String appId,
41 |  |
           @PathVariable("clusterName") String clusterName) {
42 | 0 |
         Cluster entity = clusterService.findOne(appId, clusterName);
43 | 0 |
         if (entity == null)
44 | 0 |
           throw new NotFoundException("cluster not found for clusterName " + clusterName);
45 | 0 |
         clusterService.delete(entity.getId());
46 | 0 |
       }
47 |  |
48 |  |
       @RequestMapping("/apps/{appId}/clusters")
49 |  |
       public List<ClusterDTO> find(@PathVariable("appId") String appId) {
50 | 0 |
         List<Cluster> clusters = viewService.findClusters(appId);
51 | 0 |
         return BeanUtils.batchTransform(ClusterDTO.class, clusters);
52 |  |
       }
53 |  |
54 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}")
55 |  |
       public ClusterDTO get(@PathVariable("appId") String appId,
56 |  |
           @PathVariable("clusterName") String clusterName) {
57 | 4 |
         Cluster cluster = clusterService.findOne(appId, clusterName);
58 | 4 |
         return BeanUtils.transfrom(ClusterDTO.class, cluster);
59 |  |
       }
60 |  |
61 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}", method = RequestMethod.PUT)
62 |  |
       public ClusterDTO update(@PathVariable("appId") String appId,
63 |  |
           @PathVariable("clusterName") String clusterName, @RequestBody ClusterDTO dto) {
64 | 0 |
         if (!clusterName.equals(dto.getName())) {
65 | 0 |
           throw new IllegalArgumentException(String
66 | 0 |
               .format("Path variable %s is not equals to object field %s", clusterName, dto.getName()));
67 |  |
         }
68 | 0 |
         Cluster entity = clusterService.findOne(appId, clusterName);
69 | 0 |
         if (entity == null) throw new NotFoundException("cluster not found for name " + clusterName);
70 | 0 |
         entity = clusterService.update(BeanUtils.transfrom(Cluster.class, dto));
71 | 0 |
         return BeanUtils.transfrom(ClusterDTO.class, entity);
72 |  |
       }
73 |  |
74 |  |
     }