##### Coverage Report - com.ctrip.framework.apollo.adminservice.controller.ReleaseController

Classes in this File | Line Coverage | Branch Coverage | Complexity
ReleaseController |
42% | 8/19
16% | 1/6
| 2.2;2.2

1 |  |
     package com.ctrip.framework.apollo.adminservice.controller;
2 |  |
3 |  |
     import com.google.common.base.Joiner;
4 |  |
5 |  |
     import com.ctrip.framework.apollo.biz.entity.Namespace;
6 |  |
     import com.ctrip.framework.apollo.biz.entity.Release;
7 |  |
     import com.ctrip.framework.apollo.biz.message.MessageSender;
8 |  |
     import com.ctrip.framework.apollo.biz.message.Topics;
9 |  |
     import com.ctrip.framework.apollo.biz.service.ConfigService;
10 |  |
     import com.ctrip.framework.apollo.biz.service.NamespaceService;
11 |  |
     import com.ctrip.framework.apollo.biz.service.ReleaseService;
12 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
13 |  |
     import com.ctrip.framework.apollo.core.ConfigConsts;
14 |  |
     import com.ctrip.framework.apollo.core.dto.ReleaseDTO;
15 |  |
     import com.ctrip.framework.apollo.core.exception.NotFoundException;
16 |  |
17 |  |
     import org.springframework.beans.factory.annotation.Autowired;
18 |  |
     import org.springframework.web.bind.annotation.PathVariable;
19 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
20 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
21 |  |
     import org.springframework.web.bind.annotation.RequestParam;
22 |  |
     import org.springframework.web.bind.annotation.RestController;
23 |  |
24 |  |
     import java.util.List;
25 |  |
26 |  |
     @RestController
27 | 2 |
     public class ReleaseController {
28 |  |
29 |  |
       @Autowired
30 |  |
       private ReleaseService releaseService;
31 |  |
32 |  |
       @Autowired
33 |  |
       private ConfigService configService;
34 |  |
35 |  |
       @Autowired
36 |  |
       private NamespaceService namespaceService;
37 |  |
38 |  |
       @Autowired
39 |  |
       private MessageSender messageSender;
40 |  |
41 | 1 |
       private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
42 |  |
43 |  |
       @RequestMapping("/release/{releaseId}")
44 |  |
       public ReleaseDTO get(@PathVariable("releaseId") long releaseId) {
45 | 0 |
         Release release = releaseService.findOne(releaseId);
46 | 0 |
         if (release == null) {
47 | 0 |
           throw new NotFoundException(String.format("release not found for %s", releaseId));
48 |  |
         }
49 | 0 |
         return BeanUtils.transfrom(ReleaseDTO.class, release);
50 |  |
       }
51 |  |
52 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases")
53 |  |
       public List<ReleaseDTO> find(@PathVariable("appId") String appId,
54 |  |
                                    @PathVariable("clusterName") String clusterName,
55 |  |
                                    @PathVariable("namespaceName") String namespaceName) {
56 | 0 |
         List<Release> releases = releaseService.findReleases(appId, clusterName, namespaceName);
57 | 0 |
         return BeanUtils.batchTransform(ReleaseDTO.class, releases);
58 |  |
       }
59 |  |
60 |  |
       @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases/latest")
61 |  |
       public ReleaseDTO getLatest(@PathVariable("appId") String appId,
62 |  |
                                   @PathVariable("clusterName") String clusterName,
63 |  |
                                   @PathVariable("namespaceName") String namespaceName) {
64 | 0 |
         Release release = configService.findRelease(appId, clusterName, namespaceName);
65 | 0 |
         if (release == null) {
66 | 0 |
           throw new NotFoundException(String.format("latest release not found for %s %s %s", appId,
67 |  |
               clusterName, namespaceName));
68 |  |
         } else {
69 | 0 |
           return BeanUtils.transfrom(ReleaseDTO.class, release);
70 |  |
         }
71 |  |
       }
72 |  |
73 |  |
       @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
74 |  |
       public ReleaseDTO buildRelease(@PathVariable("appId") String appId,
75 |  |
                                      @PathVariable("clusterName") String clusterName,
76 |  |
                                      @PathVariable("namespaceName") String namespaceName,
77 |  |
                                      @RequestParam("name") String name,
78 |  |
                                      @RequestParam(name = "comment", required = false) String comment,
79 |  |
                                      @RequestParam("operator") String operator) {
80 | 2 |
         Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
81 | 2 |
         if (namespace == null) {
82 | 0 |
           throw new NotFoundException(String.format("Could not find namespace for %s %s %s", appId,
83 |  |
               clusterName, namespaceName));
84 |  |
         }
85 | 2 |
         Release release = releaseService.buildRelease(name, comment, namespace, operator);
86 | 2 |
         messageSender.sendMessage(assembleKey(appId, clusterName, namespaceName),
87 |  |
             Topics.APOLLO_RELEASE_TOPIC);
88 | 2 |
         return BeanUtils.transfrom(ReleaseDTO.class, release);
89 |  |
       }
90 |  |
91 |  |
       private String assembleKey(String appId, String cluster, String namespace) {
92 | 2 |
         return STRING_JOINER.join(appId, cluster, namespace);
93 |  |
       }
94 |  |
     }