Coverage Report - com.ctrip.framework.apollo.adminservice.controller.ClusterController
 
Classes in this File Line Coverage Branch Coverage Complexity
ClusterController
18%
4/22
12%
1/8
2.4
 
 1  
 package com.ctrip.framework.apollo.adminservice.controller;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.springframework.beans.factory.annotation.Autowired;
 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.RequestParam;
 11  
 import org.springframework.web.bind.annotation.RestController;
 12  
 
 13  
 import com.ctrip.framework.apollo.biz.entity.Cluster;
 14  
 import com.ctrip.framework.apollo.biz.service.ClusterService;
 15  
 import com.ctrip.framework.apollo.common.utils.BeanUtils;
 16  
 import com.ctrip.framework.apollo.common.utils.InputValidator;
 17  
 import com.ctrip.framework.apollo.core.dto.ClusterDTO;
 18  
 import com.ctrip.framework.apollo.core.exception.BadRequestException;
 19  
 import com.ctrip.framework.apollo.core.exception.NotFoundException;
 20  
 
 21  
 @RestController
 22  1
 public class ClusterController {
 23  
 
 24  
   @Autowired
 25  
   private ClusterService clusterService;
 26  
 
 27  
   @RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST)
 28  
   public ClusterDTO createOrUpdate(@PathVariable("appId") String appId, @RequestBody ClusterDTO dto) {
 29  0
     if (!InputValidator.isValidClusterNamespace(dto.getName())) {
 30  0
       throw new BadRequestException(String.format("Cluster格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
 31  
     }
 32  0
     Cluster entity = BeanUtils.transfrom(Cluster.class, dto);
 33  0
     Cluster managedEntity = clusterService.findOne(appId, entity.getName());
 34  0
     if (managedEntity != null) {
 35  0
       BeanUtils.copyEntityProperties(entity, managedEntity);
 36  0
       entity = clusterService.update(managedEntity);
 37  
     } else {
 38  0
       entity = clusterService.save(entity);
 39  
     }
 40  
 
 41  0
     dto = BeanUtils.transfrom(ClusterDTO.class, entity);
 42  0
     return dto;
 43  
   }
 44  
 
 45  
   @RequestMapping(path = "/apps/{appId}/clusters/{clusterName:.+}", method = RequestMethod.DELETE)
 46  
   public void delete(@PathVariable("appId") String appId,
 47  
       @PathVariable("clusterName") String clusterName, @RequestParam String operator) {
 48  0
     Cluster entity = clusterService.findOne(appId, clusterName);
 49  0
     if (entity == null)
 50  0
       throw new NotFoundException("cluster not found for clusterName " + clusterName);
 51  0
     clusterService.delete(entity.getId(), operator);
 52  0
   }
 53  
 
 54  
   @RequestMapping("/apps/{appId}/clusters")
 55  
   public List<ClusterDTO> find(@PathVariable("appId") String appId) {
 56  0
     List<Cluster> clusters = clusterService.findClusters(appId);
 57  0
     return BeanUtils.batchTransform(ClusterDTO.class, clusters);
 58  
   }
 59  
 
 60  
   @RequestMapping("/apps/{appId}/clusters/{clusterName:.+}")
 61  
   public ClusterDTO get(@PathVariable("appId") String appId,
 62  
       @PathVariable("clusterName") String clusterName) {
 63  4
     Cluster cluster = clusterService.findOne(appId, clusterName);
 64  4
     if (cluster == null) throw new NotFoundException("cluster not found for name " + clusterName);
 65  4
     return BeanUtils.transfrom(ClusterDTO.class, cluster);
 66  
   }
 67  
 
 68  
   @RequestMapping("/apps/{appId}/cluster/{clusterName}/unique")
 69  
   public boolean isAppIdUnique(@PathVariable("appId") String appId,
 70  
       @PathVariable("clusterName") String clusterName) {
 71  0
     return clusterService.isClusterNameUnique(appId, clusterName);
 72  
   }
 73  
 }