##### Coverage Report - com.ctrip.framework.apollo.biz.service.ReleaseService

Classes in this File | Line Coverage | Branch Coverage | Complexity
ReleaseService |
6% | 2/31
0% | 0/6
| 2.3333333333333335;2.333

1 |  |
     package com.ctrip.framework.apollo.biz.service;
2 |  |
3 |  |
     import com.google.gson.Gson;
4 |  |
5 |  |
     import com.ctrip.framework.apollo.biz.entity.Audit;
6 |  |
     import com.ctrip.framework.apollo.biz.entity.Item;
7 |  |
     import com.ctrip.framework.apollo.biz.entity.Namespace;
8 |  |
     import com.ctrip.framework.apollo.biz.entity.Release;
9 |  |
     import com.ctrip.framework.apollo.biz.repository.ItemRepository;
10 |  |
     import com.ctrip.framework.apollo.biz.repository.ReleaseRepository;
11 |  |
     import com.ctrip.framework.apollo.biz.utils.ReleaseKeyGenerator;
12 |  |
     import com.ctrip.framework.apollo.core.utils.StringUtils;
13 |  |
14 |  |
     import org.springframework.beans.factory.annotation.Autowired;
15 |  |
     import org.springframework.data.domain.Pageable;
16 |  |
     import org.springframework.stereotype.Service;
17 |  |
     import org.springframework.transaction.annotation.Transactional;
18 |  |
19 |  |
     import java.util.Collections;
20 |  |
     import java.util.Date;
21 |  |
     import java.util.HashMap;
22 |  |
     import java.util.List;
23 |  |
     import java.util.Map;
24 |  |
25 |  |
     /**
26 |  |
      * @author Jason Song(song_s@ctrip.com)
27 |  |
      */
28 |  |
     @Service
29 | 1 |
     public class ReleaseService {
30 | 1 |
       private Gson gson = new Gson();
31 |  |
32 |  |
       @Autowired
33 |  |
       private ReleaseRepository releaseRepository;
34 |  |
35 |  |
       @Autowired
36 |  |
       private ItemRepository itemRepository;
37 |  |
38 |  |
       @Autowired
39 |  |
       private AuditService auditService;
40 |  |
41 |  |
       @Autowired
42 |  |
       private NamespaceLockService namespaceLockService;
43 |  |
44 |  |
45 |  |
       public Release findOne(long releaseId) {
46 | 0 |
         Release release = releaseRepository.findOne(releaseId);
47 | 0 |
         return release;
48 |  |
       }
49 |  |
50 |  |
       public List<Release> findReleases(String appId, String clusterName, String namespaceName, Pageable page) {
51 | 0 |
         List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(appId,
52 |  |
                                                                                                           clusterName, namespaceName, page);
53 | 0 |
         if (releases == null) {
54 | 0 |
           return Collections.emptyList();
55 |  |
         }
56 | 0 |
         return releases;
57 |  |
       }
58 |  |
59 |  |
       @Transactional
60 |  |
       public Release buildRelease(String name, String comment, Namespace namespace, String owner) {
61 |  |
62 | 0 |
         List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
63 | 0 |
         Map<String, String> configurations = new HashMap<String, String>();
64 | 0 |
         for (Item item : items) {
65 | 0 |
           if (StringUtils.isEmpty(item.getKey())) {
66 | 0 |
             continue;
67 |  |
           }
68 | 0 |
           configurations.put(item.getKey(), item.getValue());
69 | 0 |
         }
70 |  |
71 | 0 |
         Release release = new Release();
72 | 0 |
         release.setReleaseKey(ReleaseKeyGenerator.generateReleaseKey(namespace));
73 | 0 |
         release.setDataChangeCreatedTime(new Date());
74 | 0 |
         release.setDataChangeCreatedBy(owner);
75 | 0 |
         release.setDataChangeLastModifiedBy(owner);
76 | 0 |
         release.setName(name);
77 | 0 |
         release.setComment(comment);
78 | 0 |
         release.setAppId(namespace.getAppId());
79 | 0 |
         release.setClusterName(namespace.getClusterName());
80 | 0 |
         release.setNamespaceName(namespace.getNamespaceName());
81 | 0 |
         release.setConfigurations(gson.toJson(configurations));
82 | 0 |
         release = releaseRepository.save(release);
83 |  |
84 | 0 |
         namespaceLockService.unlock(namespace.getId());
85 | 0 |
         auditService.audit(Release.class.getSimpleName(), release.getId(), Audit.OP.INSERT,
86 | 0 |
             release.getDataChangeCreatedBy());
87 |  |
88 | 0 |
         return release;
89 |  |
       }
90 |  |
91 |  |
     }