private void actTest(boolean async) throws Exception { String cloudHost = FH.getCloudHost(); System.out.println("cloud host is " + cloudHost); // the fhconfig.local.properties file exists, use the host value from that // file as the cloud host assertEquals("http://localhost:9000", cloudHost); // mock response for act call MockResponse actSuccessResponse = new MockResponse(); actSuccessResponse.addHeader("Content-Type", "application/json"); actSuccessResponse.setBody("{'status':'ok', 'type': 'act'}"); mockWebServer.enqueue(actSuccessResponse); FHAct actCall = FH.buildActRequest("test", new JSONObject()); FHActCallback callback = new FHActCallback() { @Override public void success(FHResponse pResponse) { resJson = pResponse.getJson(); } @Override public void fail(FHResponse pResponse) { resJson = null; } }; if (async) { runAsyncRequest(actCall, callback); } else { actCall.execute(callback); } assertEquals(resJson.getString("type"), "act"); // verify request object RecordedRequest request = mockWebServer.takeRequest(); assertEquals("POST", request.getMethod().toUpperCase()); assertEquals("/cloud/test", request.getPath()); String requestBody = new String(request.getBody(), "UTF-8"); JSONObject requestJson = new JSONObject(requestBody); assertTrue(requestJson.has("__fh")); JSONObject fhParams = requestJson.getJSONObject("__fh"); String deviceId = fhParams.optString("cuid", null); assertEquals(getDeviceId(), deviceId); }
@Test public void testGetServerWithDepth() throws InterruptedException { MockResponse response = new MockResponse(); response.setBody(stringFromResource("/server/get-depth-5.json")); response.setHeader("Content-Type", "application/vnd.profitbricks.resource+json"); server.enqueue(response); Server server = serverApi().getServer("datacenter-id", "some-id", new DepthOptions().depth(5)); assertNotNull(server); assertEquals(server.properties().name(), "kube-lb"); assertEquals(this.server.getRequestCount(), 1); assertSent(this.server, "GET", "/datacenters/datacenter-id/servers/some-id?depth=5"); }
private void enqueueCloudResponse() throws Exception { MockResponse cloudSuccessResponse = new MockResponse(); cloudSuccessResponse.addHeader("Content-Type", "application/json"); cloudSuccessResponse.setBody("{'status':'ok', 'type': 'cloud'}"); mockWebServer.enqueue(cloudSuccessResponse); }