EventNoteValidationHook extends AbstractTrackerDtoValidationHook { @Override public void validateEvent( ValidationErrorReporter reporter, Event event ) { event.setNotes( NoteValidationUtils.getPersistableNotes( commentService, event.getNotes() ) ); } EventNoteValidationHook( TrackedEntityAttributeService teAttrService,
        TrackedEntityCommentService commentService ); @Override void validateEvent( ValidationErrorReporter reporter, Event event );  }
@Test public void verifyAllNotesArePersistedWhenNotesAreNotOnDatabase() { final List<Note> notes = rnd.randomObjects( Note.class, 5, "newNote" ); final List<String> notesUid = notes.stream().map( Note::getNote ).collect( Collectors.toList() ); event.setNotes( notes ); when( commentService.filterExistingNotes( notesUid ) ).thenReturn( notesUid ); this.hook.validateEvent( mock( ValidationErrorReporter.class ), event ); assertThat( event.getNotes(), hasSize( 5 ) ); }
@Test public void verifyAllNewNotesArePersisted() { final List<Note> notes = rnd.randomObjects( Note.class, 5 ); makeAllNotesNew( notes ); final List<String> notesUid = notes.stream().map( Note::getNote ).collect( Collectors.toList() ); event.setNotes( notes ); when( commentService.filterExistingNotes( notesUid ) ).thenReturn( new ArrayList<>() ); this.hook.validateEvent( mock( ValidationErrorReporter.class ), event ); assertThat( event.getNotes(), hasSize( 5 ) ); }
@Test public void verifyOnlyNonExistingNoteArePersisted() { final List<Note> notes = rnd.randomObjects( Note.class, 3, "newNote" ); final List<Note> existingNotes = rnd.randomObjects( Note.class, 2, "newNote" ); event.setNotes( newArrayList( concat( notes, existingNotes ) ) ); when( commentService.filterExistingNotes( anyList() ) ) .thenReturn( notes.stream().map( Note::getNote ).collect( Collectors.toList() ) ); this.hook.validateEvent( mock( ValidationErrorReporter.class ), event ); assertThat( event.getNotes(), hasSize( 3 ) ); }
@Test public void verifyOnlyNonExistingAndNonEmptyNoteArePersisted() { ArgumentCaptor<List<String>> argument = ArgumentCaptor.forClass( List.class ); final List<Note> notes = rnd.randomObjects( Note.class, 5, "newNote" ); final List<Note> emptyNotes = rnd.randomObjects( Note.class, 3, "newNote", "value" ); final List<Note> existingNotes = rnd.randomObjects( Note.class, 2, "newNote" ); event.setNotes( newArrayList( concat( notes, emptyNotes, existingNotes ) ) ); when( commentService.filterExistingNotes( argument.capture() ) ).thenReturn( notes.stream().map( Note::getNote ).collect( Collectors.toList() ) ); this.hook.validateEvent( mock( ValidationErrorReporter.class ), event ); assertThat( event.getNotes(), hasSize( 5 ) ); List<String> value = argument.getValue(); assertThat( value, containsInAnyOrder( newArrayList( concat( notes, existingNotes ) ).stream() .map( Note::getNote ).toArray() ) ); }