@Test
  public void executePreloadException() throws Exception {
    PipelinedData pipeline = getPipelinedData(CONTENT);
    final PreloadedData willThrow = control.createMock(PreloadedData.class);

    Callable<PreloadedData> callable =
        new Callable<PreloadedData>() {
          public PreloadedData call() throws Exception {
            return willThrow;
          }
        };

    // One batch
    expect(preloader.createPreloadTasks(same(context), isA(PipelinedData.Batch.class)))
        .andReturn(ImmutableList.of(callable));
    // And PreloadedData that throws an exception
    expect(willThrow.toJson()).andThrow(new PreloadException("Failed"));

    control.replay();

    PipelineExecutor.Results results = executor.execute(context, ImmutableList.of(pipeline));

    // The exception is fully handled, and leads to empty results
    assertEquals(0, results.results.size());
    assertTrue(results.keyedResults.isEmpty());
    assertTrue(results.remainingPipelines.isEmpty());

    control.verify();
  }
  @Test
  public void executeError() throws Exception {
    PipelinedData pipeline = getPipelinedData(CONTENT);

    Capture<PipelinedData.Batch> batchCapture = new Capture<PipelinedData.Batch>();

    JSONObject expectedData = new JSONObject("{error: {message: 'NO!', code: 500}}");

    // Dummy return results (the "real" return would have two values)
    Callable<PreloadedData> callable = createPreloadTask("key", expectedData.toString());

    // One batch with 1 each HTTP and Social preload
    expect(preloader.createPreloadTasks(same(context), and(eqBatch(1, 1), capture(batchCapture))))
        .andReturn(ImmutableList.of(callable));

    control.replay();

    PipelineExecutor.Results results = executor.execute(context, ImmutableList.of(pipeline));

    // Verify the data set is injected, and the os-data was deleted
    assertTrue(batchCapture.getValue().getPreloads().containsKey("me"));
    assertTrue(batchCapture.getValue().getPreloads().containsKey("json"));

    JsonAssert.assertJsonEquals(
        "[{id: 'key', error: {message: 'NO!', code: 500}}]",
        JsonSerializer.serialize(results.results));
    JsonAssert.assertJsonEquals(
        "{message: 'NO!', code: 500}", JsonSerializer.serialize(results.keyedResults.get("key")));
    assertTrue(results.remainingPipelines.isEmpty());

    control.verify();
  }
  @Test
  public void executeWithTwoBatches() throws Exception {
    PipelinedData pipeline = getPipelinedData(TWO_BATCH_CONTENT);

    context =
        new GadgetContext() {
          @Override
          public String getParameter(String property) {
            // Provide the filename to be requested in the first batch
            if ("view-params".equals(property)) {
              return "{'file': 'test.json'}";
            }
            return null;
          }
        };

    // First batch, the HTTP fetch
    Capture<PipelinedData.Batch> firstBatch = new Capture<PipelinedData.Batch>();
    Callable<PreloadedData> firstTask = createPreloadTask("json", "{result: {user: '******'}}");

    // Second batch, the user fetch
    Capture<PipelinedData.Batch> secondBatch = new Capture<PipelinedData.Batch>();
    Callable<PreloadedData> secondTask = createPreloadTask("me", "{result: {'id':'canonical'}}");

    // First, a batch with an HTTP request
    expect(preloader.createPreloadTasks(same(context), and(eqBatch(0, 1), capture(firstBatch))))
        .andReturn(ImmutableList.of(firstTask));
    // Second, a batch with a social request
    expect(preloader.createPreloadTasks(same(context), and(eqBatch(1, 0), capture(secondBatch))))
        .andReturn(ImmutableList.of(secondTask));

    control.replay();

    PipelineExecutor.Results results = executor.execute(context, ImmutableList.of(pipeline));

    JsonAssert.assertJsonEquals(
        "[{id: 'json', result: {user: '******'}}," + "{id: 'me', result: {id: 'canonical'}}]",
        JsonSerializer.serialize(results.results));
    assertEquals(ImmutableSet.of("json", "me"), results.keyedResults.keySet());
    assertTrue(results.remainingPipelines.isEmpty());

    control.verify();

    // Verify the data set is injected, and the os-data was deleted

    // Check the evaluated HTTP request
    RequestAuthenticationInfo request =
        (RequestAuthenticationInfo) firstBatch.getValue().getPreloads().get("json").getData();
    assertEquals("http://example.org/test.json", request.getHref().toString());

    // Check the evaluated person request
    JSONObject personRequest =
        (JSONObject) secondBatch.getValue().getPreloads().get("me").getData();
    assertEquals("canonical", personRequest.getJSONObject("params").getJSONArray("userId").get(0));
  }
  @Test
  public void executeWithBlockedBatch() throws Exception {
    PipelinedData pipeline = getPipelinedData(BLOCKED_FIRST_BATCH_CONTENT);

    // Expect a batch with no content
    expect(preloader.createPreloadTasks(same(context), eqBatch(0, 0)))
        .andReturn(ImmutableList.<Callable<PreloadedData>>of());

    control.replay();

    PipelineExecutor.Results results = executor.execute(context, ImmutableList.of(pipeline));
    assertEquals(0, results.results.size());
    assertTrue(results.keyedResults.isEmpty());
    assertEquals(1, results.remainingPipelines.size());
    assertSame(pipeline, results.remainingPipelines.iterator().next());

    control.verify();
  }