Skip to content

Commit

Permalink
Merge pull request #26 from WaltonG/issue-25-write-tests-for-availabl…
Browse files Browse the repository at this point in the history
…e-functionalities

Write unit tests for available functionalities
  • Loading branch information
WaltonG authored Aug 9, 2022
2 parents fb56fb1 + 74e0b11 commit 57d2ab2
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 15 deletions.
14 changes: 0 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,4 @@
</dependency>
</dependencies>

<!-- enabled to access our snapshots of Wikidata-Toolkit -->
<repositories>
<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ static private void setProgress(ImportingJob job, String querySource, int percen
job.setProgress(percent, "Reading " + querySource);
}

public List<String> getColumns() {
return columns;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ static private void setProgress(ImportingJob job, String querySource, int percen
job.setProgress(percent, "Reading " + querySource);
}

public List<String> getColumns() {
return columns;
}

}
2 changes: 1 addition & 1 deletion src/test/conf/tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</run>
</groups>
<packages>
<package name="org.openrefine.extensions.sparql.*" />
<package name="org.openrefine.sparql.*" />
</packages>
</test>
</suite>
63 changes: 63 additions & 0 deletions src/test/java/org/openrefine/sparql/utils/RefineServletStub.java
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);
}
}
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");
}
}
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"));
}
}
}
Loading

0 comments on commit 57d2ab2

Please sign in to comment.