模块化拆分
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package cn.celess.configuration;
|
||||
|
||||
import cn.celess.configuration.filter.AuthenticationFilter;
|
||||
import cn.celess.configuration.filter.MultipleSubmitFilter;
|
||||
import cn.celess.configuration.filter.VisitorRecord;
|
||||
import cn.celess.configuration.listener.SessionListener;
|
||||
@@ -16,17 +15,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
* @Description:
|
||||
*/
|
||||
@Configuration
|
||||
public class InterceptorConfig implements WebMvcConfigurer {
|
||||
public class DeployInterceptorConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new MultipleSubmitFilter()).addPathPatterns("/**");
|
||||
registry.addInterceptor(new VisitorRecord()).addPathPatterns("/**");
|
||||
registry.addInterceptor(authenticationFilter()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthenticationFilter authenticationFilter() {
|
||||
return new AuthenticationFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -1,94 +0,0 @@
|
||||
package cn.celess.configuration.filter;
|
||||
|
||||
|
||||
import cn.celess.common.enmu.ResponseEnum;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.service.UserService;
|
||||
import cn.celess.common.util.RedisUtil;
|
||||
import cn.celess.user.util.JwtUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/11/16 11:21
|
||||
* @Description: 鉴权拦截器
|
||||
*/
|
||||
public class AuthenticationFilter implements HandlerInterceptor {
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuthenticationFilter.class);
|
||||
private static final String USER_PREFIX = "/user";
|
||||
private static final String ADMIN_PREFIX = "/admin";
|
||||
private static final String ROLE_ADMIN = "admin";
|
||||
private static final String ROLE_USER = "user";
|
||||
@Autowired
|
||||
JwtUtil jwtUtil;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String path = request.getRequestURI();
|
||||
path = path.replaceAll("/+", "/");
|
||||
int indexOf = path.indexOf("/", 1);
|
||||
String rootPath = indexOf == -1 ? path : path.substring(0, indexOf);
|
||||
String jwtStr = request.getHeader("Authorization");
|
||||
if (jwtStr != null && !jwtStr.isEmpty() && !jwtUtil.isTokenExpired(jwtStr)) {
|
||||
// 已登录 记录当前email
|
||||
request.getSession().setAttribute("email", jwtUtil.getUsernameFromToken(jwtStr));
|
||||
}
|
||||
// 不需要鉴权的路径
|
||||
if (!USER_PREFIX.equalsIgnoreCase(rootPath) && !ADMIN_PREFIX.equalsIgnoreCase(rootPath)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (jwtStr == null || jwtStr.isEmpty()) {
|
||||
return writeResponse(ResponseEnum.HAVE_NOT_LOG_IN, response, request);
|
||||
}
|
||||
if (jwtUtil.isTokenExpired(jwtStr)) {
|
||||
return writeResponse(ResponseEnum.LOGIN_EXPIRED, response, request);
|
||||
}
|
||||
String email = jwtUtil.getUsernameFromToken(jwtStr);
|
||||
if (jwtUtil.isTokenExpired(jwtStr)) {
|
||||
// 登陆过期
|
||||
return writeResponse(ResponseEnum.LOGIN_EXPIRED, response, request);
|
||||
}
|
||||
if (!redisUtil.hasKey(email + "-login")) {
|
||||
return writeResponse(ResponseEnum.LOGOUT, response, request);
|
||||
}
|
||||
String role = userService.getUserRoleByEmail(email);
|
||||
if (role.equals(ROLE_USER) || role.equals(ROLE_ADMIN)) {
|
||||
// 更新token
|
||||
String token = jwtUtil.updateTokenDate(jwtStr);
|
||||
response.setHeader("Authorization", token);
|
||||
}
|
||||
if (role.equals(ROLE_ADMIN)) {
|
||||
// admin
|
||||
return true;
|
||||
}
|
||||
if (role.equals(ROLE_USER) && !rootPath.equals(ADMIN_PREFIX)) {
|
||||
// user not admin page
|
||||
return true;
|
||||
}
|
||||
return writeResponse(ResponseEnum.PERMISSION_ERROR, response, request);
|
||||
}
|
||||
|
||||
private boolean writeResponse(ResponseEnum e, HttpServletResponse response, HttpServletRequest request) {
|
||||
response.setHeader("Content-Type", "application/json;charset=UTF-8");
|
||||
try {
|
||||
logger.info("鉴权失败,[code:{},msg:{},path:{}]", e.getCode(), e.getMsg(), request.getRequestURI() + "?" + request.getQueryString());
|
||||
response.getWriter().println(new ObjectMapper().writeValueAsString(Response.response(e, null)));
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
server.port=8081
|
||||
|
||||
# 七牛的密钥配置
|
||||
qiniu.accessKey=
|
||||
qiniu.secretKey=
|
||||
qiniu.bucket=
|
||||
# sitemap 存放地址
|
||||
sitemap.path=
|
||||
# 生成JWT时候的密钥
|
||||
jwt.secret=
|
||||
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
# 上传单个文件的大小
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
# 上传文件的总大小
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
##null字段不显示
|
||||
spring.jackson.default-property-inclusion=non_null
|
||||
|
||||
|
||||
################# 数据库 ##################
|
||||
#请先填写下面的配置
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
spring.datasource.url=
|
||||
spring.datasource.username=
|
||||
spring.datasource.password=
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.platform=mysql
|
||||
# never / always / embedded
|
||||
spring.datasource.initialization-mode=never
|
||||
spring.datasource.sql-script-encoding=utf-8
|
||||
spring.datasource.schema=classpath:sql/schema.sql
|
||||
spring.datasource.data=classpath:sql/data.sql
|
||||
|
||||
|
||||
################## mybatis ##################
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
mybatis.type-aliases-package=cn.celess.common.entity
|
||||
|
||||
|
||||
pagehelper.helper-dialect=mysql
|
||||
pagehelper.reasonable=true
|
||||
pagehelper.support-methods-arguments=true
|
||||
pagehelper.params=count=countSql
|
||||
|
||||
|
||||
|
||||
|
||||
################ email ##############
|
||||
#请先填写下面的配置,不然可能运行不起来
|
||||
spring.mail.host=
|
||||
spring.mail.username=
|
||||
spring.mail.password=
|
||||
spring.mail.properties.mail.smtp.auth=true
|
||||
spring.mail.properties.mail.smtp.starttls.enable=true
|
||||
spring.mail.properties.mail.smtp.starttls.required=true
|
||||
spring.mail.default-encoding=UTF-8
|
||||
spring.mail.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
|
||||
spring.mail.properties.mail.smtp.socketFactory.fallback=false
|
||||
|
||||
|
||||
#### 用于nginx的代理 获取真实ip
|
||||
server.use-forward-headers = true
|
||||
server.tomcat.remote-ip-header = X-Real-IP
|
||||
server.tomcat.protocol-header = X-Forwarded-Proto
|
||||
|
||||
|
||||
############### redis ##############
|
||||
# REDIS (RedisProperties)
|
||||
# Redis数据库索引(默认为0)
|
||||
spring.redis.database=0
|
||||
# Redis服务器地址
|
||||
spring.redis.host=
|
||||
# Redis服务器连接端口
|
||||
spring.redis.port=6379
|
||||
# Redis服务器连接密码(默认为空)
|
||||
spring.redis.password=
|
||||
# 连接池最大连接数(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-active=-1
|
||||
# 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-wait=-1
|
||||
# 连接池中的最大空闲连接
|
||||
spring.redis.jedis.pool.max-idle=8
|
||||
# 连接池中的最小空闲连接
|
||||
spring.redis.jedis.pool.min-idle=0
|
||||
# 连接超时时间(毫秒)
|
||||
spring.redis.timeout=5000
|
||||
@@ -1,68 +0,0 @@
|
||||
server.port=8081
|
||||
# 七牛的密钥配置
|
||||
qiniu.accessKey=si3O2_Q7edFtjzmyyzXkoE9G1toxcjDfULhX5zdh
|
||||
qiniu.secretKey=Pnq8q2Iy1Ez8RQXQR33XmgAYlit7M8C197BZ4lCj
|
||||
qiniu.bucket=xiaohai
|
||||
# sitemap 存放地址
|
||||
sitemap.path=
|
||||
# 生成JWT时候的密钥
|
||||
jwt.secret=sdjfi77;47h7uuo4l;4lgiu4;dl5684aasdasdpsidf;sdf
|
||||
#mybatis.type-handlers-package=cn.celess.common.mapper.typehandler
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
# 上传单个文件的大小
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
# 上传文件的总大小
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
spring.jackson.default-property-inclusion=non_null
|
||||
################# 数据库 ##################
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
# spring.datasource.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=zhenghai
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.initialization-mode=never
|
||||
################## mybatis ##################
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
mybatis.type-aliases-package=cn.celess.common.entity
|
||||
pagehelper.helper-dialect=mysql
|
||||
pagehelper.reasonable=true
|
||||
pagehelper.support-methods-arguments=true
|
||||
pagehelper.params=count=countSql
|
||||
#### 用于nginx的代理 获取真实ip
|
||||
server.use-forward-headers=true
|
||||
server.tomcat.remote-ip-header=X-Real-IP
|
||||
server.tomcat.protocol-header=X-Forwarded-Proto
|
||||
############### email ##############
|
||||
spring.mail.host=smtp.163.com
|
||||
spring.mail.username=xiaohai2271@163.com
|
||||
spring.mail.password=56462271Zh
|
||||
spring.mail.properties.mail.smtp.auth=true
|
||||
spring.mail.properties.mail.smtp.starttls.enable=true
|
||||
spring.mail.properties.mail.smtp.starttls.required=true
|
||||
spring.mail.default-encoding=UTF-8
|
||||
spring.mail.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
|
||||
spring.mail.properties.mail.smtp.socketFactory.fallback=false
|
||||
############### redis ##############
|
||||
# REDIS (RedisProperties)
|
||||
# Redis数据库索引(默认为0)
|
||||
spring.redis.database=0
|
||||
# Redis服务器地址
|
||||
spring.redis.host=127.0.0.1
|
||||
# Redis服务器连接端口
|
||||
spring.redis.port=6379
|
||||
# Redis服务器连接密码(默认为空)
|
||||
spring.redis.password=zhenghai
|
||||
# 连接池最大连接数(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-active=-1
|
||||
# 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-wait=-1
|
||||
# 连接池中的最大空闲连接
|
||||
spring.redis.jedis.pool.max-idle=8
|
||||
# 连接池中的最小空闲连接
|
||||
spring.redis.jedis.pool.min-idle=0
|
||||
# 连接超时时间(毫秒)
|
||||
spring.redis.timeout=5000
|
||||
@@ -1,90 +0,0 @@
|
||||
server.port=8081
|
||||
|
||||
# 七牛的密钥配置
|
||||
qiniu.accessKey=
|
||||
qiniu.secretKey=
|
||||
qiniu.bucket=
|
||||
# sitemap 存放地址
|
||||
sitemap.path=
|
||||
# 生成JWT时候的密钥
|
||||
jwt.secret=sdaniod213k123123ipoeqowekqwe
|
||||
##spring.jpa.show-sql=false
|
||||
##spring.jpa.hibernate.ddl-auto=update
|
||||
mybatis.type-handlers-package=cn.celess.common.mapper.typehandler
|
||||
logging.level.cn.celess.common.mapper=debug
|
||||
# 上传单个文件的大小
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
# 上传文件的总大小
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
spring.jackson.default-property-inclusion=non_null
|
||||
################# 数据库 ##################
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
#h2
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;mode=mysql;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
|
||||
spring.datasource.platform=h2
|
||||
spring.datasource.sql-script-encoding=utf-8
|
||||
spring.datasource.initialization-mode=ALWAYS
|
||||
spring.datasource.schema=classpath:sql/schema_h2.sql
|
||||
spring.datasource.data=classpath:sql/data.sql
|
||||
|
||||
|
||||
################## mybatis ##################
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
mybatis.type-aliases-package=cn.celess.common.entity
|
||||
|
||||
|
||||
pagehelper.helper-dialect=mysql
|
||||
pagehelper.reasonable=true
|
||||
pagehelper.support-methods-arguments=true
|
||||
pagehelper.params=count=countSql
|
||||
|
||||
|
||||
|
||||
#### 用于nginx的代理 获取真实ip
|
||||
server.use-forward-headers = true
|
||||
server.tomcat.remote-ip-header = X-Real-IP
|
||||
server.tomcat.protocol-header = X-Forwarded-Proto
|
||||
|
||||
|
||||
############### email ##############
|
||||
spring.mail.host=smtp.163.com
|
||||
spring.mail.username=
|
||||
spring.mail.password=
|
||||
spring.mail.properties.mail.smtp.auth=true
|
||||
spring.mail.properties.mail.smtp.starttls.enable=true
|
||||
spring.mail.properties.mail.smtp.starttls.required=true
|
||||
spring.mail.default-encoding=UTF-8
|
||||
spring.mail.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.port=465
|
||||
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
|
||||
spring.mail.properties.mail.smtp.socketFactory.fallback=false
|
||||
|
||||
|
||||
|
||||
############### redis ##############
|
||||
|
||||
# REDIS (RedisProperties)
|
||||
# Redis数据库索引(默认为0)
|
||||
spring.redis.database=1
|
||||
# Redis服务器地址
|
||||
spring.redis.host=127.0.0.1
|
||||
# Redis服务器连接端口 解决端口冲突 防止使用本地的redis
|
||||
spring.redis.port=6380
|
||||
# Redis服务器连接密码(默认为空)
|
||||
spring.redis.password=
|
||||
# 连接池最大连接数(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-active=-1
|
||||
# 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
spring.redis.jedis.pool.max-wait=-1
|
||||
# 连接池中的最大空闲连接
|
||||
spring.redis.jedis.pool.max-idle=8
|
||||
# 连接池中的最小空闲连接
|
||||
spring.redis.jedis.pool.min-idle=0
|
||||
# 连接超时时间(毫秒)
|
||||
spring.redis.timeout=5000
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
spring.profiles.active=prod
|
||||
####七牛的配置
|
||||
####cn.celess.blog.service.serviceimpl.QiniuServiceImpl
|
||||
logging.level.cn.celess.blog=debug
|
||||
logging.level.cn.celess.common.mapper=info
|
||||
spring.cache.type=redis
|
||||
|
||||
## 修改openSource 添加-test 文件用于测试 -prod文件用于线上发布
|
||||
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -1,188 +0,0 @@
|
||||
drop view if exists articleView;
|
||||
drop view if exists commentView;
|
||||
drop table if exists article_tag;
|
||||
drop table if exists comment;
|
||||
drop table if exists article;
|
||||
drop table if exists user;
|
||||
drop table if exists tag_category;
|
||||
drop table if exists links;
|
||||
drop table if exists visitor;
|
||||
drop table if exists web_update;
|
||||
|
||||
|
||||
CREATE TABLE `user`
|
||||
(
|
||||
`u_id` int not null primary key auto_increment,
|
||||
`u_email` varchar(50) not null,
|
||||
`u_pwd` varchar(40) not null comment '密码',
|
||||
`u_email_status` boolean default false comment '邮箱验证状态',
|
||||
`u_avatar` varchar(255) default null comment '用户头像',
|
||||
`u_desc` tinytext COLLATE utf8mb4_unicode_ci default null comment '用户的描述',
|
||||
`u_recently_landed_time` datetime default null comment '最近的登录时间',
|
||||
`u_display_name` varchar(30) COLLATE utf8mb4_unicode_ci default null comment '展示的昵称',
|
||||
`u_role` varchar(40) not null default 'user' comment '权限组',
|
||||
`status` tinyint(1) not null default 0 comment '账户状态',
|
||||
unique key `uni_user_id` (`u_id`),
|
||||
unique key `uni_user_email` (`u_email`)
|
||||
) comment '用户表';
|
||||
|
||||
CREATE TABLE `tag_category`
|
||||
(
|
||||
`t_id` bigint(20) primary key auto_increment,
|
||||
`t_name` varchar(255) not null,
|
||||
`is_category` boolean not null default true,
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
) comment '标签和分类表';
|
||||
|
||||
CREATE TABLE `article`
|
||||
(
|
||||
`a_id` bigint(20) primary key auto_increment,
|
||||
`a_title` varchar(255) COLLATE utf8mb4_unicode_ci not null unique comment '文章标题',
|
||||
`a_summary` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '文章摘要',
|
||||
`a_md_content` longtext COLLATE utf8mb4_unicode_ci not null comment '文章Markdown内容',
|
||||
`a_url` tinytext default null comment '转载文章的原文链接',
|
||||
`a_author_id` int not null comment '作者id',
|
||||
`a_is_original` boolean default true comment '文章是否原创',
|
||||
`a_reading_number` int default 0 comment '文章阅读数',
|
||||
`a_like` int default 0 comment '文章点赞数',
|
||||
`a_dislike` int default 0 comment '文章不喜欢数',
|
||||
`a_category_id` bigint not null comment '文章分类id',
|
||||
`a_publish_date` datetime default CURRENT_TIMESTAMP comment '文章发布时间',
|
||||
`a_update_date` datetime default null comment '文章的更新时间',
|
||||
`a_is_open` boolean default true comment '文章是否可见',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除',
|
||||
foreign key (a_category_id) references tag_category (t_id),
|
||||
foreign key (a_author_id) references user (u_id)
|
||||
) DEFAULT CHARSET = utf8mb4
|
||||
COLLATE utf8mb4_general_ci,comment '文章表';
|
||||
|
||||
CREATE TABLE `article_tag`
|
||||
(
|
||||
`at_id` bigint(20) primary key auto_increment,
|
||||
`a_id` bigint(20) not null comment '文章id',
|
||||
`t_id` bigint not null comment 'tag/category 的id',
|
||||
foreign key (a_id) references article (a_id),
|
||||
foreign key (t_id) references tag_category (t_id)
|
||||
) comment '文章标签表';
|
||||
|
||||
CREATE TABLE `comment`
|
||||
(
|
||||
`co_id` bigint(20) primary key auto_increment,
|
||||
`co_page_path` varchar(255) not null comment '评论/留言的页面',
|
||||
`co_content` text COLLATE utf8mb4_unicode_ci not null comment '评论/留言内容',
|
||||
`co_date` datetime not null comment '评论/留言的日期',
|
||||
`co_status` tinyint not null default 0 comment '评论的状态',
|
||||
`co_pid` bigint not null default -1 comment '评论/留言的父id',
|
||||
`co_from_author_id` int not null comment '留言者id',
|
||||
`co_to_author_id` int default null comment '父评论的作者id',
|
||||
foreign key (co_from_author_id) references user (u_id),
|
||||
foreign key (co_to_author_id) references user (u_id)
|
||||
) DEFAULT CHARSET = utf8mb4
|
||||
COLLATE utf8mb4_general_ci,comment '评论/留言表';
|
||||
|
||||
CREATE TABLE `links`
|
||||
(
|
||||
`l_id` bigint(20) primary key auto_increment,
|
||||
`l_name` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '友站名称',
|
||||
`l_is_open` boolean default true comment '是否公开',
|
||||
`l_url` varchar(255) unique not null comment '首页地址',
|
||||
`l_icon_path` varchar(255) not null comment '友链的icon地址',
|
||||
`l_desc` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '友链的说明描述',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除',
|
||||
`l_email` varchar(255) comment '网站管理员的邮箱',
|
||||
`l_notification` boolean default false comment '是否通知了'
|
||||
) comment '友站表';
|
||||
|
||||
CREATE TABLE `visitor`
|
||||
(
|
||||
`v_id` bigint(20) primary key auto_increment,
|
||||
`v_date` datetime not null comment '访问时间',
|
||||
`v_ip` varchar(255) not null comment '访客ip',
|
||||
`v_user_agent` text comment '访客ua',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
) comment '访客表';
|
||||
|
||||
CREATE TABLE `web_update`
|
||||
(
|
||||
`wu_id` int primary key auto_increment,
|
||||
`wu_info` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '更新内容',
|
||||
`wu_time` datetime not null comment '更新时间',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
) comment '更新内容表';
|
||||
|
||||
CREATE VIEW articleView
|
||||
(articleId, title, summary, mdContent, url, isOriginal, readingCount, likeCount, dislikeCount,
|
||||
publishDate, updateDate, isOpen,
|
||||
categoryId, categoryName, tagId, tagName,
|
||||
authorId, userEmail, userAvatar, userDisplayName, isDelete)
|
||||
as
|
||||
select article.a_id as articleId,
|
||||
article.a_title as title,
|
||||
article.a_summary as summary,
|
||||
article.a_md_content as mdContent,
|
||||
article.a_url as url,
|
||||
article.a_is_original as isOriginal,
|
||||
article.a_reading_number as readingCount,
|
||||
article.a_like as likeCount,
|
||||
article.a_dislike as dislikeCount,
|
||||
article.a_publish_date as publishDate,
|
||||
article.a_update_date as updateDate,
|
||||
article.a_is_open as isOpen,
|
||||
category.t_id as categoryId,
|
||||
category.t_name as categoryName,
|
||||
tag.t_id as tagId,
|
||||
tag.t_name as tagName,
|
||||
user.u_id as authorId,
|
||||
user.u_email as userEmail,
|
||||
user.u_avatar as userAvatar,
|
||||
user.u_display_name as userDisplayName,
|
||||
article.is_delete as isDelete
|
||||
from article,
|
||||
tag_category as tag,
|
||||
tag_category as category,
|
||||
article_tag,
|
||||
user
|
||||
where article.a_id = article_tag.a_id
|
||||
and article_tag.t_id = tag.t_id
|
||||
and tag.is_category = false
|
||||
and article.a_category_id = category.t_id
|
||||
and category.is_category = true
|
||||
and article.a_author_id = user.u_id;
|
||||
|
||||
|
||||
CREATE VIEW commentView
|
||||
(commentId, pagePath, content, date, status, pid, toAuthorId, toAuthorEmail, toAuthorDisplayName,
|
||||
toAuthorAvatar, fromAuthorId, fromAuthorEmail, fromAuthorDisplayName,
|
||||
fromAuthorAvatar)
|
||||
as
|
||||
select cuT.co_id as commentId,
|
||||
cuT.co_page_path as pagePath,
|
||||
cuT.co_content as content,
|
||||
cuT.co_date as date,
|
||||
cuT.co_status as status,
|
||||
cuT.co_pid as pid,
|
||||
cuT.co_to_author_id as toAuthorId,
|
||||
cuT.toEmail as toAuthorEmail,
|
||||
cuT.toDisplayName as toAuthorDisplayName,
|
||||
cuT.toAvatar as toAuthorAvatar,
|
||||
userFrom.u_id as fromAuthorId,
|
||||
userFrom.u_email as fromAuthorEmail,
|
||||
userFrom.u_display_name as fromAuthorDisplayName,
|
||||
userFrom.u_avatar as fromAuthorAvatar
|
||||
from (select comment.co_id,
|
||||
comment.co_page_path,
|
||||
comment.co_content,
|
||||
comment.co_date,
|
||||
comment.co_status,
|
||||
comment.co_pid,
|
||||
comment.co_from_author_id,
|
||||
comment.co_to_author_id,
|
||||
userTo.u_email as toEmail,
|
||||
userTo.u_display_name as toDisplayName,
|
||||
userTo.u_avatar as toAvatar
|
||||
from comment
|
||||
left join user userTo on (comment.co_to_author_id = userTo.u_id)
|
||||
) as cuT,
|
||||
user as userFrom
|
||||
where cuT.co_from_author_id = userFrom.u_id;
|
||||
|
||||
@@ -1,193 +0,0 @@
|
||||
drop view if exists articleView;
|
||||
drop view if exists commentView;
|
||||
drop table if exists article_tag;
|
||||
drop table if exists comment;
|
||||
drop table if exists article;
|
||||
drop table if exists user;
|
||||
drop table if exists tag_category;
|
||||
drop table if exists links;
|
||||
drop table if exists visitor;
|
||||
drop table if exists web_update;
|
||||
|
||||
-- 用户表
|
||||
CREATE TABLE `user`
|
||||
(
|
||||
`u_id` int not null primary key auto_increment,
|
||||
`u_email` varchar(50) not null,
|
||||
`u_pwd` varchar(40) not null comment '密码',
|
||||
`u_email_status` boolean default false comment '邮箱验证状态',
|
||||
`u_avatar` varchar(255) default null comment '用户头像',
|
||||
`u_desc` tinytext default null comment '用户的描述',
|
||||
`u_recently_landed_time` datetime default null comment '最近的登录时间',
|
||||
`u_display_name` varchar(30) default null comment '展示的昵称',
|
||||
`u_role` varchar(40) not null default 'user' comment '权限组',
|
||||
`status` tinyint(1) not null default 0 comment '账户状态',
|
||||
unique key `uni_user_id` (`u_id`),
|
||||
unique key `uni_user_email` (`u_email`)
|
||||
);
|
||||
|
||||
-- 标签和分类表
|
||||
CREATE TABLE `tag_category`
|
||||
(
|
||||
`t_id` bigint(20) primary key auto_increment,
|
||||
`t_name` varchar(255) not null,
|
||||
`is_category` boolean not null default true,
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
);
|
||||
|
||||
-- 文章表
|
||||
CREATE TABLE `article`
|
||||
(
|
||||
`a_id` bigint(20) primary key auto_increment,
|
||||
`a_title` varchar(255) not null unique comment '文章标题',
|
||||
`a_summary` varchar(255) not null comment '文章摘要',
|
||||
`a_md_content` longtext not null comment '文章Markdown内容',
|
||||
`a_url` tinytext default null comment '转载文章的原文链接',
|
||||
`a_author_id` int not null comment '作者id',
|
||||
`a_is_original` boolean default true comment '文章是否原创',
|
||||
`a_reading_number` int default 0 comment '文章阅读数',
|
||||
`a_like` int default 0 comment '文章点赞数',
|
||||
`a_dislike` int default 0 comment '文章不喜欢数',
|
||||
`a_category_id` bigint not null comment '文章分类id',
|
||||
`a_publish_date` datetime default CURRENT_TIMESTAMP comment '文章发布时间',
|
||||
`a_update_date` datetime default null comment '文章的更新时间',
|
||||
`a_is_open` boolean default true comment '文章是否可见',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除',
|
||||
foreign key (a_category_id) references tag_category (t_id),
|
||||
foreign key (a_author_id) references user (u_id)
|
||||
);
|
||||
|
||||
-- 文章标签表
|
||||
CREATE TABLE `article_tag`
|
||||
(
|
||||
`at_id` bigint(20) primary key auto_increment,
|
||||
`a_id` bigint(20) not null comment '文章id',
|
||||
`t_id` bigint not null comment 'tag/category 的id',
|
||||
foreign key (a_id) references article (a_id),
|
||||
foreign key (t_id) references tag_category (t_id)
|
||||
);
|
||||
|
||||
-- 评论/留言表
|
||||
CREATE TABLE `comment`
|
||||
(
|
||||
`co_id` bigint(20) primary key auto_increment,
|
||||
`co_page_path` varchar(255) not null comment '评论/留言的页面',
|
||||
`co_content` text not null comment '评论/留言内容',
|
||||
`co_date` datetime not null comment '评论/留言的日期',
|
||||
`co_status` tinyint not null default 0 comment '评论的状态',
|
||||
`co_pid` bigint not null default -1 comment '评论/留言的父id',
|
||||
`co_from_author_id` int not null comment '留言者id',
|
||||
`co_to_author_id` int default null comment '父评论的作者id',
|
||||
foreign key (co_from_author_id) references user (u_id),
|
||||
foreign key (co_to_author_id) references user (u_id)
|
||||
);
|
||||
|
||||
-- 友站表
|
||||
CREATE TABLE `links`
|
||||
(
|
||||
`l_id` bigint(20) primary key auto_increment,
|
||||
`l_name` varchar(255) not null comment '友站名称',
|
||||
`l_is_open` boolean default true comment '是否公开',
|
||||
`l_url` varchar(255) unique not null comment '首页地址',
|
||||
`l_icon_path` varchar(255) not null comment '友链的icon地址',
|
||||
`l_desc` varchar(255) not null comment '友链的说明描述',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除',
|
||||
`l_email` varchar(255) comment '网站管理员的邮箱',
|
||||
`l_notification` boolean default false comment '是否通知了'
|
||||
);
|
||||
|
||||
-- 访客表
|
||||
CREATE TABLE `visitor`
|
||||
(
|
||||
`v_id` bigint(20) primary key auto_increment,
|
||||
`v_date` datetime not null comment '访问时间',
|
||||
`v_ip` varchar(255) not null comment '访客ip',
|
||||
`v_user_agent` text comment '访客ua',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
);
|
||||
|
||||
-- 更新内容表
|
||||
CREATE TABLE `web_update`
|
||||
(
|
||||
`wu_id` int primary key auto_increment,
|
||||
`wu_info` varchar(255) not null comment '更新内容',
|
||||
`wu_time` datetime not null comment '更新时间',
|
||||
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||
);
|
||||
|
||||
CREATE VIEW articleView
|
||||
(articleId, title, summary, mdContent, url, isOriginal, readingCount, likeCount, dislikeCount,
|
||||
publishDate, updateDate, isOpen,
|
||||
categoryId, categoryName, tagId, tagName,
|
||||
authorId, userEmail, userAvatar, userDisplayName, isDelete)
|
||||
as
|
||||
select article.a_id as articleId,
|
||||
article.a_title as title,
|
||||
article.a_summary as summary,
|
||||
article.a_md_content as mdContent,
|
||||
article.a_url as url,
|
||||
article.a_is_original as isOriginal,
|
||||
article.a_reading_number as readingCount,
|
||||
article.a_like as likeCount,
|
||||
article.a_dislike as dislikeCount,
|
||||
article.a_publish_date as publishDate,
|
||||
article.a_update_date as updateDate,
|
||||
article.a_is_open as isOpen,
|
||||
category.t_id as categoryId,
|
||||
category.t_name as categoryName,
|
||||
tag.t_id as tagId,
|
||||
tag.t_name as tagName,
|
||||
user.u_id as authorId,
|
||||
user.u_email as userEmail,
|
||||
user.u_avatar as userAvatar,
|
||||
user.u_display_name as userDisplayName,
|
||||
article.is_delete as isDelete
|
||||
from article,
|
||||
tag_category as tag,
|
||||
tag_category as category,
|
||||
article_tag,
|
||||
user
|
||||
where article.a_id = article_tag.a_id
|
||||
and article_tag.t_id = tag.t_id
|
||||
and tag.is_category = false
|
||||
and article.a_category_id = category.t_id
|
||||
and category.is_category = true
|
||||
and article.a_author_id = user.u_id;
|
||||
|
||||
|
||||
CREATE VIEW commentView
|
||||
(commentId, pagePath, content, date, status, pid, toAuthorId, toAuthorEmail, toAuthorDisplayName,
|
||||
toAuthorAvatar, fromAuthorId, fromAuthorEmail, fromAuthorDisplayName,
|
||||
fromAuthorAvatar)
|
||||
as
|
||||
select cuT.co_id as commentId,
|
||||
cuT.co_page_path as pagePath,
|
||||
cuT.co_content as content,
|
||||
cuT.co_date as date,
|
||||
cuT.co_status as status,
|
||||
cuT.co_pid as pid,
|
||||
cuT.co_to_author_id as toAuthorId,
|
||||
cuT.toEmail as toAuthorEmail,
|
||||
cuT.toDisplayName as toAuthorDisplayName,
|
||||
cuT.toAvatar as toAuthorAvatar,
|
||||
userFrom.u_id as fromAuthorId,
|
||||
userFrom.u_email as fromAuthorEmail,
|
||||
userFrom.u_display_name as fromAuthorDisplayName,
|
||||
userFrom.u_avatar as fromAuthorAvatar
|
||||
from (select comment.co_id,
|
||||
comment.co_page_path,
|
||||
comment.co_content,
|
||||
comment.co_date,
|
||||
comment.co_status,
|
||||
comment.co_pid,
|
||||
comment.co_from_author_id,
|
||||
comment.co_to_author_id,
|
||||
userTo.u_email as toEmail,
|
||||
userTo.u_display_name as toDisplayName,
|
||||
userTo.u_avatar as toAvatar
|
||||
from comment
|
||||
left join user userTo on (comment.co_to_author_id = userTo.u_id)
|
||||
) as cuT,
|
||||
user as userFrom
|
||||
where cuT.co_from_author_id = userFrom.u_id;
|
||||
|
||||
@@ -1,345 +0,0 @@
|
||||
package cn.celess;
|
||||
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.dto.LoginReq;
|
||||
import cn.celess.common.entity.vo.QiniuResponse;
|
||||
import cn.celess.common.entity.vo.UserModel;
|
||||
import cn.celess.common.service.MailService;
|
||||
import cn.celess.common.service.QiniuService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.SUCCESS;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/08/22 12:46
|
||||
* @Description: 测试基类
|
||||
*/
|
||||
@SpringBootTest
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("test")
|
||||
public class BaseTest {
|
||||
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected MockMvc mockMvc;
|
||||
protected final static String Code = "code";
|
||||
protected final static String Result = "result";
|
||||
protected final static String USERE_MAIL = "zh56462271@qq.com";
|
||||
protected final static String ADMIN_EMAIL = "a@celess.cn";
|
||||
|
||||
/**
|
||||
* jackson 序列化/反序列化Json
|
||||
*/
|
||||
protected final ObjectMapper mapper = new ObjectMapper();
|
||||
protected static final TypeReference<?> BOOLEAN_TYPE = new TypeReference<Response<Boolean>>() {
|
||||
};
|
||||
protected static final TypeReference<?> STRING_TYPE = new TypeReference<Response<String>>() {
|
||||
};
|
||||
protected static final TypeReference<?> OBJECT_TYPE = new TypeReference<Response<Object>>() {
|
||||
};
|
||||
protected static final TypeReference<?> MAP_OBJECT_TYPE = new TypeReference<Response<Map<String, Object>>>() {
|
||||
};
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
protected MockHttpSession session;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
session = new MockHttpSession();
|
||||
System.out.println("==========> 开始测试 <=========");
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
System.out.println("==========> 测试结束 <=========");
|
||||
}
|
||||
|
||||
/**
|
||||
* admin 权限用户登录
|
||||
*
|
||||
* @return token
|
||||
*/
|
||||
protected String adminLogin() {
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail(ADMIN_EMAIL);
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(true);
|
||||
String token = login(req);
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* user 权限用户登录
|
||||
*
|
||||
* @return token
|
||||
*/
|
||||
protected String userLogin() {
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail(USERE_MAIL);
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(true);
|
||||
String token = login(req);
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录逻辑
|
||||
*
|
||||
* @param req 用户信息
|
||||
* @return token | null
|
||||
*/
|
||||
protected String login(LoginReq req) {
|
||||
String str = null;
|
||||
try {
|
||||
str = getMockData(MockMvcRequestBuilders.post("/login"), null, req)
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
Response<UserModel> response = mapper.readValue(str, new TypeReference<Response<UserModel>>() {
|
||||
});
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
String token = response.getResult().getToken();
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
} catch (Exception e) {
|
||||
logger.error("测试登录错误");
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertNotNull(str);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// 测试登录
|
||||
assertNotNull(userLogin());
|
||||
assertNotNull(adminLogin());
|
||||
assertNotEquals(userLogin(), adminLogin());
|
||||
try {
|
||||
// 测试getMockData方法
|
||||
assertNotNull(getMockData(MockMvcRequestBuilders.get("/headerInfo")));
|
||||
getMockData((MockMvcRequestBuilders.get("/headerInfo"))).andDo(result -> assertNotNull(getResponse(result, OBJECT_TYPE)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 产生指定长度的随机字符
|
||||
*
|
||||
* @param len len
|
||||
* @return str
|
||||
*/
|
||||
protected String randomStr(int len) {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* 产生指定长度的随机字符
|
||||
*
|
||||
* @return str
|
||||
*/
|
||||
protected String randomStr() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽离的mock请求方法
|
||||
*
|
||||
* @param builder MockHttpServletRequestBuilder :get(...) post(...) ....
|
||||
* @return 返回 ResultActions
|
||||
* @throws Exception exc
|
||||
*/
|
||||
protected ResultActions getMockData(MockHttpServletRequestBuilder builder) throws Exception {
|
||||
return getMockData(builder, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽离的mock请求方法 重载
|
||||
*
|
||||
* @param builder ..
|
||||
* @param token 用户登录的token
|
||||
* @return ..
|
||||
* @throws Exception ..
|
||||
*/
|
||||
protected ResultActions getMockData(MockHttpServletRequestBuilder builder, String token) throws Exception {
|
||||
return getMockData(builder, token, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽离的mock请求方法 重载
|
||||
*
|
||||
* @param builder ..
|
||||
* @param token ..
|
||||
* @param content http中发送的APPLICATION_JSON的json数据
|
||||
* @return ..
|
||||
* @throws Exception ..
|
||||
*/
|
||||
protected ResultActions getMockData(MockHttpServletRequestBuilder builder, String token, Object content) throws Exception {
|
||||
// MockHttpServletRequestBuilder mockHttpServletRequestBuilder = get(url);
|
||||
if (token != null) {
|
||||
builder.header("Authorization", "Bearer "+token);
|
||||
}
|
||||
if (content != null) {
|
||||
builder.content(mapper.writeValueAsString(content)).contentType(MediaType.APPLICATION_JSON);
|
||||
logger.debug("param::json->{}", mapper.writeValueAsString(content));
|
||||
}
|
||||
return mockMvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk());
|
||||
}
|
||||
|
||||
|
||||
protected <T> Response<T> getResponse(String json) {
|
||||
return getResponse(json, OBJECT_TYPE);
|
||||
}
|
||||
|
||||
protected <T> Response<T> getResponse(MvcResult result) {
|
||||
return getResponse(result, OBJECT_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据json 信息反序列化成Response对象
|
||||
*
|
||||
* @param json json
|
||||
* @param <T> 泛型
|
||||
* @return Response对象
|
||||
*/
|
||||
protected <T> Response<T> getResponse(String json, TypeReference<?> responseType) {
|
||||
Response<T> response = null;
|
||||
try {
|
||||
response = mapper.readValue(json, responseType);
|
||||
} catch (IOException e) {
|
||||
logger.error("解析json Response对象错误,json:[{}]", json);
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertNotNull(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据json 信息反序列化成Response对象
|
||||
*
|
||||
* @param result MvcResult
|
||||
* @param <T> 泛型
|
||||
* @return Response对象
|
||||
*/
|
||||
protected <T> Response<T> getResponse(MvcResult result, TypeReference<?> responseType) {
|
||||
try {
|
||||
return getResponse(result.getResponse().getContentAsString(), responseType);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.error("解析json Response对象错误,result:[{}]", result);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改 mailService 的实现类
|
||||
*
|
||||
* @param service service 类
|
||||
* @param mailFiledName service 中自动注入的mailService字段名
|
||||
*/
|
||||
protected void mockInjectInstance(Object service, String mailFiledName, Object impl) {
|
||||
Field field;
|
||||
try {
|
||||
assertNotNull(service);
|
||||
field = service.getClass().getDeclaredField(mailFiledName);
|
||||
field.setAccessible(true);
|
||||
field.set(service, impl);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Slf4j
|
||||
public static class TestMailServiceImpl implements MailService {
|
||||
|
||||
@Override
|
||||
public Boolean AsyncSend(SimpleMailMessage message) {
|
||||
log.debug("异步邮件请求,SimpleMailMessage:[{}]", getJson(message));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean send(SimpleMailMessage message) {
|
||||
log.debug("邮件请求,SimpleMailMessage:[{}]", getJson(message));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转json
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
private String getJson(Object o) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
return mapper.writeValueAsString(o);
|
||||
} catch (JsonProcessingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public static class TestQiNiuServiceImpl implements QiniuService {
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
QiniuResponse response = new QiniuResponse();
|
||||
log.debug("上传文件请求,[fileName:{}]", fileName);
|
||||
|
||||
response.key = "key";
|
||||
response.bucket = "bucket";
|
||||
response.hash = "hash";
|
||||
response.fsize = 1;
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
log.debug("获取文件列表请求");
|
||||
return new FileInfo[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
11
blog-deploy/src/test/java/cn/celess/DeployBaseTest.java
Normal file
11
blog-deploy/src/test/java/cn/celess/DeployBaseTest.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package cn.celess;
|
||||
|
||||
import cn.celess.common.test.BaseTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@SpringBootTest(classes = {BlogApplication.class})
|
||||
@RunWith(SpringRunner.class)
|
||||
public abstract class DeployBaseTest extends BaseTest {
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package cn.celess;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2020/08/14 16:20
|
||||
*/
|
||||
@Component
|
||||
public class RedisServerMock {
|
||||
|
||||
private RedisServer redisServer;
|
||||
|
||||
/**
|
||||
* 构造方法之后执行.
|
||||
*
|
||||
* @throws IOException e
|
||||
*/
|
||||
@PostConstruct
|
||||
public void startRedis() throws IOException {
|
||||
redisServer = new RedisServer(6380);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 析构方法之后执行.
|
||||
*/
|
||||
@PreDestroy
|
||||
public void stopRedis() {
|
||||
redisServer.stop();
|
||||
}
|
||||
}
|
||||
@@ -1,303 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Article;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.Tag;
|
||||
import cn.celess.common.entity.dto.ArticleReq;
|
||||
import cn.celess.common.entity.vo.ArticleModel;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.mapper.ArticleMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class ArticleControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
private static final TypeReference<?> ARTICLE_MODEL_TYPE = new TypeReference<Response<ArticleModel>>() {
|
||||
};
|
||||
private static final TypeReference<?> ARTICLE_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<ArticleModel>>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void create() {
|
||||
ArticleReq articleReq = new ArticleReq();
|
||||
// 应该正常通过
|
||||
articleReq.setTitle("test-" + randomStr());
|
||||
articleReq.setMdContent("# test title");
|
||||
articleReq.setCategory("随笔");
|
||||
String[] tagList = {"tag", "category"};
|
||||
articleReq.setTags(tagList);
|
||||
articleReq.setOpen(true);
|
||||
articleReq.setType(true);
|
||||
articleReq.setUrl("http://xxxx.com");
|
||||
MockHttpServletRequestBuilder post = post("/admin/article/create");
|
||||
|
||||
try {
|
||||
getMockData(post, adminLogin(), articleReq).andDo(result -> {
|
||||
Response<ArticleModel> response = getResponse(result, ARTICLE_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
ArticleModel articleModel = response.getResult();
|
||||
assertNotNull(articleModel.getId());
|
||||
assertNotNull(articleModel.getTitle());
|
||||
assertNotNull(articleModel.getSummary());
|
||||
assertNotNull(articleModel.getOriginal());
|
||||
assertNotNull(articleModel.getTags());
|
||||
assertNotNull(articleModel.getCategory());
|
||||
assertNotNull(articleModel.getPublishDateFormat());
|
||||
assertNotNull(articleModel.getMdContent());
|
||||
assertNotNull(articleModel.getPreArticle());
|
||||
assertNull(articleModel.getNextArticle());
|
||||
assertNotNull(articleModel.getOpen());
|
||||
assertNotNull(articleModel.getReadingNumber());
|
||||
assertNotNull(articleModel.getAuthor());
|
||||
assertNotNull(articleModel.getUrl());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Article article;
|
||||
do {
|
||||
article = articleMapper.getLastestArticle();
|
||||
create();
|
||||
} while (article.isDeleted());
|
||||
assertFalse(article.isDeleted());
|
||||
MockHttpServletRequestBuilder delete = MockMvcRequestBuilders.delete("/admin/article/del?articleID=" + article.getId());
|
||||
try {
|
||||
Article finalArticle = article;
|
||||
getMockData(delete, adminLogin()).andDo(result -> {
|
||||
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
// 断言删除成功
|
||||
assertTrue(response.getResult());
|
||||
assertTrue(articleMapper.isDeletedById(finalArticle.getId()));
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Article article = articleMapper.getLastestArticle();
|
||||
ArticleReq articleReq = new ArticleReq();
|
||||
articleReq.setId(article.getId());
|
||||
articleReq.setUrl("http://www.test.test");
|
||||
articleReq.setType(!article.getType());
|
||||
articleReq.setCategory("test");
|
||||
articleReq.setMdContent("test-" + article.getMdContent());
|
||||
articleReq.setOpen(!article.getOpen());
|
||||
String tag1 = randomStr(4);
|
||||
String tag2 = randomStr(4);
|
||||
String[] tagList = {"test", tag1, tag2};
|
||||
articleReq.setTags(tagList);
|
||||
articleReq.setTitle("test-" + article.getTitle());
|
||||
try {
|
||||
getMockData(put("/admin/article/update"), adminLogin(), articleReq).andDo(result -> {
|
||||
Response<ArticleModel> response = getResponse(result, ARTICLE_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
ArticleModel a = response.getResult();
|
||||
assertEquals(articleReq.getCategory(), a.getCategory());
|
||||
assertEquals(articleReq.getUrl(), a.getUrl());
|
||||
assertEquals(articleReq.getMdContent(), a.getMdContent());
|
||||
assertEquals(articleReq.getTitle(), a.getTitle());
|
||||
assertEquals(articleReq.getType(), a.getOriginal());
|
||||
// Tag
|
||||
List<Tag> asList = a.getTags();
|
||||
assertEquals(3, asList.size());
|
||||
assertEquals(articleReq.getOpen(), a.getOpen());
|
||||
assertEquals(articleReq.getId(), a.getId());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveOneById() {
|
||||
try {
|
||||
long articleID = 3;
|
||||
getMockData(MockMvcRequestBuilders.get("/article/articleID/" + articleID));
|
||||
getMockData(MockMvcRequestBuilders.get("/article/articleID/" + articleID + "?update=true"));
|
||||
|
||||
// 文章不存在
|
||||
getMockData(MockMvcRequestBuilders.get("/article/articleID/-1"))
|
||||
.andDo(result -> assertEquals(ARTICLE_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
|
||||
// 正常情况
|
||||
getMockData(MockMvcRequestBuilders.get("/article/articleID/" + articleID + "?update=false")).andDo(result -> {
|
||||
Response<ArticleModel> response = getResponse(result, ARTICLE_MODEL_TYPE);
|
||||
// 断言获取数据成功
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
|
||||
ArticleModel a = response.getResult();
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getSummary());
|
||||
assertNotNull(a.getMdContent());
|
||||
assertNotNull(a.getUrl());
|
||||
assertNotNull(a.getUpdateDateFormat());
|
||||
assertTrue(a.getPreArticle() != null || a.getNextArticle() != null);
|
||||
assertNotNull(a.getReadingNumber());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getCategory());
|
||||
assertNotNull(a.getTags());
|
||||
assertNotNull(a.getAuthor());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void articles() {
|
||||
try {
|
||||
// 测试不带参数访问
|
||||
getMockData(MockMvcRequestBuilders.get("/articles"));
|
||||
getMockData(MockMvcRequestBuilders.get("/articles?page=1&count=5")).andDo(result -> {
|
||||
Response<PageData<ArticleModel>> response = getResponse(result, ARTICLE_MODEL_PAGE_TYPE);
|
||||
// 断言获取数据成功
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
// 结果集非空
|
||||
assertNotNull(response.getResult());
|
||||
// 判断pageInfo是否包装完全
|
||||
PageData<ArticleModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(5, pageData.getPageSize());
|
||||
// 内容完整
|
||||
for (ArticleModel a : pageData.getList()) {
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getSummary());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getCategory());
|
||||
assertNotNull(a.getTags());
|
||||
assertNotNull(a.getAuthor());
|
||||
assertNull(a.getOpen());
|
||||
assertNull(a.getMdContent());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adminArticles() {
|
||||
try {
|
||||
getMockData(get("/admin/articles?page=1&count=10")).andExpect(result ->
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result, STRING_TYPE).getCode())
|
||||
);
|
||||
|
||||
// User权限登陆
|
||||
getMockData(get("/admin/articles?page=1&count=10"), userLogin()).andDo(result ->
|
||||
assertEquals(PERMISSION_ERROR.getCode(), getResponse(result, STRING_TYPE).getCode())
|
||||
);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
// admin权限登陆
|
||||
int finalI = i;
|
||||
getMockData(get("/admin/articles?page=1&count=10&deleted=" + (i == 1)), adminLogin()).andDo(result -> {
|
||||
Response<PageData<ArticleModel>> response = getResponse(result, ARTICLE_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
// 判断pageInfo是否包装完全
|
||||
PageData<ArticleModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
// 内容完整
|
||||
for (ArticleModel a : pageData.getList()) {
|
||||
assertNotNull(a.getTitle());
|
||||
assertNotNull(a.getId());
|
||||
assertNotNull(a.getOriginal());
|
||||
assertNotNull(a.getPublishDateFormat());
|
||||
assertNotNull(a.getOpen());
|
||||
assertNotNull(a.getReadingNumber());
|
||||
assertNotNull(a.getLikeCount());
|
||||
assertNotNull(a.getDislikeCount());
|
||||
assertEquals((finalI == 1), a.isDeleted());
|
||||
assertNull(a.getMdContent());
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByCategory() {
|
||||
try {
|
||||
// 分类不存在
|
||||
String categoryName = "NoSuchCategory";
|
||||
getMockData(MockMvcRequestBuilders.get("/articles/category/" + categoryName + "?page=1&count=10"))
|
||||
.andDo(result -> assertEquals(CATEGORY_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
// 正常查询
|
||||
categoryName = "linux";
|
||||
getMockData(MockMvcRequestBuilders.get("/articles/category/" + categoryName + "?page=1&count=10"))
|
||||
.andDo(result -> {
|
||||
Response<PageData<ArticleModel>> response = getResponse(result, ARTICLE_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PageData<ArticleModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
for (ArticleModel arc : pageData.getList()) {
|
||||
assertNotEquals(0, arc.getId().longValue());
|
||||
assertNotNull(arc.getTitle());
|
||||
assertNotNull(arc.getSummary());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByTag() {
|
||||
try {
|
||||
// 分类不存在
|
||||
String tagName = "NoSuchTag";
|
||||
getMockData(MockMvcRequestBuilders.get("/articles/tag/" + tagName + "?page=1&count=10"))
|
||||
.andDo(result -> assertEquals(TAG_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
// 正常查询
|
||||
tagName = "linux";
|
||||
getMockData(MockMvcRequestBuilders.get("/articles/tag/" + tagName + "?page=1&count=10"))
|
||||
.andDo(result -> {
|
||||
Response<PageData<ArticleModel>> response = getResponse(result, ARTICLE_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PageData<ArticleModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
|
||||
for (ArticleModel arc : pageData.getList()) {
|
||||
assertNotEquals(0, arc.getId().longValue());
|
||||
assertNotNull(arc.getTitle());
|
||||
assertNotNull(arc.getSummary());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Category;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.vo.CategoryModel;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.mapper.CategoryMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.SUCCESS;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class CategoryControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
private static final TypeReference<?> CATEGORY_MODEL_TYPE = new TypeReference<Response<CategoryModel>>() {
|
||||
};
|
||||
private static final TypeReference<?> CATEGORY_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<CategoryModel>>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
String categoryName = randomStr(4);
|
||||
getMockData(post("/admin/category/create?name=" + categoryName), adminLogin()).andDo(result -> {
|
||||
Response<CategoryModel> response = getResponse(result, CATEGORY_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CategoryModel category = response.getResult();
|
||||
assertEquals(categoryName, category.getName());
|
||||
assertNotNull(category.getId());
|
||||
assertNull(category.getArticles());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteOne() throws Exception {
|
||||
Category category = categoryMapper.getLastestCategory();
|
||||
getMockData(delete("/admin/category/del?id=" + category.getId()), adminLogin()).andDo(result -> {
|
||||
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertTrue(response.getResult());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOne() throws Exception {
|
||||
Category category = categoryMapper.getLastestCategory();
|
||||
String name = randomStr(4);
|
||||
getMockData(put("/admin/category/update?id=" + category.getId() + "&name=" + name), adminLogin()).andDo(result -> {
|
||||
// Response<CategoryModel> response = mapper.readValue(result.getResponse().getContentAsString(), new ResponseType<Response<CategoryModel>>());
|
||||
Response<CategoryModel> response = getResponse(result, CATEGORY_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CategoryModel c = response.getResult();
|
||||
assertEquals(name, c.getName());
|
||||
assertNull(c.getArticles());
|
||||
assertNotNull(c.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPage() throws Exception {
|
||||
getMockData(get("/categories")).andDo(result -> {
|
||||
Response<PageData<CategoryModel>> response = getResponse(result, CATEGORY_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
response.getResult().getList().forEach(c -> {
|
||||
assertNotNull(c.getName());
|
||||
assertNotNull(c.getId());
|
||||
assertNotNull(c.getArticles());
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Article;
|
||||
import cn.celess.common.entity.Comment;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.common.entity.dto.CommentReq;
|
||||
import cn.celess.common.entity.vo.CommentModel;
|
||||
import cn.celess.common.mapper.ArticleMapper;
|
||||
import cn.celess.common.mapper.CommentMapper;
|
||||
import cn.celess.common.mapper.UserMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.DATA_IS_DELETED;
|
||||
import static cn.celess.common.enmu.ResponseEnum.SUCCESS;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class CommentControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
private static final TypeReference<?> COMMENT_MODEL_TYPE = new TypeReference<Response<CommentModel>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
Article article = articleMapper.getLastestArticle();
|
||||
CommentReq commentReq = new CommentReq();
|
||||
commentReq.setPagePath("/article/" + article.getId());
|
||||
commentReq.setContent(randomStr());
|
||||
List<User> all = userMapper.findAll();
|
||||
commentReq.setPid(1L);
|
||||
commentReq.setToUserId(2l);
|
||||
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CommentModel model = response.getResult();
|
||||
assertNotEquals(0, model.getId());
|
||||
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
||||
assertEquals(1, model.getPid().longValue());
|
||||
assertEquals(commentReq.getContent(), model.getContent());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getFromUser());
|
||||
assertNotNull(model.getToUser());
|
||||
});
|
||||
|
||||
|
||||
commentReq.setPagePath("/article/" + article.getId());
|
||||
commentReq.setContent(randomStr());
|
||||
commentReq.setPid(-1L);
|
||||
commentReq.setToUserId(2);
|
||||
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CommentModel model = response.getResult();
|
||||
// 响应数据的完整性
|
||||
assertNotEquals(0, model.getId());
|
||||
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
||||
assertEquals(-1, model.getPid().longValue());
|
||||
assertEquals(commentReq.getContent(), model.getContent());
|
||||
assertEquals(commentReq.getPagePath(), "/article/" + article.getId());
|
||||
assertNotNull(model.getDate());
|
||||
assertNotNull(model.getFromUser());
|
||||
assertNotNull(model.getToUser());
|
||||
});
|
||||
|
||||
// 测试二级回复
|
||||
Comment latestComment = commentMapper.getLastestComment();
|
||||
commentReq.setPagePath("/article/" + article.getId());
|
||||
commentReq.setContent(randomStr());
|
||||
commentReq.setPid(latestComment.getId());
|
||||
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CommentModel model = response.getResult();
|
||||
// 重新获取父评论信息
|
||||
Comment pCommon = commentMapper.findCommentById(latestComment.getId());
|
||||
assertEquals(pCommon.getId(), model.getPid());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteTest() throws Exception {
|
||||
// 准备数据
|
||||
User from = userMapper.findByEmail("zh56462271@qq.com");
|
||||
assertNotNull(from);
|
||||
User to = userMapper.findByEmail("a@celess.cn");
|
||||
assertNotNull(to);
|
||||
|
||||
Comment comment = new Comment();
|
||||
comment.setContent(randomStr(8));
|
||||
comment.setFromUser(from);
|
||||
comment.setToUser(to);
|
||||
comment.setPagePath("/tags");
|
||||
comment.setPid(-1L);
|
||||
commentMapper.insert(comment);
|
||||
comment = commentMapper.findCommentById(comment.getId());
|
||||
// 接口测试
|
||||
long id = comment.getId();
|
||||
assertNotEquals(0, id);
|
||||
getMockData(delete("/user/comment/del?id=" + id), userLogin()).andDo(result -> {
|
||||
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertTrue(response.getResult());
|
||||
});
|
||||
getMockData(delete("/user/comment/del?id=" + id), userLogin())
|
||||
.andDo(result -> assertEquals(DATA_IS_DELETED.getCode(), getResponse(result, COMMENT_MODEL_TYPE).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
Comment comment = commentMapper.getLastestComment();
|
||||
CommentReq commentReq = new CommentReq();
|
||||
commentReq.setId(comment.getId());
|
||||
commentReq.setContent(randomStr());
|
||||
// 不合法数据 setResponseId
|
||||
getMockData(put("/user/comment/update"), userLogin(), commentReq).andDo(result -> {
|
||||
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
CommentModel c = response.getResult();
|
||||
assertEquals(commentReq.getContent(), c.getContent());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.PartnerSite;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.dto.LinkApplyReq;
|
||||
import cn.celess.common.entity.dto.LinkReq;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.exception.MyException;
|
||||
import cn.celess.common.mapper.PartnerMapper;
|
||||
import cn.celess.common.service.PartnerSiteService;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
@Slf4j
|
||||
public class LinksControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
PartnerMapper mapper;
|
||||
private static final TypeReference<?> LINK_MODEL_TYPE = new TypeReference<Response<PartnerSite>>() {
|
||||
};
|
||||
private static final TypeReference<?> LINK_MODEL_LIST_TYPE = new TypeReference<Response<List<PartnerSite>>>() {
|
||||
};
|
||||
private static final TypeReference<?> LINK_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<PartnerSite>>>() {
|
||||
};
|
||||
@Autowired
|
||||
PartnerSiteService partnerSiteService;
|
||||
|
||||
|
||||
@Test
|
||||
public void create() throws Exception {
|
||||
LinkReq linkReq = new LinkReq();
|
||||
linkReq.setName(randomStr(4));
|
||||
linkReq.setOpen(false);
|
||||
linkReq.setUrl("https://" + randomStr(4) + "example.com");
|
||||
getMockData(post("/admin/links/create"), adminLogin(), linkReq).andDo(result -> {
|
||||
Response<PartnerSite> response = getResponse(result, LINK_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PartnerSite site = response.getResult();
|
||||
assertNotNull(site.getId());
|
||||
assertEquals(linkReq.getName(), site.getName());
|
||||
assertEquals(linkReq.getUrl(), site.getUrl());
|
||||
assertEquals(linkReq.isOpen(), site.getOpen());
|
||||
});
|
||||
|
||||
// https/http
|
||||
linkReq.setName(randomStr(4));
|
||||
linkReq.setOpen(false);
|
||||
linkReq.setUrl(randomStr(4) + ".example.com");
|
||||
getMockData(post("/admin/links/create"), adminLogin(), linkReq).andDo(result -> {
|
||||
Response<PartnerSite> response = getResponse(result, LINK_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PartnerSite site = response.getResult();
|
||||
assertEquals("http://" + linkReq.getUrl(), site.getUrl());
|
||||
});
|
||||
|
||||
// 测试已存在的数据
|
||||
getMockData(post("/admin/links/create"), adminLogin(), linkReq).andDo(result ->
|
||||
assertEquals(DATA_HAS_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() throws Exception {
|
||||
PartnerSite partnerSite = new PartnerSite();
|
||||
partnerSite.setName(randomStr(4));
|
||||
partnerSite.setOpen(true);
|
||||
partnerSite.setDesc("");
|
||||
partnerSite.setIconPath("");
|
||||
partnerSite.setUrl("https://" + randomStr(4) + ".celess.cn");
|
||||
mapper.insert(partnerSite);
|
||||
PartnerSite latest = mapper.getLastest();
|
||||
assertNotNull(latest.getId());
|
||||
getMockData(delete("/admin/links/del/" + latest.getId()), adminLogin()).andDo(result -> {
|
||||
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertTrue(response.getResult());
|
||||
});
|
||||
long id = latest.getId();
|
||||
do {
|
||||
id += 1;
|
||||
} while (mapper.existsById(id));
|
||||
System.out.println("删除ID=" + id + "的数据");
|
||||
getMockData(delete("/admin/links/del/" + id), adminLogin()).andDo(result ->
|
||||
assertEquals(DATA_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
// 增数据
|
||||
PartnerSite partnerSite = new PartnerSite();
|
||||
partnerSite.setName(randomStr(4));
|
||||
partnerSite.setOpen(true);
|
||||
partnerSite.setDesc("");
|
||||
partnerSite.setIconPath("");
|
||||
partnerSite.setDelete(false);
|
||||
partnerSite.setUrl("https://" + randomStr(5) + ".celess.cn");
|
||||
mapper.insert(partnerSite);
|
||||
// 查数据
|
||||
PartnerSite latest = mapper.getLastest();
|
||||
assertNotNull(latest.getId());
|
||||
// 构建请求
|
||||
LinkReq linkReq = new LinkReq();
|
||||
linkReq.setUrl(latest.getUrl());
|
||||
linkReq.setOpen(!latest.getOpen());
|
||||
linkReq.setName(randomStr(4));
|
||||
linkReq.setId(latest.getId());
|
||||
|
||||
getMockData(put("/admin/links/update"), adminLogin(), linkReq).andDo(result -> {
|
||||
Response<PartnerSite> response = getResponse(result, LINK_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PartnerSite site = response.getResult();
|
||||
assertNotNull(site.getId());
|
||||
assertEquals(linkReq.getId(), site.getId().longValue());
|
||||
assertEquals(linkReq.getUrl(), site.getUrl());
|
||||
assertEquals(linkReq.getName(), site.getName());
|
||||
assertEquals(linkReq.isOpen(), site.getOpen());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allForOpen() throws Exception {
|
||||
getMockData(get("/links")).andDo(result -> {
|
||||
Response<List<PartnerSite>> response = getResponse(result, LINK_MODEL_LIST_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
response.getResult().forEach(site -> {
|
||||
assertNotNull(site.getUrl());
|
||||
assertNull(site.getOpen());
|
||||
assertNotNull(site.getName());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void all() throws Exception {
|
||||
getMockData(get("/admin/links?page=1&count=10"), adminLogin()).andDo(result -> {
|
||||
Response<PageData<PartnerSite>> response = getResponse(result, LINK_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
PageData<PartnerSite> pageData = response.getResult();
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
for (PartnerSite site : pageData.getList()) {
|
||||
assertNotNull(site.getUrl());
|
||||
assertNotNull(site.getName());
|
||||
assertNotNull(site.getOpen());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply() {
|
||||
// 做service 层的测试
|
||||
// mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
mockInjectInstance(partnerSiteService, "mailService", new TestMailServiceImpl());
|
||||
LinkApplyReq req = new LinkApplyReq();
|
||||
req.setName(randomStr(4));
|
||||
req.setUrl("https://" + randomStr(4) + ".celess.cn");
|
||||
req.setIconPath("https://www.celess.cn/example.png");
|
||||
req.setDesc("desc :" + randomStr());
|
||||
req.setEmail(randomStr(4) + "@celess.cn");
|
||||
req.setLinkUrl(req.getUrl() + "/links");
|
||||
try {
|
||||
// 抓取不到数据的链接
|
||||
partnerSiteService.apply(req);
|
||||
} catch (MyException e) {
|
||||
log.debug("测试抓取不到数据");
|
||||
assertEquals(CANNOT_GET_DATA.getCode(), e.getCode());
|
||||
}
|
||||
|
||||
req.setLinkUrl("https://bing.com");
|
||||
req.setUrl(req.getLinkUrl());
|
||||
try {
|
||||
partnerSiteService.apply(req);
|
||||
} catch (MyException e) {
|
||||
log.debug("测试未添加本站链接的友链申请");
|
||||
assertEquals(APPLY_LINK_NO_ADD_THIS_SITE.getCode(), e.getCode());
|
||||
assertNotNull(e.getResult());
|
||||
try {
|
||||
// 测试uuid一致性
|
||||
log.debug("测试uuid一致性");
|
||||
partnerSiteService.apply(req);
|
||||
} catch (MyException e2) {
|
||||
assertEquals(e.getResult(), e2.getResult());
|
||||
}
|
||||
}
|
||||
log.debug("测试正常申请");
|
||||
req.setLinkUrl("https://www.celess.cn");
|
||||
req.setUrl(req.getLinkUrl());
|
||||
PartnerSite apply = partnerSiteService.apply(req);
|
||||
assertNotNull(apply);
|
||||
assertNotNull(apply.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reapply() {
|
||||
//mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
try {
|
||||
partnerSiteService.reapply(randomStr());
|
||||
throw new AssertionError();
|
||||
} catch (MyException e) {
|
||||
assertEquals(DATA_EXPIRED.getCode(), e.getCode());
|
||||
}
|
||||
|
||||
LinkApplyReq req = new LinkApplyReq();
|
||||
req.setName(randomStr(4));
|
||||
req.setIconPath("https://www.celess.cn/example.png");
|
||||
req.setDesc("desc :" + randomStr());
|
||||
req.setEmail(randomStr(4) + "@celess.cn");
|
||||
req.setLinkUrl("https://bing.com");
|
||||
req.setUrl(req.getLinkUrl());
|
||||
String uuid;
|
||||
try {
|
||||
partnerSiteService.apply(req);
|
||||
// err here
|
||||
throw new AssertionError();
|
||||
} catch (MyException e) {
|
||||
uuid = (String) e.getResult();
|
||||
String reapply = partnerSiteService.reapply(uuid);
|
||||
assertEquals(reapply, "success");
|
||||
}
|
||||
|
||||
try {
|
||||
partnerSiteService.reapply(uuid);
|
||||
throw new AssertionError();
|
||||
} catch (MyException e) {
|
||||
assertEquals(DATA_EXPIRED.getCode(), e.getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.Tag;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.TagModel;
|
||||
import cn.celess.common.mapper.TagMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
public class TagControllerTest extends BaseTest {
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
private static final TypeReference<?> TAG_MODEL_TYPE = new TypeReference<Response<TagModel>>() {
|
||||
|
||||
};
|
||||
private static final TypeReference<?> TAG_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<TagModel>>>() {
|
||||
};
|
||||
private static final TypeReference<?> TAG_NAC_LIST_TYPE = new TypeReference<Response<List<Map<String, Object>>>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
String name = randomStr(4);
|
||||
getMockData(post("/admin/tag/create?name=" + name)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
getMockData(post("/admin/tag/create?name=" + name), userLogin()).andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
getMockData(post("/admin/tag/create?name=" + name), adminLogin()).andDo(result -> {
|
||||
Response<TagModel> response = getResponse(result, TAG_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
TagModel tag = response.getResult();
|
||||
assertNotNull(tag.getId());
|
||||
assertEquals(name, tag.getName());
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delOne() throws Exception {
|
||||
Tag lastestTag = tagMapper.getLastestTag();
|
||||
assertNotNull(lastestTag.getId());
|
||||
getMockData(delete("/admin/tag/del?id=" + lastestTag.getId()), adminLogin()).andDo(result -> {
|
||||
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertTrue(response.getResult());
|
||||
});
|
||||
long id = lastestTag.getId() * 2;
|
||||
getMockData(delete("/admin/tag/del?id=" + id), adminLogin())
|
||||
.andDo(result -> assertEquals(TAG_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOne() throws Exception {
|
||||
Tag tag = tagMapper.getLastestTag();
|
||||
assertNotNull(tag.getId());
|
||||
String name = randomStr(4);
|
||||
getMockData(put("/admin/tag/update?id=" + tag.getId() + "&name=" + name), adminLogin()).andDo(result -> {
|
||||
Response<TagModel> response = getResponse(result, TAG_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
TagModel t = response.getResult();
|
||||
assertEquals(name, t.getName());
|
||||
assertEquals(tag.getId(), t.getId());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPage() throws Exception {
|
||||
getMockData(get("/tags?page=1&count=5")).andDo(result -> {
|
||||
Response<PageData<TagModel>> response = getResponse(result, TAG_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
// 结果集非空
|
||||
assertNotNull(response.getResult());
|
||||
// 判断pageInfo是否包装完全
|
||||
PageData<TagModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(5, pageData.getPageSize());
|
||||
// 内容完整
|
||||
for (TagModel t : pageData.getList()) {
|
||||
assertNotNull(t.getId());
|
||||
assertNotNull(t.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTagNameAndCount() throws Exception {
|
||||
getMockData(get("/tags/nac")).andDo(result -> {
|
||||
Response<List<Map<String, Object>>> response = getResponse(result, TAG_NAC_LIST_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
response.getResult().forEach(o -> {
|
||||
assertNotNull(o.get("name"));
|
||||
assertNotNull(o.get("size"));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.common.entity.dto.LoginReq;
|
||||
import cn.celess.common.entity.dto.UserReq;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.UserModel;
|
||||
import cn.celess.common.mapper.UserMapper;
|
||||
import cn.celess.common.service.UserService;
|
||||
import cn.celess.common.util.MD5Util;
|
||||
import cn.celess.common.util.RedisUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
|
||||
public class UserControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
|
||||
RedisUtil redisUtil;
|
||||
private static final TypeReference<?> USER_MODEL_TYPE = new TypeReference<Response<UserModel>>() {
|
||||
};
|
||||
private static final TypeReference<?> USER_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<UserModel>>>() {
|
||||
};
|
||||
private static final TypeReference<?> USER_MODEL_LIST_TYPE = new TypeReference<Response<List<Map<String, Object>>>>() {
|
||||
};
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
|
||||
@Test
|
||||
public void login() throws Exception {
|
||||
assertNotNull(userLogin());
|
||||
assertNotNull(adminLogin());
|
||||
// 用户不存在
|
||||
LoginReq req = new LoginReq();
|
||||
req.setEmail("zh@celess.cn");
|
||||
req.setPassword("123456789");
|
||||
req.setIsRememberMe(false);
|
||||
getMockData(post("/login"), null, req).andDo(result -> assertEquals(USER_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registration() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logout() throws Exception {
|
||||
getMockData(get("/logout")).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||
getMockData(get("/logout"), adminLogin()).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateInfo() throws Exception {
|
||||
String desc = randomStr(4);
|
||||
String disPlayName = randomStr(4);
|
||||
getMockData(put("/user/userInfo/update?desc=" + desc + "&displayName=" + disPlayName), userLogin()).andDo(result -> {
|
||||
Response<UserModel> response = getResponse(result, USER_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
UserModel u = response.getResult();
|
||||
assertEquals(desc, u.getDesc());
|
||||
assertEquals(disPlayName, u.getDisplayName());
|
||||
assertNotNull(u.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserInfo() throws Exception {
|
||||
getMockData(get("/user/userInfo"), adminLogin()).andDo(result -> {
|
||||
Response<UserModel> response = getResponse(result, USER_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
UserModel u = response.getResult();
|
||||
assertNotNull(u.getId());
|
||||
assertNotNull(u.getEmail());
|
||||
assertNotNull(u.getDisplayName());
|
||||
assertNotNull(u.getEmailStatus());
|
||||
assertNotNull(u.getAvatarImgUrl());
|
||||
assertNotNull(u.getDesc());
|
||||
assertNotNull(u.getRecentlyLandedDate());
|
||||
assertNotNull(u.getRole());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upload() throws Exception {
|
||||
URL url = new URL("https://56462271.oss-cn-beijing.aliyuncs.com/web/logo.png");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
assertNotNull(inputStream);
|
||||
|
||||
// mock 实现类
|
||||
mockInjectInstance(userService, "qiniuService", new TestQiNiuServiceImpl());
|
||||
|
||||
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, inputStream);
|
||||
getMockData(multipart("/user/imgUpload").file(file), userLogin()).andDo(result -> {
|
||||
Response<Object> response = getResponse(result, OBJECT_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendResetPwdEmail() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendVerifyEmail() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emailVerify() throws Exception {
|
||||
String email = randomStr(4) + "@celess.cn";
|
||||
String pwd = MD5Util.getMD5("123456789");
|
||||
userMapper.addUser(new User(email, pwd));
|
||||
String verifyId = randomStr();
|
||||
LoginReq req = new LoginReq(email, "123456789", true);
|
||||
redisUtil.setEx(email + "-verify", verifyId, 2, TimeUnit.DAYS);
|
||||
getMockData(post("/emailVerify").param("verifyId", verifyId).param("email", email), login(req)).andDo(result ->
|
||||
assertEquals(SUCCESS.getCode(), getResponse(result, OBJECT_TYPE).getCode())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resetPwd() throws Exception {
|
||||
String email = randomStr(4) + "@celess.cn";
|
||||
String pwd = MD5Util.getMD5("1234567890");
|
||||
userMapper.addUser(new User(email, pwd));
|
||||
LoginReq req = new LoginReq(email, "1234567890", true);
|
||||
String verifyId = randomStr();
|
||||
// 设置验证id
|
||||
redisUtil.setEx(email + "-resetPwd", verifyId, 2, TimeUnit.DAYS);
|
||||
MockHttpServletRequestBuilder resetPwd = post("/resetPwd").param("verifyId", verifyId).param("email", email).param("pwd", "123456789");
|
||||
// 未验证
|
||||
getMockData(resetPwd, login(req)).andDo(result -> assertEquals(USEREMAIL_NOT_VERIFY.getCode(), getResponse(result, OBJECT_TYPE).getCode()));
|
||||
// 设置未验证
|
||||
userMapper.updateEmailStatus(email, true);
|
||||
// 正常
|
||||
getMockData(resetPwd, login(req)).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result, OBJECT_TYPE).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleDelete() throws Exception {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
String s = randomStr();
|
||||
String email = s.substring(s.length() - 4) + "@celess.cn";
|
||||
String pwd = MD5Util.getMD5("123456789");
|
||||
User user = new User(email, pwd);
|
||||
int i1 = userMapper.addUser(user);
|
||||
if (i1 == 0) {
|
||||
continue;
|
||||
}
|
||||
userList.add(userMapper.findByEmail(email));
|
||||
if (i == 9) {
|
||||
//设置一个管理员
|
||||
userMapper.setUserRole(userMapper.findByEmail(email).getId(), "admin");
|
||||
}
|
||||
}
|
||||
List<Integer> idList = userList.stream().map(user -> user.getId().intValue()).collect(Collectors.toList());
|
||||
getMockData(delete("/admin/user/delete"), adminLogin(), idList).andDo(result -> {
|
||||
Response<List<Map<String, Object>>> response = getResponse(result, USER_MODEL_LIST_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
response.getResult().forEach(o -> {
|
||||
// 判断响应数据中是否包含输入的id
|
||||
assertTrue(idList.contains((int) o.get("id")));
|
||||
// 判断处理状态
|
||||
boolean status = (boolean) o.get("status");
|
||||
if (o.containsKey("msg"))
|
||||
assertFalse(status);
|
||||
else
|
||||
assertTrue(status);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateInfoByAdmin() throws Exception {
|
||||
UserReq userReq = new UserReq();
|
||||
String email = randomStr(4) + "@celess.cn";
|
||||
User user = new User(email, MD5Util.getMD5("123456789"));
|
||||
userMapper.addUser(user);
|
||||
User userByDb = userMapper.findByEmail(email);
|
||||
userReq.setId(userByDb.getId());
|
||||
userReq.setPwd(randomStr().substring(0, 10));
|
||||
userReq.setDesc(randomStr());
|
||||
userReq.setEmailStatus(new Random().nextBoolean());
|
||||
userReq.setRole("admin");
|
||||
userReq.setDisplayName(randomStr(4));
|
||||
userReq.setEmail(randomStr(5) + "@celess.cn");
|
||||
getMockData(put("/admin/user"), adminLogin(), userReq).andDo(result -> {
|
||||
Response<UserModel> response = getResponse(result, USER_MODEL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
UserModel userModel = response.getResult();
|
||||
assertEquals(userReq.getId(), userModel.getId());
|
||||
assertEquals(userReq.getRole(), userModel.getRole());
|
||||
assertEquals(userReq.getEmail(), userModel.getEmail());
|
||||
assertEquals(userReq.getDesc(), userModel.getDesc());
|
||||
assertEquals(userReq.getDisplayName(), userModel.getDisplayName());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUser() throws Exception {
|
||||
getMockData(get("/admin/users?page=1&count=10"), adminLogin()).andDo(result -> {
|
||||
Response<PageData<UserModel>> response = getResponse(result, USER_MODEL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
// 结果集非空
|
||||
assertNotNull(response.getResult());
|
||||
// 判断pageInfo是否包装完全
|
||||
PageData<UserModel> pageData = response.getResult();
|
||||
assertNotEquals(0, pageData.getTotal());
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
// 内容完整
|
||||
for (UserModel u : pageData.getList()) {
|
||||
assertNotNull(u.getId());
|
||||
assertNotNull(u.getEmail());
|
||||
assertNotNull(u.getRole());
|
||||
assertNotNull(u.getEmailStatus());
|
||||
assertNotNull(u.getDisplayName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEmailStatus() throws Exception {
|
||||
String email = randomStr(4) + "@celess.cn";
|
||||
getMockData(get("/emailStatus/" + email)).andDo(result -> assertFalse((Boolean) getResponse(result, BOOLEAN_TYPE).getResult()));
|
||||
getMockData(get("/emailStatus/" + ADMIN_EMAIL)).andDo(result -> assertTrue((Boolean) getResponse(result, BOOLEAN_TYPE).getResult()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPwd() throws Exception {
|
||||
String email = randomStr(4) + "@celess.cn";
|
||||
assertEquals(1, userMapper.addUser(new User(email, MD5Util.getMD5("1234567890"))));
|
||||
LoginReq req = new LoginReq(email, "1234567890", false);
|
||||
String token = login(req);
|
||||
assertNotNull(token);
|
||||
MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
|
||||
param.add("pwd", "1234567890");
|
||||
param.add("newPwd", "aaabbbccc");
|
||||
param.add("confirmPwd", "aaabbbccc");
|
||||
getMockData(post("/user/setPwd").params(param), token).andDo(result -> {
|
||||
assertEquals(SUCCESS.getCode(), getResponse(result).getCode());
|
||||
assertEquals(MD5Util.getMD5("aaabbbccc"), userMapper.getPwd(email));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.VisitorModel;
|
||||
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());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
package cn.celess.controller;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.WebUpdate;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.WebUpdateModel;
|
||||
import cn.celess.common.mapper.WebUpdateInfoMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.DATA_NOT_EXIST;
|
||||
import static cn.celess.common.enmu.ResponseEnum.SUCCESS;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
|
||||
@Slf4j
|
||||
public class WebUpdateInfoControllerTest extends BaseTest {
|
||||
|
||||
private final TypeReference<?> MODAL_TYPE = new TypeReference<Response<WebUpdateModel>>() {
|
||||
};
|
||||
private final TypeReference<?> MODAL_LIST_TYPE = new TypeReference<Response<List<WebUpdateModel>>>() {
|
||||
};
|
||||
private final TypeReference<?> MODAL_PAGE_TYPE = new TypeReference<Response<PageData<WebUpdateModel>>>() {
|
||||
};
|
||||
|
||||
|
||||
@Autowired
|
||||
WebUpdateInfoMapper mapper;
|
||||
|
||||
@Test
|
||||
public void create() throws Exception {
|
||||
String info = randomStr();
|
||||
getMockData(post("/admin/webUpdate/create?info=" + info), adminLogin()).andDo(result -> {
|
||||
Response<WebUpdateModel> response = getResponse(result, MODAL_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
WebUpdateModel webUpdateModel = response.getResult();
|
||||
assertEquals(info, webUpdateModel.getInfo());
|
||||
assertNotNull(webUpdateModel.getTime());
|
||||
assertNotEquals(0, webUpdateModel.getId());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void del() throws Exception {
|
||||
// 新增数据
|
||||
WebUpdate webUpdate = new WebUpdate();
|
||||
webUpdate.setUpdateInfo(randomStr());
|
||||
webUpdate.setUpdateTime(new Date());
|
||||
mapper.insert(webUpdate);
|
||||
// 接口测试
|
||||
List<WebUpdate> updateList = mapper.findAll();
|
||||
WebUpdate update = updateList.get(updateList.size() - 1);
|
||||
assertNotEquals(0, update.getId());
|
||||
|
||||
long id = update.getId();
|
||||
getMockData(delete("/admin/webUpdate/del/" + id), adminLogin()).andDo(result -> {
|
||||
Response<Object> response = getResponse(result);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
});
|
||||
do {
|
||||
id += 2;
|
||||
} while (mapper.existsById(id));
|
||||
log.debug("准备删除ID={}的不存在记录", id);
|
||||
getMockData(delete("/admin/webUpdate/del/" + id), adminLogin()).andDo(result -> assertEquals(DATA_NOT_EXIST.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
// 新增数据
|
||||
WebUpdate webUpdate = new WebUpdate();
|
||||
webUpdate.setUpdateInfo(randomStr());
|
||||
webUpdate.setUpdateTime(new Date());
|
||||
mapper.insert(webUpdate);
|
||||
List<WebUpdate> all = mapper.findAll();
|
||||
WebUpdate update = all.get(all.size() - 1);
|
||||
assertNotEquals(0, update.getId());
|
||||
assertNotNull(update.getUpdateInfo());
|
||||
String info = randomStr();
|
||||
getMockData(put("/admin/webUpdate/update?id=" + update.getId() + "&info=" + info), adminLogin()).andDo(result -> {
|
||||
List<WebUpdate> list = mapper.findAll();
|
||||
WebUpdate up = list.get(list.size() - 1);
|
||||
assertEquals(update.getId(), up.getId());
|
||||
assertEquals(update.getUpdateTime(), up.getUpdateTime());
|
||||
assertEquals(info, up.getUpdateInfo());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() throws Exception {
|
||||
getMockData(get("/webUpdate")).andDo(result -> {
|
||||
Response<List<WebUpdateModel>> response = getResponse(result, MODAL_LIST_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
assertNotEquals(0, response.getResult());
|
||||
response.getResult().forEach(webUpdate -> {
|
||||
assertNotEquals(0, webUpdate.getId());
|
||||
assertNotNull(webUpdate.getTime());
|
||||
assertNotNull(webUpdate.getInfo());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void page() throws Exception {
|
||||
getMockData(get("/webUpdate/pages?page=1&count=10")).andDo(result -> {
|
||||
Response<PageData<WebUpdateModel>> response = getResponse(result, MODAL_PAGE_TYPE);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(response.getResult());
|
||||
PageData<WebUpdateModel> pageData = response.getResult();
|
||||
assertEquals(1, pageData.getPageNum());
|
||||
assertEquals(10, pageData.getPageSize());
|
||||
for (WebUpdateModel model : pageData.getList()) {
|
||||
assertNotEquals(0, model.getId());
|
||||
assertNotNull(model.getTime());
|
||||
assertNotNull(model.getInfo());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastestUpdateTime() throws Exception {
|
||||
getMockData(get("/lastestUpdate")).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package cn.celess.enmu;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.enmu.UserAccountStatusEnum;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class UserAccountStatusEnumTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
assertEquals(UserAccountStatusEnum.NORMAL, UserAccountStatusEnum.get(0));
|
||||
assertEquals(UserAccountStatusEnum.LOCKED, UserAccountStatusEnum.get(1));
|
||||
assertEquals(UserAccountStatusEnum.DELETED, UserAccountStatusEnum.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJson() throws JsonProcessingException {
|
||||
// 序列化
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
assertEquals("{\"code\":0,\"desc\":\"正常\"}", objectMapper.writeValueAsString(UserAccountStatusEnum.NORMAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() throws IOException {
|
||||
// 反序列化
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
UserAccountStatusEnum userAccountStatusEnum = mapper.readValue(mapper.writeValueAsString(UserAccountStatusEnum.NORMAL), UserAccountStatusEnum.class);
|
||||
assertEquals(UserAccountStatusEnum.NORMAL.getCode(), userAccountStatusEnum.getCode());
|
||||
assertEquals(UserAccountStatusEnum.NORMAL.getDesc(), userAccountStatusEnum.getDesc());
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package cn.celess.filter;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Response;
|
||||
import org.junit.Test;
|
||||
|
||||
import static cn.celess.common.enmu.ResponseEnum.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
* @Date: 2019/11/28 16:05
|
||||
* @Description: 授权拦截器的测试类
|
||||
*/
|
||||
public class AuthorizationFilter extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void UserAccess() throws Exception {
|
||||
// 未登录
|
||||
getMockData(get("/user/userInfo")).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result).getCode()));
|
||||
// user权限登录
|
||||
getMockData(get("/user/userInfo"), userLogin()).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void AdminAccess() throws Exception {
|
||||
// 未登录
|
||||
getMockData(get("/admin/articles?page=1&count=1")).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result).getCode()));
|
||||
// user权限
|
||||
getMockData(get("/admin/articles?page=1&count=1"), userLogin()).andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), getResponse(result).getCode()));
|
||||
// admin 权限
|
||||
getMockData(get("/admin/articles?page=1&count=1"), adminLogin()).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void VisitorAccess() throws Exception {
|
||||
getMockData(get("/user/userInfo")).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result).getCode()));
|
||||
getMockData(get("/admin/articles?page=1&count=1")).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result).getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authorizationTest() throws Exception {
|
||||
// 测试response中有无Authorization字段
|
||||
String token = userLogin();
|
||||
getMockData(get("/user/userInfo"), token).andDo(result -> {
|
||||
Response<Object> response = getResponse(result);
|
||||
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||
assertNotNull(result.getResponse().getHeader("Authorization"));
|
||||
assertNotEquals(token, result.getResponse().getHeader("Authorization"));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package cn.celess.filter;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.DeployBaseTest;
|
||||
import cn.celess.common.enmu.ResponseEnum;
|
||||
import cn.celess.common.entity.Response;
|
||||
import org.junit.Assert;
|
||||
@@ -13,7 +13,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
* @Date: 2019/11/28 16:17
|
||||
* @Description: 测试重复请求
|
||||
*/
|
||||
public class MultipleSubmitFilter extends BaseTest {
|
||||
public class MultipleSubmitFilter extends DeployBaseTest {
|
||||
|
||||
private MockHttpSession session = null;
|
||||
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.*;
|
||||
import cn.celess.common.mapper.ArticleMapper;
|
||||
import cn.celess.common.mapper.ArticleTagMapper;
|
||||
import cn.celess.common.mapper.TagMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ArticleMapperTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
@Autowired
|
||||
ArticleTagMapper articleTagMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Article article = generateArticle().getArticle();
|
||||
assertNotNull(article.getId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Article article = generateArticle().getArticle();
|
||||
|
||||
assertFalse(articleMapper.isDeletedById(article.getId()));
|
||||
assertEquals(1, articleMapper.delete(article.getId()));
|
||||
assertTrue(articleMapper.isDeletedById(article.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Article article = generateArticle().getArticle();
|
||||
String randomText = randomStr();
|
||||
|
||||
// 此字段不会通过insert被写入数据库而是使用插入数据的默认值 数据库中该字段默认为true
|
||||
article.setOpen(true);
|
||||
|
||||
article.setTitle("test update " + randomText);
|
||||
article.setMdContent("test update ");
|
||||
article.setSummary("test update ");
|
||||
article.setType(false);
|
||||
article.setUrl("https://www.celess.cn");
|
||||
article.getCategory().setId(2L);
|
||||
article.setOpen(!article.getOpen());
|
||||
articleMapper.update(article);
|
||||
|
||||
Article articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(article.getTitle(), articleById.getTitle());
|
||||
assertEquals(article.getMdContent(), articleById.getMdContent());
|
||||
assertEquals(article.getSummary(), articleById.getSummary());
|
||||
assertEquals(article.getType(), articleById.getType());
|
||||
assertEquals(article.getCategory().getId(), articleById.getCategory().getId());
|
||||
assertEquals(article.getOpen(), articleById.getOpen());
|
||||
assertNotNull(articleById.getUpdateDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectAll() {
|
||||
List<Article> list = articleMapper.findAll();
|
||||
assertNotEquals(0, list.size());
|
||||
list.forEach(article -> {
|
||||
assertNotEquals(0, article.getTags().size());
|
||||
assertNotNull(article.getId());
|
||||
assertNotNull(article.getTitle());
|
||||
assertNotNull(article.getSummary());
|
||||
assertNotNull(article.getMdContent());
|
||||
assertNotNull(article.getType());
|
||||
if (!article.getType()) {
|
||||
assertNotNull(article.getUrl());
|
||||
}
|
||||
assertNotNull(article.getReadingNumber());
|
||||
assertNotNull(article.getLikeCount());
|
||||
assertNotNull(article.getDislikeCount());
|
||||
assertNotNull(article.getPublishDate());
|
||||
assertNotNull(article.getOpen());
|
||||
assertNotNull(article.getCategory());
|
||||
assertNotNull(article.getUser());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateReadCount() {
|
||||
Article article = generateArticle().getArticle();
|
||||
Article articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(Long.valueOf(0), articleById.getReadingNumber());
|
||||
articleMapper.updateReadingNumber(articleById.getId());
|
||||
articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(Long.valueOf(1), articleById.getReadingNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestArticle() {
|
||||
Article article = generateArticle().getArticle();
|
||||
Article lastestArticle = articleMapper.getLastestArticle();
|
||||
assertNotNull(lastestArticle);
|
||||
assertEquals(article.getId(), lastestArticle.getId());
|
||||
assertNotNull(lastestArticle.getCategory());
|
||||
assertNotEquals(0, lastestArticle.getTags().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findArticleById() {
|
||||
Article article = generateArticle().getArticle();
|
||||
Article articleById = articleMapper.findArticleById(article.getId());
|
||||
assertNotNull(articleById);
|
||||
assertEquals(article.getId(), articleById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByTitle() {
|
||||
Article article = generateArticle().getArticle();
|
||||
assertTrue(articleMapper.existsByTitle(article.getTitle()));
|
||||
assertFalse(articleMapper.existsByTitle(randomStr()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDeletedById() {
|
||||
Article article = generateArticle().getArticle();
|
||||
assertFalse(articleMapper.isDeletedById(article.getId()));
|
||||
articleMapper.delete(article.getId());
|
||||
assertTrue(articleMapper.isDeletedById(article.getId()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByAuthorId() {
|
||||
List<Article> allByAuthorId = articleMapper.findAllByAuthorId(1);
|
||||
assertNotEquals(0, allByAuthorId.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByOpen() {
|
||||
List<Article> allByOpen = articleMapper.findAllByOpen(true);
|
||||
assertNotEquals(0, allByOpen);
|
||||
|
||||
Article article = generateArticle().getArticle();
|
||||
article.setOpen(false);
|
||||
|
||||
articleMapper.update(article);
|
||||
|
||||
List<Article> privateArticles = articleMapper.findAllByOpen(false);
|
||||
assertTrue(privateArticles.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTitleById() {
|
||||
Article article = generateArticle().getArticle();
|
||||
|
||||
assertEquals(article.getTitle(), articleMapper.getTitleById(article.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByCategoryId() {
|
||||
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(1);
|
||||
assertNotEquals(0, allByCategoryId.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByCategoryIdAndOpen() {
|
||||
List<Article> allByCategoryId = articleMapper.findAllByCategoryIdAndOpen(1);
|
||||
assertNotEquals(0, allByCategoryId.size());
|
||||
allByCategoryId.forEach(article -> assertTrue(article.getOpen()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Article> allByCategoryId = articleMapper.findAll();
|
||||
assertNotEquals(0, allByCategoryId.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
assertNotEquals(0, articleMapper.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPreArticle() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
Article preArticle = articleMapper.getPreArticle(articleTag.getArticle().getId());
|
||||
assertNotNull(preArticle);
|
||||
assertTrue(preArticle.getId() < articleTag.getArticle().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNextArticle() {
|
||||
Article getNextArticle = articleMapper.getNextArticle(3L);
|
||||
assertNotNull(getNextArticle);
|
||||
assertTrue(getNextArticle.getId() > 3L);
|
||||
}
|
||||
|
||||
private ArticleTag generateArticle() {
|
||||
String randomText = randomStr();
|
||||
|
||||
Article article = new Article();
|
||||
Category category = new Category();
|
||||
category.setId(1L);
|
||||
article.setCategory(category);
|
||||
article.setTitle(" unity test for article " + randomText);
|
||||
article.setMdContent("# unity test for article");
|
||||
article.setSummary("unity test for article");
|
||||
|
||||
Tag tag = tagMapper.findTagByName("随笔");
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
article.setUser(user);
|
||||
article.setType(true);
|
||||
|
||||
articleMapper.insert(article);
|
||||
|
||||
ArticleTag articleTag = new ArticleTag();
|
||||
articleTag.setArticle(article);
|
||||
articleTag.setTag(tag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
|
||||
return articleTag;
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.*;
|
||||
import cn.celess.common.mapper.ArticleMapper;
|
||||
import cn.celess.common.mapper.ArticleTagMapper;
|
||||
import cn.celess.common.mapper.TagMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ArticleTagMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
ArticleTagMapper articleTagMapper;
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
assertNotNull(articleTag);
|
||||
assertNotNull(articleTag.getArticle());
|
||||
assertNotNull(articleTag.getTag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Tag tag = tagMapper.findTagByName("电影");
|
||||
ArticleTag articleTag = generateArticle();
|
||||
articleTag.setTag(tag);
|
||||
|
||||
articleTagMapper.update(articleTag);
|
||||
|
||||
ArticleTag oneById = articleTagMapper.findOneById(articleTag.getId());
|
||||
assertEquals(articleTag.getArticle().getId(), oneById.getArticle().getId());
|
||||
assertEquals(articleTag.getArticle().getTitle(), oneById.getArticle().getTitle());
|
||||
assertEquals(articleTag.getTag().getName(), oneById.getTag().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteById() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
|
||||
articleTagMapper.deleteById(articleTag.getId());
|
||||
|
||||
ArticleTag oneById = articleTagMapper.findOneById(articleTag.getId());
|
||||
assertNull(oneById);
|
||||
|
||||
articleTagMapper.insert(articleTag);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByArticleId() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
int i = articleTagMapper.deleteByArticleId(articleTag.getArticle().getId());
|
||||
assertNotEquals(0, i);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByArticleId() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
List<ArticleTag> allByArticleId = articleTagMapper.findAllByArticleId(articleTag.getArticle().getId());
|
||||
assertEquals(1, allByArticleId.size());
|
||||
|
||||
List<ArticleTag> list = articleTagMapper.findAllByArticleId(5L);
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteMultiById() {
|
||||
ArticleTag articleTag = new ArticleTag();
|
||||
Article article = articleMapper.getLastestArticle();
|
||||
Tag tag = tagMapper.getLastestTag();
|
||||
articleTag.setArticle(article);
|
||||
articleTag.setTag(tag);
|
||||
|
||||
articleTagMapper.insert(articleTag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
|
||||
List<ArticleTag> allByArticleId = articleTagMapper.findAllByArticleId(article.getId());
|
||||
assertTrue(allByArticleId.size() >= 6);
|
||||
int lines = articleTagMapper.deleteMultiById(allByArticleId);
|
||||
assertTrue(lines >= 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findArticleByTag() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
List<ArticleTag> articleByTag = articleTagMapper.findArticleByTag(21L);
|
||||
assertNotEquals(0, articleByTag.size());
|
||||
articleByTag.forEach(articleTag1 -> assertEquals(articleTag.getTag().getName(), articleTag1.getTag().getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findArticleByTagAndOpen() {
|
||||
ArticleTag articleTag = generateArticle();
|
||||
List<ArticleTag> articleByTag = articleTagMapper.findArticleByTagAndOpen(21L);
|
||||
assertNotEquals(0, articleByTag.size());
|
||||
articleByTag.forEach(articleTag1 -> assertEquals(articleTag.getTag().getName(), articleTag1.getTag().getName()));
|
||||
articleByTag.forEach(articleTag1 -> assertTrue(articleTag1.getArticle().getOpen()));
|
||||
}
|
||||
|
||||
private ArticleTag generateArticle() {
|
||||
String randomText = randomStr();
|
||||
|
||||
Article article = new Article();
|
||||
Category category = new Category();
|
||||
category.setId(1L);
|
||||
article.setCategory(category);
|
||||
article.setTitle(" unity test for article " + randomText);
|
||||
article.setMdContent("# unity test for article");
|
||||
article.setSummary("unity test for article");
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
article.setUser(user);
|
||||
article.setType(true);
|
||||
|
||||
articleMapper.insert(article);
|
||||
|
||||
ArticleTag articleTag = new ArticleTag();
|
||||
Tag tag = tagMapper.findTagByName("随笔");
|
||||
articleTag.setArticle(article);
|
||||
articleTag.setTag(tag);
|
||||
articleTagMapper.insert(articleTag);
|
||||
|
||||
return articleTag;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagByArticleId() {
|
||||
Article article = articleMapper.findAll().get(0);
|
||||
assertNotNull(article);
|
||||
|
||||
List<Tag> tagByArticleId = articleTagMapper.findTagByArticleId(article.getId());
|
||||
assertNotEquals(0, tagByArticleId.size());
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Category;
|
||||
import cn.celess.common.mapper.CategoryMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CategoryMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Category category = generateCategory();
|
||||
assertNotNull(category.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Category category = generateCategory();
|
||||
assertFalse(category.getDeleted());
|
||||
int lines = categoryMapper.delete(category.getId());
|
||||
assertNotEquals(0, lines);
|
||||
Category categoryById = categoryMapper.findCategoryById(category.getId());
|
||||
assertTrue(categoryById.getDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Category category = generateCategory();
|
||||
category.setName(randomStr(4));
|
||||
int update = categoryMapper.update(category);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsByName(category.getName()));
|
||||
assertFalse(categoryMapper.existsByName(randomStr(8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
Category category = generateCategory();
|
||||
assertTrue(categoryMapper.existsById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryByName() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
assertNotNull(suibi);
|
||||
|
||||
// tag 数据
|
||||
Category shiro = categoryMapper.findCategoryByName("shiro");
|
||||
assertNull(shiro);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCategoryById() {
|
||||
Category suibi = categoryMapper.findCategoryByName("随笔");
|
||||
|
||||
Category categoryById = categoryMapper.findCategoryById(suibi.getId());
|
||||
|
||||
assertEquals(suibi.getName(), categoryById.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
assertNotEquals(0, all);
|
||||
all.forEach(category -> assertTrue(category.getCategory()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllName() {
|
||||
List<String> allName = categoryMapper.getAllName();
|
||||
assertNotEquals(0, allName.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNameById() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getName(), categoryMapper.getNameById(category.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIdByName() {
|
||||
Category category = generateCategory();
|
||||
assertEquals(category.getId(), categoryMapper.getIdByName(category.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestCategory() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
all.sort((o1, o2) -> (int) (o2.getId() - o1.getId()));
|
||||
assertEquals(all.get(0).getId(), categoryMapper.getLastestCategory().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
List<Category> collect = all.stream().filter(category -> !category.getDeleted()).collect(Collectors.toList());
|
||||
assertEquals(collect.size(), categoryMapper.count());
|
||||
}
|
||||
|
||||
private Category generateCategory() {
|
||||
Category category = new Category(randomStr(4));
|
||||
|
||||
categoryMapper.insert(category);
|
||||
category.setDeleted(false);
|
||||
return category;
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.enmu.CommentStatusEnum;
|
||||
import cn.celess.common.entity.Comment;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.common.mapper.CommentMapper;
|
||||
import cn.celess.common.mapper.UserMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CommentMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Comment comment = generateComment();
|
||||
assertNotNull(comment.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateContent() {
|
||||
Comment comment = generateComment();
|
||||
comment.setContent(randomStr(10));
|
||||
assertEquals(1, commentMapper.updateContent(comment.getContent(), comment.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Comment comment = generateComment();
|
||||
assertEquals(1, commentMapper.delete(comment.getId()));
|
||||
Comment commentById = commentMapper.findCommentById(comment.getId());
|
||||
assertEquals(commentById.getStatus(), CommentStatusEnum.DELETED.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByPagePath() {
|
||||
Comment comment = generateComment();
|
||||
assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1);
|
||||
Comment commentById = commentMapper.findCommentById(comment.getId());
|
||||
assertEquals(commentById.getStatus(), CommentStatusEnum.DELETED.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
Comment comment = generateComment();
|
||||
assertTrue(commentMapper.existsById(comment.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCommentById() {
|
||||
Comment comment = generateComment();
|
||||
assertNotNull(commentMapper.findCommentById(comment.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestComment() {
|
||||
Comment comment = generateComment();
|
||||
Comment lastestComment = commentMapper.getLastestComment();
|
||||
assertEquals(comment.getId(), lastestComment.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByFromUser() {
|
||||
Comment comment = generateComment();
|
||||
List<Comment> allByFromUser = commentMapper.findAllByFromUser(comment.getFromUser().getId());
|
||||
assertNotEquals(0, allByFromUser);
|
||||
allByFromUser.forEach(comment1 -> assertEquals(comment.getFromUser().getId(), comment1.getFromUser().getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByPid() {
|
||||
Comment comment = generateComment();
|
||||
List<Comment> allByPid = commentMapper.findAllByPid(comment.getPid());
|
||||
assertTrue(allByPid.size() >= 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByPagePath() {
|
||||
Comment comment = generateComment();
|
||||
List<Comment> allByPagePath = commentMapper.findAllByPagePath(comment.getPagePath());
|
||||
assertTrue(allByPagePath.size() >= 1);
|
||||
allByPagePath.forEach(comment1 -> assertEquals(comment.getPagePath(), comment1.getPagePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllByPagePathAndPid() {
|
||||
Comment comment = generateComment();
|
||||
List<Comment> allByPagePathAndPid = commentMapper.findAllByPagePathAndPidAndNormal(comment.getPagePath(), comment.getPid());
|
||||
assertTrue(allByPagePathAndPid.size() >= 1);
|
||||
allByPagePathAndPid.forEach(comment1 -> {
|
||||
assertEquals(comment.getPagePath(), comment1.getPagePath());
|
||||
assertEquals(comment.getPid(), comment1.getPid());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllByPid() {
|
||||
Comment comment = generateComment();
|
||||
List<Comment> findAllByPid = commentMapper.findAllByPid(comment.getPid());
|
||||
assertTrue(findAllByPid.size() >= 1);
|
||||
findAllByPid.forEach(comment1 -> assertEquals(comment.getPid(), comment1.getPid()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countByType() {
|
||||
Comment comment = generateComment();
|
||||
long l = commentMapper.countByPagePath(comment.getPagePath());
|
||||
assertTrue(l >= 1);
|
||||
}
|
||||
|
||||
private Comment generateComment() {
|
||||
User from = userMapper.findById(1);
|
||||
assertNotNull(from);
|
||||
User to = userMapper.findById(2);
|
||||
assertNotNull(to);
|
||||
|
||||
Comment comment = new Comment();
|
||||
comment.setContent(randomStr(8));
|
||||
comment.setFromUser(from);
|
||||
comment.setToUser(to);
|
||||
comment.setPagePath("/tags");
|
||||
comment.setPid(-1L);
|
||||
commentMapper.insert(comment);
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.PartnerSite;
|
||||
import cn.celess.common.mapper.PartnerMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PartnerMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
PartnerMapper partnerMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertNotNull(partnerSite.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertEquals(1, partnerMapper.delete(partnerSite.getId()));
|
||||
partnerSite = partnerMapper.findById(partnerSite.getId());
|
||||
assertTrue(partnerSite.getDelete());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
partnerSite.setName(randomStr(5));
|
||||
partnerSite.setIconPath(randomStr(5));
|
||||
partnerSite.setDesc(randomStr(5));
|
||||
partnerSite.setOpen(false);
|
||||
partnerSite.setUrl("www.celess.cn/?random=" + randomStr(4));
|
||||
assertEquals(1, partnerMapper.update(partnerSite));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertTrue(partnerMapper.existsById(partnerSite.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertTrue(partnerMapper.existsByName(partnerSite.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByUrl() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertTrue(partnerMapper.existsByUrl(partnerSite.getUrl()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertNotNull(partnerMapper.findById(partnerSite.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByName() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertNotNull(partnerMapper.findByName(partnerSite.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByUrl() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
assertNotNull(partnerMapper.findByUrl(partnerSite.getUrl()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastest() {
|
||||
PartnerSite partnerSite = generatePartnerSite();
|
||||
List<PartnerSite> all = partnerMapper.findAll();
|
||||
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
assertEquals(partnerSite.getId(), all.get(0).getId());
|
||||
assertEquals(partnerSite.getId(), partnerMapper.getLastest().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<PartnerSite> all = partnerMapper.findAll();
|
||||
assertNotEquals(0, all.size());
|
||||
}
|
||||
|
||||
private PartnerSite generatePartnerSite() {
|
||||
PartnerSite ps = new PartnerSite();
|
||||
ps.setName(randomStr(4));
|
||||
ps.setUrl("https://www.celess.cn?random=" + randomStr(4));
|
||||
ps.setOpen(true);
|
||||
ps.setDesc("小海的博客呀!");
|
||||
ps.setIconPath("https://www.celess.cn/icon_path.example");
|
||||
partnerMapper.insert(ps);
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Tag;
|
||||
import cn.celess.common.mapper.TagMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TagMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Tag tag = generateTag();
|
||||
assertNotNull(tag.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Tag tag = generateTag();
|
||||
tag.setName(randomStr(4));
|
||||
int update = tagMapper.update(tag);
|
||||
assertEquals(1, update);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Tag tag = generateTag();
|
||||
assertFalse(tag.getDeleted());
|
||||
assertEquals(1, tagMapper.delete(tag.getId()));
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertTrue(tagById.getDeleted());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagById() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagById(tag.getId());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTagByName() {
|
||||
Tag tag = generateTag();
|
||||
Tag tagById = tagMapper.findTagByName(tag.getName());
|
||||
assertEquals(tag.getName(), tagById.getName());
|
||||
assertEquals(tag.getId(), tagById.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByName() {
|
||||
String s = randomStr(4);
|
||||
assertFalse(tagMapper.existsByName(s));
|
||||
Tag tag = new Tag(s);
|
||||
tagMapper.insert(tag);
|
||||
assertTrue(tagMapper.existsByName(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestTag() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
assertEquals(all.get(0).getId(), tagMapper.getLastestTag().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
assertNotEquals(0, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
assertNotEquals(0, tagMapper.count());
|
||||
List<Tag> all = tagMapper.findAll();
|
||||
List<Tag> collect = all.stream().filter(tag -> !tag.getDeleted()).collect(Collectors.toList());
|
||||
assertEquals(collect.size(), tagMapper.count());
|
||||
}
|
||||
|
||||
private Tag generateTag() {
|
||||
Tag tag = new Tag(randomStr(4));
|
||||
tagMapper.insert(tag);
|
||||
tag.setDeleted(false);
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.enmu.RoleEnum;
|
||||
import cn.celess.common.enmu.UserAccountStatusEnum;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.common.mapper.UserMapper;
|
||||
import cn.celess.common.util.MD5Util;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class UserMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
|
||||
|
||||
@Test
|
||||
public void addUser() {
|
||||
User user = generateUser();
|
||||
assertNotNull(user.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateInfo() {
|
||||
User user = generateUser();
|
||||
assertEquals(1, userMapper.updateInfo("ttt", "ttt", user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAvatarImgUrl() {
|
||||
User user = generateUser();
|
||||
assertEquals(1, userMapper.updateAvatarImgUrl("https://www.celess.cn/example.jpg", user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateLoginTime() {
|
||||
User user = generateUser();
|
||||
assertEquals(1, userMapper.updateLoginTime(user.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateEmailStatus() {
|
||||
User user = generateUser();
|
||||
assertEquals(1, userMapper.updateEmailStatus(user.getEmail(), true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePwd() {
|
||||
User user = generateUser();
|
||||
assertEquals(1, userMapper.updatePwd(user.getEmail(), MD5Util.getMD5("12345687654")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPwd() {
|
||||
User user = generateUser();
|
||||
assertEquals(user.getPwd(), userMapper.getPwd(user.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsByEmail() {
|
||||
User user = generateUser();
|
||||
assertTrue(userMapper.existsByEmail(user.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByEmail() {
|
||||
User user = generateUser();
|
||||
User byEmail = userMapper.findByEmail(user.getEmail());
|
||||
assertNotNull(byEmail);
|
||||
assertEquals(user.getId(), byEmail.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
User user = generateUser();
|
||||
User findById = userMapper.findById(user.getId());
|
||||
assertNotNull(findById);
|
||||
assertEquals(user.getEmail(), findById.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvatarImgUrlById() {
|
||||
User user = generateUser();
|
||||
assertNull(userMapper.getAvatarImgUrlById(user.getId()));
|
||||
userMapper.updateAvatarImgUrl("example.cn", user.getId());
|
||||
assertEquals("example.cn", userMapper.getAvatarImgUrlById(user.getId()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEmail() {
|
||||
User user = generateUser();
|
||||
assertEquals(user.getEmail(), userMapper.getEmail(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDisPlayName() {
|
||||
User user = generateUser();
|
||||
assertNull(userMapper.getDisPlayName(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRoleByEmail() {
|
||||
User user = generateUser();
|
||||
assertEquals(RoleEnum.USER_ROLE.getRoleName(), userMapper.getRoleByEmail(user.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRoleById() {
|
||||
User user = generateUser();
|
||||
assertEquals(RoleEnum.USER_ROLE.getRoleName(), userMapper.getRoleById(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
generateUser();
|
||||
assertTrue(userMapper.count() >= 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
User user = generateUser();
|
||||
int delete = userMapper.delete(user.getId());
|
||||
assertEquals(1, delete);
|
||||
User byId = userMapper.findById(user.getId());
|
||||
assertEquals(UserAccountStatusEnum.DELETED.getCode(), byId.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lock() {
|
||||
User user = generateUser();
|
||||
int delete = userMapper.lock(user.getId());
|
||||
assertEquals(1, delete);
|
||||
User byId = userMapper.findById(user.getId());
|
||||
assertEquals(UserAccountStatusEnum.LOCKED.getCode(), byId.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUserRole() {
|
||||
User user = generateUser();
|
||||
userMapper.setUserRole(user.getId(), RoleEnum.ADMIN_ROLE.getRoleName());
|
||||
|
||||
assertEquals(RoleEnum.ADMIN_ROLE.getRoleName(), userMapper.getRoleById(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
User user = generateUser();
|
||||
List<User> all = userMapper.findAll();
|
||||
assertTrue(all.size() >= 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
User user = generateUser();
|
||||
user.setDesc("aaa");
|
||||
user.setDisplayName("bbb");
|
||||
user.setEmailStatus(true);
|
||||
user.setRole(RoleEnum.ADMIN_ROLE.getRoleName());
|
||||
user.setAvatarImgUrl("https://celess.cn/examcple.jpg");
|
||||
user.setEmail(randomStr(8) + "@celess.cn");
|
||||
user.setPwd(MD5Util.getMD5("010100000100000"));
|
||||
assertEquals(1, userMapper.update(user));
|
||||
User byId = userMapper.findById(user.getId());
|
||||
assertEquals(user.getDesc(), byId.getDesc());
|
||||
assertEquals(user.getDisplayName(), byId.getDisplayName());
|
||||
assertEquals(user.getEmailStatus(), byId.getEmailStatus());
|
||||
assertEquals(user.getRole(), byId.getRole());
|
||||
assertEquals(user.getAvatarImgUrl(), byId.getAvatarImgUrl());
|
||||
assertEquals(user.getEmail(), byId.getEmail());
|
||||
assertEquals(user.getPwd(), byId.getPwd());
|
||||
}
|
||||
|
||||
private User generateUser() {
|
||||
User user = new User(randomStr(6) + "@celess.cn", MD5Util.getMD5("1234567890"));
|
||||
userMapper.addUser(user);
|
||||
User newUser = userMapper.findByEmail(user.getEmail());
|
||||
assertEquals(user.getId(), newUser.getId());
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.Visitor;
|
||||
import cn.celess.common.mapper.VisitorMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class VisitorMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
VisitorMapper visitorMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Visitor visitor = new Visitor();
|
||||
visitor.setDate(new Date());
|
||||
visitor.setIp("127.0.0.1");
|
||||
visitor.setUa("ua");
|
||||
assertEquals(1, visitorMapper.insert(visitor));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Visitor visitor = new Visitor();
|
||||
visitor.setDate(new Date());
|
||||
visitor.setIp("127.0.0.1");
|
||||
visitor.setUa("ua");
|
||||
visitorMapper.insert(visitor);
|
||||
assertEquals(1, visitorMapper.delete(visitor.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void count() {
|
||||
assertNotEquals(0, visitorMapper.count());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package cn.celess.mapper;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.WebUpdate;
|
||||
import cn.celess.common.mapper.WebUpdateInfoMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class WebUpdateInfoMapperTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
WebUpdateInfoMapper webUpdateInfoMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
assertNotNull(webUpdate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
assertEquals(1, webUpdateInfoMapper.delete(webUpdate.getId()));
|
||||
assertTrue(webUpdateInfoMapper.findById(webUpdate.getId()).isDelete());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
assertEquals(1, webUpdateInfoMapper.update(webUpdate.getId(), randomStr(6)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existsById() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
assertTrue(webUpdateInfoMapper.existsById(webUpdate.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
WebUpdate byId = webUpdateInfoMapper.findById(webUpdate.getId());
|
||||
assertEquals(webUpdate.getUpdateInfo(), byId.getUpdateInfo());
|
||||
assertEquals(webUpdate.getId(), byId.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
List<WebUpdate> all = webUpdateInfoMapper.findAll();
|
||||
assertNotEquals(0, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllNotDeleted() {
|
||||
this.delete();
|
||||
List<WebUpdate> allNotDeleted = webUpdateInfoMapper.findAllNotDeleted();
|
||||
allNotDeleted.forEach(webUpdate -> assertFalse(webUpdate.isDelete()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLastestOne() {
|
||||
WebUpdate webUpdate = generateUpdateInfo();
|
||||
List<WebUpdate> all = webUpdateInfoMapper.findAll();
|
||||
List<WebUpdate> allNotDeleted = webUpdateInfoMapper.findAllNotDeleted();
|
||||
all.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
allNotDeleted.sort(((o1, o2) -> (int) (o2.getId() - o1.getId())));
|
||||
assertEquals(webUpdate.getId(), all.get(0).getId());
|
||||
assertEquals(webUpdate.getId(), allNotDeleted.get(0).getId());
|
||||
assertEquals(webUpdate.getId(), webUpdateInfoMapper.getLastestOne().getId());
|
||||
}
|
||||
|
||||
private WebUpdate generateUpdateInfo() {
|
||||
WebUpdate webUpdate = new WebUpdate(randomStr(8));
|
||||
webUpdateInfoMapper.insert(webUpdate);
|
||||
return webUpdate;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package cn.celess.service;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.vo.ArticleModel;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.mapper.ArticleMapper;
|
||||
import cn.celess.common.service.ArticleService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ArticleServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
ArticleService articleService;
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Test
|
||||
public void adminArticles() {
|
||||
// 测deleted参数传值
|
||||
PageData<ArticleModel> pageData = articleService.adminArticles(10, 1, true);
|
||||
assertTrue(pageData.getList().stream().allMatch(ArticleModel::isDeleted));
|
||||
pageData = articleService.adminArticles(10, 1, false);
|
||||
assertFalse(pageData.getList().stream().allMatch(ArticleModel::isDeleted));
|
||||
pageData = articleService.adminArticles(10, 1, null);
|
||||
assertTrue(pageData.getList().stream().anyMatch(ArticleModel::isDeleted));
|
||||
assertTrue(pageData.getList().stream().anyMatch(articleModel -> !articleModel.isDeleted()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrievePageForOpen() {
|
||||
PageData<ArticleModel> articleModelPageData = articleService.retrievePageForOpen(10, 1);
|
||||
assertEquals(10, articleModelPageData.getPageSize());
|
||||
assertEquals(1, articleModelPageData.getPageNum());
|
||||
assertEquals(10, articleModelPageData.getList().size());
|
||||
articleModelPageData.getList().forEach(Assert::assertNotNull);
|
||||
|
||||
// 测试open字段
|
||||
articleModelPageData.getList().forEach(articleModel -> {
|
||||
// 当前文章
|
||||
assertTrue(articleMapper.findArticleById(articleModel.getId()).getOpen());
|
||||
if (articleModel.getPreArticle() != null) {
|
||||
assertTrue(articleMapper.findArticleById(articleModel.getPreArticle().getId()).getOpen());
|
||||
}
|
||||
if (articleModel.getNextArticle() != null) {
|
||||
assertTrue(articleMapper.findArticleById(articleModel.getNextArticle().getId()).getOpen());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package cn.celess.service;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.PartnerSite;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.service.PartnerSiteService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PartnerSiteServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
PartnerSiteService partnerSiteService;
|
||||
|
||||
@Test
|
||||
public void partnerSitePages() {
|
||||
// 测试deleted 参数
|
||||
PageData<PartnerSite> pageData = partnerSiteService.partnerSitePages(1, 10, true);
|
||||
assertTrue(pageData.getList().stream().allMatch(PartnerSite::getDelete));
|
||||
pageData = partnerSiteService.partnerSitePages(1, 10, false);
|
||||
assertTrue(pageData.getList().stream().noneMatch(PartnerSite::getDelete));
|
||||
pageData = partnerSiteService.partnerSitePages(1, 10, null);
|
||||
|
||||
List<PartnerSite> list = pageData.getList();
|
||||
assertNotEquals(0, list.stream().filter(PartnerSite::getDelete).count());
|
||||
assertNotEquals(0, list.stream().filter(partnerSite -> !partnerSite.getDelete()).count());
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package cn.celess.service;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.enmu.ResponseEnum;
|
||||
import cn.celess.common.enmu.UserAccountStatusEnum;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.common.entity.dto.LoginReq;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.UserModel;
|
||||
import cn.celess.common.exception.MyException;
|
||||
import cn.celess.common.mapper.UserMapper;
|
||||
import cn.celess.common.service.UserService;
|
||||
import cn.celess.common.util.MD5Util;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class UserServiceTest extends BaseTest {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void getUserList() {
|
||||
// 测试status 参数
|
||||
PageData<UserModel> userList = userService.getUserList(1, 10, UserAccountStatusEnum.NORMAL.getCode());
|
||||
assertTrue(userList.getList().stream().allMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.NORMAL.getCode()));
|
||||
userList = userService.getUserList(1, 10, UserAccountStatusEnum.LOCKED.getCode());
|
||||
assertTrue(userList.getList().stream().allMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.LOCKED.getCode()));
|
||||
userList = userService.getUserList(1, 10, UserAccountStatusEnum.DELETED.getCode());
|
||||
assertTrue(userList.getList().stream().allMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.DELETED.getCode()));
|
||||
userList = userService.getUserList(1, 10, null);
|
||||
assertTrue(userList.getList().stream().anyMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.NORMAL.getCode()));
|
||||
assertTrue(userList.getList().stream().anyMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.LOCKED.getCode()));
|
||||
assertTrue(userList.getList().stream().anyMatch(userModel -> userModel.getStatus().getCode() == UserAccountStatusEnum.DELETED.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogin() {
|
||||
// 测试账户 被锁 被删除 登录
|
||||
String email = randomStr(5) + "@celess.cn";
|
||||
String pwd = MD5Util.getMD5("123456789");
|
||||
User user = new User(email, pwd);
|
||||
userMapper.addUser(user);
|
||||
assertNotNull(user.getId());
|
||||
|
||||
user = userMapper.findByEmail(email);
|
||||
LoginReq loginReq = new LoginReq(email, "123456789", false);
|
||||
UserModel login = userService.login(loginReq);
|
||||
assertEquals(UserAccountStatusEnum.NORMAL, login.getStatus());
|
||||
|
||||
userMapper.lock(user.getId());
|
||||
try {
|
||||
userService.login(loginReq);
|
||||
fail("测试登录被锁账户 失败!");
|
||||
} catch (MyException e) {
|
||||
assertEquals(ResponseEnum.CAN_NOT_USE.getCode(), e.getCode());
|
||||
assertEquals(UserAccountStatusEnum.LOCKED, e.getResult());
|
||||
}
|
||||
|
||||
userMapper.delete(user.getId());
|
||||
try {
|
||||
userService.login(loginReq);
|
||||
fail("测试登录被删除账户 失败!");
|
||||
} catch (MyException e) {
|
||||
assertEquals(ResponseEnum.CAN_NOT_USE.getCode(), e.getCode());
|
||||
assertEquals(UserAccountStatusEnum.DELETED, e.getResult());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package cn.celess.service;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.vo.PageData;
|
||||
import cn.celess.common.entity.vo.VisitorModel;
|
||||
import cn.celess.common.service.VisitorService;
|
||||
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())));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package cn.celess.util;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.visitor.util.AddressUtil;
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package cn.celess.util;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.util.DateFormatUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class DateFormatUtilTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
assertNotNull(DateFormatUtil.get(new Date()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getForXmlDate() {
|
||||
assertNotNull(DateFormatUtil.getForXmlDate(new Date()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNow() {
|
||||
assertNotNull(DateFormatUtil.getNow());
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package cn.celess.util;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.util.HttpUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class HttpUtilTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
String s = HttpUtil.get("https://api.celess.cn/headerInfo");
|
||||
assertNotNull(s);
|
||||
// Response<Map<String, Object>> response = getResponse(s, MAP_OBJECT_TYPE);
|
||||
// assertEquals(ResponseEnum.SUCCESS.getCode(), response.getCode());
|
||||
// assertNotEquals(0, response.getResult().size());
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package cn.celess.util;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.entity.User;
|
||||
import cn.celess.user.util.JwtUtil;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class JwtUtilTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
JwtUtil jwtUtil;
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
|
||||
@Test
|
||||
public void testGenerateToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
String s = jwtUtil.generateToken(user, false);
|
||||
assertNotNull(s);
|
||||
String str = null;
|
||||
try {
|
||||
str = jwtUtil.generateToken(null, false);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
assertNull(str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsTokenExpired() throws InterruptedException {
|
||||
String s = Jwts.builder()
|
||||
.setClaims(null)
|
||||
.setExpiration(new Date(Instant.now().toEpochMilli() + 1000))
|
||||
.signWith(SignatureAlgorithm.HS512, secret)
|
||||
.compact();
|
||||
Thread.sleep(1010);
|
||||
assertTrue(jwtUtil.isTokenExpired(s));
|
||||
assertFalse(jwtUtil.isTokenExpired(jwtUtil.generateToken(new User(), false)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUsernameFromToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
String s = jwtUtil.generateToken(user, false);
|
||||
assertEquals(user.getEmail(), jwtUtil.getUsernameFromToken(s));
|
||||
user.setEmail("example@celess.cn");
|
||||
assertNotEquals(user.getEmail(), jwtUtil.getUsernameFromToken(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetExpirationDateFromToken() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
String s = jwtUtil.generateToken(user, false);
|
||||
assertNotNull(jwtUtil.getExpirationDateFromToken(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateTokenDate() {
|
||||
User user = new User();
|
||||
user.setEmail("a@celess.cn");
|
||||
String s = jwtUtil.generateToken(user, false);
|
||||
Date before = jwtUtil.getExpirationDateFromToken(s);
|
||||
String s1 = jwtUtil.updateTokenDate(s);
|
||||
assertTrue(jwtUtil.getExpirationDateFromToken(s1).getTime() - jwtUtil.getExpirationDateFromToken(s).getTime() > 0);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package cn.celess.util;
|
||||
|
||||
import cn.celess.BaseTest;
|
||||
import cn.celess.common.util.MD5Util;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MD5UtilTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void getMD5() {
|
||||
assertEquals("25f9e794323b453885f5181f1b624d0b", MD5Util.getMD5("123456789"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user