@Test
  public void secondPassageOfMiddleSensorIsIgnored() {
    CurrentRaceStatus currentRaceStatus = new CurrentRaceStatus();
    currentRaceStatus.setState(RaceStatus.State.ACTIVE);
    currentRaceStatus.setCallbackUrl(callbackUrl);
    repository.save(currentRaceStatus);

    given()
        .param("sensorID", "SPLIT")
        .param("timestamp", 1234)
        .when()
        .post(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.ACCEPTED.value());
    given()
        .param("sensorID", "SPLIT")
        .param("timestamp", 12345)
        .when()
        .post(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.ALREADY_REPORTED.value());

    currentRaceStatus = repository.findByRaceId(CurrentRaceStatus.ID);
    verify(restTemplateMock, times(1))
        .postForLocation(
            "http://localhost:" + port + "/onracestatusupdate",
            RaceStatus.builder()
                .event(RaceStatus.Event.MIDDLE)
                .middleTime(new Date(1234))
                .state(RaceStatus.State.ACTIVE)
                .build());
    assertNotNull(currentRaceStatus);
    assertEquals(new Long(1234), currentRaceStatus.getMiddleTime());
  }
  @Test
  public void canCancelRaceIfStartedAndOnlyPost() {
    when()
        .get(CurrentRaceController.CANCEL_RACE_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    when()
        .put(CurrentRaceController.CANCEL_RACE_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    when()
        .delete(CurrentRaceController.CANCEL_RACE_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    when()
        .patch(CurrentRaceController.CANCEL_RACE_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());

    CurrentRaceStatus currentRaceStatus = new CurrentRaceStatus();
    currentRaceStatus.setState(RaceStatus.State.ACTIVE);
    currentRaceStatus.setCallbackUrl(callbackUrl);
    repository.save(currentRaceStatus);

    when()
        .post(CurrentRaceController.CANCEL_RACE_URL)
        .then()
        .statusCode(HttpStatus.ACCEPTED.value());

    currentRaceStatus = repository.findByRaceId(CurrentRaceStatus.ID);

    verify(restTemplateMock, atLeastOnce())
        .postForLocation(
            "http://localhost:" + port + "/onracestatusupdate",
            RaceStatus.builder().state(RaceStatus.State.INACTIVE).build());

    assertNotNull(currentRaceStatus);
    assertEquals(RaceStatus.State.INACTIVE, currentRaceStatus.getState());
  }
  @Test
  public void canUpdatePassageTime_OnlyByPost() {
    given()
        .param("sensorID", "START")
        .param("timestamp", 1234)
        .when()
        .get(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    given()
        .param("sensorID", "START")
        .param("timestamp", 1234)
        .when()
        .delete(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    given()
        .param("sensorID", "START")
        .param("timestamp", 1234)
        .when()
        .put(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());
    given()
        .param("sensorID", "START")
        .param("timestamp", 1234)
        .when()
        .patch(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.METHOD_NOT_ALLOWED.value());

    CurrentRaceStatus currentRaceStatus = new CurrentRaceStatus();
    currentRaceStatus.setState(RaceStatus.State.ACTIVE);
    currentRaceStatus.setCallbackUrl(callbackUrl);
    repository.save(currentRaceStatus);

    assertEquals(1, repository.findAll().size());
    given()
        .param("sensorID", "START")
        .param("timestamp", 1234)
        .when()
        .post(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.ACCEPTED.value());
    given()
        .param("sensorID", "SPLIT")
        .param("timestamp", 12345)
        .when()
        .post(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.ACCEPTED.value());
    given()
        .param("sensorID", "FINISH")
        .param("timestamp", 123456)
        .when()
        .post(CurrentRaceController.PASSAGE_DETECTED_URL)
        .then()
        .statusCode(HttpStatus.ACCEPTED.value());

    currentRaceStatus = repository.findByRaceId(CurrentRaceStatus.ID);

    verify(restTemplateMock, times(1))
        .postForLocation(
            "http://localhost:" + port + "/onracestatusupdate",
            RaceStatus.builder()
                .event(RaceStatus.Event.START)
                .startTime(new Date(1234))
                .state(RaceStatus.State.ACTIVE)
                .build());
    verify(restTemplateMock, times(1))
        .postForLocation(
            "http://localhost:" + port + "/onracestatusupdate",
            RaceStatus.builder()
                .event(RaceStatus.Event.MIDDLE)
                .startTime(new Date(1234))
                .middleTime(new Date(12345))
                .state(RaceStatus.State.ACTIVE)
                .build());
    verify(restTemplateMock, times(1))
        .postForLocation(
            "http://localhost:" + port + "/onracestatusupdate",
            RaceStatus.builder()
                .event(RaceStatus.Event.FINISH)
                .startTime(new Date(1234))
                .middleTime(new Date(12345))
                .finishTime(new Date(123456))
                .state(RaceStatus.State.INACTIVE)
                .build());
    assertNotNull(currentRaceStatus);
    assertEquals(new Long(1234), currentRaceStatus.getStartTime());
    assertEquals(new Long(12345), currentRaceStatus.getMiddleTime());
    assertEquals(new Long(123456), currentRaceStatus.getFinishTime());
    assertEquals(RaceStatus.Event.FINISH, currentRaceStatus.getEvent());
    assertEquals(RaceStatus.State.INACTIVE, currentRaceStatus.getState());
  }