Skip to content

Commit

Permalink
[#219] Refactor all Handlers to derive from BaseRDFNodeHandler. Imple…
Browse files Browse the repository at this point in the history
…ment tests.
  • Loading branch information
palagdan committed Aug 18, 2024
1 parent 1026885 commit 9772b5d
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@


/**
* Abstract Handler for cases when native Java value can be extracted directly from a single RDFNode.
* An abstract base class for handling RDF nodes and converting them to a specific type.
* This class provides a common implementation for handling RDF nodes and setting their values
* to fields of a specified type.
*
* @param <T> The type of the value that this handler converts RDF nodes to.
*/
abstract public class BaseRdfNodeHandler<T> extends Handler<T> {
abstract public class BaseRDFNodeHandler<T> extends Handler<T> {

public BaseRdfNodeHandler(Resource resource, ExecutionContext executionContext, Setter<? super T> setter) {
public BaseRDFNodeHandler(Resource resource, ExecutionContext executionContext, Setter<? super T> setter) {
super(resource, executionContext, setter);
}

/**
* Constructs the native Java value from a given RDFNode.
* Converts an RDF node to a value of type {@code T}.
* This method must be implemented by subclasses to provide the specific conversion logic.
*
* @param node the RDFNode to extract the native Java value from
* @return the native Java value of the RDFNode
* @param node The RDF node to convert.
* @return The converted value of type {@code T}.
*/
abstract T getJavaNativeValue(RDFNode node);
abstract T getRDFNodeValue(RDFNode node) throws Exception;

/**
* Sets a value to the field associated with this handler by converting an RDF node
* retrieved from the specified RDF property.
*
* @param property The RDF property whose value is used to retrieve the RDF node.
* @throws ScriptRuntimeErrorException If an error occurs during the conversion or setting process.
*/
@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);

if (node != null) {
try {
setter.addValue(getJavaNativeValue(node));
} catch (RuntimeException ex) {
setter.addValue(getRDFNodeValue(node));
} catch (Exception ex) {
throw new ScriptRuntimeErrorException(
String.format("""
Failed to set value of the field `%s` of type `%s` within class `%s`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import org.jetbrains.annotations.NotNull;


public class BooleanHandler extends BaseRdfNodeHandler<Boolean> {
public class BooleanHandler extends BaseRDFNodeHandler<Boolean> {
public BooleanHandler(Resource resource, ExecutionContext executionContext, Setter<? super Boolean> setter) {
super(resource, executionContext, setter);
}

@Override
Boolean getJavaNativeValue(@NotNull RDFNode node) {
Boolean getRDFNodeValue(@NotNull RDFNode node) {
return node.asLiteral().getBoolean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;

public class IntegerHandler extends Handler<Integer>{
public class IntegerHandler extends BaseRDFNodeHandler<Integer>{

public IntegerHandler(Resource resource, ExecutionContext executionContext, Setter<? super Integer> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);
if (node != null && node.isLiteral()) {
setter.addValue(node.asLiteral().getInt());
}
Integer getRDFNodeValue(RDFNode node) {
return node.asLiteral().getInt();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
import java.nio.file.Paths;


public class PathHandler extends Handler<Path> {
public class PathHandler extends BaseRDFNodeHandler<Path> {
public PathHandler(Resource resource, ExecutionContext executionContext, Setter<? super Path> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode pathNode = getEffectiveValue(property);
if(pathNode != null) {
Path path = Paths.get(pathNode.toString());
setter.addValue(path);
}
Path getRDFNodeValue(RDFNode node) {
return Paths.get(node.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;

public class RDFNodeHandler extends Handler<RDFNode> {
public class RDFNodeHandler extends BaseRDFNodeHandler<RDFNode> {
public RDFNodeHandler(Resource resource, ExecutionContext executionContext, Setter<? super RDFNode> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);
if(node != null) {
setter.addValue(node);
}
RDFNode getRDFNodeValue(RDFNode node) {
return node;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;

public class ResourceHandler extends Handler<Resource> {
public class ResourceHandler extends BaseRDFNodeHandler<Resource> {
public ResourceHandler(Resource resource, ExecutionContext executionContext, Setter<? super Resource> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);
if(node != null && node.isResource()) {
setter.addValue(node.asResource());
}
Resource getRDFNodeValue(RDFNode node) {
return node.asResource();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
import org.apache.jena.rdf.model.Statement;
import org.topbraid.spin.model.Select;

public class SelectHandler extends Handler<Select> {
public class SelectHandler extends BaseRDFNodeHandler<Select> {

public SelectHandler(Resource resource, ExecutionContext executionContext, Setter<? super Select> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);
if (node != null) {
setter.addValue(node.asResource().as(Select.class));
}
Select getRDFNodeValue(RDFNode node) {
return node.asResource().as(Select.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;

public class StringHandler extends Handler<String> {
public class StringHandler extends BaseRDFNodeHandler<String> {


public StringHandler(Resource resource, ExecutionContext executionContext, Setter<? super String> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);
if (node != null) {
setter.addValue(node.toString());
}
String getRDFNodeValue(RDFNode node) {
return node.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@
import java.net.MalformedURLException;
import java.net.URL;

public class URLHandler extends Handler<URL>{
public class URLHandler extends BaseRDFNodeHandler<URL>{
public URLHandler(Resource resource, ExecutionContext executionContext, Setter<? super URL> setter) {
super(resource, executionContext, setter);
}

@Override
public void setValueByProperty(Property property) {
URL url;
RDFNode urlNode = getEffectiveValue(property);

if (urlNode != null) {
try {
url = new URL(urlNode.toString());
setter.addValue(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
URL getRDFNodeValue(RDFNode node) throws MalformedURLException {
return new URL(node.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,73 +1,43 @@
package cz.cvut.spipes.modules.handlers;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import cz.cvut.spipes.engine.ExecutionContext;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Property;
import cz.cvut.spipes.exception.ScriptRuntimeErrorException;
import org.apache.jena.rdf.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class BooleanHandlerTest {

private BooleanHandler booleanHandler;
private Resource resource;
private ExecutionContext executionContext;
private Setter<Boolean> setter;
private RDFNode rdfNode;
private Resource mockResource;
private ExecutionContext mockExecutionContext;
private Setter<Boolean> mockSetter;

@BeforeEach
public void setUp() {
resource = mock(Resource.class);
executionContext = mock(ExecutionContext.class);
setter = mock(Setter.class);
booleanHandler = new BooleanHandler(resource, executionContext, setter);
void setUp() {
mockResource = mock(Resource.class);
mockExecutionContext = mock(ExecutionContext.class);
mockSetter = mock(Setter.class);
booleanHandler = new BooleanHandler(mockResource, mockExecutionContext, mockSetter);
}

@Test
public void testGetJavaNativeValueTrue() {
Literal literal = mock(Literal.class);
when(literal.getBoolean()).thenReturn(true);
rdfNode = mock(RDFNode.class);
when(rdfNode.asLiteral()).thenReturn(literal);

Boolean result = booleanHandler.getJavaNativeValue(rdfNode);
void testGetRDFNodeValueTrue() {
RDFNode rdfNode = ResourceFactory.createTypedLiteral(true);
Boolean result = booleanHandler.getRDFNodeValue(rdfNode);
assertTrue(result);
}

@Test
public void testGetJavaNativeValueFalse() {
Literal literal = mock(Literal.class);
when(literal.getBoolean()).thenReturn(false);
rdfNode = mock(RDFNode.class);
when(rdfNode.asLiteral()).thenReturn(literal);

Boolean result = booleanHandler.getJavaNativeValue(rdfNode);
void testGetRDFNodeValueFalse() {
RDFNode rdfNode = ResourceFactory.createTypedLiteral(false);
Boolean result = booleanHandler.getRDFNodeValue(rdfNode);
assertFalse(result);
}

@Test
public void testGetJavaNativeValueInvalidType() {
Literal literal = mock(Literal.class);
when(literal.getBoolean()).thenThrow(new ClassCastException("Not a boolean"));
rdfNode = mock(RDFNode.class);
when(rdfNode.asLiteral()).thenReturn(literal);

assertThrows(ClassCastException.class, () -> booleanHandler.getJavaNativeValue(rdfNode));
}

@Test
public void testSetValueByPropertyNullNode() {
Property property = mock(Property.class);
when(booleanHandler.getEffectiveValue(property)).thenReturn(null);

booleanHandler.setValueByProperty(property);

verify(setter, never()).addValue(anyBoolean());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.apache.jena.rdf.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class IntegerHandlerTest {
Expand All @@ -12,53 +14,21 @@ public class IntegerHandlerTest {
private Resource mockResource;
private ExecutionContext mockExecutionContext;
private Setter<Integer> mockSetter;
private Property mockProperty;
private Model model;

@BeforeEach
public void setUp() {
void setUp() {
mockResource = mock(Resource.class);
mockExecutionContext = mock(ExecutionContext.class);
mockSetter = mock(Setter.class);
mockProperty = mock(Property.class);
model = ModelFactory.createDefaultModel();

integerHandler = new IntegerHandler(mockResource, mockExecutionContext, mockSetter);
}

@Test
public void testSetValueByPropertyWithIntegerLiteral() {
RDFNode integerLiteralNode = model.createLiteral(String.valueOf(42));
Statement mockStatement = mock(Statement.class);

when(mockResource.getProperty(mockProperty)).thenReturn(mockStatement);
when(mockStatement.getObject()).thenReturn(integerLiteralNode);

integerHandler.setValueByProperty(mockProperty);

verify(mockSetter).addValue(42);
}

@Test
public void testSetValueByPropertyWithNonLiteralNode() {
RDFNode nonLiteralNode = model.createResource();
Statement mockStatement = mock(Statement.class);

when(mockResource.getProperty(mockProperty)).thenReturn(mockStatement);
when(mockStatement.getObject()).thenReturn(nonLiteralNode);

integerHandler.setValueByProperty(mockProperty);

verify(mockSetter, never()).addValue(anyInt());
}

@Test
public void testSetValueByPropertyWithNullNode() {

when(mockResource.getProperty(mockProperty)).thenReturn(null);
void testGetRDFNodeValue() {
RDFNode rdfNode = ResourceFactory.createTypedLiteral(42);

integerHandler.setValueByProperty(mockProperty);
Integer result = integerHandler.getRDFNodeValue(rdfNode);

verify(mockSetter, never()).addValue(anyInt());
assertEquals(Integer.valueOf(42), result);
}
}
Loading

0 comments on commit 9772b5d

Please sign in to comment.