RESTWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { boolean handleException = false; String urlStr = (String) workItem.getParameter("Url"); String method = (String) workItem.getParameter("Method"); String handleExceptionStr = (String) workItem.getParameter("HandleResponseErrors"); String resultClass = (String) workItem.getParameter("ResultClass"); String acceptHeader = (String) workItem.getParameter("AcceptHeader"); String acceptCharset = (String) workItem.getParameter("AcceptCharset"); String headers = (String) workItem.getParameter(PARAM_HEADERS); if (urlStr == null) { throw new IllegalArgumentException("Url is a required parameter"); } if (method == null || method.trim().length() == 0) { method = "GET"; } if (handleExceptionStr != null) { handleException = Boolean.parseBoolean(handleExceptionStr); } Map<String, Object> params = workItem.getParameters(); AuthenticationType authType = type; if (params.get(PARAM_AUTH_TYPE) != null) { authType = AuthenticationType.valueOf((String) params.get(PARAM_AUTH_TYPE)); } Integer connectTimeout = getParamAsInt(params.get(PARAM_CONNECT_TIMEOUT)); if (connectTimeout == null) { connectTimeout = 60000; } Integer readTimeout = getParamAsInt(params.get(PARAM_READ_TIMEOUT)); if (readTimeout == null) { readTimeout = 60000; } if(headers == null) { headers = ""; } HttpClient httpClient = getHttpClient(readTimeout, connectTimeout); Object methodObject = configureRequest(method, urlStr, params, acceptHeader, acceptCharset, headers); try { HttpResponse response = doRequestWithAuthorization(httpClient, methodObject, params, authType); StatusLine statusLine = response.getStatusLine(); int responseCode = statusLine.getStatusCode(); Map<String, Object> results = new HashMap<String, Object>(); HttpEntity respEntity = response.getEntity(); String responseBody = null; String contentType = null; if (respEntity != null) { responseBody = EntityUtils.toString(respEntity, acceptCharset); if (respEntity.getContentType() != null) { contentType = respEntity.getContentType().getValue(); } } if (responseCode >= 200 && responseCode < 300) { postProcessResult(responseBody, resultClass, contentType, results); results.put(PARAM_STATUS_MSG, "request to endpoint " + urlStr + " successfully completed " + statusLine.getReasonPhrase()); } else { if (handleException) { handleException(new RESTServiceException(responseCode, responseBody, urlStr)); } else { logger.warn("Unsuccessful response from REST server (status: {}, endpoint: {}, response: {}", responseCode, urlStr, responseBody); results.put(PARAM_STATUS_MSG, "endpoint " + urlStr + " could not be reached: " + responseBody); } } results.put(PARAM_STATUS, responseCode); manager.completeWorkItem(workItem.getId(), results); } catch (Exception e) { handleException(e); } finally { try { close(httpClient, methodObject); } catch (Exception e) { handleException(e); } } } RESTWorkItemHandler();  RESTWorkItemHandler(boolean doCacheClient);  RESTWorkItemHandler(boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(String username,
                               String password);  RESTWorkItemHandler(String username,
                               String password,
                               boolean doCacheClient);  RESTWorkItemHandler(String username,
                               String password,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl,
                               boolean doCacheClient);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(ClassLoader classLoader);  RESTWorkItemHandler(ClassLoader classLoader,
                               boolean doCacheClient);  RESTWorkItemHandler(ClassLoader classLoader,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader,
                               boolean doCacheClient);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl,
                               ClassLoader classLoader);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl,
                               ClassLoader classLoader,
                               boolean doCacheClient);  RESTWorkItemHandler(String username,
                               String password,
                               String authUrl,
                               ClassLoader classLoader,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(String handlingProcessId,
                               String handlingStrategy,
                               String username,
                               String password);  RESTWorkItemHandler(String handlingProcessId,
                               String handlingStrategy,
                               String username,
                               String password,
                               boolean doCacheClient);  RESTWorkItemHandler(String handlingProcessId,
                               String handlingStrategy,
                               String username,
                               String password,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute);  RESTWorkItemHandler(ClassLoader classLoader, 
                               String handlingProcessId,
                               String handlingStrategy);  RESTWorkItemHandler(ClassLoader classLoader,
                               boolean doCacheClient, 
                               String handlingProcessId,
                               String handlingStrategy);  RESTWorkItemHandler(ClassLoader classLoader,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute,
                               String handlingProcessId,
                               String handlingStrategy);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader,
                               String handlingProcessId,
                               String handlingStrategy);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader,
                               boolean doCacheClient,
                               String handlingProcessId,
                               String handlingStrategy);  RESTWorkItemHandler(String username,
                               String password,
                               ClassLoader classLoader,
                               boolean doCacheClient,
                               int totalPoolConnections,
                               int maxPoolConnectionsPerRoute,
                               String handlingProcessId,
                               String handlingStrategy); String getAuthUrl(); boolean getDoCacheClient(); void executeWorkItem(WorkItem workItem,
                                WorkItemManager manager); void abortWorkItem(WorkItem workItem,
                              WorkItemManager manager); @Override void close(); static final String PARAM_AUTH_TYPE; static final String PARAM_CONNECT_TIMEOUT; static final String PARAM_READ_TIMEOUT; static final String PARAM_CONTENT_TYPE; static final String PARAM_CONTENT_TYPE_CHARSET; static final String PARAM_HEADERS; static final String PARAM_CONTENT; static final String PARAM_CONTENT_DATA; static final String PARAM_USERNAME; static final String PARAM_PASSWORD; static final String PARAM_AUTHURL; static final String PARAM_RESULT; static final String PARAM_STATUS; static final String PARAM_STATUS_MSG; }
@Test public void testGETOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); assertNotNull("results cannot be null", results); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Hello from REST", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithJSONHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); workItem.setParameter("AcceptHeader", "application/json"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(406, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("endpoint " + serverURL + " could not be reached: ", responseMsg); }
@Test public void testGETOperationWithXMLHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); workItem.setParameter("AcceptHeader", "application/xml"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(406, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("endpoint " + serverURL + " could not be reached: ", responseMsg); }
@Test public void testGETOperationWithTextHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); workItem.setParameter("AcceptHeader", "text/plain"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithCustomTimeout() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_CONNECT_TIMEOUT, "30000"); workItem.setParameter(PARAM_READ_TIMEOUT, "25000"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Hello from REST", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithInvalidTimeout() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_CONNECT_TIMEOUT, ""); workItem.setParameter(PARAM_READ_TIMEOUT, ""); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Hello from REST", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithQueryParam() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "?param=test"); workItem.setParameter("Method", "GET"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Hello from REST test", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPOSTOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Post john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "POST"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPOSTOperationWthJSONHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Post john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "POST"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("AcceptHeader", "application/json"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(406, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("endpoint " + serverURL + "/xml could not be reached: ", responseMsg); }
@Test public void testPOSTOperationWithXMLHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Post john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "POST"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("AcceptHeader", "application/xml"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPOSTOperationWithPathParamAndNoContent() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/john"); workItem.setParameter("Method", "POST"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Created resource with name john", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Put john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithXMLHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Put john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("AcceptHeader", "application/xml"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithJSONHeader() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Put john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("AcceptHeader", "application/json"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(406, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("endpoint " + serverURL + "/xml could not be reached: ", responseMsg); }
@Test public void testPATCHOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "PATCH"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNull("result should be null", result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(204, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed No Content", responseMsg); }
@Test public void testDELETEOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>-1</age><name>deleted john</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml/john"); workItem.setParameter("Method", "DELETE"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test(expected = IllegalArgumentException.class) public void testUnsupportedOperation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml/john"); workItem.setParameter("Method", "HEAD"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); }
@Test public void testHandleErrorOnNotSuccessfulResponse() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/notexisting"); workItem.setParameter("Method", "GET"); workItem.setParameter("HandleResponseErrors", "true"); WorkItemManager manager = new TestWorkItemManager(); try { handler.executeWorkItem(workItem, manager); fail("Should throw exception as it was instructed to do so"); } catch (WorkItemHandlerRuntimeException ex) { RESTServiceException e = (RESTServiceException) ex.getCause().getCause(); assertEquals(405, e.getStatus()); assertEquals(serverURL + "/notexisting", e.getEndoint()); assertEquals("", e.getResponse()); } }
@Test public void testGETOperationWithXmlTrasformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "GET"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Person Xml", result.getName()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithJsonTrasformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/json"); workItem.setParameter("Method", "GET"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Person Json", result.getName()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPOSTOperationWithXmlTransformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "POST"); workItem.setParameter(PARAM_CONTENT_TYPE, "Application/XML"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Post john", result.getName()); assertEquals(25, result.getAge().intValue()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithXmlTransformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "Application/Xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>john</name><age>25</age></person>"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Put john", result.getName()); assertEquals(25, result.getAge().intValue()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPOSTOperationWithCompleteXmlTransformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); Person request = new Person(); request.setAge(25); request.setName("john"); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml"); workItem.setParameter("Method", "POST"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, request); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Post john", result.getName()); assertEquals(25, result.getAge().intValue()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithXmlCharsetTrasformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml-charset"); workItem.setParameter("Method", "GET"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Person Xml", result.getName()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testGETOperationWithJsonCharsetTrasformation() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/json-charset"); workItem.setParameter("Method", "GET"); workItem.setParameter("ResultClass", Person.class.getName()); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); Person result = (Person) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals("Person Json", result.getName()); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithDefaultCharset() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String nonAsciiData = "\u0418\u0432\u0430\u043d"; String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Put ????</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml-charset"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(contentParamName, "<person><name>" + nonAsciiData + "</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithSetCharset() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String nonAsciiData = "\u0418\u0432\u0430\u043d"; String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person><age>25</age><name>Put Иван</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml-charset"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml"); workItem.setParameter(PARAM_CONTENT_TYPE_CHARSET, "UTF-8"); workItem.setParameter(contentParamName, "<person><name>" + nonAsciiData + "</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testPUTOperationWithCharsetUnspecifiedViaParameter() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String nonAsciiData = "\u0418\u0432\u0430\u043d"; String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><age>25</age><name>Put " + nonAsciiData + "</name></person>"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/xml-charset"); workItem.setParameter("Method", "PUT"); workItem.setParameter(PARAM_CONTENT_TYPE, "application/xml; charset=utf-8"); workItem.setParameter(contentParamName, "<person><name>" + nonAsciiData + "</name><age>25</age></person>"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testHeadersNull() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String headerKey = "headerKey"; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/header/" + headerKey); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_HEADERS, headerKey + "="); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertTrue(result.trim().isEmpty()); }
@Test public void testHeadersSingleValue() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String headerKey = "headerKey"; String headerValue = "headerValue"; String headers = headerKey + "=" + headerValue; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/header/" + headerKey); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_HEADERS, headers); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertEquals(result, headerValue); }
@Test public void testHeadersMultipleValues() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String headerKey = "headerKey"; String headerValues = "headerValue,headerValue2,headerValue3"; String headers = headerKey + "=" + headerValues; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/header/" + headerKey); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_HEADERS, headers); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertEquals(result, headerValues); }
@Test public void testHeadersMultipleHeaders() { RESTWorkItemHandler handler = new RESTWorkItemHandler(); String headerKey1 = "headerKey"; String headerValues1 = "headerValue,headerValue2,headerValue3"; String headerKey2 = "headerKey2"; String headerValues2 = "headerValue2,headerValue22,headerValue23"; String headers = headerKey1 + "=" + headerValues1 + ";" + headerKey2 + "=" + headerValues2; WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/header/" + headerKey1); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_HEADERS, headers); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(PARAM_RESULT); assertEquals(result, headerValues1); workItem.setParameter("Url", serverURL + "/header/" + headerKey2); workItem.setParameter("Method", "GET"); workItem.setParameter(PARAM_HEADERS, headers); handler.executeWorkItem(workItem, manager); results = ((TestWorkItemManager) manager).getResults(workItem.getId()); result = (String) results.get(PARAM_RESULT); assertEquals(result, headerValues2); }
@Test public void testGETCharsetOperation() throws UnsupportedEncodingException { final String charset = "Windows-1252"; String expected = "Š"; RESTWorkItemHandler handler = new RESTWorkItemHandler(); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/charset"); workItem.setParameter("AcceptCharset", charset); workItem.setParameter("Method", "GET"); WorkItemManager manager = new TestWorkItemManager(); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); assertNotNull("results cannot be null", results); String result = (String) results.get(PARAM_RESULT); assertNotNull("result cannot be null", result); assertEquals(expected, result); int responseCode = (Integer) results.get(PARAM_STATUS); assertNotNull(responseCode); assertEquals(200, responseCode); String responseMsg = (String) results.get(PARAM_STATUS_MSG); assertNotNull(responseMsg); assertEquals("request to endpoint " + workItem.getParameter("Url") + " successfully completed OK", responseMsg); }
@Test public void testHandleErrorOnNotSuccessfulResponseHandlingException() { RESTWorkItemHandler handler = new RESTWorkItemHandler("test", "COMPLETE", "user", "password"); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter("Url", serverURL + "/notexisting"); workItem.setParameter("Method", "GET"); workItem.setParameter("HandleResponseErrors", "true"); WorkItemManager manager = new TestWorkItemManager(); try { handler.executeWorkItem(workItem, manager); fail("Should throw exception as it was instructed to do so"); } catch (ProcessWorkItemHandlerException ex) { RESTServiceException e = (RESTServiceException) ex.getCause().getCause(); assertEquals(405, e.getStatus()); assertEquals(serverURL + "/notexisting", e.getEndoint()); assertEquals("", e.getResponse()); assertEquals("test", ex.getProcessId()); assertEquals("COMPLETE", ex.getStrategy().name()); } }