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

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

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