use of code.satyagraha.gfm.support.api.WebServiceClient in project gfm_viewer by satyagraha.
the class TestWebServiceClientDefault method shouldHandleSimpleRequest.
@Test
public void shouldHandleSimpleRequest() throws Exception {
ServletHolder sh = new ServletHolder(ServletContainer.class);
sh.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, StubResourceConfig.class.getName());
sh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
Server server = new Server(0);
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.addServlet(sh, "/*");
server.start();
Connector[] connectors = server.getConnectors();
int port = ((NetworkConnector) connectors[0]).getLocalPort();
LOGGER.info("server port: " + port);
Config config = mock(Config.class);
String apiUrl = "http://localhost:" + port;
given(config.getApiUrl()).willReturn(apiUrl);
WebProxyConfig webProxyConfig = mock(WebProxyConfig.class);
SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
ClientConnectionManager connectionManager = new ClientConnManagerDefault(sslSocketFactory);
WebServiceClient webServiceClient = new WebServiceClientDefault(config, webProxyConfig, connectionManager);
Random random = new Random();
// construct the tasks to run
int threadCount = 50;
List<WebServiceClientCallable> tasks = new ArrayList<WebServiceClientCallable>();
for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
String mdText = RandomStringUtils.randomAlphanumeric(random.nextInt(100));
WebServiceClientCallable task = new WebServiceClientCallable(webServiceClient, mdText);
tasks.add(task);
}
// run the tasks
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
List<Future<String>> futures = executorService.invokeAll(tasks, 10, TimeUnit.SECONDS);
// verify results
assertThat(futures, hasSize(threadCount));
for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
String result = futures.get(threadIndex).get();
WebServiceClientCallable task = tasks.get(threadIndex);
assertThat(result, is(performSampleTransformation(task.mdText)));
}
server.stop();
}
Aggregations