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

Classes in this File | Line Coverage | Branch Coverage | Complexity
AppNamespaceService |
43% | 14/32
12% | 1/8
| 1.5714285714285714;1.571

1 |  |
     package com.ctrip.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.apollo.biz.entity.AppNamespace;
13 |  |
     import com.ctrip.apollo.biz.entity.Audit;
14 |  |
     import com.ctrip.apollo.biz.repository.AppNamespaceRepository;
15 |  |
     import com.ctrip.apollo.common.utils.BeanUtils;
16 |  |
     import com.ctrip.apollo.core.ConfigConsts;
17 |  |
     import com.ctrip.apollo.core.exception.ServiceException;
18 |  |
     import com.ctrip.apollo.core.utils.StringUtils;
19 |  |
20 |  |
     @Service
21 | 1 |
     public class AppNamespaceService {
22 |  |
23 |  |
       @Autowired
24 |  |
       private AppNamespaceRepository appNamespaceRepository;
25 |  |
26 |  |
       @Autowired
27 |  |
       private AuditService auditService;
28 |  |
29 |  |
       public boolean isAppNamespaceNameUnique(String appId, String namespaceName) {
30 | 4 |
         Objects.requireNonNull(appId, "AppId must not be null");
31 | 4 |
         Objects.requireNonNull(namespaceName, "Namespace must not be null");
32 | 4 |
         return Objects.isNull(appNamespaceRepository.findByAppIdAndName(appId, namespaceName));
33 |  |
       }
34 |  |
35 |  |
       public AppNamespace findByNamespaceName(String namespaceName) {
36 | 0 |
         Preconditions.checkArgument(namespaceName != null, "Namespace must not be null");
37 | 0 |
         return appNamespaceRepository.findByName(namespaceName);
38 |  |
       }
39 |  |
40 |  |
       public AppNamespace findOne(String appId, String namespaceName){
41 | 0 |
         Preconditions.checkArgument(!StringUtils.isContainEmpty(appId, namespaceName), "appId or Namespace must not be null");
42 | 0 |
         return appNamespaceRepository.findByAppIdAndName(appId, namespaceName);
43 |  |
       }
44 |  |
45 |  |
       @Transactional
46 |  |
       public void createDefaultAppNamespace(String appId, String createBy) {
47 | 4 |
         if (!isAppNamespaceNameUnique(appId, appId)) {
48 | 0 |
           throw new ServiceException("appnamespace not unique");
49 |  |
         }
50 | 4 |
         AppNamespace appNs = new AppNamespace();
51 | 4 |
         appNs.setAppId(appId);
52 | 4 |
         appNs.setName(ConfigConsts.NAMESPACE_DEFAULT);
53 | 4 |
         appNs.setComment("default app namespace");
54 | 4 |
         appNs.setDataChangeCreatedBy(createBy);
55 | 4 |
         appNs.setDataChangeLastModifiedBy(createBy);
56 | 4 |
         appNamespaceRepository.save(appNs);
57 |  |
58 | 4 |
         auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,
59 |  |
                            createBy);
60 | 4 |
       }
61 |  |
62 |  |
       @Transactional
63 |  |
       public AppNamespace createAppNamespace(AppNamespace appNamespace, String createBy){
64 | 0 |
         if (!isAppNamespaceNameUnique(appNamespace.getAppId(), appNamespace.getName())) {
65 | 0 |
           throw new ServiceException("appnamespace not unique");
66 |  |
         }
67 | 0 |
         appNamespace.setDataChangeCreatedBy(createBy);
68 | 0 |
         appNamespace.setDataChangeLastModifiedBy(createBy);
69 | 0 |
          appNamespace = appNamespaceRepository.save(appNamespace);
70 |  |
71 | 0 |
         auditService.audit(AppNamespace.class.getSimpleName(), appNamespace.getId(), Audit.OP.INSERT,
72 |  |
                            createBy);
73 | 0 |
         return appNamespace;
74 |  |
       }
75 |  |
76 |  |
       public List<AppNamespace> findPublicAppNamespaces(){
77 | 0 |
         return appNamespaceRepository.findByNameNot(ConfigConsts.NAMESPACE_DEFAULT);
78 |  |
       }
79 |  |
80 |  |
       public AppNamespace update(AppNamespace appNamespace){
81 | 0 |
         AppNamespace managedNs = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
82 | 0 |
         BeanUtils.copyEntityProperties(appNamespace, managedNs);
83 | 0 |
         managedNs = appNamespaceRepository.save(managedNs);
84 |  |
85 | 0 |
         auditService.audit(AppNamespace.class.getSimpleName(), managedNs.getId(), Audit.OP.UPDATE, managedNs.getDataChangeLastModifiedBy());
86 |  |
87 | 0 |
         return managedNs;
88 |  |
       }
89 |  |
     }