-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from WaltonG/issue-25-write-tests-for-availabl…
…e-functionalities Write unit tests for available functionalities
- Loading branch information
Showing
8 changed files
with
479 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/test/java/org/openrefine/sparql/utils/RefineServletStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.openrefine.sparql.utils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import com.google.refine.RefineServlet; | ||
import com.google.refine.commands.Command; | ||
|
||
public class RefineServletStub extends RefineServlet { | ||
|
||
private static File tempDir = null; | ||
|
||
// requirement of extending HttpServlet, not required for testing | ||
private static final long serialVersionUID = 1L; | ||
|
||
public void wrapService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
super.service(request, response); | ||
} | ||
|
||
public String wrapGetCommandName(HttpServletRequest request) { | ||
return super.getCommandKey(request); | ||
} | ||
|
||
@Override | ||
public File getTempDir() { | ||
if (tempDir == null) { | ||
try { | ||
Path tempDirPath = Files.createTempDirectory("refine-test-dir"); | ||
tempDir = tempDirPath.toFile(); | ||
tempDir.deleteOnExit(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to create temp directory", e); | ||
} | ||
} | ||
return tempDir; | ||
} | ||
|
||
// -------------------helper methods-------------- | ||
/** | ||
* Helper method for inserting a mock object | ||
* | ||
* @param commandName | ||
* @param command | ||
*/ | ||
public void insertCommand(String commandName, Command command) { | ||
registerOneCommand("core/" + commandName, command); | ||
} | ||
|
||
/** | ||
* Helper method for clearing up after testing | ||
* | ||
* @param commandName | ||
*/ | ||
public void removeCommand(String commandName) { | ||
unregisterCommand("core/" + commandName); | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
src/test/java/org/openrefine/sparql/utils/SPARQLImportingControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
package org.openrefine.sparql.utils; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.openrefine.extensions.sparql.utils.SPARQLImportingController; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.refine.ProjectManager; | ||
import com.google.refine.ProjectMetadata; | ||
import com.google.refine.RefineServlet; | ||
import com.google.refine.importing.ImportingJob; | ||
import com.google.refine.importing.ImportingManager; | ||
import com.google.refine.io.FileProjectManager; | ||
import com.google.refine.model.ModelException; | ||
import com.google.refine.model.Project; | ||
import com.google.refine.util.ParsingUtilities; | ||
|
||
public class SPARQLImportingControllerTest { | ||
|
||
private static final String JSON_OPTION = "{\"mode\":\"row-based\"}}"; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Mock | ||
private HttpServletResponse response; | ||
|
||
// dependencies | ||
private Project project; | ||
private ProjectMetadata metadata; | ||
private ImportingJob job; | ||
private RefineServlet servlet; | ||
|
||
// System under test | ||
private SPARQLImportingController SUT = null; | ||
|
||
public static File createTempDirectory(String name) | ||
throws IOException { | ||
File dir = File.createTempFile(name, ""); | ||
dir.delete(); | ||
dir.mkdir(); | ||
return dir; | ||
} | ||
|
||
@BeforeMethod | ||
public void setUp() throws IOException, ModelException { | ||
|
||
MockitoAnnotations.initMocks(this); | ||
|
||
File dir = createTempDirectory("OR_SPARQLExtension_Test_WorkspaceDir"); | ||
FileProjectManager.initialize(dir); | ||
|
||
servlet = new RefineServlet(); | ||
ImportingManager.initialize(servlet); | ||
project = new Project(); | ||
metadata = new ProjectMetadata(); | ||
|
||
metadata.setName("SPARQL Import Test Project"); | ||
ProjectManager.singleton.registerProject(project, metadata); | ||
SUT = new SPARQLImportingController(); | ||
|
||
} | ||
|
||
@AfterMethod | ||
public void tearDown() { | ||
SUT = null; | ||
request = null; | ||
response = null; | ||
project = null; | ||
metadata = null; | ||
job = null; | ||
} | ||
|
||
@Test | ||
public void testDoGet() { | ||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
|
||
try { | ||
when(response.getWriter()).thenReturn(pw); | ||
|
||
SUT.doGet(request, response); | ||
|
||
String result = sw.getBuffer().toString().trim(); | ||
ObjectNode json = ParsingUtilities.mapper.readValue(result, ObjectNode.class); | ||
String code = json.get("status").asText(); | ||
String message = json.get("message").asText(); | ||
Assert.assertNotNull(code); | ||
Assert.assertNotNull(message); | ||
Assert.assertEquals(code, "error"); | ||
Assert.assertEquals(message, "GET not implemented"); | ||
|
||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDoPostInvalidSubCommand() throws IOException, ServletException { | ||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
when(request.getQueryString()).thenReturn( | ||
"http://127.0.0.1:3333/command/core/importing-controller?controller=sparql/sparql-import-controller&subCommand=invalid-sub-command"); | ||
|
||
when(response.getWriter()).thenReturn(pw); | ||
|
||
SUT.doPost(request, response); | ||
|
||
String result = sw.getBuffer().toString().trim(); | ||
ObjectNode json = ParsingUtilities.mapper.readValue(result, ObjectNode.class); | ||
|
||
String code = json.get("status").asText(); | ||
String message = json.get("message").asText(); | ||
Assert.assertNotNull(code); | ||
Assert.assertNotNull(message); | ||
Assert.assertEquals(code, "error"); | ||
Assert.assertEquals(message, "No such sub command"); | ||
} | ||
|
||
@Test | ||
public void testDoPostInitializeParser() throws ServletException, IOException { | ||
|
||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
|
||
when(request.getQueryString()).thenReturn( | ||
"http://127.0.0.1:3333/command/core/importing-controller?controller=sparql/sparql-import-controller&subCommand=initialize-parser-ui"); | ||
when(response.getWriter()).thenReturn(pw); | ||
|
||
SUT.doPost(request, response); | ||
|
||
String result = sw.getBuffer().toString().trim(); | ||
ObjectNode json = ParsingUtilities.mapper.readValue(result, ObjectNode.class); | ||
|
||
String status = json.get("status").asText(); | ||
Assert.assertEquals(status, "ok"); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/test/java/org/openrefine/sparql/utils/SPARQLQueryResultPreviewReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
|
||
package org.openrefine.sparql.utils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.openrefine.extensions.sparql.utils.SPARQLQueryResultPreviewReader; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.refine.ProjectManager; | ||
import com.google.refine.ProjectMetadata; | ||
import com.google.refine.RefineServlet; | ||
import com.google.refine.importing.ImportingJob; | ||
import com.google.refine.importing.ImportingManager; | ||
import com.google.refine.io.FileProjectManager; | ||
import com.google.refine.model.ModelException; | ||
import com.google.refine.model.Project; | ||
|
||
import okhttp3.HttpUrl; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
|
||
public class SPARQLQueryResultPreviewReaderTest { | ||
|
||
private static final String ENDPOINT = "wdq/sparql"; | ||
private static final String QUERY = "SELECT ?item ?itemLabel \n" | ||
+ "WHERE \n" | ||
+ "{\n" | ||
+ " ?item wdt:P31 wd:Q146. # Must be of a cat\n" | ||
+ " SERVICE wikibase:label { bd:serviceParam wikibase:language \"en,en\". } # Helps get the label in your language, if not, then en language\n" | ||
+ "}"; | ||
private static final int BATCH_SIZE = 100; | ||
|
||
// dependencies | ||
private Project project; | ||
private ProjectMetadata metadata; | ||
private ImportingJob job; | ||
private RefineServlet servlet; | ||
|
||
// System under test | ||
private SPARQLQueryResultPreviewReader SUT = null; | ||
|
||
public static File createTempDirectory(String name) | ||
throws IOException { | ||
File dir = File.createTempFile(name, ""); | ||
dir.delete(); | ||
dir.mkdir(); | ||
return dir; | ||
} | ||
|
||
@BeforeMethod | ||
public void setUp() throws IOException, ModelException { | ||
|
||
MockitoAnnotations.initMocks(this); | ||
|
||
File dir = createTempDirectory("OR_SPARQLExtension_Test_WorkspaceDir"); | ||
FileProjectManager.initialize(dir); | ||
|
||
servlet = new RefineServlet(); | ||
ImportingManager.initialize(servlet); | ||
project = new Project(); | ||
metadata = new ProjectMetadata(); | ||
job = Mockito.mock(ImportingJob.class); | ||
|
||
metadata.setName("SPARQL Import Test Project"); | ||
ProjectManager.singleton.registerProject(project, metadata); | ||
|
||
|
||
} | ||
|
||
@AfterMethod | ||
public void tearDown() { | ||
SUT = null; | ||
project = null; | ||
metadata = null; | ||
job = null; | ||
} | ||
|
||
@Test | ||
public void testGetResults() throws Exception { | ||
try (MockWebServer server = new MockWebServer()) { | ||
String jsonResponse = "{\"head\":{\"vars\":[\"item\",\"itemLabel\"]},\"results\":{\"bindings\"" | ||
+ ":[{\"item\":{\"type\":\"uri\",\"value\":\"http://www.wikidata.org/entity/Q378619\"},\"itemLabel\"" | ||
+ ":{\"xml:lang\":\"en\",\"type\":\"literal\",\"value\":\"CC\"}},{\"item\":{\"type\":\"uri\",\"value\"" | ||
+ ":\"http://www.wikidata.org/entity/Q498787\"},\"itemLabel\":{\"xml:lang\":\"en\",\"type\":\"literal\",\"value\":\"Muezza\"}}]}}"; | ||
server.enqueue(new MockResponse().setBody(jsonResponse)); | ||
server.start(); | ||
|
||
HttpUrl url = server.url(ENDPOINT); | ||
SUT = new SPARQLQueryResultPreviewReader(job, url.toString(), QUERY, BATCH_SIZE); | ||
|
||
Assert.assertEquals(SUT.getColumns(), Arrays.asList("item", "itemLabel")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetNextRowOfCells() throws Exception { | ||
try (MockWebServer server = new MockWebServer()) { | ||
String jsonResponse = "{\"head\":{\"vars\":[\"item\",\"itemLabel\"]},\"results\":{\"bindings\"" | ||
+ ":[{\"item\":{\"type\":\"uri\",\"value\":\"http://www.wikidata.org/entity/Q378619\"},\"itemLabel\"" | ||
+ ":{\"xml:lang\":\"en\",\"type\":\"literal\",\"value\":\"CC\"}},{\"item\":{\"type\":\"uri\",\"value\"" | ||
+ ":\"http://www.wikidata.org/entity/Q498787\"},\"itemLabel\":{\"xml:lang\":\"en\",\"type\":\"literal\",\"value\":\"Muezza\"}}]}}"; | ||
server.enqueue(new MockResponse().setBody(jsonResponse)); | ||
server.start(); | ||
|
||
HttpUrl url = server.url(ENDPOINT); | ||
SUT = new SPARQLQueryResultPreviewReader(job, url.toString(), QUERY, BATCH_SIZE); | ||
|
||
List<Object> currentRow = null; | ||
List<List<Object>> rows = new ArrayList<>(); | ||
while ((currentRow = SUT.getNextRowOfCells()) != null) { | ||
rows.add(currentRow); | ||
} | ||
|
||
Assert.assertEquals(rows.get(0), Arrays.asList("http://www.wikidata.org/entity/Q378619", "CC")); | ||
Assert.assertEquals(rows.get(1), Arrays.asList("http://www.wikidata.org/entity/Q498787", "Muezza")); | ||
} | ||
} | ||
} |
Oops, something went wrong.