DXHTTPRequest { @Deprecated public JsonNode request(String resource, JsonNode data) { return request(resource, data, RetryStrategy.SAFE_TO_RETRY); } DXHTTPRequest();  DXHTTPRequest(DXEnvironment env); @Deprecated JsonNode request(String resource, JsonNode data); JsonNode request(String resource, JsonNode data, RetryStrategy retryStrategy); @Deprecated String request(String resource, String data); String request(String resource, String data, RetryStrategy retryStrategy);  }
@Test public void testDXAPI() throws IOException { DXHTTPRequest c = new DXHTTPRequest(); JsonNode responseJson = c.request("/system/findDataObjects", DXJSON.parseJson("{}"), RetryStrategy.SAFE_TO_RETRY); Assert.assertEquals(responseJson.isObject(), true); String responseText = c.request("/system/findDataObjects", "{}", RetryStrategy.SAFE_TO_RETRY); Assert.assertEquals(responseText.substring(0, 1), "{"); DXHTTPRequest c2 = new DXHTTPRequest(); try { c2.request("/system/findDataObjects", DXJSON.parseJson("{\"state\": {\"invalid\": \"oops\"}}"), RetryStrategy.SAFE_TO_RETRY); Assert.fail("Expected findDataObjects to fail with InvalidInput"); } catch (InvalidInputException e) { Assert.assertTrue(e.toString().contains("key \"state\"")); Assert.assertEquals(422, e.getStatusCode()); } }
@Test public void testDXAPICustomEnvironment() throws IOException { DXEnvironment env = DXEnvironment.create(); DXHTTPRequest c = new DXHTTPRequest(env); JsonNode responseJson = c.request("/system/findDataObjects", DXJSON.parseJson("{}"), RetryStrategy.SAFE_TO_RETRY); Assert.assertEquals(responseJson.isObject(), true); String responseText = c.request("/system/findDataObjects", "{}", RetryStrategy.SAFE_TO_RETRY); Assert.assertEquals(responseText.substring(0, 1), "{"); env = DXEnvironment.Builder.fromDefaults().setBearerToken("BOGUS").build(); DXHTTPRequest c2 = new DXHTTPRequest(env); try { c2.request("/system/findDataObjects", DXJSON.parseJson("{}"), RetryStrategy.SAFE_TO_RETRY); Assert.fail("Expected findDataObjects to fail with InvalidAuthentication"); } catch (InvalidAuthenticationException e) { Assert.assertTrue(e.toString().contains("token")); Assert.assertEquals(401, e.getStatusCode()); } }
@Test public void testRequestResourceLeakage() throws InterruptedException, ExecutionException { ExecutorService threadPool = Executors.newFixedThreadPool(200); List<DXHTTPRequest> requests = Lists.newArrayList(); List<Future<String>> futures = Lists.newArrayList(); for (int i = 0; i < 5000; ++i) { final DXHTTPRequest req = new DXHTTPRequest(); requests.add(req); Future<String> f = threadPool.submit(new Callable<String>() { @Override public String call() throws Exception { WhoamiResponse response = DXJSON.safeTreeToValue(req.request("/system/whoami", mapper.valueToTree(new WhoamiRequest()), RetryStrategy.SAFE_TO_RETRY), WhoamiResponse.class); return response.id; } }); futures.add(f); } threadPool.shutdown(); threadPool.awaitTermination(1000, TimeUnit.SECONDS); }
@Test public void testRetryAfterServiceUnavailable() { long startTime = System.currentTimeMillis(); long serverTime = getServerTime(); new DXHTTPRequest().request("/system/comeBackLater", mapper.valueToTree(new ComeBackLaterRequest(serverTime + 8000)), RetryStrategy.SAFE_TO_RETRY); long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(8000 <= timeElapsed); Assert.assertTrue(timeElapsed <= 16000); }
@Test public void testRetryAfterServiceUnavailableExceedingMaxRetries() { long startTime = System.currentTimeMillis(); long serverTime = getServerTime(); new DXHTTPRequest().request("/system/comeBackLater", mapper.valueToTree(new ComeBackLaterRequest(serverTime + 20000)), RetryStrategy.SAFE_TO_RETRY); long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(16000 <= timeElapsed); Assert.assertTrue(timeElapsed <= 30000); }
@Test public void testRetryAfterServiceUnavailableWithoutRetryAfter() { long startTime = System.currentTimeMillis(); long serverTime = getServerTime(); new DXHTTPRequest().request("/system/comeBackLater", mapper.valueToTree(new ComeBackLaterRequest(serverTime + 20000, false)), RetryStrategy.SAFE_TO_RETRY); long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(50000 <= timeElapsed); }
@Test public void testRetryDisabledAfterServiceUnavailable() { DXEnvironment env = DXEnvironment.Builder.fromDefaults().disableRetry().build(); Assert.assertTrue(env.isRetryDisabled()); boolean thrown = false; long startTime = System.currentTimeMillis(); long serverTime = getServerTime(); try { new DXHTTPRequest(env).request("/system/comeBackLater", mapper.valueToTree(new ComeBackLaterRequest(serverTime + 8000)), RetryStrategy.SAFE_TO_RETRY); } catch (ServiceUnavailableException e) { thrown = true; long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(timeElapsed < 2500); } Assert.assertTrue(thrown); }
@Test public void testRetryDisabledAfterInternalError() { DXEnvironment env = DXEnvironment.Builder.fromDefaults().disableRetry().build(); Assert.assertTrue(env.isRetryDisabled()); boolean thrown = false; long startTime = System.currentTimeMillis(); Map<String, String> errorType = new HashMap<String, String>(); errorType.put("errorType", "Error not decodeable"); JsonNode input = DXObject.MAPPER.valueToTree(errorType); try { new DXHTTPRequest(env).request("/system/fakeError", input, RetryStrategy.SAFE_TO_RETRY); } catch (InternalErrorException e) { thrown = true; long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(timeElapsed < 2500); Assert.assertEquals(501, e.getStatusCode()); } Assert.assertTrue(thrown); }
@Test public void testRetryDisabledDoesNotAffectError() throws IOException { DXEnvironment env = DXEnvironment.Builder.fromDefaults().disableRetry().setBearerToken("BOGUS").build(); Assert.assertTrue(env.isRetryDisabled()); boolean thrown = false; long startTime = System.currentTimeMillis(); DXHTTPRequest c = new DXHTTPRequest(env); try { c.request("/system/findDataObjects", DXJSON.parseJson("{}"), RetryStrategy.SAFE_TO_RETRY); Assert.fail("Expected findDataObjects to fail with InvalidAuthentication"); } catch (InvalidAuthenticationException e) { thrown = true; long timeElapsed = System.currentTimeMillis() - startTime; Assert.assertTrue(timeElapsed < 2500); Assert.assertTrue(e.toString().contains("token")); Assert.assertEquals(401, e.getStatusCode()); } Assert.assertTrue(thrown); }