##### Coverage Report - com.ctrip.apollo.portal.service.NamespaceService

Classes in this File | Line Coverage | Branch Coverage | Complexity
NamespaceService |
69% | 43/62
72% | 16/22
| 4.0;4

1 |  |
     package com.ctrip.apollo.portal.service;
2 |  |
3 |  |
     import com.google.gson.Gson;
4 |  |
5 |  |
     import com.ctrip.apollo.common.utils.ExceptionUtils;
6 |  |
     import com.ctrip.apollo.core.dto.AppNamespaceDTO;
7 |  |
     import com.ctrip.apollo.core.dto.ItemDTO;
8 |  |
     import com.ctrip.apollo.core.dto.NamespaceDTO;
9 |  |
     import com.ctrip.apollo.core.dto.ReleaseDTO;
10 |  |
     import com.ctrip.apollo.core.enums.Env;
11 |  |
     import com.ctrip.apollo.core.utils.StringUtils;
12 |  |
     import com.ctrip.apollo.portal.PortalSettings;
13 |  |
     import com.ctrip.apollo.portal.api.AdminServiceAPI;
14 |  |
     import com.ctrip.apollo.portal.entity.NamespaceVO;
15 |  |
16 |  |
     import org.slf4j.Logger;
17 |  |
     import org.slf4j.LoggerFactory;
18 |  |
     import org.springframework.beans.factory.annotation.Autowired;
19 |  |
     import org.springframework.http.HttpStatus;
20 |  |
     import org.springframework.stereotype.Service;
21 |  |
     import org.springframework.web.client.HttpClientErrorException;
22 |  |
     import org.springframework.web.client.HttpStatusCodeException;
23 |  |
24 |  |
     import java.util.Collections;
25 |  |
     import java.util.HashMap;
26 |  |
     import java.util.LinkedList;
27 |  |
     import java.util.List;
28 |  |
     import java.util.Map;
29 |  |
30 |  |
     @Service
31 | 2 |
     public class NamespaceService {
32 |  |
33 | 2 |
       private Logger logger = LoggerFactory.getLogger(NamespaceService.class);
34 |  |
35 |  |
       @Autowired
36 |  |
       private AdminServiceAPI.ItemAPI itemAPI;
37 |  |
38 |  |
       @Autowired
39 |  |
       private AdminServiceAPI.ReleaseAPI releaseAPI;
40 |  |
41 |  |
       @Autowired
42 |  |
       private AdminServiceAPI.NamespaceAPI namespaceAPI;
43 |  |
44 |  |
       @Autowired
45 |  |
       private PortalSettings portalSettings;
46 |  |
47 | 2 |
       private Gson gson = new Gson();
48 |  |
49 |  |
50 |  |
       public List<AppNamespaceDTO> findPublicAppNamespaces(){
51 | 0 |
         return namespaceAPI.findPublicAppNamespaces(portalSettings.getFirstEnv());
52 |  |
       }
53 |  |
54 |  |
       public NamespaceDTO createNamespace(Env env, NamespaceDTO namespace){
55 | 0 |
         return namespaceAPI.createNamespace(env, namespace);
56 |  |
       }
57 |  |
58 |  |
       public void createAppNamespace(AppNamespaceDTO appNamespace) {
59 | 0 |
         for (Env env : portalSettings.getEnvs()) {
60 |  |
           try {
61 | 0 |
             namespaceAPI.createAppNamespace(env, appNamespace);
62 | 0 |
           } catch (HttpStatusCodeException e) {
63 | 0 |
             logger.error(ExceptionUtils.toString(e));
64 | 0 |
             throw e;
65 | 0 |
           }
66 | 0 |
         }
67 | 0 |
       }
68 |  |
69 |  |
       /**
70 |  |
        * load cluster all namespace info with items
71 |  |
        */
72 |  |
       public List<NamespaceVO> findNampspaces(String appId, Env env, String clusterName) {
73 |  |
74 | 1 |
         List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
75 | 1 |
         if (namespaces == null || namespaces.size() == 0) {
76 | 0 |
           return Collections.emptyList();
77 |  |
         }
78 |  |
79 | 1 |
         List<NamespaceVO> namespaceVOs = new LinkedList<>();
80 | 1 |
         for (NamespaceDTO namespace : namespaces) {
81 |  |
82 | 2 |
           NamespaceVO namespaceVO = null;
83 |  |
           try {
84 | 2 |
             namespaceVO = parseNamespace(appId, env, clusterName, namespace);
85 | 2 |
             namespaceVOs.add(namespaceVO);
86 | 0 |
           } catch (Exception e) {
87 | 0 |
             logger.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
88 | 0 |
                          appId, env, clusterName, namespace.getNamespaceName(), e);
89 | 0 |
             throw e;
90 | 2 |
           }
91 | 2 |
         }
92 |  |
93 | 1 |
         return namespaceVOs;
94 |  |
       }
95 |  |
96 |  |
       @SuppressWarnings("unchecked")
97 |  |
       private NamespaceVO parseNamespace(String appId, Env env, String clusterName, NamespaceDTO namespace) {
98 | 2 |
         NamespaceVO namespaceVO = new NamespaceVO();
99 | 2 |
         namespaceVO.setNamespace(namespace);
100 |  |
101 | 2 |
         List<NamespaceVO.ItemVO> itemVos = new LinkedList<>();
102 | 2 |
         namespaceVO.setItems(itemVos);
103 |  |
104 | 2 |
         String namespaceName = namespace.getNamespaceName();
105 |  |
106 |  |
         //latest Release
107 | 2 |
         ReleaseDTO release = null;
108 | 2 |
         Map<String, String> releaseItems = new HashMap<>();
109 |  |
         try {
110 | 2 |
           release = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
111 | 2 |
           releaseItems = gson.fromJson(release.getConfigurations(), Map.class);
112 | 0 |
         } catch (HttpClientErrorException e) {
113 | 0 |
           if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
114 | 0 |
             logger.warn(ExceptionUtils.toString(e));
115 |  |
           } else {
116 | 0 |
             throw e;
117 |  |
           }
118 | 2 |
         }
119 |  |
120 |  |
         //not Release config items
121 | 2 |
         List<ItemDTO> items = itemAPI.findItems(appId, env, clusterName, namespaceName);
122 | 2 |
         int modifiedItemCnt = 0;
123 | 2 |
         for (ItemDTO itemDTO : items) {
124 |  |
125 | 4 |
           NamespaceVO.ItemVO itemVO = parseItemVO(itemDTO, releaseItems);
126 |  |
127 | 4 |
           if (itemVO.isModified()) {
128 | 2 |
             modifiedItemCnt++;
129 |  |
           }
130 |  |
131 | 4 |
           itemVos.add(itemVO);
132 | 4 |
         }
133 | 2 |
         namespaceVO.setItemModifiedCnt(modifiedItemCnt);
134 |  |
135 | 2 |
         return namespaceVO;
136 |  |
       }
137 |  |
138 |  |
       private NamespaceVO.ItemVO parseItemVO(ItemDTO itemDTO, Map<String, String> releaseItems) {
139 | 4 |
         String key = itemDTO.getKey();
140 | 4 |
         NamespaceVO.ItemVO itemVO = new NamespaceVO.ItemVO();
141 | 4 |
         itemVO.setItem(itemDTO);
142 | 4 |
         String newValue = itemDTO.getValue();
143 | 4 |
         String oldValue = releaseItems.get(key);
144 | 4 |
         if (!StringUtils.isEmpty(key) && (oldValue == null || !newValue.equals(oldValue))) {
145 | 2 |
           itemVO.setModified(true);
146 | 2 |
           itemVO.setOldValue(oldValue == null ? "" : oldValue);
147 | 2 |
           itemVO.setNewValue(newValue);
148 |  |
         }
149 | 4 |
         return itemVO;
150 |  |
       }
151 |  |
152 |  |
     }