Coverage Report - com.ctrip.framework.apollo.adminservice.controller.NamespaceController
 
Classes in this File Line Coverage Branch Coverage Complexity
NamespaceController
15%
4/26
10%
1/10
2.8
 
 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.Namespace;
 14  
 import com.ctrip.framework.apollo.biz.service.NamespaceService;
 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.NamespaceDTO;
 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 NamespaceController {
 23  
 
 24  
   @Autowired
 25  
   private NamespaceService namespaceService;
 26  
 
 27  
   @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
 28  
   public NamespaceDTO createOrUpdate(@PathVariable("appId") String appId,
 29  
                                      @PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
 30  0
     if (!InputValidator.isValidClusterNamespace(dto.getNamespaceName())) {
 31  0
       throw new BadRequestException(String.format("Namespace格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
 32  
     }
 33  0
     Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
 34  0
     Namespace managedEntity = namespaceService.findOne(appId, clusterName, entity.getNamespaceName());
 35  0
     if (managedEntity != null) {
 36  0
       BeanUtils.copyEntityProperties(entity, managedEntity);
 37  0
       entity = namespaceService.update(managedEntity);
 38  
     } else {
 39  0
       entity = namespaceService.save(entity);
 40  
     }
 41  
 
 42  0
     dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
 43  0
     return dto;
 44  
   }
 45  
 
 46  
   @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}", method = RequestMethod.DELETE)
 47  
   public void delete(@PathVariable("appId") String appId,
 48  
       @PathVariable("clusterName") String clusterName,
 49  
       @PathVariable("namespaceName") String namespaceName, @RequestParam String operator) {
 50  0
     Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
 51  0
     if (entity == null) throw new NotFoundException(
 52  0
         String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
 53  0
     namespaceService.delete(entity.getId(), operator);
 54  0
   }
 55  
 
 56  
   @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
 57  
   public List<NamespaceDTO> find(@PathVariable("appId") String appId,
 58  
       @PathVariable("clusterName") String clusterName) {
 59  0
     List<Namespace> groups = namespaceService.findNamespaces(appId, clusterName);
 60  0
     return BeanUtils.batchTransform(NamespaceDTO.class, groups);
 61  
   }
 62  
 
 63  
   @RequestMapping("/namespaces/{namespaceId}")
 64  
   public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
 65  0
     Namespace namespace = namespaceService.findOne(namespaceId);
 66  0
     if (namespace == null)
 67  0
       throw new NotFoundException(String.format("namespace not found for %s", namespaceId));
 68  0
     return BeanUtils.transfrom(NamespaceDTO.class, namespace);
 69  
   }
 70  
 
 71  
   @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}")
 72  
   public NamespaceDTO get(@PathVariable("appId") String appId,
 73  
       @PathVariable("clusterName") String clusterName,
 74  
       @PathVariable("namespaceName") String namespaceName) {
 75  4
     Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
 76  4
     if (namespace == null) throw new NotFoundException(
 77  0
         String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
 78  4
     return BeanUtils.transfrom(NamespaceDTO.class, namespace);
 79  
   }
 80  
 
 81  
 }