|
| 1 | +package org.openrefine.extensions.commons.importer; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.mockito.Mockito; |
| 7 | +import org.testng.Assert; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 11 | +import com.google.refine.ProjectMetadata; |
| 12 | +import com.google.refine.RefineServlet; |
| 13 | +import com.google.refine.importing.ImportingJob; |
| 14 | +import com.google.refine.importing.ImportingManager; |
| 15 | +import com.google.refine.model.Cell; |
| 16 | +import com.google.refine.model.Project; |
| 17 | +import com.google.refine.util.ParsingUtilities; |
| 18 | + |
| 19 | +import okhttp3.HttpUrl; |
| 20 | +import okhttp3.mockwebserver.MockResponse; |
| 21 | +import okhttp3.mockwebserver.MockWebServer; |
| 22 | + |
| 23 | +public class CommonsImporterTest { |
| 24 | + |
| 25 | + protected RefineServlet servlet; |
| 26 | + |
| 27 | + // dependencies |
| 28 | + private Project project; |
| 29 | + private ProjectMetadata metadata; |
| 30 | + private ImportingJob job; |
| 31 | + |
| 32 | + /** |
| 33 | + * Test column names upon project creation as well as reconciled cells |
| 34 | + */ |
| 35 | + @Test |
| 36 | + public void testParse() throws Exception { |
| 37 | + |
| 38 | + try (MockWebServer server = new MockWebServer()) { |
| 39 | + server.start(); |
| 40 | + HttpUrl url = server.url("/w/api.php"); |
| 41 | + String jsonResponse = "{\"batchcomplete\":\"\",\"query\":{\"categorymembers\":" |
| 42 | + + "[{\"pageid\":127722,\"ns\":6,\"title\":\"File:3 Puppies.jpg\",\"type\":\"file\"}]}}"; |
| 43 | + server.enqueue(new MockResponse().setBody(jsonResponse)); |
| 44 | + servlet = new RefineServlet(); |
| 45 | + ImportingManager.initialize(servlet); |
| 46 | + project = new Project(); |
| 47 | + metadata = new ProjectMetadata(); |
| 48 | + metadata.setName("Commons Import Test Project"); |
| 49 | + job = Mockito.mock(ImportingJob.class); |
| 50 | + ObjectNode options = ParsingUtilities.evaluateJsonStringToObjectNode( |
| 51 | + "{\"categoryJsonValue\":[{\"category\":\"Category:Costa Rica\",\"depth\":\"0\"}],\"skipDataLines\":0," |
| 52 | + + "\"limit\":-1,\"disableAutoPreview\":false,\"categoriesColumn\":true,\"mIdsColumn\":true}"); |
| 53 | + List<Exception> exceptions = new ArrayList<Exception>(); |
| 54 | + CommonsImporter importer = new CommonsImporter(); |
| 55 | + |
| 56 | + importer.setApiUrl(url.toString()); |
| 57 | + CommonsImporter.parse(project, metadata, job, 0, options, exceptions); |
| 58 | + project.update(); |
| 59 | + Cell cell = project.rows.get(0).cells.get(0); |
| 60 | + |
| 61 | + Assert.assertEquals(project.columnModel.columns.get(0).getName(), "File"); |
| 62 | + Assert.assertEquals(project.columnModel.columns.get(1).getName(), "M-ids"); |
| 63 | + Assert.assertEquals(project.columnModel.columns.get(2).getName(), "Categories"); |
| 64 | + Assert.assertEquals(cell.recon.match.id, "M127722"); |
| 65 | + Assert.assertEquals(cell.recon.match.name, "File:3 Puppies.jpg"); |
| 66 | + |
| 67 | + server.close(); |
| 68 | + |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments