Skip to content

Commit

Permalink
Merge pull request #97 from gitxiaofeng/master
Browse files Browse the repository at this point in the history
feature:历史版本功能查看
  • Loading branch information
gitxiaofeng authored Jul 5, 2021
2 parents 615832b + 1d8485c commit 7e0eef7
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 9 deletions.
8 changes: 8 additions & 0 deletions case-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>

<!-- json patch组件 -->
<dependency>
<groupId>com.flipkart.zjsonpatch</groupId>
<artifactId>zjsonpatch</artifactId>
<version>0.4.11</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoju.framework.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.xiaoju.framework.constants.enums.StatusCode;
import com.xiaoju.framework.entity.exception.CaseServerException;
import com.xiaoju.framework.entity.persistent.CaseBackup;
Expand Down Expand Up @@ -43,6 +44,19 @@ public Response<List<CaseBackup>> getBackupByCaseId(@RequestParam @NotNull(messa
return Response.success(caseBackupService.getBackupByCaseId(caseId, beginTime, endTime));
}

/**
* 查询某个用例所有的备份记录
*
* @param caseId1 用例备份id1
* @param caseId2 用例备份id2
* @return 响应体
*/
@GetMapping(value = "/getCaseDiff")
public Response<JsonNode> getCaseDiff(@RequestParam @NotNull(message = "备份id1") Long caseId1,
@RequestParam @NotNull(message = "备份id2") Long caseId2) {
return Response.success(caseBackupService.getCaseDiff(caseId1, caseId2));
}

/**
* 删除某个用例所有的备份记录
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public String login(){
return "index";
}

@RequestMapping("/history/*")
public String history(){
return "index";
}

@RequestMapping("/caseManager/historyContrast/*/*")
public String historyContrast(){
return "index";
}

@RequestMapping("/case/caseList/1")
public String index(HttpServletRequest request){
return "index";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ List<CaseBackup> selectByCaseId(@Param("caseId") Long caseId,
@Param("beginTime") Date beginTime,
@Param("endTime") Date endTime);

CaseBackup selectByBackupId(@Param("id") Long id);

/**
* 删除一批备份记录
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaoju.framework.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.xiaoju.framework.entity.persistent.CaseBackup;

import java.util.List;
Expand Down Expand Up @@ -38,4 +39,6 @@ public interface CaseBackupService {
* @return int
*/
int deleteBackup(Long caseId);

JsonNode getCaseDiff(Long backupId1, Long backupId2);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.xiaoju.framework.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.flipkart.zjsonpatch.JsonDiff;
import com.flipkart.zjsonpatch.JsonPatch;
import com.xiaoju.framework.entity.persistent.CaseBackup;
import com.xiaoju.framework.mapper.CaseBackupMapper;
import com.xiaoju.framework.service.CaseBackupService;
Expand All @@ -8,10 +15,16 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;

import static com.flipkart.zjsonpatch.DiffFlags.ADD_EXPLICIT_REMOVE_ADD_ON_REPLACE;
import static com.flipkart.zjsonpatch.DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE;

/**
* 备份记录
Expand All @@ -33,12 +46,19 @@ public synchronized CaseBackup insertBackup(CaseBackup caseBackup) {
List<CaseBackup> caseBackups = caseBackupMapper.selectByCaseId(caseBackup.getCaseId(), null, null);

// 如果当前已有,则直接返回
// todo: 此处应该是比较json或者base字段信息,此处可能存在json字段位置不一致导致的字符串不一致问题。
if (caseBackups.size() > 0 &&
caseBackups.get(0).getCaseContent().equals(caseBackup.getCaseContent()) &&
caseBackups.get(0).getRecordContent().equals(caseBackup.getRecordContent())) {
LOGGER.info("当前内容已经保存过了,不再重复保存。");
return caseBackups.get(0);
// todo 此处还是用版本信息控制更加合理
ObjectMapper jsonMapper = new ObjectMapper();
try {
if (caseBackups.size() > 0 &&
JsonDiff.asJson(jsonMapper.readTree(caseBackups.get(0).getCaseContent()),
jsonMapper.readTree(caseBackup.getCaseContent())).size() == 0 &&
JsonDiff.asJson(jsonMapper.readTree(caseBackups.get(0).getRecordContent()),
jsonMapper.readTree(caseBackup.getRecordContent())).size() == 0) {
LOGGER.info("当前内容已经保存过了,不再重复保存。");
return caseBackups.get(0);
}
} catch (IOException e) {
LOGGER.info("json转换异常. 数据继续备份", e);
}

int ret = caseBackupMapper.insert(caseBackup);
Expand All @@ -53,9 +73,127 @@ public synchronized CaseBackup insertBackup(CaseBackup caseBackup) {
return caseBackup;
}

@Override
public JsonNode getCaseDiff(Long backupId2, Long backupId1) {
ObjectMapper jsonMapper = new ObjectMapper();
CaseBackup caseBackup1 = caseBackupMapper.selectByBackupId(backupId1);
CaseBackup caseBackup2 = caseBackupMapper.selectByBackupId(backupId2);

try {
JsonNode content1 = jsonMapper.readTree(caseBackup1.getCaseContent());
JsonNode content2 = jsonMapper.readTree(caseBackup2.getCaseContent());
ArrayNode patches = (ArrayNode) JsonDiff.asJson(content1, content2, EnumSet.of(ADD_ORIGINAL_VALUE_ON_REPLACE));

JsonNodeFactory FACTORY = JsonNodeFactory.instance;
ArrayNode patchesNew = FACTORY.arrayNode();
ObjectNode retJson = FACTORY.objectNode();
Iterator<JsonNode> it = patches.elements();

while (it.hasNext()) {
JsonNode element = it.next();
String op = element.get("op").textValue();
if (op.equals("replace")) {
if (element.get("path").textValue().endsWith("base")) {
continue;
}
ObjectNode node1 = element.deepCopy();

if(!element.get("path").textValue().endsWith("image") && !element.get("path").textValue().contains("imageSize")) {
node1.remove("value");
node1.put("value", "旧内容:" + element.get("fromValue").textValue() + "\n新内容:" + element.get("value").textValue());
}
patchesNew.add(node1);
ObjectNode node2 = FACTORY.objectNode();
node2.put("op", "add");
String srcPath = element.get("path").textValue();

node2.put("path", srcPath.substring(0, srcPath.lastIndexOf('/')) + "/background");
node2.put("value", "#d6f0ff");
patchesNew.add(node2);
} else if (op.equals("add")) {
ObjectNode node1 = element.deepCopy();
traverse(node1, "add");

patchesNew.add(node1);

} else if (op.equals("remove")) {
patchesNew.add(element);
ObjectNode node1 = element.deepCopy();
node1.remove("op");
node1.put("op", "add");
traverse(node1, "remove");
patchesNew.add(node1);

} else {
LOGGER.info("op is: " + element.toString());
}
}
JsonNode target = JsonPatch.apply((JsonNode) patchesNew, content1);

retJson.set("content", target);
ArrayNode cardJson = FACTORY.arrayNode();
ObjectNode backup1 = FACTORY.objectNode();
backup1.put("user", caseBackup1.getCreator());
backup1.put("time", caseBackup1.getGmtCreated().toString());
ObjectNode backup2 = FACTORY.objectNode();
backup2.put("user", caseBackup2.getCreator());
backup2.put("time", caseBackup2.getGmtCreated().toString());
cardJson.add(backup1);
cardJson.add(backup2);

retJson.set("backupinfo", cardJson);

return retJson;

} catch (Exception e) {
LOGGER.error("json mapper read tree exception. ", e);
return null;
}

}

private void traverse(JsonNode node, String op) {
Iterator<JsonNode> iterator = node.iterator();

while (iterator.hasNext()) {
JsonNode n = iterator.next();
if (n.size() > 0) {
if (n.has("id")) {
if (op.equals("add")) {
((ObjectNode) n).put("background", "#ddfade");
} else {
((ObjectNode) n).put("background", "#ffe7e7");
}
}
traverse(n, op);
} else {
// System.out.println(n.toString());
}
}

}
@Override
public List<CaseBackup> getBackupByCaseId(Long caseId, String beginTime, String endTime) {
return caseBackupMapper.selectByCaseId(caseId, transferTime(beginTime), transferTime(endTime));
List<CaseBackup> backupsSrc = caseBackupMapper.selectByCaseId(caseId, transferTime(beginTime), transferTime(endTime));
List<CaseBackup> backups = new ArrayList<>();
String pattern = "\"base\":(\\d+).*";
Pattern r = Pattern.compile(pattern);
Integer compareVersion = 0;
for (CaseBackup cb:backupsSrc) {
Matcher m = r.matcher(cb.getCaseContent());
if (m.find()) {
Integer currentVersion = Integer.valueOf(m.group(1));
if (!currentVersion.equals(compareVersion)) {
backups.add(cb);
compareVersion = currentVersion;
} else {
LOGGER.error("base信息一致。过滤信息。base: " + currentVersion);
}
} else {
LOGGER.error("未找到base信息。用例内容是:" + cb.getCaseContent());
}
}
return backups;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
order by id desc
</select>

<select id="selectByBackupId" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from case_backup
where id = #{id,jdbcType=BIGINT}
</select>

<delete id="updateByCaseId" parameterType="java.lang.Long">
update case_backup set is_delete =1
where case_id = #{case_id,jdbcType=BIGINT} and is_delete = 0
Expand Down

0 comments on commit 7e0eef7

Please sign in to comment.