Repository: corona-warn-app/cwa-server
File: services/submission/src/test/java/app/coronawarn/server/services/submission/integration/SubmissionPersistenceIT.java (Java)

Code before:
  }

  @Test
  void testDiagnosisKeyRollingPeriodIsZeroShouldNotBeSaved() {

    // given
    List<TemporaryExposureKey> invalidKeys = createValidTemporaryExposureKeys(0);

    List<TemporaryExposureKey> validKeys = createValidTemporaryExposureKeys(1);

    SubmissionPayload invalidSubmissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        invalidKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);
    String tan = "tan";
    HttpHeaders headers = new HttpHeaders();
    headers.add("cwa-fake", "0");
    headers.add("cwa-authorization", tan);
    headers.setContentType(MediaType.valueOf("Application/x-protobuf"));

    SubmissionPayload validSubmissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        validKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);

    testRestTemplate.postForEntity("/version/v1/diagnosis-keys", new HttpEntity<>(invalidSubmissionPayload, headers), Void.class);
    assertEquals(0, diagnosisKeyRepository.count());

    testRestTemplate.postForEntity("/version/v1/diagnosis-keys", new HttpEntity<>(validSubmissionPayload, headers), Void.class);
    assertEquals(100, diagnosisKeyRepository.count());
  }


  @ParameterizedTest
  @ValueSource(strings = {PATH_MOBILE_CLIENT_PAYLOAD_PB + "/" + FILENAME_MOBILE_CLIENT_PAYLOAD_PB})
  void testKeyInsertionWithMobileClientProtoBuf(String testFile) throws IOException {
    List<TemporaryExposureKey> temporaryExposureKeys = createValidTemporaryExposureKeys();
    SubmissionPayload submissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        temporaryExposureKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);

    writeSubmissionPayloadProtobufFile(submissionPayload);

    Path path = Paths.get(testFile);
    InputStream input = new FileInputStream(path.toFile());
    SubmissionPayload payload = SubmissionPayload.parseFrom(input);

    logger.info("Submitting payload: " + System.lineSeparator()
        + JsonFormat.printer().preservingProtoFieldNames().omittingInsignificantWhitespace().print(payload));

    executor.executePost(payload);

    String presenceVerificationSql = generateDebugSqlStatement(payload);
    logger.info("SQL debugging statement: " + System.lineSeparator() + presenceVerificationSql);
    Integer result = jdbcTemplate.queryForObject(presenceVerificationSql, Integer.class);


Review comment (suggestion):
```suggestion
final int expectedNumberOfKeys =
        validSubmissionPayload.getKeysList().size() * submissionServiceConfig.getRandomKeyPaddingMultiplier();
    assertEquals(expectedNumberOfKeys,
        diagnosisKeyRepository.count());
```

Code after:
  }

  @Test
  void testDiagnosisKeyRollingPeriodIsZeroShouldNotBeSaved() {

    // given
    List<TemporaryExposureKey> invalidKeys = createValidTemporaryExposureKeys(0);

    List<TemporaryExposureKey> validKeys = createValidTemporaryExposureKeys(1);

    SubmissionPayload invalidSubmissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        invalidKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);
    String tan = "tan";
    HttpHeaders headers = new HttpHeaders();
    headers.add("cwa-fake", "0");
    headers.add("cwa-authorization", tan);
    headers.setContentType(MediaType.valueOf("Application/x-protobuf"));

    SubmissionPayload validSubmissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        validKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);

    testRestTemplate
        .postForEntity("/version/v1/diagnosis-keys", new HttpEntity<>(invalidSubmissionPayload, headers), Void.class);
    assertEquals(0, diagnosisKeyRepository.count());

    testRestTemplate
        .postForEntity("/version/v1/diagnosis-keys", new HttpEntity<>(validSubmissionPayload, headers), Void.class);

    final int expectedNumberOfKeys =
        validSubmissionPayload.getKeysList().size() * config.getRandomKeyPaddingMultiplier();
    assertEquals(expectedNumberOfKeys, diagnosisKeyRepository.count());
  }


  @ParameterizedTest
  @ValueSource(strings = {PATH_MOBILE_CLIENT_PAYLOAD_PB + "/" + FILENAME_MOBILE_CLIENT_PAYLOAD_PB})
  void testKeyInsertionWithMobileClientProtoBuf(String testFile) throws IOException {
    List<TemporaryExposureKey> temporaryExposureKeys = createValidTemporaryExposureKeys();
    SubmissionPayload submissionPayload = buildSubmissionPayload(List.of("DE"), "DE", true,
        temporaryExposureKeys, SubmissionType.SUBMISSION_TYPE_PCR_TEST);

    writeSubmissionPayloadProtobufFile(submissionPayload);

    Path path = Paths.get(testFile);
    InputStream input = new FileInputStream(path.toFile());
    SubmissionPayload payload = SubmissionPayload.parseFrom(input);

    logger.info("Submitting payload: " + System.lineSeparator()
        + JsonFormat.printer().preservingProtoFieldNames().omittingInsignificantWhitespace().print(payload));

    executor.executePost(payload);