Coverage Report - com.ctrip.framework.apollo.adminservice.controller.AppController
 
Classes in this File Line Coverage Branch Coverage Complexity
AppController
95%
23/24
90%
9/10
2.6
 
 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.data.domain.Pageable;
 7  
 import org.springframework.web.bind.annotation.PathVariable;
 8  
 import org.springframework.web.bind.annotation.RequestBody;
 9  
 import org.springframework.web.bind.annotation.RequestMapping;
 10  
 import org.springframework.web.bind.annotation.RequestMethod;
 11  
 import org.springframework.web.bind.annotation.RequestParam;
 12  
 import org.springframework.web.bind.annotation.RestController;
 13  
 
 14  
 import com.ctrip.framework.apollo.common.entity.App;
 15  
 import com.ctrip.framework.apollo.biz.service.AdminService;
 16  
 import com.ctrip.framework.apollo.biz.service.AppService;
 17  
 import com.ctrip.framework.apollo.common.utils.BeanUtils;
 18  
 import com.ctrip.framework.apollo.common.utils.InputValidator;
 19  
 import com.ctrip.framework.apollo.core.dto.AppDTO;
 20  
 import com.ctrip.framework.apollo.core.exception.BadRequestException;
 21  
 import com.ctrip.framework.apollo.core.exception.NotFoundException;
 22  
 import com.ctrip.framework.apollo.core.utils.StringUtils;
 23  
 
 24  
 @RestController
 25  6
 public class AppController {
 26  
 
 27  
   @Autowired
 28  
   private AppService appService;
 29  
 
 30  
   @Autowired
 31  
   private AdminService adminService;
 32  
 
 33  
   @RequestMapping(path = "/apps", method = RequestMethod.POST)
 34  
   public AppDTO createOrUpdate(@RequestBody AppDTO dto) {
 35  7
     if (!InputValidator.isValidClusterNamespace(dto.getAppId())) {
 36  0
       throw new BadRequestException(String.format("AppId格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
 37  
     }
 38  7
     App entity = BeanUtils.transfrom(App.class, dto);
 39  7
     App managedEntity = appService.findOne(entity.getAppId());
 40  7
     if (managedEntity != null) {
 41  3
       BeanUtils.copyEntityProperties(entity, managedEntity);
 42  3
       entity = appService.update(managedEntity);
 43  
     } else {
 44  4
       entity = adminService.createNewApp(entity);
 45  
     }
 46  
 
 47  5
     dto = BeanUtils.transfrom(AppDTO.class, entity);
 48  5
     return dto;
 49  
   }
 50  
 
 51  
   @RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE)
 52  
   public void delete(@PathVariable("appId") String appId, @RequestParam String operator) {
 53  2
     App entity = appService.findOne(appId);
 54  2
     if (entity == null) throw new NotFoundException("app not found for appId " + appId);
 55  1
     appService.delete(entity.getId(), operator);
 56  1
   }
 57  
 
 58  
   @RequestMapping("/apps")
 59  
   public List<AppDTO> find(@RequestParam(value = "name", required = false) String name,
 60  
       Pageable pageable) {
 61  3
     List<App> app = null;
 62  3
     if (StringUtils.isBlank(name)) {
 63  2
       app = appService.findAll(pageable);
 64  
     } else {
 65  1
       app = appService.findByName(name);
 66  
     }
 67  3
     return BeanUtils.batchTransform(AppDTO.class, app);
 68  
   }
 69  
 
 70  
   @RequestMapping("/apps/{appId}")
 71  
   public AppDTO get(@PathVariable("appId") String appId) {
 72  7
     App app = appService.findOne(appId);
 73  7
     if (app == null) throw new NotFoundException("app not found for appId " + appId);
 74  5
     return BeanUtils.transfrom(AppDTO.class, app);
 75  
   }
 76  
 
 77  
   @RequestMapping("/apps/{appId}/unique")
 78  
   public boolean isAppIdUnique(@PathVariable("appId") String appId) {
 79  2
     return appService.isAppIdUnique(appId);
 80  
   }
 81  
 
 82  
 }