##### Coverage Report - com.ctrip.framework.apollo.portal.controller.PortalConfigController

Classes in this File | Line Coverage | Branch Coverage | Complexity
PortalConfigController |
2% | 1/49
0% | 0/34
| 4.333333333333333;4.333

1 |  |
     package com.ctrip.framework.apollo.portal.controller;
2 |  |
3 |  |
4 |  |
     import com.ctrip.framework.apollo.core.dto.ItemDTO;
5 |  |
     import com.ctrip.framework.apollo.core.enums.Env;
6 |  |
     import com.ctrip.framework.apollo.core.dto.ReleaseDTO;
7 |  |
     import com.ctrip.framework.apollo.core.exception.BadRequestException;
8 |  |
     import com.ctrip.framework.apollo.core.utils.StringUtils;
9 |  |
     import com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs;
10 |  |
     import com.ctrip.framework.apollo.portal.entity.form.NamespaceSyncModel;
11 |  |
     import com.ctrip.framework.apollo.portal.entity.form.NamespaceTextModel;
12 |  |
     import com.ctrip.framework.apollo.portal.entity.form.NamespaceReleaseModel;
13 |  |
     import com.ctrip.framework.apollo.portal.service.PortalConfigService;
14 |  |
15 |  |
     import org.springframework.beans.factory.annotation.Autowired;
16 |  |
     import org.springframework.http.HttpStatus;
17 |  |
     import org.springframework.http.ResponseEntity;
18 |  |
     import org.springframework.web.bind.annotation.PathVariable;
19 |  |
     import org.springframework.web.bind.annotation.RequestBody;
20 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
21 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
22 |  |
     import org.springframework.web.bind.annotation.RestController;
23 |  |
24 |  |
     import java.util.List;
25 |  |
26 |  |
     @RestController
27 |  |
     @RequestMapping("")
28 | 1 |
     public class PortalConfigController {
29 |  |
30 |  |
       @Autowired
31 |  |
       private PortalConfigService configService;
32 |  |
33 |  |
       @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = {
34 |  |
           "application/json"})
35 |  |
       public void modifyItems(@PathVariable String appId, @PathVariable String env,
36 |  |
           @PathVariable String clusterName, @PathVariable String namespaceName,
37 |  |
           @RequestBody NamespaceTextModel model) {
38 |  |
39 | 0 |
         if (model == null) {
40 | 0 |
           throw new BadRequestException("request payload should not be null");
41 |  |
         }
42 | 0 |
         model.setAppId(appId);
43 | 0 |
         model.setClusterName(clusterName);
44 | 0 |
         model.setEnv(env);
45 | 0 |
         model.setNamespaceName(namespaceName);
46 |  |
47 | 0 |
         if (model.isInvalid()) {
48 | 0 |
           throw new BadRequestException("request model is invalid");
49 |  |
         }
50 |  |
51 | 0 |
         configService.updateConfigItemByText(model);
52 | 0 |
       }
53 |  |
54 |  |
       @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.POST)
55 |  |
       public ItemDTO createItem(@PathVariable String appId, @PathVariable String env,
56 |  |
                                 @PathVariable String clusterName, @PathVariable String namespaceName,
57 |  |
                                 @RequestBody ItemDTO item){
58 | 0 |
         if (StringUtils.isContainEmpty(appId, env, clusterName, namespaceName)){
59 | 0 |
           throw new BadRequestException("request payload should not be contain empty.");
60 |  |
         }
61 | 0 |
         if (!isValidItem(item) && item.getNamespaceId() <= 0){
62 | 0 |
           throw new BadRequestException("request model is invalid");
63 |  |
         }
64 | 0 |
         return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item);
65 |  |
       }
66 |  |
67 |  |
       @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.PUT)
68 |  |
       public ItemDTO updateItem(@PathVariable String appId, @PathVariable String env,
69 |  |
                                 @PathVariable String clusterName, @PathVariable String namespaceName,
70 |  |
                                 @RequestBody ItemDTO item){
71 | 0 |
         if (StringUtils.isContainEmpty(appId, env, clusterName, namespaceName)){
72 | 0 |
           throw new BadRequestException("request payload should not be contain empty.");
73 |  |
         }
74 | 0 |
         if (!isValidItem(item)){
75 | 0 |
           throw new BadRequestException("request model is invalid");
76 |  |
         }
77 | 0 |
         return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item);
78 |  |
       }
79 |  |
80 |  |
       @RequestMapping(value = "/envs/{env}/items/{itemId}", method = RequestMethod.DELETE)
81 |  |
       public void deleteItem(@PathVariable String env, @PathVariable long itemId){
82 | 0 |
         if (itemId <= 0){
83 | 0 |
           throw new BadRequestException("item id invalid");
84 |  |
         }
85 | 0 |
         configService.deleteItem(Env.valueOf(env), itemId);
86 | 0 |
       }
87 |  |
88 |  |
       @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = {
89 |  |
           "application/json"})
90 |  |
       public ReleaseDTO createRelease(@PathVariable String appId,
91 |  |
                                       @PathVariable String env, @PathVariable String clusterName,
92 |  |
                                       @PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) {
93 | 0 |
         if (model == null) {
94 | 0 |
           throw new BadRequestException("request payload should not be null");
95 |  |
         }
96 | 0 |
         model.setAppId(appId);
97 | 0 |
         model.setClusterName(clusterName);
98 | 0 |
         model.setEnv(env);
99 | 0 |
         model.setNamespaceName(namespaceName);
100 |  |
101 | 0 |
         if (model.isInvalid()) {
102 | 0 |
           throw new BadRequestException("request model is invalid");
103 |  |
         }
104 |  |
105 | 0 |
         return configService.createRelease(model);
106 |  |
107 |  |
       }
108 |  |
109 |  |
       @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items")
110 |  |
       public List<ItemDTO> findItems(@PathVariable String appId, @PathVariable String env,
111 |  |
                                      @PathVariable String clusterName, @PathVariable String namespaceName){
112 |  |
113 | 0 |
         if (StringUtils.isContainEmpty(appId, env, clusterName, namespaceName)){
114 | 0 |
           throw new BadRequestException("appid,env,cluster name,namespace name can not be empty");
115 |  |
         }
116 |  |
117 | 0 |
         return configService.findItems(appId, Env.valueOf(env), clusterName, namespaceName);
118 |  |
       }
119 |  |
120 |  |
       @RequestMapping(value = "/namespaces/{namespaceName}/diff", method = RequestMethod.POST, consumes = {
121 |  |
           "application/json"})
122 |  |
       public List<ItemDiffs> diff(@RequestBody NamespaceSyncModel model){
123 | 0 |
         if (model == null){
124 | 0 |
           throw new BadRequestException("request payload should not be null");
125 |  |
         }
126 | 0 |
         if (model.isInvalid()) {
127 | 0 |
           throw new BadRequestException("request model is invalid");
128 |  |
         }
129 |  |
130 | 0 |
         return configService.compare(model.getSyncToNamespaces(), model.getSyncItems());
131 |  |
       }
132 |  |
133 |  |
       @RequestMapping(value = "/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = {
134 |  |
           "application/json"})
135 |  |
       public ResponseEntity<Void> update(@RequestBody NamespaceSyncModel model){
136 | 0 |
         if (model == null){
137 | 0 |
           throw new BadRequestException("request payload should not be null");
138 |  |
         }
139 | 0 |
         if (model.isInvalid()) {
140 | 0 |
           throw new BadRequestException("request model is invalid");
141 |  |
         }
142 | 0 |
         configService.syncItems(model.getSyncToNamespaces(), model.getSyncItems());
143 | 0 |
         return ResponseEntity.status(HttpStatus.OK).build();
144 |  |
       }
145 |  |
146 |  |
       private boolean isValidItem(ItemDTO item){
147 | 0 |
         return item != null && !StringUtils.isContainEmpty(item.getKey(), item.getValue());
148 |  |
       }
149 |  |
150 |  |
     }