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

Classes in this File | Line Coverage | Branch Coverage | Complexity
AppNamespaceService |
33% | 16/48
8% | 1/12
| 1.75;1.75

1 |  |
     package com.ctrip.framework.apollo.biz.service;
2 |  |
3 |  |
     import com.google.common.base.Preconditions;
4 |  |
5 |  |
     import java.util.List;
6 |  |
     import java.util.Objects;
7 |  |
8 |  |
     import org.springframework.beans.factory.annotation.Autowired;
9 |  |
     import org.springframework.stereotype.Service;
10 |  |
     import org.springframework.transaction.annotation.Transactional;
11 |  |
12 |  |
     import com.ctrip.framework.apollo.biz.entity.Cluster;
13 |  |
     import com.ctrip.framework.apollo.biz.entity.Namespace;
14 |  |
     import com.ctrip.framework.apollo.common.entity.AppNamespace;
15 |  |
     import com.ctrip.framework.apollo.biz.entity.Audit;
16 |  |
     import com.ctrip.framework.apollo.biz.repository.AppNamespaceRepository;
17 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
18 |  |
     import com.ctrip.framework.apollo.core.ConfigConsts;
19 |  |
     import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
20 |  |
     import com.ctrip.framework.apollo.core.exception.ServiceException;
21 |  |
     import com.ctrip.framework.apollo.core.utils.StringUtils;
22 |  |
23 |  |
     @Service
24 | 1 |
     public class AppNamespaceService {
25 |  |
26 |  |
       @Autowired
27 |  |
       private AppNamespaceRepository appNamespaceRepository;
28 |  |
       @Autowired
29 |  |
       private NamespaceService namespaceService;
30 |  |
       @Autowired
31 |  |
       private ClusterService clusterService;
32 |  |
       @Autowired
33 |  |
       private AuditService auditService;
34 |  |
       @Autowired
35 |  |
       private ServerConfigService serverConfigService;
36 |  |
37 |  |
       public boolean isAppNamespaceNameUnique(String appId, String namespaceName) {
38 | 5 |
         Objects.requireNonNull(appId, "AppId must not be null");
39 | 5 |
         Objects.requireNonNull(namespaceName, "Namespace must not be null");
40 | 5 |
         return Objects.isNull(appNamespaceRepository.findByAppIdAndName(appId, namespaceName));
41 |  |
       }
42 |  |
43 |  |
       public AppNamespace findPublicNamespaceByName(String namespaceName) {
44 | 0 |
         Preconditions.checkArgument(namespaceName != null, "Namespace must not be null");
45 | 0 |
         return appNamespaceRepository.findByNameAndIsPublicTrue(namespaceName);
46 |  |
       }
47 |  |
48 |  |
       public List<AppNamespace> findPrivateAppNamespace(String appId){
49 | 5 |
         return appNamespaceRepository.findByAppIdAndIsPublic(appId, false);
50 |  |
       }
51 |  |
52 |  |
       public AppNamespace findOne(String appId, String namespaceName){
53 | 0 |
         Preconditions.checkArgument(!StringUtils.isContainEmpty(appId, namespaceName), "appId or Namespace must not be null");
54 | 0 |
         return appNamespaceRepository.findByAppIdAndName(appId, namespaceName);
55 |  |
       }
56 |  |
57 |  |
       @Transactional
58 |  |
       public void createDefaultAppNamespace(String appId, String createBy) {
59 | 5 |
         if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {
60 | 0 |
           throw new ServiceException("appnamespace not unique");
61 |  |
         }
62 | 5 |
         AppNamespace appNs = new AppNamespace();
63 | 5 |
         appNs.setAppId(appId);
64 | 5 |
         appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);
65 | 5 |
         appNs.setComment("default app namespace");
66 | 5 |
         appNs.setFormat(ConfigFileFormat.Properties.getValue());
67 | 5 |
         appNs.setDataChangeCreatedBy(createBy);
68 | 5 |
         appNs.setDataChangeLastModifiedBy(createBy);
69 | 5 |
         appNamespaceRepository.save(appNs);
70 |  |
71 | 5 |
         auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,
72 |  |
                            createBy);
73 | 5 |
       }
74 |  |
75 |  |
       @Transactional
76 |  |
       public AppNamespace createAppNamespace(AppNamespace appNamespace){
77 | 0 |
         String createBy = appNamespace.getDataChangeCreatedBy();
78 | 0 |
         if (!isAppNamespaceNameUnique(appNamespace.getAppId(), appNamespace.getName())) {
79 | 0 |
           throw new ServiceException("appnamespace not unique");
80 |  |
         }
81 | 0 |
         appNamespace.setId(0);//protection
82 | 0 |
         appNamespace.setDataChangeCreatedBy(createBy);
83 | 0 |
         appNamespace.setDataChangeLastModifiedBy(createBy);
84 | 0 |
         appNamespace = appNamespaceRepository.save(appNamespace);
85 |  |
86 | 0 |
         if (!appNamespace.isPublic()) {
87 | 0 |
           linkPrivateAppNamespaceInAllCluster(appNamespace.getAppId(), appNamespace.getName(), createBy);
88 |  |
         }
89 |  |
90 | 0 |
         auditService.audit(AppNamespace.class.getSimpleName(), appNamespace.getId(), Audit.OP.INSERT,
91 |  |
                            createBy);
92 | 0 |
         return appNamespace;
93 |  |
       }
94 |  |
95 |  |
       public AppNamespace update(AppNamespace appNamespace){
96 | 0 |
         AppNamespace managedNs = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
97 | 0 |
         BeanUtils.copyEntityProperties(appNamespace, managedNs);
98 | 0 |
         managedNs = appNamespaceRepository.save(managedNs);
99 |  |
100 | 0 |
         auditService.audit(AppNamespace.class.getSimpleName(), managedNs.getId(), Audit.OP.UPDATE, managedNs.getDataChangeLastModifiedBy());
101 |  |
102 | 0 |
         return managedNs;
103 |  |
       }
104 |  |
105 |  |
       private void linkPrivateAppNamespaceInAllCluster(String appId, String namespaceName, String createBy) {
106 | 0 |
         List<Cluster> clusters = clusterService.findClusters(appId);
107 | 0 |
         for (Cluster cluster : clusters) {
108 | 0 |
           Namespace namespace = new Namespace();
109 | 0 |
           namespace.setClusterName(cluster.getName());
110 | 0 |
           namespace.setAppId(appId);
111 | 0 |
           namespace.setNamespaceName(namespaceName);
112 | 0 |
           namespace.setDataChangeCreatedBy(createBy);
113 | 0 |
           namespace.setDataChangeLastModifiedBy(createBy);
114 | 0 |
           namespaceService.save(namespace);
115 | 0 |
         }
116 | 0 |
       }
117 |  |
     }