模块化拆分
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package cn.celess.visitor.controller;
|
||||
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.VisitorModel;
|
||||
import cn.celess.common.test.BaseTest;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.SUCCESS;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
|
||||
public class VisitorControllerTest extends BaseTest {
|
||||
private final TypeReference<?> VISITOR_PAGE_TYPE = new TypeReference<Response<PageData<VisitorModel>>>() {
|
||||
};
|
||||
private final TypeReference<?> VISITOR_TYPE = new TypeReference<Response<VisitorModel>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void getVisitorCount() throws Exception {
|
||||
getMockData(get("/visitor/count")).andDo(result -> {
|
||||
Response<Object> response = getResponse(result);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void page() throws Exception {
|
||||
int count = 10;
|
||||
int page = 1;
|
||||
// 默认显示location
|
||||
getMockData(get("/admin/visitor/page?count=" + count + "&page=" + page), adminLogin()).andDo(result -> {
|
||||
Response<PageData<VisitorModel>> response = getResponse(result, VISITOR_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PageData<VisitorModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
for (VisitorModel v : pageData.getList()) {
|
||||
assertNotEquals(0, v.getId());
|
||||
assertNotNull(v.getDate());
|
||||
assertNotNull(v.getLocation());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void add() throws Exception {
|
||||
getMockData(post("/visit")).andDo(result -> {
|
||||
Response<VisitorModel> response = getResponse(result, VISITOR_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
VisitorModel visitorModel = response.getResult();
|
||||
assertNotEquals(0, visitorModel.getId());
|
||||
assertNotNull(visitorModel.getIp());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dayVisitCount() throws Exception {
|
||||
getMockData(get("/dayVisitCount")).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
// 手动测试
|
||||
// @Test
|
||||
public void ipLocation() throws Exception {
|
||||
String ip = "127.0.0.1";
|
||||
getMockData(get("/ip/" + ip)).andDo(result -> {
|
||||
Response<Object> response = getResponse(result);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIp() throws Exception {
|
||||
getMockData(get("/ip")).andDo(result -> {
|
||||
Response<String> response = getResponse(result, STRING_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertEquals("127.0.0.1", response.getResult());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.celess.visitor.serviceimpl;
|
||||
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.VisitorModel;
|
||||
import cn.celess.common.service.VisitorService;
|
||||
import cn.celess.common.test.BaseTest;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class VisitorServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
VisitorService visitorService;
|
||||
|
||||
@Test
|
||||
public void location() {
|
||||
assertEquals("0|0|0|内网IP|内网IP", visitorService.location("127.0.0.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void visitorPage() {
|
||||
long start = System.currentTimeMillis();
|
||||
PageData<VisitorModel> visitorModelPageData = visitorService.visitorPage(1, 10, true);
|
||||
assertTrue(System.currentTimeMillis() - start <= 1500);
|
||||
assertTrue(visitorModelPageData.getList().stream().noneMatch(visitor -> StringUtils.isEmpty(visitor.getLocation())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.celess.visitor.util;
|
||||
|
||||
import cn.celess.common.test.BaseTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AddressUtilTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void getCityInfo() {
|
||||
assertEquals("0|0|0|内网IP|内网IP", AddressUtil.getCityInfo("127.0.0.1"));
|
||||
assertEquals("中国|0|上海|上海市|阿里云", AddressUtil.getCityInfo("106.15.205.190"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user