Commit 648bad52 authored by Su-Yi's avatar Su-Yi

更新上传素材case、优化网络模块

parent b54bed8c
......@@ -38,7 +38,18 @@
<scope>test</scope>
</dependency>
<!-- 导入commons-lang的jar包 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
......
package com.kjj.qa.cases;
import com.kjj.qa.services.NetworkOperation;
import com.kjj.qa.utils.NetworkUtil;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.*;
......@@ -12,16 +12,12 @@ import java.util.HashMap;
import java.util.Map;
public class Home {
private NetworkOperation network = new NetworkOperation();
NetworkUtil network = NetworkUtil.getInstance();
// http://kjj.m.duibatest.com.cn/kjy/mp/developer/changeUser?uid=1515
@BeforeClass
public void setUp() {
// 代理人 cookies
network.agentCookies.put("tku","3wSM7Cd4wsesMcuzVhBQVGVupkmB3tT1JHyRbv9sK6ts7f6P67rmuygUqGfPc2Xd9YduvdByMocmpuzNMzKKWd48");
network.agentHeaders.put("Content-Type","application/json");
}
......
package com.kjj.qa.cases;
import com.alibaba.fastjson.JSONObject;
import com.kjj.qa.services.NetworkOperation;
import com.kjj.qa.utils.NetworkUtil;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.*;
......@@ -15,14 +14,11 @@ import java.util.Map;
public class Pay {
private NetworkOperation network = new NetworkOperation();
// http://kjj.m.duibatest.com.cn/kjy/mp/developer/changeUser?uid=1515
NetworkUtil network = NetworkUtil.getInstance();
@BeforeClass
public void setUp() {
// 代理人 cookies
network.agentCookies.put("tku","T3gM31f18j8w3o5y1iEkAGkGxfNgri1yZC1Qix9Y8JawhrzhpZMDhgTzDvhzXSbagbXZxA8AYCbxLPG4SG");
network.agentHeaders.put("Content-Type","application/json");
}
@Test (description = "默认付费_付费页",priority = 1) //priorty 方法执行优先级
......
package com.kjj.qa.cases;
import com.alibaba.fastjson.JSONObject;
import com.kjj.qa.services.NetworkOperation;
import com.kjj.qa.utils.NetworkUtil;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.*;
......@@ -14,15 +13,14 @@ import java.util.HashMap;
import java.util.Map;
public class SellerCard {
private NetworkOperation network = new NetworkOperation();
NetworkUtil network = NetworkUtil.getInstance();
private String scid = "";
private String sellerId = "";
@BeforeClass
public void setUp() {
// 代理人 cookies
network.agentCookies.put("tku","T3gM31f18j8w3o5y1iEkAGkGxfNgri1yZC1Qix9Y8JawhrzhpZMDhgTzDvhzXSbagbXZxA8AYCbxLPG4SG");
network.agentHeaders.put("Content-Type","application/json");
}
@Test (description = "获取用户版本",priority = 1) //priorty 方法执行优先级
......@@ -35,11 +33,11 @@ public class SellerCard {
Assert.assertEquals(userVersion,1,"会员类型版本判断:判断错误,此版本为保险版");
}
@Test (description = "是否关注红号-弹框",priority = 2) //priorty 方法执行优先级
@Test (description = "是否关注红号",priority = 2) //priorty 方法执行优先级
public void 是否关注红号() throws IOException {
Response response = network.getResponse("/kjy/mp/seller/isVipSubscribe");
boolean isVip = response.jsonPath().getBoolean("data.vip");
Assert.assertEquals(isVip,false,"vip弹框判断错误");
boolean vipSubscribe = response.jsonPath().getBoolean("data.vipSubscribe");
Assert.assertFalse(false,"vip号是否关注判断错误");
}
......@@ -171,11 +169,19 @@ public class SellerCard {
params.put("pageSize",30);
Response response = network.getResponse(params,"/kjy/mp/chat/sellerContentHistory");
// int totalCount = response.jsonPath().getInt("data.totalCount");
// Assert.assertEquals(totalCount,0,"获取分享失败");
String contentId = response.jsonPath().getString("data.list[0].contentId");
Assert.assertEquals(contentId,"Kj21NTgxMjc","获取分享失败");
int totalCount = response.jsonPath().getInt("data.totalCount");
Assert.assertEquals(totalCount,5,"获取分享失败");
// 转发一篇素材
Map<String, Object> forwardParams = new HashMap<String, Object>();
forwardParams.put("scId","Kj20MTI2MjIz");//文章ID
forwardParams.put("forwardType",1);
Response forwardResponse = network.getResponse(params,"/kjy/mp/forward");
// 查询最新的分享内容
response = network.getResponse(params,"/kjy/mp/chat/sellerContentHistory");
String scId = response.jsonPath().getString("data.list[0].scId");
Assert.assertEquals(scId,"Kj20MTI2MjIz","获取分享失败");
}
}
This diff is collapsed.
package com.kjj.qa.utils;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
/**
* Created with IntelliJ IDEA.
* User: noah
* Date: 8/2/13
* Time: 10:36 AM
* To change this template use File | Settings | File Templates.
*/
public class Base58 {
private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
private static final int[] INDEXES = new int[128];
static {
for (int i = 0; i < INDEXES.length; i++) {
INDEXES[i] = -1;
}
for (int i = 0; i < ALPHABET.length; i++) {
INDEXES[ALPHABET[i]] = i;
}
}
/**
* Encodes the given bytes in base58. No checksum is appended.
*/
public static String encode(byte[] input) {
if (input.length == 0) {
return "";
}
input = copyOfRange(input, 0, input.length);
// Count leading zeroes.
int zeroCount = 0;
while (zeroCount < input.length && input[zeroCount] == 0) {
++zeroCount;
}
// The actual encoding.
byte[] temp = new byte[input.length * 2];
int j = temp.length;
int startAt = zeroCount;
while (startAt < input.length) {
byte mod = divmod58(input, startAt);
if (input[startAt] == 0) {
++startAt;
}
temp[--j] = (byte) ALPHABET[mod];
}
// Strip extra '1' if there are some after decoding.
while (j < temp.length && temp[j] == ALPHABET[0]) {
++j;
}
// Add as many leading '1' as there were leading zeros.
while (--zeroCount >= 0) {
temp[--j] = (byte) ALPHABET[0];
}
byte[] output = copyOfRange(temp, j, temp.length);
try {
return new String(output, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
public static byte[] decode(String input) throws IllegalArgumentException {
if (input.length() == 0) {
return new byte[0];
}
byte[] input58 = new byte[input.length()];
// Transform the String to a base58 byte sequence
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
int digit58 = -1;
if (c >= 0 && c < 128) {
digit58 = INDEXES[c];
}
if (digit58 < 0) {
throw new IllegalArgumentException("Illegal character " + c + " at " + i);
}
input58[i] = (byte) digit58;
}
// Count leading zeroes
int zeroCount = 0;
while (zeroCount < input58.length && input58[zeroCount] == 0) {
++zeroCount;
}
// The encoding
byte[] temp = new byte[input.length()];
int j = temp.length;
int startAt = zeroCount;
while (startAt < input58.length) {
byte mod = divmod256(input58, startAt);
if (input58[startAt] == 0) {
++startAt;
}
temp[--j] = mod;
}
// Do no add extra leading zeroes, move j to first non null byte.
while (j < temp.length && temp[j] == 0) {
++j;
}
return copyOfRange(temp, j - zeroCount, temp.length);
}
public static BigInteger decodeToBigInteger(String input) throws IllegalArgumentException {
return new BigInteger(1, decode(input));
}
//
// number -> number / 58, returns number % 58
//
private static byte divmod58(byte[] number, int startAt) {
int remainder = 0;
for (int i = startAt; i < number.length; i++) {
int digit256 = (int) number[i] & 0xFF;
int temp = remainder * 256 + digit256;
number[i] = (byte) (temp / 58);
remainder = temp % 58;
}
return (byte) remainder;
}
//
// number -> number / 256, returns number % 256
//
private static byte divmod256(byte[] number58, int startAt) {
int remainder = 0;
for (int i = startAt; i < number58.length; i++) {
int digit58 = (int) number58[i] & 0xFF;
int temp = remainder * 58 + digit58;
number58[i] = (byte) (temp / 256);
remainder = temp % 256;
}
return (byte) remainder;
}
private static byte[] copyOfRange(byte[] source, int from, int to) {
byte[] range = new byte[to - from];
System.arraycopy(source, from, range, 0, range.length);
return range;
}
}
package com.kjj.qa.utils;
import java.util.HashMap;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
/**
* Blowfish加密解密的方
*
* @link http://www.schneier.com/blowfish.html
*/
public class BlowfishUtils {
private static final String CIPHER_NAME="Blowfish/CFB8/NoPadding";
private static final String KEY_SPEC_NAME="Blowfish";
private static final ThreadLocal<HashMap<String, BlowfishUtils>> pool=new ThreadLocal<HashMap<String,BlowfishUtils>>();
private Cipher enCipher;
private Cipher deCipher;
private String key;
private BlowfishUtils(String key){
try {
this.key=key;
String iv= StringUtils.substring(DigestUtils.md5Hex(key), 0,8);
SecretKeySpec secretKeySpec=new SecretKeySpec(key.getBytes(), KEY_SPEC_NAME);
IvParameterSpec ivParameterSpec=new IvParameterSpec(iv.getBytes());
enCipher=Cipher.getInstance(CIPHER_NAME);
deCipher=Cipher.getInstance(CIPHER_NAME);
enCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,ivParameterSpec);
deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec,ivParameterSpec);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encryptBlowfish(String s,String key){
return getInstance(key).encrypt(s);
}
public static String decryptBlowfish(String s,String key){
return getInstance(key).decrypt(s);
}
public static String encryptBlowfish(String s){
return encryptBlowfish(s, "abc");
}
public static String decryptBlowfish(String s){
return decryptBlowfish(s, "abc");
}
private static BlowfishUtils getInstance(String key){
HashMap<String, BlowfishUtils> keyMap=pool.get();
if(keyMap==null || keyMap.isEmpty()){
keyMap=new HashMap<String, BlowfishUtils>();
pool.set(keyMap);
}
BlowfishUtils instance=keyMap.get(key);
if(instance==null || !StringUtils.equals(instance.key, key)){
instance=new BlowfishUtils(key);
keyMap.put(key, instance);
}
return instance;
}
/**
* 加密
* @param s
* @return
*/
private String encrypt(String s){
String result=null;
if(StringUtils.isNotBlank(s)){
try {
byte[] encrypted=enCipher.doFinal(s.getBytes());
result=new String(Base58.encode(encrypted));
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 解密
* @param s
* @return
*/
private String decrypt(String s){
String result=null;
if(StringUtils.isNotBlank(s)){
try {
byte[] decrypted=Base58.decode(s);
result=new String(deCipher.doFinal(decrypted));
} catch (Exception e) {
resetInstance();
e.printStackTrace();
}
}
return result;
}
private void resetInstance(){
pool.set(null);
}
}
package com.kjj.qa.utils;
import com.kjj.qa.services.NetworkOperation;
import java.util.HashMap;
import java.util.Map;
public class DingTalkUtil {
//通过钉钉机器人发送消息到钉钉群中
public static void sendVal(String context)throws Exception{
//测试结果
context = "【测试结果】\n"
+ context
+ "报告地址: http://10.254.187.25/view/kjj_test/job/kejiji_interface/ws/test-output/report.html";
+ "报告地址: http://10.254.187.25/view/kjj_test/job/kejiji_interface/ws/test-output/report.html\n"
+ "@张震 @别湘灵 ";//@张艳玲 @张慧锋
Map<String, Object> contentParams = new HashMap<String, Object>();
contentParams.put("content",context);
......@@ -20,11 +21,9 @@ public class DingTalkUtil {
params.put("msgtype","text");
params.put("text",contentParams);
// System.out.println(params);
NetworkOperation network = new NetworkOperation();
network.dingTalkRobot(params); //发起dingding机器人通知
//发起dingding机器人通知
// NetworkUtil network = NetworkUtil.getInstance();
// network.dingTalkRobot(params);
}
......@@ -34,7 +33,6 @@ public class DingTalkUtil {
// long currentTime = System.currentTimeMillis();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
// Date date = new Date(currentTime);
// System.out.println(formatter.format(date));
// Reporter.log(formatter.format(date) + splitTimeAndMsg + msg, true);
......
package com.kjj.qa.utils;
import com.kjj.qa.services.NetworkOperation;
public final class KjyUtil {
}
package com.kjj.qa.utils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MysqlUtil {
private static final String PREFIX = "dbseccode";
private static final String secret = "CNxgrtFG2nYQUfu";
private static Map<String,MysqlUtil> pool=new HashMap<>();
//数据库用户名
private String USERNAME;// = "root";
//数据库密码
private String PASSWORD; //= "yanzi";
//驱动信息
private String DRIVER="com.mysql.cj.jdbc.Driver";
//数据库地址
private String URL;// = "jdbc:mysql://localhost:3306/mydb";
private Connection connection;
private PreparedStatement pstmt;
private ResultSet resultSet;
private MysqlUtil(String username,String password,String url){
USERNAME = username;
PASSWORD = decode(password);
URL = url;
MysqlUtil mu = pool.get(url);
if(mu!=null){
System.out.println("--------exists");
connection = mu.connection;
}else{
connection = getConnection();
pool.put(url,this);
}
// if(mu!=null) {
// try {
// mu.connection.close();
// } catch (Exception e) {
// System.out.println("mysql error:" + e.getMessage());
// }
// }
//// }else {
// connection = getConnection();
// pool.put(url,this);
}
/**
*
* @param username 用户名
* @param password 密码
* @param ip 数据库ip
* @param port 端口号
* @param databaseName 数据库名
*/
public MysqlUtil(String username,String password,String ip,String port,String databaseName){
this(username,password,"jdbc:mysql://"+ip+":"+port+"/"+databaseName);
}
/**
* 阿里云数据库连接
* @param databaseName 数据库名字
*/
public MysqlUtil(String databaseName){
this("tuia_test","dbseccode2CJMouQ2xyuEXnhe4vBzGtrDpaDbuK8Lt","47.111.157.152","3306",databaseName);
}
public static MysqlUtil mysqlDuiba(String databaseName){
return new MysqlUtil("tuia_test","dbseccode2CJMouQ2xyuEXnhe4vGtrDpavyDbuK8Lt","47.111.157.152","3306",databaseName);
}
/**
* 获得数据库的连接
* @return
*/
public Connection getConnection(){
try {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// "jdbc:mysql://"+ip+":"+port+"/"+databaseName);
String url = URL+"?autoReconnect=true&testOnBorrow=true&testWhileIdle=true";
connection = DriverManager.getConnection(url,USERNAME,PASSWORD);
System.out.println("数据库连接成功!");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 增加、删除、改
* @param sql sql语句
* @param params 参数,替换 ?
* @return true or false
* @throws SQLException
*/
public boolean update(String sql, Object ...params)throws SQLException{
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
}
boolean flag = false;
int result = -1;
pstmt = connection.prepareStatement(sql);
int index = 1;
if(params != null && !(params.length==0)){
for(int i=0; i<params.length; i++){
pstmt.setObject(index++, params[i]);
}
}
System.out.println("SQL语句"+pstmt.toString());
result = pstmt.executeUpdate();
flag = result > 0 ? true : false;
return flag;
}
/**
* 查询结果只有一行数据
* @param sql sql语句
* @param params 可变参数,替换 ?
* @return map
* @throws SQLException
*/
public Map<String, Object> findSimpleResult(String sql, Object ...params) throws SQLException{
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
}
Map<String, Object> map = new HashMap<String, Object>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if(params != null && !(params.length==0)){
for(int i=0; i<params.length; i++){
pstmt.setObject(index++, params[i]);
}
}
System.out.println("sql:"+pstmt.toString());
resultSet = pstmt.executeQuery();//返回查询结果
ResultSetMetaData metaData = resultSet.getMetaData();
int col_len = metaData.getColumnCount();
while(resultSet.next()){
for(int i=0; i<col_len; i++ ){
String cols_name = metaData.getColumnName(i+1);
Object cols_value = resultSet.getObject(cols_name);
if(cols_value == null){
cols_value = "";
}
map.put(cols_name, cols_value);
}
}
return map;
}
/**
* 返回多条数据
* @param sql sql语句
* @param params 可变参数,替换 ?
* @return list
* @throws SQLException
*/
public List<Map<String, Object>> findModeResult(String sql, Object ...params) throws SQLException{
if(!connection.isValid(3)){
System.out.println("mysql connect is not vaild,reconnect.");
connection.close();
connection = getConnection();
pool.put(URL,this);
}
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if(params != null && !(params.length==0)){
for(int i=0; i<params.length; i++){
pstmt.setObject(index++, params[i]);
}
}
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols_len = metaData.getColumnCount();
while(resultSet.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i=0; i<cols_len; i++){
String cols_name = metaData.getColumnName(i+1);
Object cols_value = resultSet.getObject(cols_name);
if(cols_value == null){
cols_value = "";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
return list;
}
/**
* 释放数据库连接
*/
public void releaseConn(){
if(resultSet != null){
try{
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
// JdbcUtils jdbc = new JdbcUtils("root", "root", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/learning");
//
// Map<String, Object> map = jdbc.findSimpleResult("select * from t_user where USER_ID='1'", null);
MysqlUtil jdbcUtils = new MysqlUtil("tuia_adver");
jdbcUtils.getConnection();
Map<String, Object> map = jdbcUtils.findSimpleResult("select * from account where company_name=?","对吧科技有限公司");
jdbcUtils.releaseConn();
}
private static String decode(String value) {
if (value.startsWith(PREFIX)) {
value = value.substring(PREFIX.length());
return BlowfishUtils.decryptBlowfish(value, secret);
}
return value;
}
}
package com.kjj.qa.services;
package com.kjj.qa.utils;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
......@@ -15,7 +15,7 @@ import java.util.HashMap;
import java.util.Map;
public class NetworkOperation {
public class NetworkUtil {
// 代理人的 cookies和headers
public Map<String, Object> agentCookies = new HashMap<String, Object>();
......@@ -36,8 +36,34 @@ public class NetworkOperation {
private static String host = "https://kjj.m.duibatest.com.cn"; //测试环境
//钉钉机器人域名
// private static String dingtalkPath = "https://oapi.dingtalk.com/robot/send?access_token=e4b7d997fb9f90b17672dcdb9c9922d52c59f741b82081a368f4d3c305c1afcd";
private static String dingtalkPath = "https://oapi.dingtalk.com/robot/send?access_token=e4b7d997fb9f90b17672dcdb9c9922d52c59f741b82081a368f4d3c305c1afcd";
private volatile static NetworkUtil network;
// 构造函数 初始化tku、content-type
private NetworkUtil (){
agentCookies.put("tku","T3gM31f18j8w3o5y1iEkAGkGxfNgri1yZC1Qix9Y8Jawib11KkHjRWnHg9xsEMve3FVAFkVLyKLdSzqNQY");
agentHeaders.put("Content-Type","application/json");
}
//单例模式
//通过设置同步代码块,使用DCL双检查锁机制
//使用双检查锁机制成功的解决了单例模式的懒汉实现的线程不安全问题和效率问题
//DCL 也是大多数多线程结合单例模式使用的解决方案
public static NetworkUtil getInstance() {
if (network == null) {
synchronized (NetworkUtil.class) {
if (network == null) {
network = new NetworkUtil();
}
}
}
return network;
}
// get 不带参请求
public Response getResponse(String path) {
return given()
......@@ -86,7 +112,7 @@ public class NetworkOperation {
}
public static void main(String[] args) {
// NetworkOperation network = new NetworkOperation();
// NetworkUtil network = new NetworkUtil();
//
// Map<String,Object> json=new HashMap();
// Map<String,Object> text=new HashMap();
......
This diff is collapsed.
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