|
| 1 | +package org.apache.zeppelin.search; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.apache.zeppelin.notebook.Note; |
| 10 | +import org.apache.zeppelin.notebook.Paragraph; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class SearchServiceTest { |
| 15 | + |
| 16 | + SearchService notebookIndex; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void startUp() { |
| 20 | + notebookIndex = new SearchService(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test public void canIndexNotebook() { |
| 24 | + //give |
| 25 | + Note note1 = newNoteWithParapgraph("Notebook1", "test"); |
| 26 | + Note note2 = newNoteWithParapgraph("Notebook2", "not test"); |
| 27 | + List<Note> notebook = Arrays.asList(note1, note2); |
| 28 | + |
| 29 | + //when |
| 30 | + notebookIndex.index(notebook); |
| 31 | + } |
| 32 | + |
| 33 | + @Test public void canIndexAndQuery() { |
| 34 | + //given |
| 35 | + Note note1 = newNoteWithParapgraph("Notebook1", "test"); |
| 36 | + Note note2 = newNoteWithParapgraphs("Notebook2", "not test", "not test at all"); |
| 37 | + notebookIndex.index(Arrays.asList(note1, note2)); |
| 38 | + |
| 39 | + //when |
| 40 | + List<Map<String, String>> results = notebookIndex.search("all"); |
| 41 | + |
| 42 | + //then |
| 43 | + assertThat(results).isNotEmpty(); |
| 44 | + assertThat(results.size()).isEqualTo(1); |
| 45 | + assertThat(results.get(0)).containsEntry("id", |
| 46 | + String.format("%s/paragraph/%s", note2.getId(), note2.getLastParagraph().getId())); |
| 47 | + } |
| 48 | + |
| 49 | + @Test(expected=IllegalStateException.class) |
| 50 | + public void canNotSearchBeforeIndexing() { |
| 51 | + //given no notebookIndex.index() was made |
| 52 | + //when |
| 53 | + notebookIndex.search("anything"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new Note \w given name, |
| 58 | + * adds a new paragraph \w given text |
| 59 | + * |
| 60 | + * @param noteName name of the note |
| 61 | + * @param parText text of the paragraph |
| 62 | + * @return Note |
| 63 | + */ |
| 64 | + private Note newNoteWithParapgraph(String noteName, String parText) { |
| 65 | + Note note1 = newNote(noteName); |
| 66 | + addParagraphWithText(note1, parText); |
| 67 | + return note1; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a new Note \w given name, |
| 72 | + * adds N paragraphs \w given texts |
| 73 | + */ |
| 74 | + private Note newNoteWithParapgraphs(String noteName, String... parTexts) { |
| 75 | + Note note1 = newNote(noteName); |
| 76 | + for (String parText : parTexts) { |
| 77 | + addParagraphWithText(note1, parText); |
| 78 | + } |
| 79 | + return note1; |
| 80 | + } |
| 81 | + |
| 82 | + private Paragraph addParagraphWithText(Note note, String text) { |
| 83 | + Paragraph p = note.addParagraph(); |
| 84 | + p.setText(text); |
| 85 | + return p; |
| 86 | + } |
| 87 | + |
| 88 | + private Note newNote(String name) { |
| 89 | + Note note = new Note(null, null, null); |
| 90 | + note.setName(name); |
| 91 | + return note; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments