@Override
  public WebhookPayload create(ProjectAnalysis analysis) {
    Writer string = new StringWriter();
    try (JsonWriter writer = JsonWriter.of(string)) {
      writer.beginObject();
      writeServer(writer);
      writeTask(writer, analysis.getCeTask());
      writeAnalysis(writer, analysis, system2);
      writeProject(analysis, writer, analysis.getProject());
      analysis.getBranch().ifPresent(b -> writeBranch(writer, analysis.getProject(), b));
      analysis.getQualityGate().ifPresent(qualityGate -> writeQualityGate(writer, qualityGate));
      writeAnalysisProperties(writer, analysis.getProperties());
      writer.endObject().close();
      return new WebhookPayload(analysis.getProject().getKey(), string.toString());
    }
  }
@Test
  public void create_payload_for_successful_analysis() {
    CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
    Condition condition = new Condition("coverage", Condition.Operator.GREATER_THAN, "70.0");
    EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder()
      .setQualityGate(new QualityGate("G1", "Gate One", singleton(condition)))
      .setStatus(Metric.Level.ERROR)
      .addEvaluatedCondition(condition, EvaluatedCondition.EvaluationStatus.ERROR, "74.0")
      .build();
    ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());

    WebhookPayload payload = underTest.create(analysis);
    assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
    assertJson(payload.getJson())
      .isSimilarTo("{" +
        "  \"serverUrl\": \"http://foo\"," +
        "  \"taskId\": \"#1\"," +
        "  \"status\": \"SUCCESS\"," +
        "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
        "  \"revision\": \"sha1\"," +
        "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
        "  \"project\": {" +
        "    \"key\": \"P1\"," +
        "    \"name\": \"Project One\"," +
        "    \"url\": \"http://foo/dashboard?id=P1\"" +
        "  }," +
        "  \"qualityGate\": {" +
        "    \"name\": \"Gate One\"," +
        "    \"status\": \"ERROR\"," +
        "    \"conditions\": [" +
        "      {" +
        "        \"metric\": \"coverage\"," +
        "        \"operator\": \"GREATER_THAN\"," +
        "        \"value\": \"74.0\"," +
        "        \"status\": \"ERROR\"," +
        "        \"errorThreshold\": \"70.0\"" +
        "      }" +
        "    ]" +
        "  }," +
        "  \"properties\": {" +
        "  }" +
        "}");
  }