Repository: corona-warn-app/cwa-server
File: services/distribution/src/main/java/app/coronawarn/server/services/distribution/runner/TestDataGeneration.java (Java)

Code before:
   * retention period in the application properties is set to 14 days, then this function would return the timestamp for
   * 14 days ago (from now) at 00:00 UTC.
   */
  private long getRetentionStartTimestamp() {
    return TimeUtils.getNow().truncatedTo(ChronoUnit.DAYS).minus(retentionDays, ChronoUnit.DAYS).getEpochSecond()
        / ONE_HOUR_INTERVAL_SECONDS;
  }

  /**
   * Either returns the list of all possible visited countries or only current distribution This ensure that when test
   * generation runs for a country (e.g. FR) all keys in the distribution will contain FR in the vistied_countries
   * array.
   *
   * @return
   */
  private Set<String> generateSetOfVisitedCountries(String distributionCountry) {
    if (random.nextBoolean()) {
      return supportedCountries;
    } else {
      return Set.of(distributionCountry);
    }
  }

  protected DiagnosisKey generateDiagnosisKey(long submissionTimestamp, String country) {
    return DiagnosisKey.builder()
        .withKeyData(randomByteData())
        .withRollingStartIntervalNumber(generateRollingStartIntervalNumber(submissionTimestamp))
        .withTransmissionRiskLevel(generateTransmissionRiskLevel())
        .withSubmissionTimestamp(submissionTimestamp)
        .withCountryCode(country)
        .withVisitedCountries(generateSetOfVisitedCountries(country))
        .withReportType(ReportType.CONFIRMED_TEST)
        .withConsentToFederation(this.config.getDistributionTestdataConsentToFederation())
        .build();
  }
}


Review comment (suggestion):
I think the old method name `generateDiagnosisKeyBytes` was better in that it was clearer what the method actually did...

Code after:
   * retention period in the application properties is set to 14 days, then this function would return the timestamp for
   * 14 days ago (from now) at 00:00 UTC.
   */
  private long getRetentionStartTimestamp() {
    return TimeUtils.getNow().truncatedTo(ChronoUnit.DAYS).minus(retentionDays, ChronoUnit.DAYS).getEpochSecond()
        / ONE_HOUR_INTERVAL_SECONDS;
  }

  /**
   * Either returns the list of all possible visited countries or only current distribution This ensure that when test
   * generation runs for a country (e.g. FR) all keys in the distribution will contain FR in the vistied_countries
   * array.
   *
   * @return
   */
  private Set<String> generateSetOfVisitedCountries(String distributionCountry) {
    if (random.nextBoolean()) {
      return supportedCountries;
    } else {
      return Set.of(distributionCountry);
    }
  }

  @Override
  protected DiagnosisKey generateDiagnosisKey(long submissionTimestamp, String country) {
    return DiagnosisKey.builder()
        .withKeyData(generateDiagnosisKeyBytes())
        .withRollingStartIntervalNumber(generateRollingStartIntervalNumber(submissionTimestamp))
        .withTransmissionRiskLevel(generateTransmissionRiskLevel())
        .withSubmissionTimestamp(submissionTimestamp)
        .withCountryCode(country)
        .withVisitedCountries(generateSetOfVisitedCountries(country))
        .withReportType(ReportType.CONFIRMED_TEST)
        .withConsentToFederation(this.config.getDistributionTestdataConsentToFederation())
        .build();
  }
}