feat: 问卷保存,获取,查询历史记录,获取banner配置接口开发 (#297)

Co-authored-by: yefeng <lukechang@aspirecn.com>
This commit is contained in:
yefeng 2024-06-17 11:30:08 +08:00 committed by GitHub
parent caad93555d
commit f96d4948fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 751 additions and 96 deletions

View File

@ -0,0 +1,25 @@
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 java.util.Map;
/**
* @Author: maple
* @CreateTime: 2024/6/7 21:27
* @Description: 问卷配置
*/
@Document("surveyConf")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
public class SurveyConf extends BaseEntity {
private String pageId;
private Map<String, Object> code;
}

View File

@ -0,0 +1,29 @@
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 java.util.Map;
/**
* @Author: maple
* @CreateTime: 2024/6/7 21:28
* @Description: 问卷历史记录
*/
@Document("surveyHistory")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
public class SurveyHistory extends BaseEntity {
private String pageId;
private String type;
private Map<String, Object> schema;
private Map<String, Object> operator;
}

View File

@ -0,0 +1,25 @@
package com.xiaojusurvey.engine.common.enums;
import lombok.Getter;
/**
* @Author: maple
* @CreateTime: 2024/6/10 21:28
* @Description: 历史记录枚举值
*/
@Getter
public enum HistoryTypeEnum {
//保存历史
DAILY_HIS("dailyHis"),
//发布历史
PUBLISH_HIS("publishHis");
private final String historyType;
HistoryTypeEnum(String historyType) {
this.historyType = historyType;
}
}

View File

@ -0,0 +1,10 @@
package com.xiaojusurvey.engine.core.survey;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
public interface SurveyConfService {
void saveSurveyConfig(SurveyConf surveyConf);
SurveyConf getSurveyConfBySurveyId(String surveyId);
}

View File

@ -0,0 +1,12 @@
package com.xiaojusurvey.engine.core.survey;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import java.util.List;
public interface SurveyHistoryService {
SurveyHistory addHistory(SurveyHistory surveyHistory);
List<SurveyHistory> getHistoryList(String surveyId, String historyType);
}

View File

@ -10,4 +10,6 @@ import com.xiaojusurvey.engine.core.reslut.IdResult;
*/
public interface SurveyService {
IdResult createSurvey(SurveyMeta surveyMeta);
SurveyMeta getSurveyMeta(String surveyId);
}

View File

@ -0,0 +1,42 @@
package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.exception.ServiceException;
import com.xiaojusurvey.engine.core.survey.SurveyConfService;
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 javax.annotation.Resource;
/**
* @Author: maple
* @CreateTime: 2024/6/9 13:52
* @Description:
*/
@Service
public class SurveyConfServiceImpl implements SurveyConfService {
@Resource
private MongoRepository mongoRepository;
@Override
public void saveSurveyConfig(SurveyConf surveyConf) {
SurveyConf codeInfo = this.getSurveyConfBySurveyId(surveyConf.getPageId());
if (null == codeInfo) {
throw new ServiceException(RespErrorCode.SURVEY_NOT_FOUND.getMessage(), RespErrorCode.SURVEY_NOT_FOUND.getCode());
}
codeInfo.setCode(surveyConf.getCode());
codeInfo.setUpdateDate(System.currentTimeMillis());
mongoRepository.save(codeInfo);
}
@Override
public SurveyConf getSurveyConfBySurveyId(String surveyId) {
Criteria criteria = Criteria.where("pageId").is(surveyId);
Query query = new Query(criteria);
return this.mongoRepository.findOne(query, SurveyConf.class);
}
}

View File

@ -0,0 +1,45 @@
package com.xiaojusurvey.engine.core.survey.impl;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: maple
* @CreateTime: 2024/6/10 21:29
* @Description: 问卷历史记录服务
*/
@Service
public class SurveyHistoryServiceImpl implements SurveyHistoryService {
@Resource
private MongoRepository mongoRepository;
@Override
public SurveyHistory addHistory(SurveyHistory surveyHistory) {
return mongoRepository.save(surveyHistory);
}
@Override
public List<SurveyHistory> getHistoryList(String surveyId, String historyType) {
// 组合查询条件
Query query = new Query();
query.addCriteria(Criteria.where("pageId").is(surveyId))
.addCriteria(Criteria.where("type").is(historyType));
// 设置查询选项
query.limit(100);
query.with(Sort.by(Sort.Direction.DESC, "createDate"));
// 选择查询的字段
query.fields().include("createDate").include("operator").include("type").include("_id");
return mongoRepository.page(query, 0, 100, SurveyHistory.class);
}
}

View File

@ -4,6 +4,8 @@ import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.SurveyService;
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 javax.annotation.Resource;
@ -18,6 +20,7 @@ public class SurveyServiceImpl implements SurveyService {
@Resource
private MongoRepository mongoRepository;
/**
* 创建问卷
*/
@ -27,4 +30,11 @@ public class SurveyServiceImpl implements SurveyService {
idResult.setId(mongoRepository.save(surveyMeta).getId());
return idResult;
}
@Override
public SurveyMeta getSurveyMeta(String surveyId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(surveyId));
return mongoRepository.findOne(query, SurveyMeta.class);
}
}

View File

@ -0,0 +1,22 @@
package com.xiaojusurvey.engine.core.survey.vo;
import lombok.Data;
import java.util.Map;
/**
* @Author: maple
* @CreateTime: 2024/6/15 20:16
* @Description:
*/
@Data
public class SurveyHistoryOutVO {
private String id;
private String type;
private Long createDate;
private Map<String, Object> operator;
}

View File

@ -0,0 +1,25 @@
package com.xiaojusurvey.engine.core.survey.vo;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;
/**
* @Author: maple
* @CreateTime: 2024/6/9 13:40
* @Description:
*/
@Data
public class SurveyInfoInVO {
@NotBlank(message = "问卷id不能为空", groups = {UpdateConf.class})
private String surveyId;
@NotNull(message = "问卷数据不能为空", groups = {UpdateConf.class})
private Map<String, Object> configData;
public interface UpdateConf {
}
}

View File

@ -0,0 +1,22 @@
package com.xiaojusurvey.engine.core.survey.vo;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author: maple
* @CreateTime: 2024/6/9 14:16
* @Description:
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SurveyInfoOutVO {
private SurveyMeta surveyMetaRes;
private SurveyConf surveyConfRes;
}

View File

@ -0,0 +1,40 @@
package com.xiaojusurvey.engine.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
/**
* @Author: maple
* @CreateTime: 2024/6/9 14:23
* @Description:
*/
@Getter
@Configuration
@Slf4j
public class BannerDataConfig implements InitializingBean {
@Value("classpath:banner.json")
private Resource bannerResource;
private JSONObject bannerData;
@Override
public void afterPropertiesSet() throws Exception {
try {
String bannerContent = new String(Files.readAllBytes(bannerResource.getFile().toPath()), StandardCharsets.UTF_8);
bannerData = JSON.parseObject(bannerContent);
} catch (Exception e) {
log.error("Initializing BannerDataConfig fail, e:", e);
}
}
}

View File

@ -1,16 +1,28 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.common.entity.user.User;
import com.xiaojusurvey.engine.common.enums.HistoryTypeEnum;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.common.util.RpcResultUtil;
import com.xiaojusurvey.engine.config.BannerDataConfig;
import com.xiaojusurvey.engine.core.reslut.IdResult;
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.vo.SurveyInfoInVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: LYF
@ -23,6 +35,15 @@ public class SurveyController {
@Resource
private SurveyService surveyService;
@Resource
private SurveyConfService surveyConfService;
@Resource
private SurveyHistoryService surveyHistoryService;
@Resource
private BannerDataConfig bannerDataConfig;
/**
* 创建问卷
*/
@ -30,4 +51,51 @@ public class SurveyController {
public RpcResult<IdResult> createSurvey(@Validated @RequestBody SurveyMeta surveyMeta) {
return RpcResultUtil.createSuccessResult(surveyService.createSurvey(surveyMeta));
}
/**
* 获取banner数据
*/
@GetMapping("/getBannerData")
public RpcResult<Object> getBannerData() {
return RpcResultUtil.createSuccessResult(bannerDataConfig.getBannerData());
}
/**
* 更新问卷
*/
@PostMapping("/updateConf")
public RpcResult<Boolean> updateConf(HttpServletRequest request,
@RequestBody @Validated(SurveyInfoInVO.UpdateConf.class) SurveyInfoInVO infoInVO) {
SurveyConf surveyConf = new SurveyConf();
surveyConf.setCode(infoInVO.getConfigData());
surveyConf.setPageId(infoInVO.getSurveyId());
surveyConfService.saveSurveyConfig(surveyConf);
User user = (User) request.getAttribute("user");
SurveyHistory surveyHistory = new SurveyHistory();
surveyHistory.setPageId(infoInVO.getSurveyId());
surveyHistory.setType(HistoryTypeEnum.DAILY_HIS.getHistoryType());
surveyHistory.setSchema(infoInVO.getConfigData());
surveyHistory.setCreateDate(System.currentTimeMillis());
surveyHistory.setUpdateDate(System.currentTimeMillis());
Map<String, Object> operator = new HashMap<>(2);
operator.put("_id", user.getId());
operator.put("username", user.getUsername());
surveyHistory.setOperator(operator);
surveyHistoryService.addHistory(surveyHistory);
return RpcResultUtil.createSuccessResult(true);
}
/**
* 获取问卷
*/
@GetMapping("/getSurvey")
public RpcResult<SurveyInfoOutVO> getSurvey(@RequestParam("surveyId") @NotBlank String surveyId) {
SurveyMeta surveyMeta = surveyService.getSurveyMeta(surveyId);
if (null == surveyMeta) {
return RpcResultUtil.createFailedResult(RespErrorCode.SURVEY_NOT_FOUND.getCode(), RespErrorCode.SURVEY_NOT_FOUND.getMessage());
}
SurveyConf surveyConf = surveyConfService.getSurveyConfBySurveyId(surveyId);
return RpcResultUtil.createSuccessResult(new SurveyInfoOutVO(surveyMeta, surveyConf));
}
}

View File

@ -0,0 +1,49 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.common.util.RpcResultUtil;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.vo.SurveyHistoryOutVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Author: maple
* @CreateTime: 2024/6/10 21:10
* @Description:
*/
@RequestMapping("/api/surveyHistory")
@RestController
public class SurveyHistoryController {
@Resource
private SurveyHistoryService surveyHistoryService;
/**
* 获取问卷历史列表
*/
@GetMapping("/getList")
public RpcResult<List<SurveyHistoryOutVO>> getList(@RequestParam("surveyId") String surveyId, @RequestParam("historyType") String historyType) {
List<SurveyHistory> historyList = surveyHistoryService.getHistoryList(surveyId, historyType);
if (null == historyList || historyList.isEmpty()) {
return RpcResultUtil.createSuccessResult(new ArrayList<>());
}
List<SurveyHistoryOutVO> resultList = historyList.stream().map(e -> {
SurveyHistoryOutVO outVO = new SurveyHistoryOutVO();
outVO.setId(e.getId());
outVO.setOperator(e.getOperator());
outVO.setCreateDate(e.getCreateDate());
return outVO;
}).collect(Collectors.toList());
return RpcResultUtil.createSuccessResult(resultList);
}
}

View File

@ -47,6 +47,7 @@ public class LoginInterceptor implements HandlerInterceptor {
throw new ServiceException(RespErrorCode.USER_CREDENTIALS_ERROR.getMessage(), RespErrorCode.USER_CREDENTIALS_ERROR.getCode());
}
User user = userService.loadUserByUsernameAndPassword(username, password);
request.setAttribute("user", user);
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}

View File

@ -0,0 +1,175 @@
{
"temp": {
"key": "default",
"name": "全部",
"list": [{
"src": "/imgs/skin/17e06b7604a007e1d3e1453b9ddadc3c.webp",
"title": "1"
}]
},
"scenery": {
"key": "scenery",
"name": "风景",
"list": [{
"src": "/imgs/skin/SyiLRcukyE1558430525760.webp",
"title": "1"
}, {
"src": "/imgs/skin/sqYig4AcWr1558430525663.webp",
"title": "2"
}, {
"src": "/imgs/skin/ElNeqJT2I21558430526165.webp",
"title": "3"
}, {
"src": "/imgs/skin/CxQkSU6AY21558430526163.webp",
"title": "4"
}, {
"src": "/imgs/skin/VTUwbp6vY61558430527320.webp",
"title": "5"
}, {
"src": "/imgs/skin/SHs0K703Yn1558430527218.webp",
"title": "6"
}, {
"src": "/imgs/skin/oVTedX9V4s1558430527671.webp",
"title": "7"
}]
},
"business": {
"key": "business",
"name": "商务",
"list": [{
"src": "/imgs/skin/3ABKqvDaVn1558514860472.webp",
"title": "1"
}, {
"src": "/imgs/skin/OewuaQmWoq1558514860285.webp",
"title": "2"
}, {
"src": "/imgs/skin/HuVqqtbFjs1558514860570.webp",
"title": "3"
}, {
"src": "/imgs/skin/icSlqsr0uZ1558514860875.webp",
"title": "4"
}, {
"src": "/imgs/skin/Qu9rg33wmq1558514861015.webp",
"title": "5"
}, {
"src": "/imgs/skin/145gBCRtNP1558514861211.webp",
"title": "6"
}, {
"src": "/imgs/skin/ykWLFV0QWj1558514861444.webp",
"title": "7"
}]
},
"activity": {
"key": "activity",
"name": "节日",
"list": [{
"src": "/imgs/skin/Ixx8hqiwwk1660979120801.webp",
"title": "1"
}]
},
"creative": {
"key": "creative",
"name": "创意",
"list": [{
"src": "/imgs/skin/PLwNH1rAie1558430219772.webp",
"title": "1"
}, {
"src": "/imgs/skin/NnXsAOhBNm1558430219312.webp",
"title": "2"
}, {
"src": "/imgs/skin/ujpUoWqhw31558430220124.webp",
"title": "3"
}, {
"src": "/imgs/skin/5OCvbjqJQm1558430220362.webp",
"title": "4"
}, {
"src": "/imgs/skin/0k7Jg7In8I1558430221154.webp",
"title": "5"
}, {
"src": "/imgs/skin/UH0A8DbTai1558430221033.webp",
"title": "6"
}, {
"src": "/imgs/skin/FRIzPC6ZtN1558430221344.webp",
"title": "7"
}]
},
"transportation": {
"key": "transportation",
"name": "交通",
"list": [{
"src": "/imgs/skin/XYKqJZuMig1558430904735.webp",
"title": "1"
}, {
"src": "/imgs/skin/GnPatsr48Z1558430904680.webp",
"title": "2"
}, {
"src": "/imgs/skin/UqIvVvEXAK1558430905204.webp",
"title": "3"
}, {
"src": "/imgs/skin/PUssufh5uI1558430905104.webp",
"title": "4"
}, {
"src": "/imgs/skin/O409pRTDlW1558430905738.webp",
"title": "5"
}, {
"src": "/imgs/skin/A9FzlbYXqI1558430905739.webp",
"title": "6"
}, {
"src": "/imgs/skin/HN9YGctDeF1558430906686.webp",
"title": "7"
}]
},
"delicacy": {
"key": "delicacy",
"name": "美食",
"list": [{
"src": "/imgs/skin/lE6PSclCcU1558434536703.webp",
"title": "1"
}, {
"src": "/imgs/skin/OnSdbm7u6n1558434536641.webp",
"title": "2"
}, {
"src": "/imgs/skin/N9Z2ZuyO731558434537314.webp",
"title": "3"
}, {
"src": "/imgs/skin/YP9PoW8pX51558434537301.webp",
"title": "4"
}, {
"src": "/imgs/skin/zUtDv378bg1558434538351.webp",
"title": "5"
}, {
"src": "/imgs/skin/gY1JljCow21558434538303.webp",
"title": "6"
}, {
"src": "/imgs/skin/oOjHPbABdd1558434538864.webp",
"title": "7"
}]
},
"campus": {
"key": "campus",
"name": "校园",
"list": [{
"src": "/imgs/skin/4aWi5JxG471558514268698.webp",
"title": "1"
}, {
"src": "/imgs/skin/j8C2OBP7WK1558514268563.webp",
"title": "2"
}, {
"src": "/imgs/skin/q3uJoQhYsR1558514268877.webp",
"title": "3"
}, {
"src": "/imgs/skin/W5PPlNsmsr1558514269088.webp",
"title": "4"
}, {
"src": "/imgs/skin/6xQk1IAmKt1558514269874.webp",
"title": "5"
}, {
"src": "/imgs/skin/XQE2iyF0rj1558514269935.webp",
"title": "6"
}, {
"src": "/imgs/skin/POHlQiSwPR1558514270379.webp",
"title": "7"
}]
}
}

View File

@ -1,60 +0,0 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.SurveyApplication;
import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.core.auth.param.UserParam;
import com.xiaojusurvey.engine.core.auth.vo.UserVo;
import com.xiaojusurvey.engine.core.auth.util.JwtTokenUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
/**
* 问卷单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SurveyApplication.class)
public class AuthTest {
@Resource
private JwtTokenUtil jwtTokenUtil;
@Resource
AuthController authController;
@Test
public void captcha() {
RpcResult<CaptchaVo> captcha = authController.captcha();
System.out.println(captcha);
}
@Test
public void register() {
UserParam userParam = new UserParam();
userParam.setUsername("admin");
userParam.setPassword("admin");
userParam.setCaptchaId("666169681618c21f3f6b90cd");
userParam.setCaptcha("3377");
RpcResult<UserVo> register = authController.register(userParam);
System.out.println(register);
}
@Test
public void login() {
//eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNzE3Njg4ODcyLCJqdGkiOiI4MDYwMDhhOC0xODVkLTQ1YjgtYmNhMy04YzRiNmRhN2IyNjcifQ.Sq2WMKsdOTqo-Xbw_g_FjQWv1pwRwWwUAJIDON9c2jA
UserParam userParam = new UserParam();
userParam.setUsername("admin");
userParam.setPassword("admin");
userParam.setCaptchaId("66617239de72dc5238d1c32e");
userParam.setCaptcha("1167");
RpcResult<UserVo> register = authController.login(userParam);
System.out.println(register);
}
}

View File

@ -0,0 +1,93 @@
package com.xiaojusurvey.engine.controller;
import com.alibaba.fastjson.JSONObject;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.common.entity.user.User;
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.vo.SurveyInfoInVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO;
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.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: maple
* @CreateTime: 2024/6/15 20:40
* @Description:
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class SurveyControllerTest {
@InjectMocks
SurveyController surveyController;
@Mock
private SurveyService surveyService;
@Mock
private SurveyConfService surveyConfService;
@Mock
private SurveyHistoryService surveyHistoryService;
@Mock
private BannerDataConfig bannerDataConfig;
private HttpServletRequest httpServletRequest;
@Before
public void initBean() {
httpServletRequest = new MockHttpServletRequest();
User user = new User();
user.setId("123");
user.setUsername("maple");
httpServletRequest.setAttribute("user", user);
}
@Test
public void getBannerData() {
Mockito.when(bannerDataConfig.getBannerData()).thenReturn(new JSONObject());
RpcResult<Object> bannerData = surveyController.getBannerData();
Assert.assertTrue(bannerData.getSuccess());
Assert.assertEquals(new Integer(200), bannerData.getCode());
}
@Test
public void updateConf() {
Mockito.doNothing().when(surveyConfService).saveSurveyConfig(Mockito.any());
Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory());
RpcResult<Boolean> updateConf = surveyController.updateConf(httpServletRequest, new SurveyInfoInVO());
Assert.assertTrue(updateConf.getSuccess());
Assert.assertEquals(new Integer(200), updateConf.getCode());
}
@Test
public void getSurvey() {
Mockito.when(surveyService.getSurveyMeta(Mockito.anyString())).thenReturn(null);
Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory());
RpcResult<SurveyInfoOutVO> surveyResult = surveyController.getSurvey("112133");
Assert.assertFalse(surveyResult.getSuccess());
Assert.assertEquals(new Integer(3004), surveyResult.getCode());
Mockito.when(surveyService.getSurveyMeta(Mockito.anyString())).thenReturn(new SurveyMeta());
Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory());
RpcResult<SurveyInfoOutVO> surveyResult2 = surveyController.getSurvey("112133");
Assert.assertTrue(surveyResult2.getSuccess());
Assert.assertEquals(new Integer(200), surveyResult2.getCode());
}
}

View File

@ -0,0 +1,52 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.vo.SurveyHistoryOutVO;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @Author: maple
* @CreateTime: 2024/6/15 20:30
* @Description:
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class SurveyHistoryControllerTest {
@InjectMocks
SurveyHistoryController surveyHistoryController;
@Mock
SurveyHistoryService surveyHistoryService;
@Test
public void getList() {
Mockito.when(surveyHistoryService.getHistoryList(Mockito.any(), Mockito.any())).thenReturn(new ArrayList<>());
RpcResult<List<SurveyHistoryOutVO>> result = surveyHistoryController.getList("1", "dailyHis");
Assert.assertTrue(result.getSuccess());
Assert.assertEquals(new Integer(200), result.getCode());
List<SurveyHistory> historyList = new ArrayList<>();
SurveyHistory history = new SurveyHistory();
history.setId("111");
history.setCreateDate(System.currentTimeMillis());
history.setOperator(new HashMap<>());
historyList.add(history);
Mockito.when(surveyHistoryService.getHistoryList(Mockito.any(), Mockito.any())).thenReturn(historyList);
RpcResult<List<SurveyHistoryOutVO>> resultList = surveyHistoryController.getList("1", "dailyHis");
Assert.assertTrue(resultList.getSuccess());
Assert.assertEquals(new Integer(200), resultList.getCode());
Assert.assertFalse(resultList.getData().isEmpty());
}
}

View File

@ -1,32 +0,0 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.SurveyApplication;
import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
/**
* 问卷单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SurveyApplication.class)
public class SurveyMetaTest {
@Resource
private SurveyController surveyController;
@Test
public void createSurvey() {
SurveyMeta surveyMeta = new SurveyMeta();
surveyMeta.setTitle("测试问卷");
surveyMeta.setSurveyType("1");
surveyMeta.setCreateMethod("1");
surveyMeta.setCreateFrom("1");
surveyController.createSurvey(surveyMeta);
}
}