-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...rc/test/java/com/vip/saturn/job/console/springboot/test/RegistryCenterControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.vip.saturn.job.console.springboot.test; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.vip.saturn.job.console.AbstractSaturnConsoleTest; | ||
import com.vip.saturn.job.console.controller.rest.AlarmRestApiController; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebMvcTest(AlarmRestApiController.class) | ||
public class RegistryCenterControllerTest extends AbstractSaturnConsoleTest { | ||
|
||
@Autowired | ||
private MockMvc mvc; | ||
|
||
@Test | ||
public void testCreateAndUpdateZkClusterInfo() throws Exception { | ||
// craete a new zkCluster | ||
ZkClusterInfoForTest zkClusterInfo = new ZkClusterInfoForTest("clusterx", "alias1", "127.0.0.1:2181"); | ||
MvcResult result = mvc | ||
.perform(post("/console/zkClusters").contentType(MediaType.APPLICATION_FORM_URLENCODED).content(zkClusterInfo.toContent())) | ||
.andExpect(status().isOk()).andReturn(); | ||
String responseBody = result.getResponse().getContentAsString(); | ||
Map<String, Object> resultMap = JSONObject.parseObject(responseBody, Map.class); | ||
assertEquals(0, resultMap.get("status")); | ||
|
||
// refresh | ||
mvc.perform(get("/console/registryCenter/refresh")).andExpect(status().isOk()).andReturn(); | ||
// get and compare | ||
int size; | ||
int count = 0; | ||
List<Map<String, String>> objValue; | ||
do { | ||
Thread.sleep(3000L); | ||
result = mvc.perform(get("/console/zkClusters")).andExpect(status().isOk()).andReturn(); | ||
responseBody = result.getResponse().getContentAsString(); | ||
resultMap = JSONObject.parseObject(responseBody, Map.class); | ||
objValue = (List<Map<String, String>>) resultMap.get("obj"); | ||
size = objValue.size(); | ||
} while (size == 1 && count++ < 6); | ||
|
||
assertEquals(2, size); | ||
|
||
String connectionString = ""; | ||
for (Map<String, String> clusterInfo : objValue) { | ||
String clusterKey = clusterInfo.get("zkClusterKey"); | ||
if (clusterKey.equals("clusterx")) { | ||
connectionString = clusterInfo.get("zkAddr"); | ||
break; | ||
} | ||
} | ||
|
||
assertEquals("127.0.0.1:2181", connectionString); | ||
|
||
// update | ||
zkClusterInfo = new ZkClusterInfoForTest("clusterx", "alias1", "127.0.0.1:2182 "); | ||
result = mvc.perform(post("/console/zkClusters").contentType(MediaType.APPLICATION_FORM_URLENCODED).content(zkClusterInfo.toContent())) | ||
.andExpect(status().isOk()).andReturn(); | ||
responseBody = result.getResponse().getContentAsString(); | ||
resultMap = JSONObject.parseObject(responseBody, Map.class); | ||
assertEquals(0, resultMap.get("status")); | ||
|
||
// refresh | ||
mvc.perform(get("/console/registryCenter/refresh")).andExpect(status().isOk()).andReturn(); | ||
// get and compare | ||
count = 0; | ||
do { | ||
Thread.sleep(3000L); | ||
result = mvc.perform(get("/console/zkClusters")).andExpect(status().isOk()).andReturn(); | ||
responseBody = result.getResponse().getContentAsString(); | ||
resultMap = JSONObject.parseObject(responseBody, Map.class); | ||
objValue = (List<Map<String, String>>) resultMap.get("obj"); | ||
size = objValue.size(); | ||
for (Map<String, String> clusterInfo : objValue) { | ||
String clusterKey = clusterInfo.get("zkClusterKey"); | ||
if (clusterKey.equals("clusterx")) { | ||
connectionString = clusterInfo.get("zkAddr"); | ||
break; | ||
} | ||
} | ||
} while (!connectionString.equals("127.0.0.1:2182") && count++ < 6); | ||
|
||
} | ||
|
||
private static class ZkClusterInfoForTest { | ||
private String zkClusterKey; | ||
|
||
private String alias; | ||
|
||
private String connectString; | ||
|
||
public ZkClusterInfoForTest(String zkClusterKey, String alias, String connectString) { | ||
this.zkClusterKey = zkClusterKey; | ||
this.alias = alias; | ||
this.connectString = connectString; | ||
} | ||
|
||
public String toContent() { | ||
return String.format("zkClusterKey=%s&alias=%s&connectString=%s", zkClusterKey, alias, connectString); | ||
} | ||
|
||
public String getZkClusterKey() { | ||
return zkClusterKey; | ||
} | ||
|
||
public void setZkClusterKey(String zkClusterKey) { | ||
this.zkClusterKey = zkClusterKey; | ||
} | ||
|
||
public String getAlias() { | ||
return alias; | ||
} | ||
|
||
public void setAlias(String alias) { | ||
this.alias = alias; | ||
} | ||
|
||
public String getConnectString() { | ||
return connectString; | ||
} | ||
|
||
public void setConnectString(String connectString) { | ||
this.connectString = connectString; | ||
} | ||
} | ||
} |