##### Coverage Report - com.ctrip.framework.apollo.portal.service.PortalAppService

Classes in this File | Line Coverage | Branch Coverage | Complexity
PortalAppService |
22% | 10/44
10% | 1/10
| 2.7142857142857144;2.714

1 |  |
     package com.ctrip.framework.apollo.portal.service;
2 |  |
3 |  |
     import java.util.List;
4 |  |
5 |  |
     import org.slf4j.Logger;
6 |  |
     import org.slf4j.LoggerFactory;
7 |  |
     import org.springframework.beans.factory.annotation.Autowired;
8 |  |
     import org.springframework.http.HttpStatus;
9 |  |
     import org.springframework.stereotype.Service;
10 |  |
     import org.springframework.web.client.HttpClientErrorException;
11 |  |
     import org.springframework.web.client.HttpStatusCodeException;
12 |  |
13 |  |
     import com.ctrip.framework.apollo.common.utils.ExceptionUtils;
14 |  |
     import com.ctrip.framework.apollo.core.dto.AppDTO;
15 |  |
     import com.ctrip.framework.apollo.core.enums.Env;
16 |  |
     import com.ctrip.framework.apollo.core.exception.BadRequestException;
17 |  |
     import com.ctrip.framework.apollo.core.exception.ServiceException;
18 |  |
     import com.ctrip.framework.apollo.portal.PortalSettings;
19 |  |
     import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
20 |  |
     import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
21 |  |
     import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
22 |  |
23 |  |
     @Service
24 | 1 |
     public class PortalAppService {
25 |  |
26 | 1 |
       private Logger logger = LoggerFactory.getLogger(PortalAppService.class);
27 |  |
28 |  |
       @Autowired
29 |  |
       private UserInfoHolder userInfoHolder;
30 |  |
31 |  |
       @Autowired
32 |  |
       private PortalClusterService clusterService;
33 |  |
34 |  |
       @Autowired
35 |  |
       private PortalSettings portalSettings;
36 |  |
37 |  |
       @Autowired
38 |  |
       private AdminServiceAPI.AppAPI appAPI;
39 |  |
40 |  |
       public List<AppDTO> findAll(Env env) {
41 | 0 |
         return appAPI.findApps(env);
42 |  |
       }
43 |  |
44 |  |
       public AppDTO load(String appId) {
45 |  |
         //轮询环境直到能找到此app的信息
46 | 0 |
         AppDTO app = null;
47 | 0 |
         boolean isCallAdminServiceError = false;
48 | 0 |
         for (Env env : portalSettings.getActiveEnvs()) {
49 |  |
           try {
50 | 0 |
             app = appAPI.loadApp(env, appId);
51 | 0 |
             break;
52 | 0 |
           } catch (HttpClientErrorException e) {
53 |  |
             //not exist maybe because create app fail.
54 | 0 |
             if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
55 | 0 |
               logger.warn("app:{} in {} not exist", appId, env);
56 |  |
             } else {
57 | 0 |
               isCallAdminServiceError = true;
58 | 0 |
               logger.error("load app info({}) from env:{} error.", appId, env);
59 |  |
             }
60 |  |
           }
61 | 0 |
         }
62 | 0 |
         if (app == null) {
63 | 0 |
           if (isCallAdminServiceError){
64 | 0 |
             throw new ServiceException("call admin service error");
65 |  |
           }else {
66 | 0 |
             throw new BadRequestException(String.format("invalid app id %s", appId));
67 |  |
           }
68 |  |
         }
69 |  |
70 | 0 |
         return app;
71 |  |
       }
72 |  |
73 |  |
       public AppDTO load(Env env, String appId){
74 | 0 |
         return appAPI.loadApp(env, appId);
75 |  |
       }
76 |  |
77 |  |
       public void createAppInAllEnvs(AppDTO app) {
78 | 1 |
         enrichUserInfo(app);
79 | 1 |
         List<Env> envs = portalSettings.getActiveEnvs();
80 | 1 |
         for (Env env : envs) {
81 |  |
           try {
82 | 0 |
             appAPI.createApp(env, app);
83 | 0 |
           } catch (HttpStatusCodeException e) {
84 | 0 |
             logger.error(ExceptionUtils.toString(e));
85 | 0 |
             throw e;
86 | 0 |
           }
87 | 0 |
         }
88 | 1 |
       }
89 |  |
90 |  |
       public void createApp(Env env, AppDTO app) {
91 | 0 |
         enrichUserInfo(app);
92 |  |
         try {
93 | 0 |
           appAPI.createApp(env, app);
94 | 0 |
         } catch (HttpStatusCodeException e) {
95 | 0 |
           logger.error(ExceptionUtils.toString(e));
96 | 0 |
           throw e;
97 | 0 |
         }
98 | 0 |
       }
99 |  |
100 |  |
       private void enrichUserInfo(AppDTO app){
101 | 1 |
         String username = userInfoHolder.getUser().getUserId();
102 | 1 |
         app.setDataChangeCreatedBy(username);
103 | 1 |
         app.setDataChangeLastModifiedBy(username);
104 | 1 |
       }
105 |  |
106 |  |
       public EnvClusterInfo createEnvNavNode(Env env, String appId){
107 | 0 |
         EnvClusterInfo node = new EnvClusterInfo(env);
108 | 0 |
         node.setClusters(clusterService.findClusters(env, appId));
109 | 0 |
         return node;
110 |  |
       }
111 |  |
112 |  |
     }