diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/controller/ForwardController.java b/weevent-governance/src/main/java/com/webank/weevent/governance/controller/ForwardController.java index 6caaef740..3996effa2 100644 --- a/weevent-governance/src/main/java/com/webank/weevent/governance/controller/ForwardController.java +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/controller/ForwardController.java @@ -1,48 +1,45 @@ package com.webank.weevent.governance.controller; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.webank.weevent.governance.exception.GovernanceException; +import com.webank.weevent.governance.service.CommonService; + import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.client.RestTemplate; +import org.springframework.web.bind.annotation.RestController; @CrossOrigin @Slf4j +@RestController public class ForwardController { + @Autowired - private RestTemplate restTemplate; + private CommonService commonService; @Value("${weevent.url}") private String url; @RequestMapping(value = "/weevent/{path1}/{path2}", method = RequestMethod.GET) - public Object forward(@PathVariable(name = "path1") String path1, @PathVariable(name = "path2") String path2) { + public Object forward(HttpServletRequest request, HttpServletResponse response, @PathVariable(name = "path1") String path1, @PathVariable(name = "path2") String path2)throws GovernanceException { log.info("weevent url: /wevent/ {} \"/\" {}", path1, path2); String forwarUrl = new StringBuffer(this.url).append("/").append(path1).append("/").append(path2).toString(); - Object result = restTemplate.getForEntity(forwarUrl, Object.class).getBody(); - return result; + try { + CloseableHttpResponse closeResponse = commonService.getCloseResponse(request, forwarUrl); + return EntityUtils.toString(closeResponse.getEntity()); + } catch (Exception e) { + throw new GovernanceException(e.getMessage()); + } } - @RequestMapping(value = "/weevent/admin/deploy_topic_control", method = RequestMethod.GET) - public Object forward() { - log.info("wevent url: /weevent/admin/deploy_topic_control"); - String forwarUrl = new StringBuffer(this.url).append("/admin/deploy_topic_control").toString(); - String result = restTemplate.getForEntity(forwarUrl, String.class).getBody(); - return result; - } - @RequestMapping(value = "/weevent/{path1}/{path2}") - public Object forward(@PathVariable(name = "path1") String path1, @PathVariable(name = "path2") String path2, - @RequestParam String topic) { - log.info("weevent url: /wevent/ {} \"/\" {}", path1, path2); - - String forwarUrl = new StringBuffer(this.url).append("/").append(path1).append("/").append(path2).append("?topic=").append(topic).toString(); - Object result = restTemplate.getForEntity(forwarUrl, Object.class).getBody(); - return result; - } } diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/mapper/RuleEngineMapper.java b/weevent-governance/src/main/java/com/webank/weevent/governance/mapper/RuleEngineMapper.java index 41cd9b460..162a59668 100644 --- a/weevent-governance/src/main/java/com/webank/weevent/governance/mapper/RuleEngineMapper.java +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/mapper/RuleEngineMapper.java @@ -24,10 +24,14 @@ public interface RuleEngineMapper { // update RuleEngineEntity Boolean updateRuleEngine(RuleEngineEntity ruleEngineEntity); + List getRuleTopicList(RuleEngineEntity ruleEngineEntity); + Boolean updateRuleEngineStatus(RuleEngineEntity ruleEngineEntity); int countRuleEngine(RuleEngineEntity ruleEngineEntity); RuleEngineEntity getRuleById(Integer id); + List checkRuleNameRepeat(RuleEngineEntity ruleEngineEntity); + } diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/service/CommonService.java b/weevent-governance/src/main/java/com/webank/weevent/governance/service/CommonService.java index 089880ad8..618628c55 100644 --- a/weevent-governance/src/main/java/com/webank/weevent/governance/service/CommonService.java +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/service/CommonService.java @@ -12,8 +12,10 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; @@ -85,7 +87,6 @@ public CloseableHttpResponse getCloseResponse(HttpServletRequest req, String new public CloseableHttpResponse getCloseResponse(HttpServletRequest req, String newUrl, String jsonString) throws ServletException { CloseableHttpResponse closeResponse; try { - log.info("url {}", newUrl); CloseableHttpClient client = this.generateHttpClient(newUrl); if (req.getMethod().equals(METHOD_TYPE)) { @@ -276,6 +277,13 @@ private static String truncateUrlPage(String strURL) { return strAllParam; } + public Set mergeSet(Set list1, Set list2) { + Set set = new HashSet<>(); + set.addAll(list1); + set.addAll(list2); + return set; + } + @Override public void close() throws Exception { diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/service/RuleEngineService.java b/weevent-governance/src/main/java/com/webank/weevent/governance/service/RuleEngineService.java index 14eff7e25..16be123ae 100644 --- a/weevent-governance/src/main/java/com/webank/weevent/governance/service/RuleEngineService.java +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/service/RuleEngineService.java @@ -3,6 +3,7 @@ import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -29,6 +30,7 @@ import com.webank.weevent.governance.mapper.RuleEngineMapper; import com.webank.weevent.governance.properties.ConstantProperties; import com.webank.weevent.governance.utils.CookiesTools; +import com.webank.weevent.governance.utils.DAGDetectUtil; import com.webank.weevent.governance.utils.NumberValidationUtils; import com.alibaba.fastjson.JSONObject; @@ -67,6 +69,10 @@ public class RuleEngineService { @Autowired private BrokerMapper brokerMapper; + @Autowired + private DAGDetectUtil dagDetectUtil; + + @Value("${weevent.processor.url:http://127.0.0.1:7008}") private String processorUrl; @@ -271,6 +277,10 @@ public boolean updateRuleEngine(RuleEngineEntity ruleEngineEntity, HttpServletRe if (!flag) { throw new GovernanceException("conditional is illegal"); } + flag = verifyInfiniteLoop(ruleEngineEntity); + if (!flag) { + throw new GovernanceException("update rule failed, detected DAG loop at topic [" + ruleEngineEntity.getFromDestination() + "]"); + } RuleDatabaseEntity ruleDataBase = getRuleDataBase(ruleEngineEntity.getRuleDataBaseId()); if (ruleDataBase != null) { ruleEngineEntity.setDatabaseUrl(ruleDataBase.getDatabaseUrl() + "&tableName=" + ruleDataBase.getTableName()); @@ -661,8 +671,8 @@ private void checkRule(RuleEngineEntity ruleEngineEntity) throws GovernanceExcep if (ruleEngineEntity.getPayloadMap().isEmpty()) { throw new GovernanceException("rule description is empty"); } - if (ruleEngineEntity.getPayload() != null && ruleEngineEntity.getPayload().length() > 100) { - throw new GovernanceException("rule description length cannot exceed 100"); + if (ruleEngineEntity.getPayload() != null && ruleEngineEntity.getPayload().length() > 4096) { + throw new GovernanceException("rule description length cannot exceed 4096"); } } @@ -677,12 +687,9 @@ private boolean checkRuleName(String ruleName, String regex) { //check name repeat private boolean checkRuleNameRepeat(RuleEngineEntity ruleEngineEntity) { RuleEngineEntity rule = new RuleEngineEntity(); - rule.setGroupId(ruleEngineEntity.getGroupId()); - rule.setUserId(ruleEngineEntity.getUserId()); - rule.setBrokerId(ruleEngineEntity.getBrokerId()); rule.setRuleName(ruleEngineEntity.getRuleName()); rule.setSystemTag("2"); - List ruleEngines = ruleEngineMapper.getRuleEngines(rule); + List ruleEngines = ruleEngineMapper.checkRuleNameRepeat(rule); if (CollectionUtils.isEmpty(ruleEngines)) { return true; } @@ -789,4 +796,28 @@ private RuleDatabaseEntity getRuleDataBase(Integer id) { return ruleDatabaseMapper.getRuleDataBaseById(id); } + private boolean verifyInfiniteLoop(RuleEngineEntity ruleEngineEntity) { + if (!ConditionTypeEnum.TOPIC.getCode().equals(ruleEngineEntity.getConditionType())) { + return true; + } + RuleEngineEntity rule = new RuleEngineEntity(); + rule.setGroupId(ruleEngineEntity.getGroupId()); + rule.setBrokerId(ruleEngineEntity.getBrokerId()); + + //query all historical rules according to brokerId groupId + List ruleTopicList = ruleEngineMapper.getRuleTopicList(rule); + if (CollectionUtils.isEmpty(ruleTopicList)) { + return true; + } + ruleTopicList.add(ruleEngineEntity); + Map> map = new HashMap<>(); + + for (RuleEngineEntity engineEntity : ruleTopicList) { + map.merge(engineEntity.getFromDestination(), new HashSet<>(Collections.singleton(engineEntity.getToDestination())), (a, b) -> commonService.mergeSet(a, b)); + } + Set set = map.keySet(); + return dagDetectUtil.checkLoop(map, set); + } + + } diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/service/TopicHistoricalService.java b/weevent-governance/src/main/java/com/webank/weevent/governance/service/TopicHistoricalService.java index 029669787..19c6724c7 100644 --- a/weevent-governance/src/main/java/com/webank/weevent/governance/service/TopicHistoricalService.java +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/service/TopicHistoricalService.java @@ -3,7 +3,6 @@ import java.io.FileInputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; @@ -249,8 +248,8 @@ private RuleEngineEntity initializationRule(String ruleName, BrokerEntity broker return ruleEngineEntity; } - private List getGroupList(HttpServletRequest request, BrokerEntity brokerEntity) throws GovernanceException { - List groupList; + private List getGroupList(HttpServletRequest request, BrokerEntity brokerEntity) throws GovernanceException { + List groupList; String url = new StringBuffer(brokerEntity.getBrokerUrl()).append(ConstantProperties.REST_LIST_SUBSCRIPTION).toString(); try { log.info("url:{}", url); @@ -259,8 +258,14 @@ private List getGroupList(HttpServletRequest request, BrokerEntity broke if (StringUtil.isBlank(mes)) { throw new GovernanceException("group is empty"); } - String[] split = mes.replace("[", "").replace("]", "").split(","); - groupList = Arrays.asList(split); + JSONObject jsonObject = JSONObject.parseObject(mes); + Object data = jsonObject.get("data"); + if ("0".equals(jsonObject.get("code").toString()) && data instanceof List) { + groupList = (List) data; + } else { + throw new GovernanceException(jsonObject.get("message").toString()); + } + return groupList; } catch (Exception e) { log.error("get group list fail", e); diff --git a/weevent-governance/src/main/java/com/webank/weevent/governance/utils/DAGDetectUtil.java b/weevent-governance/src/main/java/com/webank/weevent/governance/utils/DAGDetectUtil.java new file mode 100644 index 000000000..84e2ea16f --- /dev/null +++ b/weevent-governance/src/main/java/com/webank/weevent/governance/utils/DAGDetectUtil.java @@ -0,0 +1,62 @@ +package com.webank.weevent.governance.utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + +import org.springframework.stereotype.Component; + +@SuppressWarnings("unchecked") +@Component +public class DAGDetectUtil { + private Stack stack = new Stack<>(); + + public boolean checkLoop(Map> map, Set topicSet) { + Map start = createNode("start"); + Map nodeMap = new HashMap<>(); + topicSet.forEach(it -> { + Map node = createNode(it); + ((List) start.get("child")).add(node); + nodeMap.put(it, node); + }); + map.forEach((k, v) -> { + v.forEach(it -> { + if (nodeMap.containsKey(it)) { + ((List) nodeMap.get(k).get("child")).add(nodeMap.get(it)); + } + }); + }); + return checkChild(start); + } + + private Map createNode(String name) { + HashMap node = new HashMap(); + node.put("topic", name); + node.put("child", new ArrayList()); + return node; + } + + + private boolean checkChild(Map cursor) { + if (stack.contains(cursor.get("topic"))) { + stack = new Stack<>(); + return false; + } + stack.push((String) cursor.get("topic")); + List childs = (List) cursor.get("child"); + if (childs != null) { + for (Object child : childs) { + if (!checkChild((Map) child)) { + return false; + } + } + } + stack.pop(); + return true; + } + + +} diff --git a/weevent-governance/src/main/resources/application-dev.properties b/weevent-governance/src/main/resources/application-dev.properties index 0f033b27b..82edca214 100644 --- a/weevent-governance/src/main/resources/application-dev.properties +++ b/weevent-governance/src/main/resources/application-dev.properties @@ -26,4 +26,6 @@ logging.config=classpath:log4j2.xml spring.pid.file=./logs/governance.pid spring.pid.fail-on-write-error=true -weevent.processor.url=http://localhost:7008 +weevent.processor.url=http://127.0.0.1:8080 + +weevent.url=http://127.0.0.1:8080/weevent diff --git a/weevent-governance/src/main/resources/application-prod.properties b/weevent-governance/src/main/resources/application-prod.properties index 873dffe3f..5ff204583 100644 --- a/weevent-governance/src/main/resources/application-prod.properties +++ b/weevent-governance/src/main/resources/application-prod.properties @@ -26,3 +26,5 @@ logging.config=classpath:log4j2.xml spring.pid.file=./logs/governance.pid spring.pid.fail-on-write-error=true weevent.processor.url=http://127.0.0.1:7008 + +weevent.url=http://127.0.0.1:8080/weevent diff --git a/weevent-governance/src/main/resources/mappers/RuleEngineMapper.xml b/weevent-governance/src/main/resources/mappers/RuleEngineMapper.xml index 646601215..998078ae2 100644 --- a/weevent-governance/src/main/resources/mappers/RuleEngineMapper.xml +++ b/weevent-governance/src/main/resources/mappers/RuleEngineMapper.xml @@ -11,7 +11,6 @@ - @@ -24,23 +23,24 @@ + - id,create_date,last_update, rule_name,payload_type,payload,broker_id,cep_id,broker_url,user_id, + id,create_date,last_update, rule_name,payload_type,payload,broker_id,broker_url,user_id,cep_id, from_destination,to_destination,select_field,condition_field,condition_type, rule_database_id,error_destination,error_message,status,group_id,system_tag - insert into t_rule_engine(rule_name, payload_type, payload, broker_id, broker_url, user_id, cep_id, + insert into t_rule_engine(rule_name, payload_type, payload, broker_id, broker_url, user_id, from_destination, to_destination, select_field, - condition_field, condition_type, status, group_id, error_message,rule_database_id,system_tag) - values (#{ruleName}, #{payloadType}, #{payload}, #{brokerId}, #{brokerUrl}, #{userId}, #{cepId}, + condition_field, condition_type, status, group_id, error_message,rule_database_id,system_tag,cep_id) + values (#{ruleName}, #{payloadType}, #{payload}, #{brokerId}, #{brokerUrl}, #{userId}, #{fromDestination}, #{toDestination}, #{selectField}, - #{conditionField}, #{conditionType}, #{status}, #{groupId}, #{errorMessage},#{ruleDataBaseId},#{systemTag}) + #{conditionField}, #{conditionType}, #{status}, #{groupId}, #{errorMessage},#{ruleDataBaseId},#{systemTag},#{cepId}) + select distinct from_destination,to_destination + from t_rule_engine + where status != 2 and condition_type=1 + + + + + update t_rule_engine set rule_name=#{ruleName}, @@ -111,7 +125,8 @@ to_destination=#{toDestination}, rule_database_id=#{ruleDataBaseId}, error_destination=#{errorDestination}, - error_message=#{errorMessage} + error_message=#{errorMessage}, + cep_id=#{cepId} where id = #{id,jdbcType=INTEGER} and status != 2 diff --git a/weevent-governance/src/main/resources/script/governance.sql b/weevent-governance/src/main/resources/script/governance.sql index 4618505e7..953b1a5ee 100644 --- a/weevent-governance/src/main/resources/script/governance.sql +++ b/weevent-governance/src/main/resources/script/governance.sql @@ -52,16 +52,16 @@ CREATE TABLE t_rule_engine ( `last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update date', `rule_name` VARCHAR(128) NOT NULL COMMENT 'rule name', `payload_type` INT(4) NULL DEFAULT NULL COMMENT '1 means JASON, 2 means binary', - `payload` VARCHAR(255) NULL DEFAULT NULL COMMENT 'message payload', + `payload` VARCHAR(4096) NULL DEFAULT NULL COMMENT 'message payload', `broker_id` INT(11) not NULL COMMENT 'broker id', - `cep_id` VARCHAR(64) NULL COMMENT 'complex event processing id', `user_id` INT(11) not NULL COMMENT 'user id', `group_id` VARCHAR(64) not NULL COMMENT 'group id', + `cep_id` VARCHAR(64) NULL COMMENT 'complex event processing id', `broker_url` VARCHAR(255) NULL DEFAULT NULL COMMENT 'broker url', `from_destination` VARCHAR(64) NULL DEFAULT NULL COMMENT 'message origin', `to_destination` VARCHAR(64) NULL DEFAULT NULL COMMENT 'message reached', - `select_field` VARCHAR(255) NULL DEFAULT NULL COMMENT 'selected field', - `condition_field` VARCHAR(255) NULL DEFAULT NULL COMMENT 'condition field', + `select_field` VARCHAR(4096) NULL DEFAULT NULL COMMENT 'selected field', + `condition_field` VARCHAR(4096) NULL DEFAULT NULL COMMENT 'condition field', `condition_type` INT(2) NULL DEFAULT NULL COMMENT 'condition type', `status` INT(2) NULL DEFAULT null COMMENT '0 means not started, 1 means running,2 means is deleted', `database_url` VARCHAR(255) NULL DEFAULT NULL COMMENT 'database url', diff --git a/weevent-governance/src/test/java/com/webank/weevent/governance/junit/DAGDetectTest.java b/weevent-governance/src/test/java/com/webank/weevent/governance/junit/DAGDetectTest.java new file mode 100644 index 000000000..3738a7493 --- /dev/null +++ b/weevent-governance/src/test/java/com/webank/weevent/governance/junit/DAGDetectTest.java @@ -0,0 +1,124 @@ +package com.webank.weevent.governance.junit; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import com.webank.weevent.governance.JUnitTestBase; +import com.webank.weevent.governance.utils.DAGDetectUtil; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +@Slf4j +public class DAGDetectTest extends JUnitTestBase { + + + @Autowired + private DAGDetectUtil dagDetectUtil; + + + @Before + public void before() { + log.info("=============================={}.{}==============================", + this.getClass().getSimpleName(), + this.testName.getMethodName()); + } + + @Test + public void testNormal001() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "C"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "J"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertTrue(b); + } + + @Test + public void testNormal002() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "CEE", "DFF", "EAA", "FC"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "F", "H", "I"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertTrue(b); + } + + @Test + public void testNormal003() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "C", "D", "E", "F"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "J"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "F", "H", "I"))); + map.put("D", new HashSet<>(Arrays.asList("E", "AA", "X", "W", "Y"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertTrue(b); + } + + @Test + public void testNormal004() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "C", "D", "E", "F"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "J"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "F", "H", "I"))); + map.put("D", new HashSet<>(Arrays.asList("E", "AA", "X", "W", "Y"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertTrue(b); + } + + @Test + public void testException001() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "CEE", "DFF", "EAA", "FC"))); + map.put("B", new HashSet<>(Arrays.asList("A", "E", "H", "I", "DJ"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertFalse(b); + } + + @Test + public void testException002() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "CEE", "DFF", "EAA", "FC"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "DJ"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "B", "H", "I"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertFalse(b); + } + + @Test + public void testException003() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "CEE", "DFF", "EAA", "FC"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "DJ"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "F", "H", "I"))); + map.put("D", new HashSet<>(Arrays.asList("E", "A", "C", "W", "Y"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertFalse(b); + } + + @Test + public void testException004() { + Map> map = new HashMap<>(); + map.put("A", new HashSet<>(Arrays.asList("B", "CEE", "DFF", "EAA", "FC"))); + map.put("B", new HashSet<>(Arrays.asList("C", "E", "H", "I", "DJ"))); + map.put("C", new HashSet<>(Arrays.asList("D", "E", "F", "H", "I"))); + map.put("D", new HashSet<>(Arrays.asList("E", "Abc", "C", "W", "Y"))); + Set topicSet = map.keySet(); + boolean b = dagDetectUtil.checkLoop(map, topicSet); + Assert.assertFalse(b); + } + + +} diff --git a/weevent-governance/src/test/java/com/webank/weevent/governance/junit/ForwardControllerTest.java b/weevent-governance/src/test/java/com/webank/weevent/governance/junit/ForwardControllerTest.java new file mode 100644 index 000000000..bbe8ca1b9 --- /dev/null +++ b/weevent-governance/src/test/java/com/webank/weevent/governance/junit/ForwardControllerTest.java @@ -0,0 +1,61 @@ +package com.webank.weevent.governance.junit; + +import java.util.List; + +import javax.servlet.http.Cookie; + +import com.webank.weevent.governance.JUnitTestBase; +import com.webank.weevent.governance.properties.ConstantProperties; + +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@Slf4j +public class ForwardControllerTest extends JUnitTestBase { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + private Cookie cookie; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); + this.cookie = new Cookie(ConstantProperties.COOKIE_MGR_ACCOUNT_ID, "1"); + + } + + @Before + public void before() { + log.info("=============================={}.{}==============================", + this.getClass().getSimpleName(), + this.testName.getMethodName()); + } + + + @Test + public void testGroupList() throws Exception { + MockHttpServletResponse response = mockMvc.perform(MockMvcRequestBuilders.get("/weevent/" + "admin" + "/" + "listGroup").contentType(MediaType.APPLICATION_JSON_UTF8).cookie(this.cookie)) + .andReturn().getResponse(); + String contentAsString = response.getContentAsString(); + Assert.assertEquals(response.getStatus(), 200); + Assert.assertNotNull(contentAsString); + JSONObject jsonObject = JSONObject.parseObject(contentAsString); + Object code = jsonObject.get("code"); + Object data = jsonObject.get("data"); + Assert.assertEquals(0, code); + Assert.assertTrue(data instanceof List); + } +}