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

Classes in this File | Line Coverage | Branch Coverage | Complexity
PortalAppService |
5% | 2/38
0% | 0/10
| 3.0;3

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