RuleDatabaseItemUpdateRunnable implements Runnable { boolean validateResponse(HttpURLConnection connection) throws IOException { Log.d(TAG, "validateResponse: " + item.title + ": local = " + new Date(connection.getIfModifiedSince()) + " remote = " + new Date(connection.getLastModified())); if (connection.getResponseCode() != 200) { Log.d(TAG, "validateResponse: " + item.title + ": Skipping: Server responded with " + connection.getResponseCode() + " for " + item.location); if (connection.getResponseCode() == 404) { parentTask.addError(item, context.getString(R.string.file_not_found)); } else if (connection.getResponseCode() != 304) { context.getResources().getString(R.string.host_update_error_item); parentTask.addError(item, context.getResources().getString(R.string.host_update_error_item, connection.getResponseCode(), connection.getResponseMessage())); } return false; } return true; } RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item); @Override void run();  }
@Test public void testValidateResponse() throws Exception { RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class)); Resources resources = mock(Resources.class); when(mockContext.getResources()).thenReturn(resources); when(resources.getString(anyInt(), anyString(), anyInt(), anyString())).thenReturn("%s %s %s"); when(connection.getResponseCode()).thenReturn(200); assertTrue("200 is OK", itemUpdateRunnable.validateResponse(connection)); assertEquals(0, realTask.errors.size()); when(connection.getResponseCode()).thenReturn(404); assertFalse("404 is not OK", itemUpdateRunnable.validateResponse(connection)); assertEquals(1, realTask.errors.size()); when(connection.getResponseCode()).thenReturn(304); assertFalse("304 is not OK", itemUpdateRunnable.validateResponse(connection)); assertEquals(1, realTask.errors.size()); }