### Question:
Orchestrator { public boolean ping() throws Exception { logDebug(MODULE_NAME, "Inside ping"); try { JsonObject result = getJSON(controllerUrl + "status"); logDebug(MODULE_NAME, "Finished pinging"); return !result.isNull("status"); } catch (Exception exp) { logError(MODULE_NAME, "Error pinging", new AgentSystemException(exp.getMessage(), exp)); throw exp; } } Orchestrator(); boolean ping(); JsonObject provision(String key); JsonObject request(String command, RequestType requestType, Map<String, Object> queryParams, JsonObject json); void sendFileToController(String command, File file); void update();  }### Answer:
@Test public void testPingSuccess() { try { assertTrue(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: } catch (Exception e) { fail("This should not happen"); } }
@Test public void testPingFailure() { try { PowerMockito.when(jsonObject.isNull("status")).thenReturn(true); assertFalse(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: } catch (Exception e) { fail("This should not happen"); } }
@Test (expected = Exception.class) public void throwsExceptionWhenPingIsCalled() throws Exception{ PowerMockito.when(jsonReader.readObject()).thenReturn(null); assertFalse(orchestrator.ping()); }
@Test (expected = AgentUserException.class) public void testPingWhenControllerUrlIsHttpsAndNotDevMode() throws Exception{ PowerMockito.when(Configuration.getControllerUrl()).thenReturn("https: PowerMockito.when(Configuration.isDeveloperMode()).thenReturn(false); assertFalse(orchestrator.ping()); }
@Test (expected = AgentUserException.class) public void throwsExceptionWhenResponseCodeIsNotOkOnPing() throws Exception{ PowerMockito.when(statusLine.getStatusCode()).thenReturn(400); assertFalse(orchestrator.ping()); }
@Test (expected = AgentUserException.class) public void throwsExceptionWhenResponseCodeIs404OnPing() throws Exception{ PowerMockito.when(statusLine.getStatusCode()).thenReturn(404); assertFalse(orchestrator.ping()); }
@Test (expected = AgentUserException.class) public void throwsUnsupportedEncodingExceptionWhenInputStreamIsCreatedInPing() throws Exception{ PowerMockito.whenNew(InputStreamReader.class) .withParameterTypes(InputStream.class, String.class) .withArguments(Mockito.any(), Mockito.anyString()) .thenThrow(mock(UnsupportedEncodingException.class)); orchestrator.ping(); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: }
@Test (expected = AgentUserException.class) public void throwsClientProtocolExceptionWhenHttpsClientExecuteIsCalledInPing() throws Exception{ PowerMockito.doThrow(mock(ClientProtocolException.class)).when(httpClients).execute(Mockito.any()); assertFalse(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: }
@Test (expected = AgentUserException.class) public void throwsIOExceptionWhenHttpsClientExecuteIsCalledInPing() throws Exception{ PowerMockito.doThrow(mock(IOException.class)).when(httpClients).execute(Mockito.any()); assertFalse(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: }
@Test (expected = AgentUserException.class) public void throwsExceptionWhenResponseIsNullCalledInPing() throws Exception{ PowerMockito.when(httpClients.execute(Mockito.any())).thenReturn(null); assertFalse(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: }
@Test (expected = AgentUserException.class) public void throwsUnsupportedOperationExceptionWhenResponseContentIsCalledInPing() throws Exception{ PowerMockito.doThrow(mock(UnsupportedOperationException.class)).when(httpEntity).getContent(); assertFalse(orchestrator.ping()); PowerMockito.verifyPrivate(orchestrator).invoke("getJSON", Mockito.eq("http: }