Commit a8a4a0d3 authored by Jason Song's avatar Jason Song Committed by GitHub

Merge pull request #1586 from zouyx/master

Block delete default cluster
parents 2dbb8112 ee136b7d
...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.common.exception.NotFoundException; ...@@ -8,6 +8,7 @@ import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils; import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.InputValidator; import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.core.ConfigConsts;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
...@@ -51,10 +52,17 @@ public class ClusterController { ...@@ -51,10 +52,17 @@ public class ClusterController {
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName:.+}", method = RequestMethod.DELETE) @RequestMapping(path = "/apps/{appId}/clusters/{clusterName:.+}", method = RequestMethod.DELETE)
public void delete(@PathVariable("appId") String appId, public void delete(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @RequestParam String operator) { @PathVariable("clusterName") String clusterName, @RequestParam String operator) {
Cluster entity = clusterService.findOne(appId, clusterName); Cluster entity = clusterService.findOne(appId, clusterName);
if (entity == null) { if (entity == null) {
throw new NotFoundException("cluster not found for clusterName " + clusterName); throw new NotFoundException("cluster not found for clusterName " + clusterName);
} }
if(ConfigConsts.CLUSTER_NAME_DEFAULT.equals(entity.getName())){
throw new BadRequestException("default cluster can not be delete.");
}
clusterService.delete(entity.getId(), operator); clusterService.delete(entity.getId(), operator);
} }
......
package com.ctrip.framework.apollo.adminservice.controller;
import com.ctrip.framework.apollo.biz.entity.Cluster;
import com.ctrip.framework.apollo.biz.service.ClusterService;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.ConfigConsts;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
public class ClusterControllerTest{
ClusterController clusterController=new ClusterController();
@Mock
ClusterService clusterService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(clusterController, "clusterService", clusterService);
}
@Test(expected = BadRequestException.class)
public void testDeleteDefaultFail() {
Cluster cluster=new Cluster();
cluster.setName(ConfigConsts.CLUSTER_NAME_DEFAULT);
when(clusterService.findOne(any(String.class), any(String.class))).thenReturn(cluster);
clusterController.delete("1","2","d");
}
@Test()
public void testDeleteSuccess() {
Cluster cluster=new Cluster();
when(clusterService.findOne(any(String.class), any(String.class))).thenReturn(cluster);
clusterController.delete("1","2","d");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment