[Feature]:添加修改问卷,删除问卷接口,增加相关单元测试 (#369)

* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* style:代码格式化调整

* style:代码格式化调整

---------

Co-authored-by: kui <likui@jiuhy.cn>
This commit is contained in:
likui63 2024-07-29 15:21:51 +08:00 committed by GitHub
parent db9735b4a2
commit ad42623ce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 508 additions and 2 deletions

View File

@ -16,6 +16,8 @@ public enum RespErrorCode {
SURVEY_TYPE_ERROR(3003, "问卷类型错误"), SURVEY_TYPE_ERROR(3003, "问卷类型错误"),
SURVEY_NOT_FOUND(3004, "问卷不存在"), SURVEY_NOT_FOUND(3004, "问卷不存在"),
SURVEY_CONTENT_NOT_ALLOW(3005, "存在禁用内容"), SURVEY_CONTENT_NOT_ALLOW(3005, "存在禁用内容"),
UPDATE_SURVEY_META_ERROR(3006, "修改问卷失败"),
DELETE_SURVEY_ERROR(3007, "删除问卷失败"),
CAPTCHA_INCORRECT(4001, "验证码不正确"), CAPTCHA_INCORRECT(4001, "验证码不正确"),
RESPONSE_SIGN_ERROR(9001, "签名不正确"), RESPONSE_SIGN_ERROR(9001, "签名不正确"),
RESPONSE_CURRENT_TIME_NOT_ALLOW(9002, "当前时间不允许提交"), RESPONSE_CURRENT_TIME_NOT_ALLOW(9002, "当前时间不允许提交"),
@ -24,6 +26,9 @@ public enum RespErrorCode {
RESPONSE_DATA_DECRYPT_ERROR(9005, "问卷已删除"), RESPONSE_DATA_DECRYPT_ERROR(9005, "问卷已删除"),
UPLOAD_FILE_ERROR(5001, "上传文件错误"); UPLOAD_FILE_ERROR(5001, "上传文件错误");
private final int code; private final int code;
private final String message; private final String message;

View File

@ -11,4 +11,21 @@ public class Status implements Serializable {
private Long date; private Long date;
private String status; private String status;
public Long getDate() {
return date;
}
public void setDate(Long date) {
this.date = date;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
} }

View File

@ -26,6 +26,11 @@ public class SurveyMeta extends BaseEntity {
@NotBlank(message = "问卷类型不能为空") @NotBlank(message = "问卷类型不能为空")
@NotNull(message = "问卷类型不能为空") @NotNull(message = "问卷类型不能为空")
private String surveyType; private String surveyType;
/**
* 问卷短链
*/
private String surveyPath;
/** /**
* 问卷标题 * 问卷标题
*/ */
@ -43,5 +48,14 @@ public class SurveyMeta extends BaseEntity {
*/ */
private String createFrom; private String createFrom;
/**
* 创建人
*/
private String creator;
/**
* 所有者
*/
private String owner;
} }

View File

@ -0,0 +1,51 @@
package com.xiaojusurvey.engine.common.entity.survey;
import com.xiaojusurvey.engine.common.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* 问卷发布后的配置表
*
* @author likui63@163.com
* @date: 2024/7/27 13:59
*/
@Document("surveySubmit")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
public class SurveyPublish extends BaseEntity {
/**
* 问卷配置ID
*/
@NotBlank(message = "问卷配置ID不能为空")
@NotNull(message = "问卷配置ID不能为空")
private String pageId;
/**
* 问卷标题
*/
@NotBlank(message = "问卷标题不能为空")
@NotNull(message = "问卷标题不能为空")
private String title;
/**
* 问卷短链
*/
@NotBlank(message = "问卷短链不能为空")
@NotNull(message = "问卷短链不能为空")
private String surveyPath;
/**
* 问卷schema
*/
@NotBlank(message = "问卷schema不能为空")
@NotNull(message = "问卷schema不能为空")
private String code;
}

View File

@ -0,0 +1,37 @@
package com.xiaojusurvey.engine.common.enums;
import com.xiaojusurvey.engine.common.entity.Status;
/**
* Survey状态
*
* @author likui63@163.com
* @Date 2024/7/26 23:32
*/
public enum SurveyStatusEnum {
NEW("new"),
EDITING("editing"),
PAUSING("pausing"),
PUBLISHED("published"),
REMOVED("removed"),
FORCE_REMOVED("FORCE_REMOVED");
private String status;
SurveyStatusEnum(String status) {
this.status = status;
}
public String getStatus() {
return this.status;
}
public static Status getSpecStatus(SurveyStatusEnum statusEnum) {
Status newStatus = new Status();
newStatus.setStatus(statusEnum.getStatus());
newStatus.setDate(System.currentTimeMillis());
return newStatus;
}
}

View File

@ -33,6 +33,16 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,15 @@
package com.xiaojusurvey.engine.core.survey;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
/**
* 问卷发布配置服务
*
* @author likui63@163.com
* @date: 2024/7/27 13:54
*/
public interface SurveyPublishService {
boolean delete(SurveyMeta param);
}

View File

@ -2,6 +2,7 @@ package com.xiaojusurvey.engine.core.survey;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult; import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
/** /**
* @Author: LYF * @Author: LYF
@ -12,4 +13,8 @@ public interface SurveyService {
IdResult createSurvey(SurveyMeta surveyMeta); IdResult createSurvey(SurveyMeta surveyMeta);
SurveyMeta getSurveyMeta(String surveyId); SurveyMeta getSurveyMeta(String surveyId);
boolean updateMeta(SurveyMetaUpdateParam param);
boolean deleteSurvey(String surveyId);
} }

View File

@ -0,0 +1,58 @@
package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.entity.survey.SurveyPublish;
import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* 问卷发布配置服务实现
*
* @author likui63@163.com
* @Date 2024/7/27 14:02
*/
@Service
public class SurveyPublishServiceImpl implements SurveyPublishService {
@Resource
private MongoRepository mongoRepository;
public MongoRepository getMongoRepository() {
return mongoRepository;
}
public void setMongoRepository(MongoRepository mongoRepository) {
this.mongoRepository = mongoRepository;
}
@Override
public boolean delete(SurveyMeta param) {
if (StringUtils.hasLength(param.getSurveyPath())) {
SurveyPublish surveyPublish = getBysurveyPath(param.getSurveyPath());
if (surveyPublish != null) {
Status st = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.PUBLISHED);
surveyPublish.setCurStatus(st);
surveyPublish.getStatusList().add(st);
mongoRepository.save(surveyPublish);
}
}
return true;
}
public SurveyPublish getBysurveyPath(String surveyPath) {
Query query = new Query();
query.addCriteria(Criteria.where("surveyPath").is(surveyPath));
SurveyPublish surveyPublish = mongoRepository.findOne(query, SurveyPublish.class);
return surveyPublish;
}
}

View File

@ -1,32 +1,58 @@
package com.xiaojusurvey.engine.core.survey.impl; package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.common.exception.ServiceException;
import com.xiaojusurvey.engine.core.reslut.IdResult; import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.repository.MongoRepository; import com.xiaojusurvey.engine.repository.MongoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
/** /**
* @Author: LYF * @Author: LYF
* @CreateTime: 2024-06-05 * @CreateTime: 2024-06-05
* @Description: 问卷ServiceImpl * @Description: 问卷ServiceImpl
*/ */
@Service("surveyService") @Service
@Slf4j
public class SurveyServiceImpl implements SurveyService { public class SurveyServiceImpl implements SurveyService {
@Resource @Resource
private MongoRepository mongoRepository; private MongoRepository mongoRepository;
@Resource
private SurveyPublishService surveyPublishService;
public MongoRepository getMongoRepository() {
return mongoRepository;
}
public void setMongoRepository(MongoRepository mongoRepository) {
this.mongoRepository = mongoRepository;
}
/** /**
* 创建问卷 * 创建问卷
*/ */
@Override @Override
public IdResult createSurvey(SurveyMeta surveyMeta) { public IdResult createSurvey(SurveyMeta surveyMeta) {
IdResult idResult = new IdResult(); IdResult idResult = new IdResult();
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.NEW);
surveyMeta.setCurStatus(newStatus);
List<Status> statusList = Arrays.asList(newStatus);
surveyMeta.setStatusList(statusList);
idResult.setId(mongoRepository.save(surveyMeta).getId()); idResult.setId(mongoRepository.save(surveyMeta).getId());
return idResult; return idResult;
} }
@ -37,4 +63,46 @@ public class SurveyServiceImpl implements SurveyService {
query.addCriteria(Criteria.where("_id").is(surveyId)); query.addCriteria(Criteria.where("_id").is(surveyId));
return mongoRepository.findOne(query, SurveyMeta.class); return mongoRepository.findOne(query, SurveyMeta.class);
} }
@Override
public boolean updateMeta(SurveyMetaUpdateParam param) {
SurveyMeta temp = getSurveyMeta(param.getSurveyId());
Status st = temp.getCurStatus();
if (!st.getStatus().equals(SurveyStatusEnum.NEW.getStatus()) && !st.getStatus().equals(SurveyStatusEnum.EDITING.getStatus())) {
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.EDITING);
temp.setCurStatus(newStatus);
temp.setTitle(param.getTitle());
temp.setRemark(param.getRemark());
temp.getStatusList().add(newStatus);
}
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(param.getSurveyId()));
Update update = new Update();
update.set("title", param.getTitle()).set("remark", param.getRemark());
update.set("curStatus", temp.getCurStatus());
update.set("statusList", temp.getStatusList());
mongoRepository.updateFirst(query, update, SurveyMeta.class);
return true;
}
@Override
public boolean deleteSurvey(String surveyId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(surveyId));
SurveyMeta meta = mongoRepository.findOne(query, SurveyMeta.class);
if (meta.getCurStatus().getStatus().equals(SurveyStatusEnum.REMOVED.getStatus())) {
log.error("[deleteSurvey] 问卷已删除,不能重复删除,surveyId={}", surveyId);
throw new ServiceException(RespErrorCode.SURVEY_STATUS_TRANSFORM_ERROR.getMessage(), RespErrorCode.SURVEY_STATUS_TRANSFORM_ERROR.getCode()); // 问卷状态转换报错
}
Status newStatus = SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.REMOVED);
meta.setCurStatus(newStatus);
meta.getStatusList().add(newStatus);
mongoRepository.save(meta);
//删除问卷回收表
surveyPublishService.delete(meta);
return true;
}
} }

View File

@ -0,0 +1,28 @@
package com.xiaojusurvey.engine.core.survey.param;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 修改问卷实体
*
* @author likui63@163.com
* @Date 2024/7/24 21:46
*/
@Data
public class SurveyMetaUpdateParam implements Serializable {
@NotBlank(message = "问卷id不能为空", groups = {Update.class, Delete.class})
private String surveyId;
@NotBlank(message = "问卷标题不能为空", groups = {Update.class})
private String title;
@NotBlank(message = "问卷描述不能为空", groups = {Update.class})
private String remark;
public interface Update {
}
public interface Delete {
}
}

View File

@ -0,0 +1,87 @@
package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* 问卷service单元测试
*
* @author likui63@163.com
* @Date 2024/7/27 14:27
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class SurveyServiceImplTest {
@InjectMocks
private SurveyServiceImpl surveyService;
@Mock
SurveyPublishService surveyPublishService;
@Mock
MongoRepository mongoRepository;
SurveyMetaUpdateParam updateParam;
SurveyMeta surveyMeta;
String surveyid = "111111111111111111";
@Before
public void before(){
surveyMeta = new SurveyMeta();
surveyMeta.setId(surveyid);
surveyMeta.setTitle("title");
surveyMeta.setRemark("remark");
surveyMeta.setSurveyType("type1");
surveyMeta.setCreator("likui");
surveyMeta.setOwner("likui");
surveyMeta.setCurStatus(SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.PUBLISHED));
List<Status> list = new ArrayList();
list.add(SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.NEW));
list.add(SurveyStatusEnum.getSpecStatus(SurveyStatusEnum.PUBLISHED));
surveyMeta.setStatusList(list);
}
@Test
public void updateMetaTest() {
updateParam = new SurveyMetaUpdateParam();
updateParam.setSurveyId(surveyid);
updateParam.setTitle("新时代");
updateParam.setRemark("这是一个测试");
when(mongoRepository.findOne(any(),any())).thenReturn(surveyMeta);
boolean flag = surveyService.updateMeta(updateParam);
Assert.assertEquals(true,flag);
}
@Test
public void deleteSurveyTest() {
when(mongoRepository.findOne(any(),any())).thenReturn(surveyMeta);
when(surveyPublishService.delete(any())).thenReturn(true);
boolean flag = surveyService.deleteSurvey(surveyid);
Assert.assertEquals(true,flag);
}
}

View File

@ -13,6 +13,7 @@ import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -66,7 +67,7 @@ public class SurveyController {
@PostMapping("/updateConf") @PostMapping("/updateConf")
public RpcResult<Boolean> updateConf(HttpServletRequest request, public RpcResult<Boolean> updateConf(HttpServletRequest request,
@RequestBody @Validated(SurveyInfoInVO.UpdateConf.class) SurveyInfoInVO infoInVO) { @RequestBody @Validated(SurveyInfoInVO.UpdateConf.class) SurveyInfoInVO infoInVO) {
SurveyConf surveyConf = new SurveyConf(); SurveyConf surveyConf = new SurveyConf();
surveyConf.setCode(infoInVO.getConfigData()); surveyConf.setCode(infoInVO.getConfigData());
surveyConf.setPageId(infoInVO.getSurveyId()); surveyConf.setPageId(infoInVO.getSurveyId());
surveyConfService.saveSurveyConfig(surveyConf); surveyConfService.saveSurveyConfig(surveyConf);
@ -98,4 +99,31 @@ public class SurveyController {
SurveyConf surveyConf = surveyConfService.getSurveyConfBySurveyId(surveyId); SurveyConf surveyConf = surveyConfService.getSurveyConfBySurveyId(surveyId);
return RpcResultUtil.createSuccessResult(new SurveyInfoOutVO(surveyMeta, surveyConf)); return RpcResultUtil.createSuccessResult(new SurveyInfoOutVO(surveyMeta, surveyConf));
} }
/**
* 修改问卷
*/
@PostMapping("/updateMeta")
public RpcResult updateMeta(@RequestBody @Validated(SurveyMetaUpdateParam.Update.class) SurveyMetaUpdateParam param) {
boolean flag = surveyService.updateMeta(param);
if (flag) {
return RpcResultUtil.createSuccessResult(null);
}
return RpcResultUtil.createFailedResult(RespErrorCode.UPDATE_SURVEY_META_ERROR.getCode(), RespErrorCode.UPDATE_SURVEY_META_ERROR.getMessage());
}
/**
* 删除问卷
*
* @param param
* @return
*/
@PostMapping("/deleteSurvey")
public RpcResult deleteSurvey(@RequestBody @Validated(SurveyMetaUpdateParam.Delete.class) SurveyMetaUpdateParam param) {
boolean flag = surveyService.deleteSurvey(param.getSurveyId());
if (flag) {
return RpcResultUtil.createSuccessResult(null);
}
return RpcResultUtil.createFailedResult(RespErrorCode.DELETE_SURVEY_ERROR.getCode(), RespErrorCode.DELETE_SURVEY_ERROR.getMessage());
}
} }

View File

@ -0,0 +1,83 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.config.BannerDataConfig;
import com.xiaojusurvey.engine.core.survey.SurveyConfService;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.servlet.http.HttpServletRequest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* @Author: maple
* @CreateTime: 2024/6/15 20:40
* @Description:
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class SurveyMetaControllerTest {
@InjectMocks
SurveyController surveyController;
@Mock
private SurveyService surveyService;
@Mock
private SurveyConfService surveyConfService;
@Mock
private SurveyHistoryService surveyHistoryService;
@Mock
private BannerDataConfig bannerDataConfig;
@Mock
MongoRepository mongoRepository;
private HttpServletRequest httpServletRequest;
@Test
public void updateMetaTest() {
when(surveyService.updateMeta(any())).thenReturn(true);
SurveyMetaUpdateParam param = new SurveyMetaUpdateParam();
param.setSurveyId(null);
param.setTitle(null);
param.setRemark(null);
RpcResult surveyResult = surveyController.updateMeta(param);
Assert.assertTrue(surveyResult.getSuccess());
}
@Test
public void deleteSurveyTest() {
when(surveyService.deleteSurvey(any())).thenReturn(true);
SurveyMetaUpdateParam param = new SurveyMetaUpdateParam();
param.setSurveyId(null);
param.setTitle(null);
param.setRemark(null);
RpcResult surveyResult = surveyController.deleteSurvey(param);
Assert.assertTrue(surveyResult.getSuccess());
}
}