|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.nutch.indexer; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 25 | + |
| 26 | +/** |
| 27 | + * Abstract base for IndexWriter integration tests. Provides common test logic |
| 28 | + * for write/commit and delete operations. |
| 29 | + */ |
| 30 | +@Testcontainers(disabledWithoutDocker = true) |
| 31 | +public abstract class AbstractIndexWriterIT |
| 32 | + implements IndexWriterIntegrationTest { |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp() throws Exception { |
| 36 | + setUpIndexWriter(); |
| 37 | + } |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + void tearDown() throws Exception { |
| 41 | + tearDownIndexWriter(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testWriteAndCommitDocument() throws Exception { |
| 46 | + NutchDocument doc = createTestDocument("test-doc-1", "Test Document", |
| 47 | + "This is a test document for integration testing."); |
| 48 | + assertDoesNotThrow(() -> getIndexWriter().write(doc)); |
| 49 | + assertDoesNotThrow(() -> getIndexWriter().commit()); |
| 50 | + tearDownIndexWriter(); |
| 51 | + verifyDocumentWritten("test-doc-1", "Test Document"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testDeleteDocument() throws Exception { |
| 56 | + if (!supportsDelete()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + String docId = "test-doc-to-delete"; |
| 60 | + NutchDocument doc = createTestDocument(docId, "Document to Delete", ""); |
| 61 | + |
| 62 | + IndexWriter writer = getIndexWriter(); |
| 63 | + writer.write(doc); |
| 64 | + writer.commit(); |
| 65 | + |
| 66 | + IndexWriter deleteWriter = prepareWriterForDeleteTest(); |
| 67 | + if (deleteWriter == null) { |
| 68 | + deleteWriter = writer; |
| 69 | + } |
| 70 | + final IndexWriter writerForDelete = deleteWriter; |
| 71 | + assertDoesNotThrow(() -> writerForDelete.delete(docId)); |
| 72 | + assertDoesNotThrow(() -> writerForDelete.commit()); |
| 73 | + if (deleteWriter != writer) { |
| 74 | + try { |
| 75 | + deleteWriter.close(); |
| 76 | + } catch (Exception e) { |
| 77 | + // Ignore |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** Create a NutchDocument with id, title, and content. */ |
| 83 | + protected NutchDocument createTestDocument(String id, String title, |
| 84 | + String content) { |
| 85 | + NutchDocument doc = new NutchDocument(); |
| 86 | + doc.add("id", id); |
| 87 | + doc.add("title", title); |
| 88 | + doc.add("content", content); |
| 89 | + return doc; |
| 90 | + } |
| 91 | +} |
0 commit comments