| margaretha | 05a4bc1 | 2022-02-11 10:55:43 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.cache; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertThrows; |
| 5 | |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStream; |
| 8 | |
| 9 | import org.apache.commons.io.IOUtils; |
| 10 | import org.junit.Test; |
| 11 | |
| 12 | import de.ids_mannheim.korap.Krill; |
| 13 | import de.ids_mannheim.korap.KrillIndex; |
| 14 | import de.ids_mannheim.korap.response.Message; |
| 15 | import de.ids_mannheim.korap.response.Result; |
| 16 | import de.ids_mannheim.korap.util.StatusCodes; |
| 17 | |
| 18 | public class TestInvalidVcId { |
| 19 | |
| 20 | private KrillIndex ki; |
| 21 | |
| 22 | public TestInvalidVcId () throws IOException { |
| 23 | ki = TestVirtualCorpusCache.createIndex(); |
| 24 | } |
| 25 | |
| 26 | @Test |
| 27 | public void testStoreVcNotExist () { |
| 28 | String vcId = "snx"; |
| 29 | RuntimeException ex = assertThrows(RuntimeException.class, () -> { |
| 30 | VirtualCorpusCache.store(vcId, ki); |
| 31 | }); |
| 32 | assertEquals("de.ids_mannheim.korap.util.QueryException: " |
| 33 | + "Collection is not found queries/collections/named-vcs/snx.jsonld", |
| 34 | ex.getMessage()); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | @Test |
| 39 | public void testReferVcNotExist () throws IOException { |
| 40 | String file = "/queries/collections/vc-ref/query-with-unknown-vc.jsonld"; |
| 41 | InputStream is = getClass().getResourceAsStream(file); |
| 42 | String queryRefJson = IOUtils.toString(is, "utf-8"); |
| 43 | |
| 44 | Krill krill = new Krill(queryRefJson); |
| 45 | Result result = krill.apply(ki); |
| 46 | Message m = result.getError(0); |
| 47 | assertEquals(StatusCodes.MISSING_COLLECTION, m.getCode()); |
| 48 | assertEquals( |
| 49 | "Collection is not found queries/collections/named-vcs/unknown-vc.jsonld", |
| 50 | m.getMessage()); |
| 51 | assertEquals(0, result.getTotalResults()); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | @Test |
| 56 | public void testDeleteVcNotExist () throws IOException { |
| 57 | VirtualCorpusCache.delete("unknown-vc-id"); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | @Test |
| 62 | public void testStoreVcInvalidChars () { |
| 63 | String vcId = "inval!d-vc-id"; |
| 64 | IllegalArgumentException ex = assertThrows( |
| 65 | IllegalArgumentException.class, () -> { |
| 66 | VirtualCorpusCache.store(vcId, ki); |
| 67 | }); |
| 68 | assertEquals("Cannot cache VC due to invalid VC ID", ex.getMessage()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testStoreVcInvalidParentPath () { |
| 73 | String vcId = ".."; |
| 74 | IllegalArgumentException ex = assertThrows( |
| 75 | IllegalArgumentException.class, () -> { |
| 76 | VirtualCorpusCache.store(vcId, ki); |
| 77 | }); |
| 78 | assertEquals("Cannot cache VC due to invalid VC ID", ex.getMessage()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testStoreVcInvalidNonASCII () { |
| 83 | String vcId = "aßäüö"; |
| 84 | IllegalArgumentException ex = assertThrows( |
| 85 | IllegalArgumentException.class, () -> { |
| 86 | VirtualCorpusCache.store(vcId, ki); |
| 87 | }); |
| 88 | assertEquals("Cannot cache VC due to invalid VC ID", ex.getMessage()); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testReferVcInvalidChars () throws IOException { |
| 93 | String file = "/queries/collections/vc-ref/query-with-invalid-vc.jsonld"; |
| 94 | InputStream is = getClass().getResourceAsStream(file); |
| 95 | String queryRefJson = IOUtils.toString(is, "utf-8"); |
| 96 | |
| 97 | Krill krill = new Krill(queryRefJson); |
| 98 | Result result = krill.apply(ki); |
| 99 | Message m = result.getError(0); |
| 100 | assertEquals("Cannot cache VC due to invalid VC ID", m.getMessage()); |
| 101 | assertEquals(104, m.getCode()); |
| 102 | } |
| 103 | } |