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