use of javax.transaction.Transactional in project neubbs by nuitcoder.
the class MessageDAOTest method testSaveMessage.
/**
* 测试保存消息
*/
@Test
@Transactional
public void testSaveMessage() {
MessageDO message = this.saveTestMessageDOToDatabase();
System.out.println("success save message: " + message);
}
use of javax.transaction.Transactional in project neubbs by nuitcoder.
the class TopicControllerTest method testReleaseTopicReplySuccess.
/**
* 测试 /api/topic/reply (POST)
* - 发布话题回复成功
* - 需要权限: @LoginAuthorization @AccountActivation
*/
@Test
@Transactional
public void testReleaseTopicReplySuccess() throws Exception {
int topicId = 1;
String content = "new reply content";
String requestBody = "{" + util.getJsonField("topicid", topicId) + "," + util.getJsonField("content", content) + "}";
System.out.println("input request-body: " + requestBody);
// before publish reply
TopicDO beforeTopic = topicDAO.getTopicById(topicId);
Assert.assertNotNull(beforeTopic);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/api/topic/reply").cookie(util.getAlreadyLoginUserCookie()).contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.model").exists()).andReturn();
Map resultMap = (Map) JSON.parse(result.getResponse().getContentAsString());
// judge $.model.replyid
Map modelMap = (Map) resultMap.get("model");
// validate reply information
TopicReplyDO reply = topicReplyDAO.getTopicReplyById((Integer) modelMap.get("replyid"));
Assert.assertEquals(topicId, (int) reply.getTopicid());
Assert.assertEquals(content, reply.getContent());
// validate topic information
TopicDO afterTopic = topicDAO.getTopicById(reply.getTopicid());
Assert.assertNotNull(afterTopic);
Assert.assertEquals(beforeTopic.getReplies() + 1, (int) afterTopic.getReplies());
Assert.assertNotEquals(beforeTopic.getLastreplytime(), afterTopic.getLastreplytime());
Assert.assertEquals(reply.getCreatetime(), afterTopic.getLastreplytime());
util.printSuccessMessage();
}
use of javax.transaction.Transactional in project neubbs by nuitcoder.
the class TopicControllerTest method testLikeTopicSuccess.
/**
* 测试 /api/topic/like
* - 话题点赞 + 1 成功 & 话题点赞 - 1 成功
* - 需要权限:@LoginAuthorization @AccountActivation
* - 旧接口,需要输入 command 命令参数(inc - 点赞 or dec - 取消)
*/
@Test
@Transactional
public void testLikeTopicSuccess() throws Exception {
int topicId = 1;
// topic like + 1
String command = "inc";
String requestBody = "{" + util.getJsonField("topicid", topicId) + ", " + util.getJsonField("command", command) + "}";
System.out.println("input dec request-body: " + requestBody);
Cookie cookie = util.getAlreadyLoginUserCookie();
UserDO cookieUser = SecretUtil.decryptUserInfoToken(cookie.getValue());
int cookieUserId = cookieUser.getId();
int beforeTopicContentLike = topicContentDAO.getTopicContentByTopicId(topicId).getLike();
mockMvc.perform(MockMvcRequestBuilders.post("/api/topic/like").cookie(cookie).contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.model").exists()).andExpect(MockMvcResultMatchers.jsonPath("$.model.like").value(CoreMatchers.is(beforeTopicContentLike + 1)));
// judge 'forum_user_action' new insert topicId
JSONArray jsonArray = JSON.parseArray(userActionDAO.getUserActionLikeTopicIdJsonArray(cookieUserId));
Assert.assertEquals(topicId, (int) jsonArray.get(jsonArray.size() - 1));
// judge 'forum_topic_action' new insert userId
jsonArray = JSON.parseArray(topicActionDAO.getTopicActionLikeUserIdJsonArray(topicId));
Assert.assertEquals(cookieUserId, jsonArray.get(jsonArray.size() - 1));
// topic like - 1
command = "dec";
requestBody = "{" + util.getJsonField("topicid", topicId) + ", " + util.getJsonField("command", command) + "}";
System.out.println("input 'dec' request-body: " + requestBody);
mockMvc.perform(MockMvcRequestBuilders.post("/api/topic/like").cookie(cookie).contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.model").exists()).andExpect(MockMvcResultMatchers.jsonPath("$.model.like").value(CoreMatchers.is(beforeTopicContentLike)));
Assert.assertEquals(beforeTopicContentLike, (int) topicContentDAO.getTopicContentByTopicId(topicId).getLike());
Assert.assertEquals(-1, JsonUtil.getIntElementIndex(userActionDAO.getUserActionLikeTopicIdJsonArray(cookieUserId), topicId));
Assert.assertEquals(-1, JsonUtil.getIntElementIndex(topicActionDAO.getTopicActionLikeUserIdJsonArray(topicId), cookieUserId));
util.printSuccessMessage();
}
use of javax.transaction.Transactional in project neubbs by nuitcoder.
the class FileControllerTest method testUploadUserAvatarSuccess.
/*
* ***********************************************
* api test method
* ***********************************************
*/
/**
* 测试 /api/file/avatar
* - 测试上传用户头像成功
* - 需要权限:@LoginAuthorization, @AccountActivation
*/
@Test
@Transactional
public void testUploadUserAvatarSuccess() throws Exception {
// build MultipartFile type(html <> name, file name,file type,size)
String htmlInputName = "avatarImageFile";
String uploadFileName = "testAvatarFile";
byte[] fileBytes = new byte[1024 * 1];
MockMultipartFile[] files = { new MockMultipartFile(htmlInputName, uploadFileName + ".jpg", "image/jpg", fileBytes), new MockMultipartFile(htmlInputName, uploadFileName + ".png", "image/png", fileBytes), new MockMultipartFile(htmlInputName, uploadFileName + ".jpeg", "image/jpeg", fileBytes) };
// upload user avatar image file
for (MockMultipartFile file : files) {
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/api/file/avatar").file(file).cookie(util.getAlreadyLoginUserCookie()).contentType(MediaType.MULTIPART_FORM_DATA_VALUE).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.UPLOAD_SUCCESS)).andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
System.out.println("upload " + file.getOriginalFilename() + " success!");
}
/*
* delete already upload user avatar image file
* - test local
* - upload path: user.image.upload.path = ... (neubbs.properties)
* - test ftp server
*/
// System.out.println("delete local has bean uploaded test avatar file ......");
// String localUploadPath = webApplicationContext.getServletContext()
// .getRealPath(neubbsConfig.getUserImageUploadPath());
// File directory = new File(localUploadPath);
// File[] allFiles = directory.listFiles();
// for (File file: allFiles) {
// if (file.getName().contains(uploadFileName)) {
// file.deleteOnExit();
// System.out.println("already delete " + file.getName());
// }
// }
// System.out.println("all local test file delete finished!");
System.out.println("delete ftp server has bean uploaded test avatar file ......");
UserDO user = userService.getUserInfoByName("suvan");
String ftpUserAvatarPath = "/user/" + user.getId() + "-" + user.getName() + "/avator/";
List<String> listFileName = FtpUtil.listServerPathFileName(ftpUserAvatarPath);
for (String fName : listFileName) {
if (fName.contains(uploadFileName)) {
FtpUtil.delete(ftpUserAvatarPath, fName);
System.out.println("already delete " + fName);
}
}
System.out.println("all ftp server test file delete finished!");
util.printSuccessMessage();
}
use of javax.transaction.Transactional in project neubbs by nuitcoder.
the class TopicControllerTest method testAttentionTopicSuccess.
/**
* 测试 /api/topic/attention
* - 关注话题成功 + 1 和 -1
* - 需要权限: @LoginAuthorization @AccountActivation
*/
@Test
@Transactional
public void testAttentionTopicSuccess() throws Exception {
int topicId = 1;
Cookie cookie = util.getAlreadyLoginUserCookie();
UserDO cookieUser = SecretUtil.decryptUserInfoToken(cookie.getValue());
int cookieUserId = cookieUser.getId();
String requestBody = "{" + util.getJsonField("topicid", topicId) + "}";
// attention topic + 1
System.out.println("input 'inc' request-body: " + requestBody);
mockMvc.perform(MockMvcRequestBuilders.post("/api/topic/attention").cookie(cookie).contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.model").exists()).andExpect(MockMvcResultMatchers.jsonPath("$.model.userAttentionTopicId").value(CoreMatchers.hasItem(topicId)));
// compare forum_topic_action 'fta_attention_fu_id_array'
JSONArray jsonArray = JSON.parseArray(topicActionDAO.getTopicActionAttentionUserIdJsonArray(topicId));
Assert.assertEquals(cookieUserId, jsonArray.get(jsonArray.size() - 1));
// compare forum_user_action 'fua_following_fu_id_array'
jsonArray = JSON.parseArray(userActionDAO.getUserActionAttentionTopicIdJsonArray(cookieUserId));
Assert.assertEquals(topicId, jsonArray.get(jsonArray.size() - 1));
// collect topic - 1
System.out.println("input 'dec' request-body: " + requestBody);
mockMvc.perform(MockMvcRequestBuilders.post("/api/topic/attention").cookie(cookie).contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.message").exists()).andExpect(MockMvcResultMatchers.jsonPath("$.model.userAttentionTopicId").isArray());
Assert.assertEquals(-1, JsonUtil.getIntElementIndex(topicActionDAO.getTopicActionAttentionUserIdJsonArray(topicId), cookieUserId));
Assert.assertEquals(-1, JsonUtil.getIntElementIndex(userActionDAO.getUserActionAttentionTopicIdJsonArray(cookieUserId), topicId));
util.printSuccessMessage();
}
Aggregations