private void uploadDiffs(
      Map<String, String> patternsNamesAndCorrespondingDiffs,
      String timestampWithoutWhiteSpaces,
      String testSuiteRunID) {
    String suiteName = grapheneVisualTestingConf.get().getTestSuiteName();
    GrapheneVisualTestingConfiguration gVC = grapheneVisualTestingConf.get();
    CloseableHttpClient httpclient =
        RestUtils.getHTTPClient(
            gVC.getJcrContextRootURL(), gVC.getJcrUserName(), gVC.getJcrPassword());
    for (final Map.Entry<String, String> patternAndDiff :
        patternsNamesAndCorrespondingDiffs.entrySet()) {
      File diffsDir =
          new File(
              rusheyeConf.get().getWorkingDirectory()
                  + File.separator
                  + rusheyeConf.get().getDiffsDir());
      File[] diffsWithSearchedName =
          diffsDir.listFiles(
              new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                  return name.contains(patternAndDiff.getValue());
                }
              });
      if (diffsWithSearchedName.length > 1) {
        throw new RuntimeException(
            "There are two or more diffs which names contains: " + patternAndDiff.getValue());
      } else if (diffsWithSearchedName.length == 0) {
        throw new RuntimeException(
            "Diff with filename: " + patternAndDiff.getValue() + " was not found!");
      } else {
        // UPLOADING DIFF
        File diffToUpload = diffsWithSearchedName[0];
        String diffURL =
            gVC.getJcrContextRootURL()
                + "/upload/"
                + suiteName
                + "/runs/"
                + timestampWithoutWhiteSpaces
                + "/diffs/"
                + diffToUpload.getName();
        HttpPost postResultDescriptor = new HttpPost(diffURL);
        FileEntity diffEntity = new FileEntity(diffToUpload);
        postResultDescriptor.setEntity(diffEntity);
        RestUtils.executePost(
            postResultDescriptor,
            httpclient,
            String.format("Diff for %s uploaded!", suiteName),
            String.format("Error while uploading diff for test suite: %s", suiteName));

        // CREATING DIFF IN DATABASE
        HttpPost postCreateDiff =
            new HttpPost(
                gVC.getManagerContextRootURL() + "graphene-visual-testing-webapp/rest/diffs");
        postCreateDiff.setHeader("Content-Type", "application/json");
        Long sampleID = sampleAndItsIDs.get(patternAndDiff.getKey());
        StringEntity toDatabaseDiff =
            new StringEntity(
                "{\"name\":\""
                    + diffToUpload.getName()
                    + "\",\"urlOfScreenshot\":\""
                    + diffURL.replace("/upload/", "/binary/")
                    + "/jcr%3acontent/jcr%3adata"
                    + "\",\"sample\":{\"sampleID\":\""
                    + sampleID
                    + "\"},\"testSuiteRun\":{\"testSuiteRunID\":\""
                    + testSuiteRunID
                    + "\"}}",
                ContentType.APPLICATION_JSON);
        postCreateDiff.setEntity(toDatabaseDiff);
        RestUtils.executePost(
            postCreateDiff,
            httpclient,
            String.format("Diff in database for %s created!", suiteName),
            String.format("Error while diff in database for test suite: %s", suiteName));
      }
    }
  }
  @Override
  public void saveSamplesAndDiffs(@Observes ParsingDoneEvent parsingDoneEvent) {
    Date timestamp = new Date();
    String timestampWithoutWhiteSpaces = "" + timestamp.getTime();
    String suiteName = grapheneVisualTestingConf.get().getTestSuiteName();
    GrapheneVisualTestingConfiguration gVC = grapheneVisualTestingConf.get();
    CloseableHttpClient httpclient =
        RestUtils.getHTTPClient(
            gVC.getJcrContextRootURL(), gVC.getJcrUserName(), gVC.getJcrPassword());
    File resultDescriptor =
        new File(
            rusheyeConf.get().getWorkingDirectory()
                + File.separator
                + rusheyeConf.get().getResultOutputFile());

    // UPLOADING RESULT DESCRIPTOR
    HttpPost postResultDescriptor =
        new HttpPost(
            gVC.getJcrContextRootURL()
                + "/upload/"
                + suiteName
                + "/runs/"
                + timestampWithoutWhiteSpaces
                + "/result.xml");
    FileEntity descriptorEntity = new FileEntity(resultDescriptor, ContentType.APPLICATION_XML);
    postResultDescriptor.setEntity(descriptorEntity);
    RestUtils.executePost(
        postResultDescriptor,
        httpclient,
        String.format("Suite result descriptor for %s uploaded!", suiteName),
        String.format(
            "Error while uploading test suite result descriptor for test suite: %s", suiteName));

    // CREATE TEST SUITE RUN IN DATABASE
    HttpPost postCreateSuiteRun =
        new HttpPost(gVC.getManagerContextRootURL() + "graphene-visual-testing-webapp/rest/runs");
    postCreateSuiteRun.setHeader("Content-Type", "application/json");
    StringEntity suiteRunEntity =
        new StringEntity(
            "{\"timestamp\":\""
                + timestamp.getTime()
                + "\",\"projectRevision\":\"ffff1111\","
                + "\"numberOfFailedFunctionalTests\":\"-1\","
                + "\"numberOfFailedComparisons\":\""
                + getDiffNames().size()
                + "\","
                + "\"testSuite\":{\"name\":\""
                + suiteName
                + "\"}}",
            ContentType.APPLICATION_JSON);
    postCreateSuiteRun.setEntity(suiteRunEntity);
    String testSuiteRunID =
        RestUtils.executePost(
            postCreateSuiteRun,
            httpclient,
            String.format("SuiteRun in database for %s created!", suiteName),
            String.format("Error while SuiteRun name in database for test suite: %s", suiteName));

    // UPLOADING DIFFS AND SAMPLES IF ANY
    Map<String, String> patternsNamesAndCorrespondingDiffs = getDiffNames();
    if (!patternsNamesAndCorrespondingDiffs.isEmpty()) {
      diffsUtils.get().setDiffCreated(true);
      uploadSamples(
          patternsNamesAndCorrespondingDiffs, timestampWithoutWhiteSpaces, testSuiteRunID);
      uploadDiffs(patternsNamesAndCorrespondingDiffs, timestampWithoutWhiteSpaces, testSuiteRunID);
    }
  }
  private void uploadSamples(
      Map<String, String> patternsNamesAndCorrespondingDiffs,
      String timestamp,
      String testSuiteRunID) {
    final File screenshotsDir = screenshooterConf.get().getRootDir();
    GrapheneVisualTestingConfiguration gVC = grapheneVisualTestingConf.get();
    final String suiteName = gVC.getTestSuiteName();
    CloseableHttpClient httpclient =
        RestUtils.getHTTPClient(
            gVC.getJcrContextRootURL(), gVC.getJcrUserName(), gVC.getJcrPassword());

    NodeList testNodes = getDOMFromSuiteXML().getElementsByTagName("test");
    for (Map.Entry<String, String> entry : patternsNamesAndCorrespondingDiffs.entrySet()) {
      for (int i = 0; i < testNodes.getLength(); i++) {
        if (testNodes
            .item(i)
            .getAttributes()
            .getNamedItem("name")
            .getNodeValue()
            .equals(entry.getKey())) {
          String patternSource =
              testNodes
                  .item(i)
                  .getChildNodes()
                  .item(1)
                  .getAttributes()
                  .getNamedItem("source")
                  .getNodeValue();
          // UPLOAD SAMPLE
          File sampleToUpload = new File(screenshotsDir + File.separator + patternSource);
          String url =
              grapheneVisualTestingConf.get().getJcrContextRootURL()
                  + "/upload/"
                  + suiteName
                  + "/runs/"
                  + timestamp
                  + "/samples/"
                  + patternSource;
          HttpPost postResultDescriptor = new HttpPost(url);
          FileEntity sampleEntity = new FileEntity(sampleToUpload);
          postResultDescriptor.setEntity(sampleEntity);
          RestUtils.executePost(
              postResultDescriptor,
              httpclient,
              String.format("Sample for %s uploaded!", suiteName),
              String.format("Error while uploading sample for test suite: %s", suiteName));

          // CREATE SAMPLE IN DATABASE
          HttpPost postCreateSample =
              new HttpPost(
                  gVC.getManagerContextRootURL() + "graphene-visual-testing-webapp/rest/samples");
          postCreateSample.setHeader("Content-Type", "application/json");
          StringEntity toDatabaseSample =
              new StringEntity(
                  "{\"name\":\""
                      + patternSource
                      + "\",\"urlOfScreenshot\":\""
                      + url.replace("/upload/", "/binary/")
                      + "/jcr%3acontent/jcr%3adata"
                      + "\",\"testSuiteRun\":{\"testSuiteRunID\":\""
                      + testSuiteRunID
                      + "\"}}",
                  ContentType.APPLICATION_JSON);
          postCreateSample.setEntity(toDatabaseSample);
          String sampleID =
              RestUtils.executePost(
                  postCreateSample,
                  httpclient,
                  String.format("Sample in database for %s created!", suiteName),
                  String.format("Error while Sample in database for test suite: %s", suiteName));
          sampleAndItsIDs.put(
              patternSource
                  .replaceAll("/", "\\.")
                  .replaceAll(
                      "\\." + screenshooterConf.get().getScreenshotType().toLowerCase(), ""),
              Long.valueOf(sampleID));
        }
      }
    }
  }