@Ignore("needs to check if finishing creates a proper task")
  @Test
  public void shouldSetSessionEnd() {
    sessionManager.startMobileSession(false);
    triggerMeasurement();

    sessionManager.finishSession(0);

    verify(sessionManager.sessionRepository)
        .save(
            Mockito.argThat(
                new BaseMatcher<Session>() {
                  @Override
                  public boolean matches(Object o) {
                    Session other = (Session) o;
                    long oneSecond = 1000;
                    return new Date().getTime() - other.getEnd().getTime() < oneSecond;
                  }

                  @Override
                  public void describeTo(Description description) {
                    description.appendText("Session with end set");
                  }
                }));
  }
  @Test
  public void shouldStartASession() {
    sessionManager.startMobileSession(false);

    verify(sessionManager.locationHelper, times(2)).start();
    verify(sessionManager.audioReader).start();
    assertThat(sessionManager.isSessionStarted(), equalTo(true));
  }
  @Test
  public void shouldJustDeleteNotesIfSessionInProgress() {
    sessionManager.startMobileSession(false);
    Note note = sessionManager.makeANote(null, null, null);

    sessionManager.deleteNote(note);

    assertThat(sessionManager.getNotes(), not(hasItem(note)));
  }
  @Test
  public void shouldSetSessionStart() {
    sessionManager.startMobileSession(false);

    int oneSecond = 1000;
    assertThat(
        new Date().getTime() - sessionManager.session.getStart().getTime() < oneSecond,
        equalTo(true));
  }
  @Test
  public void shouldAllowAccessToAParticularStream() {
    sessionManager.startMobileSession(false);

    triggerMeasurement();

    MeasurementStream expected = Iterables.getOnlyElement(sessionManager.getMeasurementStreams());
    assertThat(sessionManager.getMeasurementStream("LHC") == expected, equalTo(true));
  }
  @Test
  public void shouldNotStopSensorsDuringASession() {
    sessionManager.startMobileSession(false);
    sessionManager.stopSensors();

    verify(sessionManager.locationHelper, never()).stop();
    verify(sessionManager.audioReader, never()).stop();
    assertThat(sessionManager.isSessionStarted(), equalTo(true));
  }
  @Test
  public void shouldCreateOnlyOneStreamPerSensor() {
    sessionManager.startMobileSession(false);

    triggerMeasurement();
    triggerMeasurement();

    assertThat(sessionManager.getMeasurementStreams().size(), equalTo(1));
  }
  @Test
  public void afterDeletingNotesShouldHaveNewNumbers() {
    sessionManager.startMobileSession(false);
    Note note1 = sessionManager.makeANote(null, "Note1", null);
    Note note2 = sessionManager.makeANote(null, "Note2", null);

    sessionManager.deleteNote(note1);
    Note note3 = sessionManager.makeANote(null, "Note3", null);

    assertThat(note3.getNumber(), not(equalTo(note2.getNumber())));
  }
  @Test
  public void startSession_should_be_indicated_in_recording_state() throws Exception {
    // given

    // when
    sessionManager.startMobileSession(false);

    // then
    org.fest.assertions.Assertions.assertThat(sessionManager.state.recording().isRecording())
        .isTrue();
  }
  @Test
  public void measurements_withoutLocation_should_get_a_fake() {
    sessionManager.startMobileSession(false);
    sessionManager.session.setLocationless(true);
    when(sessionManager.locationHelper.getLastLocation()).thenReturn(null);

    triggerMeasurement();

    assertThat(sessionManager.getMeasurementStreams().isEmpty(), equalTo(false));
    assertThat(sessionManager.getSession().isLocationless(), equalTo(true));
  }
  @Test
  public void shouldStoreMeasurements() {
    sessionManager.startMobileSession(false);

    triggerMeasurement(22);

    Measurement expected = new Measurement(location.getLatitude(), location.getLongitude(), 22);
    org.fest.assertions.Assertions.assertThat(
            sessionManager.getMeasurementStream("LHC").getMeasurements())
        .contains(expected);
  }
  @Test
  public void shouldCreateMeasurementStreams() {
    sessionManager.startMobileSession(false);

    triggerMeasurement();

    MeasurementStream expected = lastEvent.stream();
    Collection<MeasurementStream> streams = sessionManager.getMeasurementStreams();

    assertThat(streams, hasItem(expected));
  }
  @Test
  public void stopSession_should_changeRecordingState() throws Exception {
    // given
    sessionManager.startMobileSession(false);

    // when
    sessionManager.stopSession();

    // then
    org.fest.assertions.Assertions.assertThat(sessionManager.state.recording().isRecording())
        .isFalse();
  }
  @Test
  public void shouldCreateAStreamForEachSensor() {
    sessionManager.startMobileSession(false);

    triggerMeasurement();
    SensorEvent event =
        new SensorEvent("CERN", "LHC2", "Siggh boson", "SB", "number", "#", 1, 2, 3, 4, 5, 10);
    sessionManager.onEvent(event);

    MeasurementStream expected = event.stream();
    assertThat(sessionManager.getMeasurementStreams(), hasItem(expected));
  }
  @Test
  public void shouldDiscardASession() {
    sessionManager.startMobileSession(false);
    sessionManager.getSession().setId(1234);

    triggerMeasurement(13.5);
    sessionManager.discardSession();

    verify(sessionManager.audioReader, never()).stop();
    verify(sessionManager.locationHelper).stop();
    verify(sessionManager.sessionRepository, never()).save(Mockito.any(Session.class));
    assertThat(sessionManager.getMeasurementStreams().isEmpty(), equalTo(true));
    assertThat(sessionManager.isSessionStarted(), equalTo(false));
  }