Commit 1bada988 by pengdan

支持 api信息, 账户信息从数据库读取

parent f6f168d9
......@@ -152,6 +152,11 @@
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--/spring -->
<dependency>
......
......@@ -3,6 +3,8 @@ package com.live.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.live.jdbc.dao.DataDao;
import com.live.jdbc.entity.ApiInfo;
import com.live.utils.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -24,6 +26,7 @@ import java.util.*;
public class BaseTest extends AbstractTestNGSpringContextTests {
private static final Log logger = LogFactory.getLog(BaseTest.class);
private static DataDao dataDao;
public static RestfulClient httpclient = null;
public static SSLRestfulClient sslhttpclient = null;
......@@ -124,24 +127,20 @@ public class BaseTest extends AbstractTestNGSpringContextTests {
private void dataPreparation(XMAppApi executionApi, HashMap<String, String> executionHeaders) {
// Assert.assertNotNull(executionApi.getApiModule(), "必要的api信息不存在!");
prop = CommUtil.getconfig();
JsonAndFile fileHandle = new JsonAndFile();
String apiModule = prop.getProperty(executionApi.getApiModule());
logger.info(executionApi.getApiModule());
// 获取接口模块参数
String fileParam = fileHandle.readTxtFile(System.getProperty("user.dir") + apiModule);
// Assert.assertNotNull(fileParam, "必要的API模块信息未找到,请检查api模块文件路径!");
JSONObject api=null;
if (fileParam.isEmpty()){
logger.error("必要的API模块信息未找到,请检查api模块文件路径!");
}else {
api = (JSONObject) JSON.parseObject(fileParam).get(executionApi.getApiName());
logger.info(api);
executionApi.setApiDesc(api.getString("apiName"));
if (api.containsKey("requestParamter")) {
executionApi.setRequestParamterTemplate(api.getString("requestParamter"));
}
dataDao = (DataDao) SpringContextUtil.getBean("dataDao");
ApiInfo apiInfo = dataDao.getApiInfo(executionApi.getApiModule(), executionApi.getApiName());
if (null == apiInfo) {
logger.warn("接口信息不存在!");
return;
}
executionApi.setRequestParamterTemplate(apiInfo.getRequestParameter());
executionApi.setApiDesc(apiInfo.getApi_name());
/**
* 默认执行线上环境
......@@ -170,7 +169,7 @@ public class BaseTest extends AbstractTestNGSpringContextTests {
}
// 组装URL
url = host + api.getString("apiPath") + loginInfo.getCommonParam(executionApi.getTerminal());
url = host +apiInfo.getApi_path() + loginInfo.getCommonParam(executionApi.getTerminal());
executionApi.setUrl(url);
// 业务url
logger.info("业务请求URL==>>"+url);
......@@ -198,10 +197,10 @@ public class BaseTest extends AbstractTestNGSpringContextTests {
executionHeaders.put("Accept", "*/*");
executionHeaders.put("Connection", "Keep-Alive");
// 接口未设置请求头,则默认为 application/json
if (api.containsKey("apiContentType") || StringUtils.isEmpty(api.getString("apiContentType"))) {
if (org.apache.commons.lang3.StringUtils.isEmpty(apiInfo.getApi_contentType())) {
executionHeaders.put("Content-type", "application/json;charset=utf-8");
} else {
executionHeaders.put("Content-type", api.getString("apiContentType"));
executionHeaders.put("Content-type", apiInfo.getApi_contentType());
}
}
......
package com.live.jdbc.dao;
import com.live.jdbc.entity.ApiInfo;
import com.live.jdbc.entity.UserInfo;
import java.util.List;
/**
* @Auther: pdd
* @Date: 2021/04/27/16:12
* @Description: 获取数据信息
*/
public interface DataDao {
/**
* @param userName
* @Description: 获取登录账号信息
* @Author: pdd
* @Date: 2021/4/27/16:09
*/
UserInfo getUserInfo(String userName, String env);
List<UserInfo> getUserInfos(String userType, String env);
/**
* @param apiModule
* @param apiName
* @Description: 获取测试接口信息
* @Author: pdd
* @Date: 2021/4/27/16:34
*/
ApiInfo getApiInfo(String apiModule, String apiName);
}
package com.live.jdbc.dao.impl;
import com.live.jdbc.dao.DataDao;
import com.live.jdbc.entity.ApiInfo;
import com.live.jdbc.entity.UserInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
/**
* @Auther: pdd
* @Date: 2021/04/27/16:29
* @Description: 获取数据信息
*/
public class DataDaoImpl implements DataDao {
// 声明一个JdbcTmplate属性及其Setter方法
private static JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public UserInfo getUserInfo(String userName, String env) {
// 定义SQL
String sql = "SELECT * FROM user_info WHERE user_name = ? and env = ? limit 1";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[]{userName, env};
RowMapper<UserInfo> rowMapper = new BeanPropertyRowMapper<>(UserInfo.class);
List<UserInfo> userInfos = this.jdbcTemplate.query(sql, obj, rowMapper);
return userInfos.size() > 0 ? userInfos.get(0) : null;
}
@Override
public List<UserInfo> getUserInfos(String userType, String env) {
// 定义SQL
String sql = "SELECT * FROM user_info WHERE user_type = ? and env = ? limit 1";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[]{userType, env};
RowMapper<UserInfo> rowMapper = new BeanPropertyRowMapper<>(UserInfo.class);
List<UserInfo> userInfos = this.jdbcTemplate.query(sql, obj, rowMapper);
return userInfos;
}
/**
* @Description: 获取测试接口信息
* @Author: pdd
* @Date: 2021/4/27/16:09
*/
@Override
public ApiInfo getApiInfo(String apiModule, String apiName) {
// 定义SQL
String sql = "SELECT * FROM api_info WHERE api_module = ? and api_name = ? limit 1";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[]{apiModule, apiName};
RowMapper<ApiInfo> rowMapper = new BeanPropertyRowMapper<>(ApiInfo.class);
List<ApiInfo> apiInfos = this.jdbcTemplate.query(sql, obj, rowMapper);
return apiInfos.size() > 0 ? apiInfos.get(0) : null;
}
}
package com.live.jdbc.entity;
/**
* @Auther: pdd
* @Date: 2021/04/27/16:00
* @Description: 测试接口信息
*/
public class ApiInfo {
String api_name;
String business_module;
String api_module;
String api_path;
String api_contentType;
String api_describe;
String requestParameter;
public String getApi_name() {
return api_name;
}
public void setApi_name(String api_name) {
this.api_name = api_name;
}
public String getBusiness_module() {
return business_module;
}
public void setBusiness_module(String business_module) {
this.business_module = business_module;
}
public String getApi_module() {
return api_module;
}
public void setApi_module(String api_module) {
this.api_module = api_module;
}
public String getApi_path() {
return api_path;
}
public void setApi_path(String api_path) {
this.api_path = api_path;
}
public String getApi_contentType() {
return api_contentType;
}
public void setApi_contentType(String api_contentType) {
this.api_contentType = api_contentType;
}
public String getApi_describe() {
return api_describe;
}
public void setApi_describe(String api_describe) {
this.api_describe = api_describe;
}
public String getRequestParameter() {
return requestParameter;
}
public void setRequestParameter(String requestParameter) {
this.requestParameter = requestParameter;
}
@Override
public String toString() {
return "ApiInfo{" +
"api_name='" + api_name + '\'' +
", business_module='" + business_module + '\'' +
", api_module='" + api_module + '\'' +
", api_path='" + api_path + '\'' +
", api_contentType='" + api_contentType + '\'' +
", api_describe='" + api_describe + '\'' +
", requestParameter='" + requestParameter + '\'' +
'}';
}
}
package com.live.jdbc.entity;
/**
* @Auther: pdd
* @Date: 2021/04/27/15:40
* @Description: 登录账号信息
*/
public class UserInfo {
String user_name;
String inst_id;
String account_no;
String passwd;
String env;
String uid;
String aid;
String user_type;
String open_id;
String app_id;
String xcx_open_id;
String xm_token;
String p;
String deptpath;
String tid;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getInst_id() {
return inst_id;
}
public void setInst_id(String inst_id) {
this.inst_id = inst_id;
}
public String getAccount_no() {
return account_no;
}
public void setAccount_no(String account_no) {
this.account_no = account_no;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUser_type() {
return user_type;
}
public void setUser_type(String user_type) {
this.user_type = user_type;
}
public String getOpen_id() {
return open_id;
}
public void setOpen_id(String open_id) {
this.open_id = open_id;
}
public String getApp_id() {
return app_id;
}
public void setApp_id(String app_id) {
this.app_id = app_id;
}
public String getXcx_open_id() {
return xcx_open_id;
}
public void setXcx_open_id(String xcx_open_id) {
this.xcx_open_id = xcx_open_id;
}
public String getXm_token() {
return xm_token;
}
public void setXm_token(String xm_token) {
this.xm_token = xm_token;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public String getDeptpath() {
return deptpath;
}
public void setDeptpath(String deptpath) {
this.deptpath = deptpath;
}
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
@Override
public String toString() {
return "UserInfo{" +
"user_name='" + user_name + '\'' +
", inst_id='" + inst_id + '\'' +
", account_no='" + account_no + '\'' +
", passwd='" + passwd + '\'' +
", env='" + env + '\'' +
", uid='" + uid + '\'' +
", aid='" + aid + '\'' +
", user_type='" + user_type + '\'' +
", open_id='" + open_id + '\'' +
", app_id='" + app_id + '\'' +
", xcx_open_id='" + xcx_open_id + '\'' +
", xm_token='" + xm_token + '\'' +
", p='" + p + '\'' +
", deptpath='" + deptpath + '\'' +
'}';
}
}
......@@ -8,7 +8,7 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
@ContextConfiguration(locations = {"classpath:/spring-core.xml"})
@ContextConfiguration(locations = {"classpath:/spring-core.xml", "classpath:/applicationContext.xml"})
public class XMBaseTest extends BaseTest {
@BeforeClass
public static void beforeClass() {
......
package com.live.utils.pddtest;
import com.alibaba.fastjson.JSONObject;
import com.live.utils.JsonAndFile;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* @Auther: pdd
* @Date: 2021/04/27/18:10
* @Description:
*/
public class AddUserInfo {
static String caseURI = "cloudclass_video_b = /src/main/resources/apicase/api-video-b.json;" +
"cloudclass_video_c = /src/main/resources/apicase/api-video-c.json;" +
"cloudclass_file_b = /src/main/resources/apicase/api-file-b.json;" +
"cloudclass_file_c = /src/main/resources/apicase/api-file-c.json;" +
"cloudclass_customerLive_api = /src/main/resources/apicase/customerLive.json;" +
"cloudclass_apollo_api = /src/main/resources/apicase/apollo.json;" +
"cloudclass_series_b = /src/main/resources/apicase/series-course-b.json;";
public static void main(String[] args) {
insetApiInfo();
//insetApiUserInfo();
}
public static void insetApiInfo() {
caseURI = caseURI.replaceAll("\\s*", ""); //可以替换大部分空白字符, 不限于空格 ;
String[] strs = caseURI.split(";");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
List<Object[]> batchArgs = new ArrayList<>();
String sql_1 = "SELECT count(1) from api_info WHERE api_name = ? and business_module =? and api_module = ? ";
String sql_2 = "INSERT INTO api_info (api_name,business_module,api_module,api_path,api_contentType,api_describe,api_alias,request_parameter) VALUES(?,?,?,?,?,?,?,?)";
Arrays.stream(strs).forEach(str -> {
String api_module = str.substring(0, str.indexOf("="));
String path = str.substring(str.indexOf("=") + 1);
String fileParam = new JsonAndFile().readTxtFile(System.getProperty("user.dir") + path);
String business_module = "live";
JSONObject apis = JSONObject.parseObject(fileParam);
if(null==apis){
return;
}
Set<String> apiNames = apis.keySet();
apiNames.stream().forEach(apiName -> {
int i = jdbcTemplate.queryForObject(sql_1, new Object[]{apiName, business_module, api_module}, Integer.class);
if (i < 1) {
JSONObject api = apis.getJSONObject(apiName);
String api_path = api.getString("apiPath");
String api_contentType = api.getString("apiContentType");
String api_alias = api.getString("apiName");
String api_describe = api.getString("desc");
String request_parameter = api.containsKey("request_parameter") ? api.getString("request_parameter") : "";
batchArgs.add(new Object[]{apiName, business_module, api_module, api_path, api_contentType, api_describe, api_alias, request_parameter});
}
});
});
System.out.println("size========" + batchArgs.size());
if (batchArgs.size() > 0) {
jdbcTemplate.batchUpdate(sql_2, batchArgs);
}
}
// 补充新增账号
public static void insetApiUserInfo() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
String sql = "INSERT INTO user_info (user_name,inst_id,inst_name,account_no,passwd,env,uid,aid, user_type,open_id,app_id,xcx_open_id,p,xm_token,deptpath,tid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
List<Object[]> batchArgs = new ArrayList<>();
String parameters = JsonAndFile.readTxtFile(System.getProperty("user.dir") + "/src/main/resources/parameter.json");
JSONObject object = JSONObject.parseObject(parameters);
Set<String> keySet_userName = object.keySet();
for (String key_userName : keySet_userName) {
String userName = key_userName;
JSONObject json_userName = object.getJSONObject(key_userName);
Set<String> keySet_env = json_userName.keySet();
for (String key_env : keySet_env) {
String env = key_env;
JSONObject json_env = json_userName.getJSONObject(key_env);
String sql_2 = "SELECT count(1) FROM user_info WHERE user_name = ? and env = ? ";
int i = jdbcTemplate.queryForObject(sql_2, new Object[]{userName, env}, Integer.class);
if (i > 0) {
continue;
}
String instId = json_env.containsKey("inst_Id") ? json_env.getString("instId") : "";
String instName = json_env.containsKey("instName") ? json_env.getString("instName") : "";
String account_no = json_env.containsKey("accountNo") ? json_env.getString("accountNo") : "";
String passwd = json_env.containsKey("certificate") ? json_env.getString("certificate") : "";
String uid = json_env.containsKey("uid") ? json_env.getString("uid") : "";
String user_type = json_env.containsKey("userType") ? json_env.getString("userType") : "";
String open_id = json_env.containsKey("openId") ? json_env.getString("openId") : "";
String app_id = json_env.containsKey("appId") ? json_env.getString("appId") : "";
String xcx_open_id = json_env.containsKey("xcxOpenId") ? json_env.getString("xcxOpenId") : "";
String p = json_env.containsKey("p") ? json_env.getString("p") : "";
String xm_token = json_env.containsKey("xmtoken") ? json_env.getString("xmtoken") : "";
String deptpath = json_env.containsKey("deptpath") ? json_env.getString("deptpath") : "";
String aid = json_env.containsKey("aid") ? json_env.getString("aid") : "";
String tid = json_env.containsKey("tid") ? json_env.getString("tid") : "";
batchArgs.add(new Object[]{userName, instId, instName, account_no, passwd, env, uid, aid, user_type, open_id, app_id, xcx_open_id, p, xm_token, deptpath,tid});
}
}
batchArgs.size();
jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println("size========" + batchArgs.size());
}
}
package com.live.utils.pddtest;
import com.live.jdbc.dao.DataDao;
import com.live.jdbc.entity.ApiInfo;
import com.live.jdbc.entity.UserInfo;
import com.live.utils.SpringContextUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Auther: pdd
* @Date: 2021/04/27/14:14
* @Description:
*/
public class JDBCTest {
/* @BeforeMethod
public void beforeTest() {
xmAppApi.setApiModule(ApiModule.God_B) // API 所属模块
.setApiName("API_saveStudent") // API 名称
//.setLoginUser("pdd-b") // http 接口,测试账号
//.setTerminal(Terminal.B); // 所属端位(B端,C端,M端等, 必传)
.setLoginUser("祁海洋_dev")
.setTerminal(Terminal.APP_C)
.setEnv("dev");
dal.setCase_owner("pengdan")
.setCase_name(Thread.currentThread().getStackTrace()[1].getFileName().split("\\.")[0]);
super.beforeTest();
}*/
/* @Test*/
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataDao dataDao = (DataDao) SpringContextUtil.getBean("dataDao");
UserInfo info = dataDao.getUserInfo("祁海洋_dev", "dev");
System.out.println(info.toString());
ApiInfo info2 = dataDao.getApiInfo("god_order_api", "API_order_payVoucher");
System.out.println(info2.toString());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 1.配置数据源 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<!-- 1.1.数据库驱动 -->
<property name="driverClassName"
value="${jdbc.driverClassName}"></property>
<!-- 1.2.连接数据库的url -->
<property name="url"
value="${jdbc.url}"></property>
<!-- 1.3.连接数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 1.4.连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"></property>
<property name="filters" value="${jdbc.filters}"></property>
</bean>
<!-- 2配置JDBC模板 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义id为dataDao的Bean -->
<bean id="dataDao" class="com.live.jdbc.dao.impl.DataDaoImpl">
<!-- 将jdbcTemplate注入到dataDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
\ No newline at end of file
# 数据库配置
jdbc.name=test
jdbc.url=jdbc:mysql://10.158.0.123:3306/xm_data?characterEncoding=UTF-8
jdbc.username=qaman
jdbc.password=qaman
#jdbc.url=jdbc:mysql://120.27.248.253:3306/xm_data?characterEncoding=UTF-8
#jdbc.username=root
#jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver
######################### 连接池的配置信息 #################
#初始化连接大小
jdbc.initialSize=5
#最小连接池数量
jdbc.minIdle=5
#最大连接池数量
jdbc.maxActive=20
#获取连接时最大等待时间,单位毫秒
jdbc.maxWait=30000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
jdbc.timeBetweenEvictionRunsMillis=30000
#配置一个连接在池中最小生存的时间,单位是毫秒
jdbc.minEvictableIdleTimeMillis=300000
#测试连接
jdbc.validationQuery=SELECT 1 FROM DUAL
#申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性
jdbc.testWhileIdle=true
#获取连接时执行检测,建议关闭,影响性能
jdbc.testOnBorrow=false
#归还连接时执行检测,建议关闭,影响性能
jdbc.testOnReturn=false
#是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭
jdbc.poolPreparedStatements=false
#开启poolPreparedStatements后生效
jdbc.maxPoolPreparedStatementPerConnectionSize=20
#配置扩展插件,常用的插件有=>stat:监控统计 log4j:日志 wall:防御sql注入
jdbc.filters=stat,wall,log4j
......@@ -293,19 +293,19 @@
"uid": "1291264680439021569",
"userType": "B",
"tid": "1291264680405467138"
},
"zhangyumeng_sowplan_b": {
"prod": {
"storeId":"1211601438838495868",
"storeUserId":"1338693764895141890",
"userId":"1338693764747345922",
"accountNo": "15656960812",
"certificate": "290112bfbde696fcb91284bba1e2655b",
"aid": "1291264680405467138",
"uid": "1291264680439021569",
"userType": "B",
"tid": "1291264680405467138"
}
}
},
"zhangyumeng_sowplan_b": {
"prod": {
"storeId":"1211601438838495868",
"storeUserId":"1338693764895141890",
"userId":"1338693764747345922",
"accountNo": "15656960812",
"certificate": "290112bfbde696fcb91284bba1e2655b",
"aid": "1291264680405467138",
"uid": "1291264680439021569",
"userType": "B",
"tid": "1291264680405467138"
}
},
"fym-b": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment