package com.qiwu.chatbotbackend; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.ArchiveInfo; import com.qiwu.chatbotbackend.entity.ChatCache; import com.qiwu.chatbotbackend.processing.ChatCacheController; import com.qiwu.chatbotbackend.task.utils.DataDeliver; import org.junit.Test; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.web.WebAppConfiguration; import javax.servlet.ServletContext; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.junit.Assert.assertEquals; @WebAppConfiguration public class ChatCacheTests { @Test public void testFindChatCache() throws Exception { ServletContext context = new MockServletContext(); Map hisInfoPoint = new HashMap(){ { put("site", "Junit"); } }; GameEnv gameEnv = new GameEnv("7011489633"); ChatCache chatCache = new ChatCache("1112@@@123456789", "rand"); chatCache.setGameEnv(gameEnv); chatCache.setInfoPoint(hisInfoPoint); chatCache.setGameStart(true); //先删除就数据 SqlHelper helper = new SqlHelper(); ChatCacheController.deleteChatCache(helper, "123456789"); Map chatCacheMap = null; chatCacheMap = new HashMap(); context.setAttribute("chatCache", chatCacheMap); assertEquals(false, ChatCacheController.findChatCache("111@@@123456789", context)); assertEquals(false, ChatCacheController.findChatCache("1112@@@123456789", context)); chatCacheMap.put("1112@@@123456789", chatCache); assertEquals(true, ChatCacheController.findChatCache("1112@@@123456789", context)); Boolean flag = ChatCacheController.findChatCache("1112@@@123456789", context); assertEquals(true, flag); } public void testDataDeliver(){ System.out.println("开始"); DataDeliver deliver = new DataDeliver(); JSONObject data = new JSONObject(); data.put("chat_key", "101@@@123456789@@@@123"); data.put("data_type", "archiveList"); data.put("data", new JSONObject()); deliver.setData(data); deliver.start(); //while(true){} } public void testArchiveQuery(){ SqlHelper helper = null; try { helper = new SqlHelper(); } catch (SQLException e) { e.printStackTrace(); } List archiveList = helper.queryArchiveByUserKey("@jXgDt40DXCR22Jk7XcJU"); assertEquals(1, archiveList.size()); } }