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

Classes in this File | Line Coverage | Branch Coverage | Complexity
NamespaceService |
34% | 16/46
37% | 3/8
| 1.875;1.875

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.Namespace;
13 |  |
     import com.ctrip.framework.apollo.biz.repository.NamespaceRepository;
14 |  |
     import com.ctrip.framework.apollo.common.entity.AppNamespace;
15 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
16 |  |
     import com.ctrip.framework.apollo.core.exception.ServiceException;
17 |  |
18 |  |
     @Service
19 | 1 |
     public class NamespaceService {
20 |  |
21 |  |
       @Autowired
22 |  |
       private NamespaceRepository namespaceRepository;
23 |  |
       @Autowired
24 |  |
       private AuditService auditService;
25 |  |
       @Autowired
26 |  |
       private AppNamespaceService appNamespaceService;
27 |  |
28 |  |
       public boolean isNamespaceUnique(String appId, String cluster, String namespace) {
29 | 0 |
         Objects.requireNonNull(appId, "AppId must not be null");
30 | 0 |
         Objects.requireNonNull(cluster, "Cluster must not be null");
31 | 0 |
         Objects.requireNonNull(namespace, "Namespace must not be null");
32 | 0 |
         return Objects.isNull(
33 | 0 |
             namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, cluster, namespace));
34 |  |
       }
35 |  |
36 |  |
       @Transactional
37 |  |
       public void delete(long id, String operator) {
38 | 0 |
         Namespace namespace = namespaceRepository.findOne(id);
39 | 0 |
         if (namespace == null) {
40 | 0 |
           return;
41 |  |
         }
42 |  |
43 | 0 |
         namespace.setDeleted(true);
44 | 0 |
         namespace.setDataChangeLastModifiedBy(operator);
45 | 0 |
         namespaceRepository.save(namespace);
46 |  |
47 | 0 |
         auditService.audit(Namespace.class.getSimpleName(), id, Audit.OP.DELETE, operator);
48 | 0 |
       }
49 |  |
50 |  |
       public Namespace findOne(Long namespaceId) {
51 | 0 |
         return namespaceRepository.findOne(namespaceId);
52 |  |
       }
53 |  |
54 |  |
       public Namespace findOne(String appId, String clusterName, String namespaceName) {
55 | 0 |
         return namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
56 |  |
             namespaceName);
57 |  |
       }
58 |  |
59 |  |
       public List<Namespace> findNamespaces(String appId, String clusterName) {
60 | 3 |
         List<Namespace> groups = namespaceRepository.findByAppIdAndClusterNameOrderByIdAsc(appId, clusterName);
61 | 3 |
         if (groups == null) {
62 | 0 |
           return Collections.emptyList();
63 |  |
         }
64 | 3 |
         return groups;
65 |  |
       }
66 |  |
67 |  |
       @Transactional
68 |  |
       public Namespace save(Namespace entity) {
69 | 0 |
         if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
70 | 0 |
           throw new ServiceException("namespace not unique");
71 |  |
         }
72 | 0 |
         entity.setId(0);//protection
73 | 0 |
         Namespace namespace = namespaceRepository.save(entity);
74 |  |
75 | 0 |
         auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT,
76 | 0 |
             namespace.getDataChangeCreatedBy());
77 |  |
78 | 0 |
         return namespace;
79 |  |
       }
80 |  |
81 |  |
       @Transactional
82 |  |
       public Namespace update(Namespace namespace) {
83 | 0 |
         Namespace managedNamespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(
84 | 0 |
             namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
85 | 0 |
         BeanUtils.copyEntityProperties(namespace, managedNamespace);
86 | 0 |
         managedNamespace = namespaceRepository.save(managedNamespace);
87 |  |
88 | 0 |
         auditService.audit(Namespace.class.getSimpleName(), managedNamespace.getId(), Audit.OP.UPDATE,
89 | 0 |
             managedNamespace.getDataChangeLastModifiedBy());
90 |  |
91 | 0 |
         return managedNamespace;
92 |  |
       }
93 |  |
94 |  |
       @Transactional
95 |  |
       public void createPrivateNamespace(String appId, String clusterName, String createBy) {
96 |  |
97 |  |
         //load all private app namespace
98 | 5 |
         List<AppNamespace> privateAppNamespaces = appNamespaceService.findPrivateAppNamespace(appId);
99 |  |
         //create all private namespace
100 | 5 |
         for (AppNamespace appNamespace: privateAppNamespaces){
101 | 5 |
           Namespace ns = new Namespace();
102 | 5 |
           ns.setAppId(appId);
103 | 5 |
           ns.setClusterName(clusterName);
104 | 5 |
           ns.setNamespaceName(appNamespace.getName());
105 | 5 |
           ns.setDataChangeCreatedBy(createBy);
106 | 5 |
           ns.setDataChangeLastModifiedBy(createBy);
107 | 5 |
           namespaceRepository.save(ns);
108 | 5 |
           auditService.audit(Namespace.class.getSimpleName(), ns.getId(), Audit.OP.INSERT, createBy);
109 | 5 |
         }
110 |  |
111 | 5 |
       }
112 |  |
     }