##### Coverage Report - com.ctrip.apollo.portal.controller.AppController

Classes in this File | Line Coverage | Branch Coverage | Complexity
AppController |
2% | 1/40
0% | 0/18
| 3.5;3.5

1 |  |
     package com.ctrip.apollo.portal.controller;
2 |  |
3 |  |
     import org.springframework.beans.factory.annotation.Autowired;
4 |  |
     import org.springframework.http.HttpStatus;
5 |  |
     import org.springframework.http.ResponseEntity;
6 |  |
     import org.springframework.web.bind.annotation.PathVariable;
7 |  |
     import org.springframework.web.bind.annotation.RequestBody;
8 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
9 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
10 |  |
     import org.springframework.web.bind.annotation.RestController;
11 |  |
     import org.springframework.web.client.HttpClientErrorException;
12 |  |
13 |  |
     import com.ctrip.apollo.common.http.MultiResponseEntity;
14 |  |
     import com.ctrip.apollo.common.http.RichResponseEntity;
15 |  |
     import com.ctrip.apollo.core.dto.AppDTO;
16 |  |
     import com.ctrip.apollo.core.enums.Env;
17 |  |
     import com.ctrip.apollo.core.exception.BadRequestException;
18 |  |
     import com.ctrip.apollo.core.utils.StringUtils;
19 |  |
     import com.ctrip.apollo.portal.PortalSettings;
20 |  |
     import com.ctrip.apollo.portal.entity.EnvClusterInfo;
21 |  |
     import com.ctrip.apollo.portal.service.AppService;
22 |  |
23 |  |
     import java.util.List;
24 |  |
25 |  |
     @RestController
26 |  |
     @RequestMapping("/apps")
27 | 1 |
     public class AppController {
28 |  |
29 |  |
       @Autowired
30 |  |
       private AppService appService;
31 |  |
32 |  |
       @Autowired
33 |  |
       private PortalSettings portalSettings;
34 |  |
35 |  |
       @RequestMapping("/envs/{env}")
36 |  |
       public List<AppDTO> findAllApp(@PathVariable String env){
37 | 0 |
         if (StringUtils.isEmpty(env)){
38 | 0 |
           throw new BadRequestException("env can not be empty");
39 |  |
         }
40 | 0 |
         return appService.findAll(Env.valueOf(env));
41 |  |
       }
42 |  |
43 |  |
       @RequestMapping("/{appId}/navtree")
44 |  |
       public MultiResponseEntity<EnvClusterInfo> nav(@PathVariable String appId) {
45 |  |
46 | 0 |
         if (StringUtils.isEmpty(appId)) {
47 | 0 |
           throw new BadRequestException("app id can not be empty.");
48 |  |
         }
49 | 0 |
         MultiResponseEntity<EnvClusterInfo> response = MultiResponseEntity.ok();
50 | 0 |
         List<Env> envs = portalSettings.getEnvs();
51 | 0 |
         for (Env env : envs) {
52 |  |
           try {
53 | 0 |
             response.addResponseEntity(RichResponseEntity.ok(appService.createEnvNavNode(env, appId)));
54 | 0 |
           } catch (Exception e) {
55 | 0 |
             response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR,
56 | 0 |
                                                                 "load env:" + env.name() + " cluster error." + e
57 | 0 |
                                                                     .getMessage()));
58 | 0 |
           }
59 | 0 |
         }
60 | 0 |
         return response;
61 |  |
       }
62 |  |
63 |  |
       @RequestMapping(value = "/envs/{env}", method = RequestMethod.POST, consumes = {"application/json"})
64 |  |
       public ResponseEntity<Void> create(@PathVariable String env, @RequestBody AppDTO app) {
65 | 0 |
         if (isInvalidApp(app)){
66 | 0 |
           throw new BadRequestException("request payload contains empty");
67 |  |
         }
68 | 0 |
         if ("ALL".equals(env)){
69 | 0 |
           appService.createAppInAllEnvs(app);
70 |  |
         } else {
71 | 0 |
           appService.createApp(Env.valueOf(env), app);
72 |  |
         }
73 | 0 |
         return ResponseEntity.ok().build();
74 |  |
       }
75 |  |
76 |  |
       @RequestMapping(value = "/{appId}", method = RequestMethod.GET)
77 |  |
       public AppDTO load(@PathVariable String appId){
78 | 0 |
         if (StringUtils.isEmpty(appId)){
79 | 0 |
           throw new BadRequestException("app id can not be empty.");
80 |  |
         }
81 | 0 |
         return appService.load(appId);
82 |  |
       }
83 |  |
84 |  |
       @RequestMapping(value = "/{appId}/miss_envs")
85 |  |
       public MultiResponseEntity<Env> findMissEnvs(@PathVariable String appId) {
86 | 0 |
         MultiResponseEntity<Env> response = MultiResponseEntity.ok();
87 | 0 |
         for (Env env : portalSettings.getEnvs()) {
88 |  |
           try {
89 | 0 |
             appService.load(env, appId);
90 | 0 |
           } catch (Exception e) {
91 | 0 |
             if (e instanceof HttpClientErrorException &&
92 | 0 |
                 ((HttpClientErrorException)e).getStatusCode() == HttpStatus.NOT_FOUND){
93 | 0 |
               response.addResponseEntity(RichResponseEntity.ok(env));
94 |  |
             } else {
95 | 0 |
               response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR,
96 |  |
                                                                   String
97 | 0 |
                                                                       .format("load appId:%s from env %s error.", appId,
98 |  |
                                                                               env)
99 | 0 |
                                                                   + e.getMessage()));
100 |  |
             }
101 | 0 |
           }
102 |  |
103 | 0 |
         }
104 |  |
105 | 0 |
         return response;
106 |  |
107 |  |
       }
108 |  |
109 |  |
       private boolean isInvalidApp(AppDTO app) {
110 | 0 |
         return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
111 |  |
       }
112 |  |
     }
113 |  |