下面列出了org.apache.http.util.Asserts#check ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
/**
* 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");
}
@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");
}
@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 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();
}
}
};
}
/**
* 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();
}
}
}