package com.facebook.buck.event.listener;

import com.facebook.buck.test.TestCaseSummary;
import com.facebook.buck.test.TestResultSummary;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.List;
import java.util.concurrent.TimeUnit;

class TestMethodResultMapBuilder implements ResultBuilder {

  @Override
  public List<TestRun.Result> build(
      ImmutableSet<String> contacts,
      TestCaseSummary testCaseSummary) {
    ImmutableList.Builder<TestRun.Result> builder = new ImmutableList.Builder<>();
    for (TestResultSummary testResultSummary : testCaseSummary.getTestResults()) {
      TestRun.Status status = TestRun.Status.convertResultType(testResultSummary.getType());
      String name = String.format(
          "%s#%s",
          testCaseSummary.getTestCaseName(),
          testResultSummary.getTestName());
      builder.add(new TestRun.Result(
          /* name */ name,
          /* contacts */ contacts,
          /* durationSecs */ testResultSummary.getTime() / 1000,
          /* status */ status,
          /* endedTime */ TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()),
          /* summary */ status.toString().toUpperCase(),
          /* details */ buildDetails(testResultSummary)));
    }
    return builder.build();
  }

  private String buildDetails(TestResultSummary testResultSummary) {
    ImmutableList.Builder<String> lines = new ImmutableList.Builder<>();
    TestResultFormatter.reportResultSummary(lines, testResultSummary);
    return Joiner.on("\n").join(lines.build());
  }
}

/*
 * Copyright 2014-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.event.listener;

import com.facebook.buck.test.TestCaseSummary;
import com.facebook.buck.test.TestResultSummary;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.List;
import java.util.concurrent.TimeUnit;

class TestMethodResultMapBuilder implements ResultBuilder {

  @Override
  public List<TestRun.Result> build(
      ImmutableSet<String> contacts,
      TestCaseSummary testCaseSummary) {
    ImmutableList.Builder<TestRun.Result> builder = new ImmutableList.Builder<>();
    for (TestResultSummary testResultSummary : testCaseSummary.getTestResults()) {
      TestRun.Status status = TestRun.Status.convertResultType(testResultSummary.getType());
      String name = String.format(
          "%s#%s",
          testCaseSummary.getTestCaseName(),
          testResultSummary.getTestName());
      builder.add(new TestRun.Result(
          /* name */ name,
          /* contacts */ contacts,
          /* durationSecs */ testResultSummary.getTime() / 1000,
          /* status */ status,
          /* endedTime */ TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()),
          /* summary */ status.toString().toUpperCase(),
          /* details */ buildDetails(testResultSummary)));
    }
    return builder.build();
  }

  private String buildDetails(TestResultSummary testResultSummary) {
    ImmutableList.Builder<String> lines = new ImmutableList.Builder<>();
    TestResultFormatter.reportResultSummary(lines, testResultSummary);
    return Joiner.on("\n").join(lines.build());
  }
}

Fix lint error *grumble grumble grumble*