下面列出了怎么用org.apache.http.util.Asserts的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Refund createRefund(Refund refund) throws HTTPException, ConnectionException {
Asserts.notNull(refund, "Refund");
try {
String response = HttpUtils.post(context.getApiPath(Constants.PATH_REFUND + refund.getPaymentId() + "" +
"/refund/"), getHeaders(), gson.toJson(refund));
Type type = new TypeToken<ApiResponse<Refund>>() {
}.getType();
ApiResponse<Refund> createRefundResponse = gson.fromJson(response, type);
return createRefundResponse.getResult();
} catch (IOException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
@Override
public Payout getPayout(String id) throws ConnectionException, HTTPException {
Asserts.notEmpty(id, "Payout Id");
try {
String response = HttpUtils.get(context.getApiPath(Constants.PATH_PAYOUT + "id:" + id + "/"),
getHeaders());
Type type = new TypeToken<ApiResponse<Payout>>() {
}.getType();
ApiResponse<Payout> payoutResponse = gson.fromJson(response, type);
return payoutResponse.getResult();
} catch (IOException | URISyntaxException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
@Test
public void setTargetHostsFromJsonPathTest() {
String jsonPath = "$.sample.small-target-hosts[*].hostName";
List<String> targetHosts = thb.setTargetHostsFromJsonPath(jsonPath,
URL_JSON_PATH, SOURCE_URL);
logger.info("Get list " + targetHosts.size() + " from json path "
+ jsonPath + " from file " + URL_JSON_PATH);
Asserts.check(targetHosts.size() > 0,
"fail setTargetHostsFromJsonPathTest");
// try bad
try {
thb.setTargetHostsFromJsonPath(jsonPath,
FILEPATH_JSON_PATH + "bad", SOURCE_LOCAL);
} catch (TargetHostsLoadException e) {
logger.info("expected error. Get bad list " + " from json path "
+ jsonPath + " from file " + URL_JSON_PATH);
}
}
@Override
public IndexResult index(List<Document> docs) {
Asserts.notNull(docs,"Document to index should not be null.");
Asserts.check(!docs.isEmpty(), "Should be at least one document to index.");
return indexMultipleDocuments(docs, -1);
}
@Override
public IndexResult indexWithin(List<Document> docs, int withinMs) {
Asserts.notNull(docs,"Document to index should not be null.");
Asserts.check(!docs.isEmpty(), "Should be at least one document to index.");
log.warn("Parameter 'within' not in use in elastic search backend");
return indexMultipleDocuments(docs, withinMs);
}
@Override
public IndexResult index(List<Document> docs) {
Asserts.notNull(docs,"Document to index should not be null.");
Asserts.check(docs.size() > 0, "Should be at least one document to index.");
return indexMultipleDocuments(docs, -1);
}
@Override
public PaymentOrderResponse createPaymentOrder(PaymentOrder paymentOrder) throws HTTPException, ConnectionException {
Asserts.notNull(paymentOrder, "Payment Order");
try {
String response = HttpUtils.post(context.getApiPath(Constants.PATH_PAYMENT_ORDER), getHeaders(),
gson.toJson(paymentOrder));
return gson.fromJson(response, PaymentOrderResponse.class);
} catch (IOException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
@Override
public PaymentOrder getPaymentOrder(String id) throws ConnectionException, HTTPException {
Asserts.notEmpty(id, "Order Id");
try {
String response = HttpUtils.get(context.getApiPath(Constants.PATH_PAYMENT_ORDER + "id:" + id + "/"),
getHeaders());
return gson.fromJson(response, PaymentOrder.class);
} catch (IOException | URISyntaxException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
@Override
public PaymentOrder getPaymentOrderByTransactionId(String transactionId) throws ConnectionException, HTTPException {
Asserts.notEmpty(transactionId, "Transaction Id");
try {
String response = HttpUtils.get(
context.getApiPath(Constants.PATH_PAYMENT_ORDER + "transaction_id:" + transactionId + "/"),
getHeaders());
return gson.fromJson(response, PaymentOrder.class);
} catch (IOException | URISyntaxException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
@Override
public PaymentRequest createPaymentRequest(PaymentRequest paymentRequest) throws ConnectionException, HTTPException {
Asserts.notNull(paymentRequest, "payment request");
try {
String response = HttpUtils.post(
context.getApiPath(Constants.PATH_PAYMENT_REQUEST), getHeaders(), gson.toJson(paymentRequest));
return gson.fromJson(response, PaymentRequest.class);
} catch (IOException e) {
throw new ConnectionException(e.getMessage(), e);
}
}
/**
* Test with diable request when need to disable the request.
*/
@Test
public void hitWebsitesMinTargetHostSpecificReplacementWithNA() {
Map<String, StrStrMap> replacementVarMapNodeSpecific = new HashMap<String, StrStrMap>();
replacementVarMapNodeSpecific.put("www.parallec.io",
new StrStrMap().addPair("JOB_ID", "job_a"));
replacementVarMapNodeSpecific.put("www.jeffpei.com", new StrStrMap()
.addPair("JOB_ID", "job_b").addPair("NA", "NA"));
replacementVarMapNodeSpecific.put("www.restcommander.com",
new StrStrMap().addPair("JOB_ID", "job_c"));
ParallelTask task = pc
.prepareHttpGet("/$JOB_ID.html")
.setConcurrency(1700)
.setTargetHostsFromString(
"www.parallec.io www.jeffpei.com www.restcommander.com")
.setReplacementVarMapNodeSpecific(replacementVarMapNodeSpecific)
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
}
});
logger.info(task.toString());
Asserts.check(task.getRequestNum() == 3
&& task.getRequestNumActual() == 2,
"NA is not able to disable the request");
}
/**
* test all requests are disabled
*/
@Test
public void hitWebsitesMinTargetHostSpecificReplacementWithAllNA() {
Map<String, StrStrMap> replacementVarMapNodeSpecific = new HashMap<String, StrStrMap>();
replacementVarMapNodeSpecific.put("www.parallec.io", new StrStrMap()
.addPair("JOB_ID", "job_a").addPair("NA", "NA"));
replacementVarMapNodeSpecific.put("www.jeffpei.com", new StrStrMap()
.addPair("JOB_ID", "job_b").addPair("NA", "NA"));
replacementVarMapNodeSpecific.put("www.restcommander.com",
new StrStrMap().addPair("JOB_ID", "job_c").addPair("NA", "NA"));
ParallelTask task = pc
.prepareHttpGet("/$JOB_ID.html")
.setConcurrency(1700)
.setTargetHostsFromString(
"www.parallec.io www.jeffpei.com www.restcommander.com")
.setReplacementVarMapNodeSpecific(replacementVarMapNodeSpecific)
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
}
});
logger.info(task.toString());
Asserts.check(task.getRequestNum() == 3
&& task.getRequestNumActual() == 0,
"NA is not able to disable the request");
}
/**
* same requests to different target URLs
*
* demonstrate the uniform variable replacement
*
* here: the "$URL_VARIABLE" is replaced by validateInternals
*/
@Test
public void hitWebsitesMinTargeUniformReplacement() {
Map<String, String> replacementVarMap = new HashMap<String, String>();
replacementVarMap.put("URL_VARIABLE", "validateInternals");
pc.prepareHttpGet("/$URL_VARIABLE.html")
.setConcurrency(1700)
.setTargetHostsFromString(
"www.parallec.io www.jeffpei.com www.restcommander.com")
.setReplacementVarMap(replacementVarMap)
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
String cpu = new FilterRegex(
".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>.*")
.filter(res.getResponseContent());
String memory = new FilterRegex(
".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>.*")
.filter(res.getResponseContent());
Map<String, Object> metricMap = new HashMap<String, Object>();
metricMap.put("CpuUsage", cpu);
metricMap.put("MemoryUsage", memory);
logger.info("cpu:" + cpu + " memory: " + memory
+ " host: " + res.getHost());
Double cpuDouble = Double.parseDouble(cpu);
Asserts.check(cpuDouble <= 100.0 && cpuDouble >= 0.0,
" Fail to extract cpu values");
}
});
}
@Test
public void cancelSingleHostAfter200MillisGoodAndBadHostName() {
ParallelTask pt = pc
.prepareHttpGet("")
.async()
.setConcurrency(100)
.setTargetHostsFromLineByLineText(FILEPATH_TOP_100,
SOURCE_LOCAL).execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
logger.info("Responose Code:" + res.getStatusCode()
+ " host: " + res.getHost());
}
});
boolean hasCanceled = false;
boolean cancelSuccess = false;
while (!pt.isCompleted()) {
try {
Thread.sleep(200L);
if (!hasCanceled) {
logger.info("try to cancel on target");
cancelSuccess = pt.cancelOnTargetHosts(Arrays.asList(
"badhostName", "www.walmart.com"));
hasCanceled = true;
}
System.err.println(String.format(
"POLL_JOB_PROGRESS (%.5g%%) PT jobid: %s",
pt.getProgress(), pt.getTaskId()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Asserts.check(cancelSuccess == true,
"fail cancelSingleHostAfter200Millis");
pt.saveLogToLocal();
}
@Test
public void setTargetHostsFromListTest() {
List<String> targetHosts = thb.setTargetHostsFromList(Arrays.asList("www.jeffpei.com", "www.restcommander.com"));
logger.info("Get list " + targetHosts.size()
+ " setTargetHostsFromListTest ");
Asserts.check(targetHosts.size()>0, "fail test");
}
@Test
public void setTargetHostsFromListTestDup() {
List<String> targetHostsOrg = new ArrayList<String>();
targetHostsOrg.add("www.restcommander.com");
targetHostsOrg.add("www.restcommander.com");
List<String> targetHosts = thb.setTargetHostsFromList(targetHostsOrg);
logger.info("Get list " + targetHosts.size()
+ " setTargetHostsFromListTest ");
Asserts.check(targetHosts.size()==1, "fail test");
}
@Test
public void setTargetHostsFromListBadHostTest() {
List<String> targetHostsOrg = new ArrayList<String>();
targetHostsOrg.add("www.restco mmander.com");
List<String> targetHosts = thb.setTargetHostsFromList(targetHostsOrg);
logger.info("Get list " + targetHosts.size()
+ " setTargetHostsFromListTest ");
Asserts.check(targetHosts.size()>0, "fail test");
}
@Test
public void setTargetHostsFromLineByLineText() {
List<String> targetHosts = thb.setTargetHostsFromLineByLineText(
FILEPATH_TOP_100, SOURCE_LOCAL);
logger.info("Get list " + targetHosts.size() + " from "
+ FILEPATH_TOP_100);
Asserts.check(targetHosts.size()>0, "fail test");
// bad path
try {
thb.setTargetHostsFromLineByLineText("/badpath", SOURCE_LOCAL);
} catch (TargetHostsLoadException e) {
logger.info("expected exception: " + e);
}
}
@Test
public void setTargetHostsFromUrl() {
List<String> targetHosts = thb.setTargetHostsFromLineByLineText(
URL_TOP_100, SOURCE_URL);
logger.info("Get list " + targetHosts.size() + " from " + URL_TOP_100);
Asserts.check(targetHosts.size() > 0,
"fail setTargetHostsFromLineByLineText setTargetHostsFromUrl");
}
public HttpEntity builder() {
Asserts.notNull(this.entityBuilder, "entityBuilder should not be null");
return this.entityBuilder.build();
}
public HttpEntity builder() {
Asserts.notNull(multipartEntityBuilder, "entityBuilder should not be null");
return this.multipartEntityBuilder.build();
}
@Override
public IndexResult index(Document... docs) {
Asserts.notNull(docs,"Document to index should not be null.");
Asserts.check(docs.length > 0, "Should be at least one document to index.");
return indexMultipleDocuments(Arrays.asList(docs), -1);
}
@Override
public IndexResult indexWithin(Document doc, int withinMs) {
Asserts.notNull(doc,"Document to index should not be null.");
log.warn("Parameter 'within' not in use in elastic search backend");
return indexMultipleDocuments(Collections.singletonList(doc), withinMs);
}
@Override
public IndexResult index(Document ... docs) {
Asserts.notNull(docs,"Document to index should not be null.");
Asserts.check(docs.length > 0, "Should be at least one document to index.");
return indexMultipleDocuments(Arrays.asList(docs), -1);
}
@SuppressWarnings("deprecation")
public static InputStream makeRequestGetStream(HttpMethod httpMethod, String address, Map<String, String> requestHeaders, String requestBody,
List<NameValuePair> queryParams, boolean authenticate) throws HttpException, IOException, HMACSecurityException {
final HttpMethodBase method = getMethod(httpMethod, address);
if (requestHeaders != null) {
for (Entry<String, String> entry : requestHeaders.entrySet()) {
method.addRequestHeader(entry.getKey(), entry.getValue());
}
}
if (queryParams != null) {
// add uri query params to provided query params present in query
List<NameValuePair> addressPairs = getAddressPairs(address);
List<NameValuePair> totalPairs = new ArrayList<NameValuePair>(addressPairs);
totalPairs.addAll(queryParams);
method.setQueryString(totalPairs.toArray(new NameValuePair[queryParams.size()]));
}
if (method instanceof EntityEnclosingMethod) {
EntityEnclosingMethod eem = (EntityEnclosingMethod) method;
// charset of request currently not used
eem.setRequestBody(requestBody);
}
if (authenticate) {
String hmacKey = SpagoBIUtilities.getHmacKey();
if (hmacKey != null && !hmacKey.isEmpty()) {
logger.debug("HMAC key found with value [" + hmacKey + "]. Requests will be authenticated.");
HMACFilterAuthenticationProvider authenticationProvider = new HMACFilterAuthenticationProvider(hmacKey);
authenticationProvider.provideAuthentication(method, requestBody);
} else {
throw new SpagoBIRuntimeException("The request need to be authenticated, but hmacKey wasn't found.");
}
}
HttpClient client = getHttpClient(address);
int statusCode = client.executeMethod(method);
logger.debug("Status code " + statusCode);
Header[] headers = method.getResponseHeaders();
logger.debug("Response header " + headers);
Asserts.check(statusCode == HttpStatus.SC_OK, "Response not OK.\nStatus code: " + statusCode);
return new FilterInputStream(method.getResponseBodyAsStream()) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
method.releaseConnection();
}
}
};
}
public static JkesProperties getJkesProperties() {
Asserts.notNull(jkesProperties, "JkesProperties config must be set into Config");
return jkesProperties;
}
/**
* 查询历史命令分页列表请求构造方法
* @param devId
* @param key
* @param start
* @param end
* @param duration
* @param limit
* @param cursor
* @param sort
* @param status
*/
public GetCmdsHistoryApi(String devId, String key, String start, String end, Integer duration, Integer limit, String cursor, String sort, Integer status) {
// this.devId = devId;
// this.key = key;
// this.start = start;
// this.end = end;
// this.duration = duration;
// this.limit = limit;
// this.cursor = cursor;
// this.sort = sort;
// this.status = status;
this.method = Method.GET;
Map<String, Object> headmap = new HashMap<String, Object>();
Map<String, Object> urlmap = new HashMap<String, Object>();
this.HttpMethod = new HttpGetMethod(method);
Asserts.notBlank(devId,"devId");
this.url = Config.getString("test.url") + "/cmds/history/"+devId;
Asserts.notBlank(key,"api-key");
headmap.put("api-key", key);
if(start!=null){
urlmap.put("start",start);
}
if(end!=null){
urlmap.put("end",end);
}
if(duration!=null){
urlmap.put("duration",duration);
}
if(limit!=null){
urlmap.put("limit",limit);
}
if(cursor!=null){
urlmap.put("cursor",cursor);
}
if(cursor!=null){
urlmap.put("cursor",cursor);
}
if(sort!=null){
urlmap.put("sort",sort);
}
if(status!=null){
urlmap.put("status",status);
}
this.HttpMethod.setHeader(headmap);
this.HttpMethod.setcompleteUrl(this.url,urlmap);
}
/**
* With CMS query; async timeout 15 seconds
* Added token
*/
@Test(timeout = 15000)
public void hitCmsQuerySinglePageWithoutTokenAsync() {
// http://ccoetech.ebay.com/cms-configuration-management-service-based-mongodb
String cmsQueryUrl = URL_CMS_QUERY_SINGLE_PAGE;
ParallelTask pt = pc.prepareHttpGet("/validateInternals.html")
.setTargetHostsFromCmsQueryUrl(cmsQueryUrl, "label")
.setConcurrency(1700).async()
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
String cpu = new FilterRegex(
".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
String memory = new FilterRegex(
".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
Map<String, Object> metricMap = new HashMap<String, Object>();
metricMap.put("CpuUsage", cpu);
metricMap.put("MemoryUsage", memory);
metricMap.put("LastUpdated",
PcDateUtils.getNowDateTimeStrStandard());
metricMap.put("NodeGroupType", "OpenSource");
logger.info("cpu:" + cpu + " host: " + res.getHost());
}
});
logger.info(pt.toString());
Asserts.check(pt.getRequestNum() == 3, "fail to load all target hosts");
while (!pt.isCompleted()) {
try {
Thread.sleep(100L);
System.err.println(String.format("POLL_JOB_PROGRESS (%.5g%%)",
pt.getProgress()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* With CMS query; async timeout 15 seconds
* Added token
*/
@Test(timeout = 15000)
public void hitCmsQuerySinglePageWithTokenAsync() {
// http://ccoetech.ebay.com/cms-configuration-management-service-based-mongodb
String cmsQueryUrl = URL_CMS_QUERY_SINGLE_PAGE;
ParallelTask pt = pc.prepareHttpGet("/validateInternals.html")
.setTargetHostsFromCmsQueryUrl(cmsQueryUrl, "label", "someToken")
.setConcurrency(1700).async()
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
String cpu = new FilterRegex(
".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
String memory = new FilterRegex(
".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
Map<String, Object> metricMap = new HashMap<String, Object>();
metricMap.put("CpuUsage", cpu);
metricMap.put("MemoryUsage", memory);
metricMap.put("LastUpdated",
PcDateUtils.getNowDateTimeStrStandard());
metricMap.put("NodeGroupType", "OpenSource");
logger.info("cpu:" + cpu + " host: " + res.getHost());
}
});
logger.info(pt.toString());
Asserts.check(pt.getRequestNum() == 3, "fail to load all target hosts");
while (!pt.isCompleted()) {
try {
Thread.sleep(100L);
System.err.println(String.format("POLL_JOB_PROGRESS (%.5g%%)",
pt.getProgress()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* With CMS(YiDB) query; async timeout 15 seconds CMS Example:
* http://ccoetech
* .ebay.com/cms-configuration-management-service-based-mongodb
*/
@Test(timeout = 15000)
public void hitCmsQueryMultiPageAsync() {
String cmsQueryUrl = URL_CMS_QUERY_MULTI_PAGE;
ParallelTask pt = pc.prepareHttpGet("/validateInternals.html")
.setTargetHostsFromCmsQueryUrl(cmsQueryUrl)
.setConcurrency(1700).async()
.execute(new ParallecResponseHandler() {
@Override
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
String cpu = new FilterRegex(
".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
String memory = new FilterRegex(
".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>[\\s\\S]*")
.filter(res.getResponseContent());
Map<String, Object> metricMap = new HashMap<String, Object>();
metricMap.put("CpuUsage", cpu);
metricMap.put("MemoryUsage", memory);
logger.info("cpu:" + cpu + " memory: " + memory
+ " host: " + res.getHost());
Double cpuDouble = Double.parseDouble(cpu);
Asserts.check(cpuDouble <= 100.0 && cpuDouble >= 0.0,
" Fail to extract cpu values");
}
});
logger.info(pt.toString());
Asserts.check(pt.getRequestNum() == 3, "fail to load all target hosts");
while (!pt.isCompleted()) {
try {
Thread.sleep(100L);
System.err.println(String.format("POLL_JOB_PROGRESS (%.5g%%)",
pt.getProgress()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}