Commit 886640a6 authored by huangcb's avatar huangcb

增加删除功能单元测试

parent 280410ec
......@@ -32,6 +32,13 @@ public class DeleteController {
this.fileMetaService = fileMetaService;
}
/**
* description 删除单个文件
* param [fileForm]
* return com.esv.freight.file.common.response.EResponse
* author Administrator
* createTime 2020/05/22 9:26
**/
@PostMapping("/single")
public EResponse deleteSingleFile(@RequestBody @Validated(ValidatorDelete.class) FileForm fileForm) throws EException {
fileMetaService.deleteFile(fileForm);
......
......@@ -4,6 +4,8 @@ import com.esv.freight.file.common.validator.groups.ValidatorDelete;
import com.esv.freight.file.common.validator.groups.ValidatorDetail;
import com.esv.freight.file.common.validator.groups.ValidatorInsert;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
......@@ -34,4 +36,9 @@ public class FileForm {
@Length(max = 64, message = "参数id长度不合法", groups = {ValidatorDelete.class, ValidatorDetail.class})
@NotBlank(message = "参数id不能为空", groups = {ValidatorDelete.class, ValidatorDetail.class})
private String id;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
package com.esv.freight.file.module.file.controller;
import com.alibaba.fastjson.JSONObject;
import com.esv.freight.file.BaseTestController;
import com.esv.freight.file.common.response.ECode;
import com.esv.freight.file.module.file.form.FileForm;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
/**
* @description:
* @project: freight-file-service
* @name: com.esv.freight.file.module.file.controller.DeleteControllerTest
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/05/22 9:25
* @version:1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DeleteControllerTest extends BaseTestController {
@Test
public void a1_deleteSingleFile_success_test() throws Exception {
String url = "/delete/single";
FileForm form = new FileForm();
form.setId("5ec6469a29c8560d78274b4b");
MvcResult mvcResult = this.getMockMvc().perform(MockMvcRequestBuilders.post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(form.toString()))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
String responseStr = mvcResult.getResponse().getContentAsString();
log.info(responseStr);
JSONObject result = JSONObject.parseObject(responseStr);
Assert.assertEquals(ECode.SUCCESS.code(), result.getIntValue("code"));
}
}
......@@ -100,6 +100,35 @@ public class UploadControllerTest extends BaseTestController {
fileUrl = result.getJSONObject("data").getString("url");
}
@Test
public void a3_uploadSingleFile_word_success_test() throws Exception {
String url = "/upload/single";
JSONObject reqJson = new JSONObject();
// 构造数据
String filepath = "D:\\test\\线上合同-货主.docx";
File file = new File(filepath);
InputStream input = new FileInputStream(file);
reqJson.put("fileType", "word");
reqJson.put("fileData", Base64.getEncoder().encodeToString(IOUtils.toByteArray(input)));
reqJson.put("fileName", "线下合同-货主");
MvcResult mvcResult = this.getMockMvc().perform(MockMvcRequestBuilders.post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(reqJson.toJSONString()))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
String responseStr = mvcResult.getResponse().getContentAsString();
log.info(responseStr);
JSONObject result = JSONObject.parseObject(responseStr);
Assert.assertEquals(ECode.SUCCESS.code(), result.getIntValue("code"));
fileId = result.getJSONObject("data").getString("id");
fileUrl = result.getJSONObject("data").getString("url");
}
@Test
public void b1_uploadSingleFile_file_type_error_test() throws Exception {
String url = "/upload/single";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment