package com.alisoft.aep.sip.core;
import static org.junit.Assert.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.alisoft.sip.sdk.isv.SignatureUtil;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.UploadFileSpec;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
public class TestSIPUploadAPI {
public static String src_url="http://10.0.68.183:3000/images";
public static String file144kb="C:\\Sample 144KB.JPG";
public static String file200kb="C:\\Sample 200KB.JPG";
public static String file255kb="C:\\Sample 255KB.JPG";
public static String receive_path="C:\\Received.bmp";
private String api_server = "http://10.2.226.19:8180/sip/rest";
public static char[] hexChar = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'};
//hashType = "SHA1","SHA-256","SHA-384","SHA-512";
public static String getHash(String fileName, String hashType) throws
Exception {
InputStream fis;
fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
}
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString();
}
private String prepareUrl(String sip_appKey, String sip_apiname,
String sip_appsecret, String api_server, String sessionid) {
String timestamp = getTimestamp();
StringBuffer urlStr = new StringBuffer(api_server);
urlStr.append(urlStr.indexOf("?") != -1 ? "&" : "?");
urlStr.append("sip_timestamp=").append(timestamp).append("&");
urlStr.append("sip_appkey=").append(sip_appKey).append("&");
urlStr.append("sip_apiname=").append(sip_apiname).append("&");
urlStr.append("sip_sessionid=").append(sessionid).append("&");
Map<String, String> map = new HashMap<String, String>();
map.put("sip_timestamp", timestamp);
map.put("sip_appkey", sip_appKey);
map.put("sip_apiname", sip_apiname);
map.put("sip_sessionid", sessionid);
String sign = SignatureUtil.Signature(map, sip_appsecret);
urlStr.append("sip_sign=").append(sign).append("&");
return urlStr.toString().replace(" ", "%20");
}
private String getTimestamp() {
java.text.SimpleDateFormat formater = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return formater.format(new Date());
}
public void caseMaker(String filein_path,String fileout_path,int assertequal) throws Exception
{
System.out.println("使用Post方式向服务器发送数据,然后获取网页内容:");
String sip_appkey = "test_app003";
String sip_apiname = "alitest.ali-001-upload";
String sip_appsecret = "secret_app003";
String sip_sessionid = "sddddeeefg" + getTimestamp();
String url = prepareUrl(sip_appkey, sip_apiname, sip_appsecret,
api_server, sip_sessionid);
//建立一个WebConversation实例
WebConversation wc = new WebConversation();
//向指定的URL发出请求
File fi=new File(filein_path);
//UploadFileSpec[] ufs = {new UploadFileSpec(fi)};
PostMethodWebRequest req = new PostMethodWebRequest(url);
req.setMimeEncoded(true);
//req.setParameter("image[file]",ufs);
req.selectFile("image[file]",fi);
WebResponse resp = wc.getResponse( req );
try{
byte[] buf = new byte[1024];
OutputStream bufferOut = new BufferedOutputStream(new FileOutputStream(fileout_path));
InputStream respis = resp.getInputStream();
int numRead = 0;
do {
numRead = respis.read(buf, 0, buf.length);
if (numRead > 0) {
bufferOut.write(buf, 0, numRead);
}
} while (numRead >= 0);
bufferOut.flush();
bufferOut.close();
}
catch(IOException e){}
//uoload will fail when SIP limit 0KB
switch(assertequal){
case 0:assertNotSame(getHash(filein_path,"MD5"),getHash(fileout_path,"MD5"));break;
case 1:assertEquals(getHash(filein_path,"MD5"),getHash(fileout_path,"MD5"));break;
default :break;
}
System.out.printf("Src file's MD5 is %s\nRecevied file's MD5 is %s.",getHash(filein_path,"MD5"),getHash(fileout_path,"MD5"));
}
//@Test
public void testCase1() throws Exception
{
//Need to preset SIP uoload limit of file size equal 0KB
caseMaker(file144kb,receive_path,0);
}
@Test
public void testCase2() throws Exception
{
//Need to preset SIP uoload limit of file size equal 200KB
caseMaker(file144kb,receive_path,1);
}
@Test
public void testCase3() throws Exception
{
//Need to preset SIP uoload limit of file size equal 200KB
caseMaker(file200kb,receive_path,1);
}
@Test
public void testCase4() throws Exception
{
//Need to preset SIP uoload limit of file size equal 200KB
caseMaker(file255kb,receive_path,0);
}
}
断言是通过比较上传前的文件和上传后服务器返回的文件的MD5值来判断的,MD5算法是我们坛子里的某个兄台的,忘了叫啥了。