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

Classes in this File | Line Coverage | Branch Coverage | Complexity
NamespaceController |
9% | 4/41
5% | 1/18
| 4.0;4

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.Namespace;
15 |  |
     import com.ctrip.apollo.biz.service.NamespaceService;
16 |  |
     import com.ctrip.apollo.biz.service.ViewService;
17 |  |
     import com.ctrip.apollo.common.utils.BeanUtils;
18 |  |
     import com.ctrip.apollo.core.dto.NamespaceDTO;
19 |  |
     import com.ctrip.apollo.core.exception.NotFoundException;
20 |  |
21 |  |
     @RestController
22 | 1 |
     public class NamespaceController {
23 |  |
24 |  |
       @Autowired
25 |  |
       private ViewService viewService;
26 |  |
27 |  |
       @Autowired
28 |  |
       private NamespaceService namespaceService;
29 |  |
30 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
31 |  |
       public ResponseEntity<NamespaceDTO> create(@PathVariable("appId") String appId,
32 |  |
           @PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
33 | 0 |
         if (!appId.equals(dto.getAppId())) {
34 | 0 |
           throw new IllegalArgumentException(String
35 | 0 |
               .format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
36 |  |
         }
37 | 0 |
         if (!clusterName.equals(dto.getClusterName())) {
38 | 0 |
           throw new IllegalArgumentException(String.format(
39 | 0 |
               "Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
40 |  |
         }
41 | 0 |
         Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
42 | 0 |
         entity = namespaceService.save(entity);
43 | 0 |
         dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
44 | 0 |
         return ResponseEntity.status(HttpStatus.CREATED).body(dto);
45 |  |
       }
46 |  |
47 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.DELETE)
48 |  |
       public void delete(@PathVariable("appId") String appId,
49 |  |
           @PathVariable("clusterName") String clusterName,
50 |  |
           @PathVariable("namespaceName") String namespaceName) {
51 | 0 |
         Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
52 | 0 |
         if (entity == null) throw new NotFoundException(
53 | 0 |
             String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
54 | 0 |
         namespaceService.delete(entity.getId());
55 | 0 |
       }
56 |  |
57 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
58 |  |
       public List<NamespaceDTO> find(@PathVariable("appId") String appId,
59 |  |
           @PathVariable("clusterName") String clusterName) {
60 | 0 |
         List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
61 | 0 |
         return BeanUtils.batchTransform(NamespaceDTO.class, groups);
62 |  |
       }
63 |  |
64 |  |
       @RequestMapping("/namespaces/{namespaceId}")
65 |  |
       public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
66 | 0 |
         Namespace namespace = namespaceService.findOne(namespaceId);
67 | 0 |
         if (namespace == null)
68 | 0 |
           throw new NotFoundException(String.format("namespace not found for %s", namespaceId));
69 | 0 |
         return BeanUtils.transfrom(NamespaceDTO.class, namespace);
70 |  |
       }
71 |  |
72 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}")
73 |  |
       public NamespaceDTO get(@PathVariable("appId") String appId,
74 |  |
           @PathVariable("clusterName") String clusterName,
75 |  |
           @PathVariable("namespaceName") String namespaceName) {
76 | 4 |
         Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
77 | 4 |
         if (namespace == null) throw new NotFoundException(
78 | 0 |
             String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
79 | 4 |
         return BeanUtils.transfrom(NamespaceDTO.class, namespace);
80 |  |
       }
81 |  |
82 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.PUT)
83 |  |
       public NamespaceDTO update(@PathVariable("appId") String appId,
84 |  |
           @PathVariable("clusterName") String clusterName,
85 |  |
           @PathVariable("namespaceName") String namespaceName, @RequestBody NamespaceDTO dto) {
86 | 0 |
         if (!appId.equals(dto.getAppId())) {
87 | 0 |
           throw new IllegalArgumentException(String
88 | 0 |
               .format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
89 |  |
         }
90 | 0 |
         if (!clusterName.equals(dto.getClusterName())) {
91 | 0 |
           throw new IllegalArgumentException(String.format(
92 | 0 |
               "Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
93 |  |
         }
94 | 0 |
         if (!namespaceName.equals(dto.getNamespaceName())) {
95 | 0 |
           throw new IllegalArgumentException(
96 | 0 |
               String.format("Path variable %s is not equals to object field %s", namespaceName,
97 | 0 |
                   dto.getNamespaceName()));
98 |  |
         }
99 | 0 |
         Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
100 | 0 |
         if (entity == null) throw new NotFoundException(
101 | 0 |
             String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
102 | 0 |
         entity = namespaceService.update(BeanUtils.transfrom(Namespace.class, dto));
103 | 0 |
         return BeanUtils.transfrom(NamespaceDTO.class, entity);
104 |  |
       }
105 |  |
     }