Coverage Report - com.ctrip.framework.apollo.biz.service.ClusterService
 
Classes in this File Line Coverage Branch Coverage Complexity
ClusterService
40%
18/44
40%
4/10
2.429
 
 1  
 package com.ctrip.framework.apollo.biz.service;
 2  
 
 3  
 import java.util.Collections;
 4  
 import java.util.List;
 5  
 import java.util.Objects;
 6  
 
 7  
 import org.springframework.beans.factory.annotation.Autowired;
 8  
 import org.springframework.stereotype.Service;
 9  
 import org.springframework.transaction.annotation.Transactional;
 10  
 
 11  
 import com.ctrip.framework.apollo.biz.entity.Audit;
 12  
 import com.ctrip.framework.apollo.biz.entity.Cluster;
 13  
 import com.ctrip.framework.apollo.biz.repository.ClusterRepository;
 14  
 import com.ctrip.framework.apollo.common.utils.BeanUtils;
 15  
 import com.ctrip.framework.apollo.core.ConfigConsts;
 16  
 import com.ctrip.framework.apollo.core.exception.ServiceException;
 17  
 
 18  
 import com.google.common.base.Strings;
 19  
 
 20  
 @Service
 21  1
 public class ClusterService {
 22  
 
 23  
   @Autowired
 24  
   private ClusterRepository clusterRepository;
 25  
   @Autowired
 26  
   private AuditService auditService;
 27  
   @Autowired
 28  
   private NamespaceService namespaceService;
 29  
   @Autowired
 30  
   private AppNamespaceService appNamespaceService;
 31  
 
 32  
 
 33  
   public boolean isClusterNameUnique(String appId, String clusterName) {
 34  6
     Objects.requireNonNull(appId, "AppId must not be null");
 35  6
     Objects.requireNonNull(clusterName, "ClusterName must not be null");
 36  6
     return Objects.isNull(clusterRepository.findByAppIdAndName(appId, clusterName));
 37  
   }
 38  
 
 39  
   public Cluster findOne(String appId, String name) {
 40  0
     return clusterRepository.findByAppIdAndName(appId, name);
 41  
   }
 42  
 
 43  
   public List<Cluster> findClusters(String appId) {
 44  3
     if (Strings.isNullOrEmpty(appId)) {
 45  0
       return Collections.emptyList();
 46  
     }
 47  
 
 48  3
     List<Cluster> clusters = clusterRepository.findByAppId(appId);
 49  3
     if (clusters == null) {
 50  0
       return Collections.emptyList();
 51  
     }
 52  3
     return clusters;
 53  
   }
 54  
 
 55  
   @Transactional
 56  
   public Cluster save(Cluster entity) {
 57  0
     if (!isClusterNameUnique(entity.getAppId(), entity.getName())) {
 58  0
       throw new ServiceException("cluster not unique");
 59  
     }
 60  0
     entity.setId(0);//protection
 61  0
     Cluster cluster = clusterRepository.save(entity);
 62  
 
 63  0
     namespaceService.createPrivateNamespace(cluster.getAppId(), cluster.getName(), cluster.getDataChangeCreatedBy());
 64  
 
 65  0
     auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT,
 66  0
                        cluster.getDataChangeCreatedBy());
 67  
 
 68  0
     return cluster;
 69  
   }
 70  
 
 71  
   @Transactional
 72  
   public void delete(long id, String operator) {
 73  0
     Cluster cluster = clusterRepository.findOne(id);
 74  0
     if (cluster == null) {
 75  0
       return;
 76  
     }
 77  
 
 78  0
     cluster.setDeleted(true);
 79  0
     cluster.setDataChangeLastModifiedBy(operator);
 80  0
     clusterRepository.save(cluster);
 81  
 
 82  0
     auditService.audit(Cluster.class.getSimpleName(), id, Audit.OP.DELETE, operator);
 83  0
   }
 84  
 
 85  
   @Transactional
 86  
   public Cluster update(Cluster cluster) {
 87  0
     Cluster managedCluster =
 88  0
         clusterRepository.findByAppIdAndName(cluster.getAppId(), cluster.getName());
 89  0
     BeanUtils.copyEntityProperties(cluster, managedCluster);
 90  0
     managedCluster = clusterRepository.save(managedCluster);
 91  
 
 92  0
     auditService.audit(Cluster.class.getSimpleName(), managedCluster.getId(), Audit.OP.UPDATE,
 93  0
         managedCluster.getDataChangeLastModifiedBy());
 94  
 
 95  0
     return managedCluster;
 96  
   }
 97  
 
 98  
   @Transactional
 99  
   public void createDefaultCluster(String appId, String createBy) {
 100  6
     if (!isClusterNameUnique(appId, ConfigConsts.CLUSTER_NAME_DEFAULT)) {
 101  1
       throw new ServiceException("cluster not unique");
 102  
     }
 103  5
     Cluster cluster = new Cluster();
 104  5
     cluster.setName(ConfigConsts.CLUSTER_NAME_DEFAULT);
 105  5
     cluster.setAppId(appId);
 106  5
     cluster.setDataChangeCreatedBy(createBy);
 107  5
     cluster.setDataChangeLastModifiedBy(createBy);
 108  5
     clusterRepository.save(cluster);
 109  
 
 110  5
     auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, createBy);
 111  5
   }
 112  
 }