提交 2c0fad42 authored 作者: ww1xhqc's avatar ww1xhqc

[数据模型] 用MockMVC Post请求测试成功两个接口

上级 2eca9b9d
...@@ -100,7 +100,11 @@ ...@@ -100,7 +100,11 @@
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
......
...@@ -45,8 +45,8 @@ public class ModelController { ...@@ -45,8 +45,8 @@ public class ModelController {
* @Date 16:19 2021/3/4 * @Date 16:19 2021/3/4
**/ **/
@ApiOperation("得到所有数据表信息") @ApiOperation("得到所有数据表信息")
@GetMapping("/getAllEntity") @PostMapping("/getAllEntity")
public ResponseEntity getAllEntity(SearchTableInfoVo searchTableInfoVo) { public ResponseEntity getAllEntity(@RequestBody SearchTableInfoVo searchTableInfoVo) {
Page<TableInfo> tableInfos = null; Page<TableInfo> tableInfos = null;
try { try {
tableInfos = modelService.listAllEntities(searchTableInfoVo); tableInfos = modelService.listAllEntities(searchTableInfoVo);
...@@ -64,9 +64,9 @@ public class ModelController { ...@@ -64,9 +64,9 @@ public class ModelController {
* @Description 根据表名得到所有字段名 * @Description 根据表名得到所有字段名
* @Date 16:20 2021/3/4 * @Date 16:20 2021/3/4
**/ **/
@ApiOperation("根据表名获查询所有字段") @ApiOperation("根据表名或者id查询表中所有字段")
@GetMapping("/getAllField") @PostMapping("/getAllField")
public ResponseEntity getFields(SearchColumnInfoVo searchColumnInfoVo) { public ResponseEntity getFields(@RequestBody SearchColumnInfoVo searchColumnInfoVo) {
return ResultUtil.success(modelService.showModelFields(searchColumnInfoVo), ""); return ResultUtil.success(modelService.showModelFields(searchColumnInfoVo), "");
} }
......
...@@ -23,7 +23,7 @@ import java.io.Serializable; ...@@ -23,7 +23,7 @@ import java.io.Serializable;
@Data @Data
@SQLDelete(sql = "update column_info set deleted = 1 where id = ?") @SQLDelete(sql = "update column_info set deleted = 1 where id = ?")
@Where(clause = "deleted = 0") @Where(clause = "deleted = 0")
public class ColumnInfo extends BaseEntity implements Serializable { public class ColumnInfo extends BaseEntity {
/** /**
* 是否primary key, 0是,1否 * 是否primary key, 0是,1否
......
...@@ -25,7 +25,7 @@ import java.io.Serializable; ...@@ -25,7 +25,7 @@ import java.io.Serializable;
@Data @Data
@SQLDelete(sql = "update table_info set deleted = 1 where id = ?") @SQLDelete(sql = "update table_info set deleted = 1 where id = ?")
@Where(clause = "deleted = 0") @Where(clause = "deleted = 0")
public class TableInfo extends BaseEntity implements Serializable { public class TableInfo extends BaseEntity {
@ApiModelProperty("表名,不能为空") @ApiModelProperty("表名,不能为空")
......
...@@ -21,6 +21,8 @@ public class TableVO { ...@@ -21,6 +21,8 @@ public class TableVO {
private String modelTitle; private String modelTitle;
@ApiModelProperty("表名") @ApiModelProperty("表名")
private String modelName; private String modelName;
@ApiModelProperty("详细描述")
private String description;
/** /**
* 0扫描,1自建,2扫描后自选 * 0扫描,1自建,2扫描后自选
*/ */
......
...@@ -13,6 +13,7 @@ import com.tykj.workflowcore.model_layer.utils.CreateTableUtil; ...@@ -13,6 +13,7 @@ import com.tykj.workflowcore.model_layer.utils.CreateTableUtil;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.internal.SessionImpl; import org.hibernate.internal.SessionImpl;
...@@ -81,25 +82,25 @@ public class ModelImpl implements ModelService { ...@@ -81,25 +82,25 @@ public class ModelImpl implements ModelService {
@Override @Override
public Page<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) { public Page<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) {
PredicateBuilder<TableInfo> and = Specifications.and(); PredicateBuilder<TableInfo> and = Specifications.and();
and.like(searchTableInfoVo.getModelName() != null, "modelName", "%" + searchTableInfoVo.getModelName() + "%"); and.like(searchTableInfoVo.getModelName() != null&& StringUtils.isNotEmpty(searchTableInfoVo.getModelName()), "modelName", "%" + searchTableInfoVo.getModelName() + "%");
and.like(searchTableInfoVo.getModelTitle() != null, "modelTitle", "%" + searchTableInfoVo.getModelTitle() + "%"); and.like(searchTableInfoVo.getModelTitle() != null&& StringUtils.isNotEmpty(searchTableInfoVo.getModelTitle()), "modelTitle", "%" + searchTableInfoVo.getModelTitle() + "%");
and.eq(searchTableInfoVo.getModelType() != null, "modelType", searchTableInfoVo.getModelType()); and.eq(searchTableInfoVo.getModelType() != null, "modelType", searchTableInfoVo.getModelType());
return tableInfoDao.findAll(and.build(), searchTableInfoVo.getPageable()); return tableInfoDao.findAll(and.build(), searchTableInfoVo.getPageable());
} }
/** /**
* @param SearchColumnInfoVo * @param searchColumnInfoVo
* @return java.util.List<com.tykj.workflowcore.model_layer.model.ColumnInfo> * @return java.util.List<com.tykj.workflowcore.model_layer.model.ColumnInfo>
* @Author WWW * @Author WWW
* @Description 根据表名得到所有字段名 * @Description 根据表名得到所有字段名
* @Date 16:15 2021/3/5 * @Date 16:15 2021/3/5
**/ **/
@Override @Override
public List<ColumnInfo> showModelFields(SearchColumnInfoVo SearchColumnInfoVo) { public List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo) {
PredicateBuilder<ColumnInfo> and = Specifications.and(); PredicateBuilder<ColumnInfo> and = Specifications.and();
and.eq(SearchColumnInfoVo.getDbId() != null, "dbId", SearchColumnInfoVo.getDbId()); and.eq(searchColumnInfoVo.getDbId() != null, "dbId",searchColumnInfoVo.getDbId());
and.eq(SearchColumnInfoVo.getDbName() != null, "dbName", SearchColumnInfoVo.getDbName()); and.eq(searchColumnInfoVo.getDbName() != null&&StringUtils.isNotEmpty(searchColumnInfoVo.getDbName()), "dbName", searchColumnInfoVo.getDbName());
return columnInfoDao.findAll(and.build()); return columnInfoDao.findAll(and.build());
} }
......
...@@ -2,7 +2,7 @@ spring: ...@@ -2,7 +2,7 @@ spring:
datasource: datasource:
username: root username: root
password: Huang123+ password: Huang123+
url: jdbc:mysql://47.106.142.73:3306/www2?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&nullCatalogMeansCurrent=true url: jdbc:mysql://47.106.142.73:3306/www2?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
jpa: jpa:
show-sql: true show-sql: true
......
## db.setting文件
url = jdbc:mysql://47.106.142.73:3306/www2?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&nullCatalogMeansCurrent=true
user = root
pass = Huang123+
## 可选配置
# 是否在日志中显示执行的SQL
showSql = true
# 是否格式化显示的SQL
formatSql = true
# 是否显示SQL参数
showParams = true
# 打印SQL的日志等级,默认debug,可以是info、warn、error
sqlLevel = info
\ No newline at end of file
package com.tykj.workflowcore; package com.tykj.workflowcore;
import com.tykj.workflowcore.model_layer.controller.ModelController;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
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.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@SpringBootTest import javax.print.attribute.standard.Media;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class WorkflowCoreApplicationTests { class WorkflowCoreApplicationTests {
@Autowired
ModelController modelController;
@Autowired
MockMvc mockMvc;
@Before("")
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(modelController).build();
}
@Test @Test
void contextLoads() { void contextLoads() {
System.out.println("11111111111111111111111");
}
//#####################################模型层测试 start##############################################
RequestBuilder request;
@Test
public void SearchTableVOTest() throws Exception {
String SearchTableinfo_Json="{\n" +
" \"modelName\":\"peop\",\n" +
" \"modelTitle\":\"\",\n" +
" \"modelType\":\"\"\n" +
"}";
request = post("/model/getAllEntity/")
.contentType(MediaType.APPLICATION_JSON)
.content(SearchTableinfo_Json)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(not(""))) //条件
.andExpect(content().string(not("[]")))
.andDo(print());//打印输出结果
}
@Test
public void testSearchColumnVO() throws Exception {
String searchColumnV0_json="{\n" +
"\"dbId\":2\n" +
"}";
String searchColumnV0_json2= "{\n" +
"\"dbName\":\"techer\"\n" +
"}";
request = post("/model/getAllField/")
.contentType(MediaType.APPLICATION_JSON)
.content(searchColumnV0_json2)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(not("")))
.andExpect(content().string(not("[]")))
.andDo(print());//打印输出结果
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论