MobiPIP implements PIP { @Override public List<Literal> findAttribute(AttributeDesignator attributeDesignator, Request request) throws MissingAttributeException, ProcessingException { IRI attributeId = attributeDesignator.attributeId(); IRI category = attributeDesignator.category(); IRI pathSource; if (category.equals(request.getSubjectCategory())) { pathSource = request.getSubjectId(); } else if (category.equals(request.getResourceCategory())) { pathSource = request.getResourceId(); } else { return Collections.emptyList(); } try (RepositoryConnection conn = repo.getConnection()) { if (attributeId.stringValue().startsWith(PROP_PATH_NAMESPACE)) { int firstIdx = attributeId.stringValue().lastIndexOf("(") + 1; int lastIdx = attributeId.stringValue().lastIndexOf(")"); String path = decode(attributeId.stringValue().substring(firstIdx, lastIdx)); TupleQuery query = conn.prepareTupleQuery(String.format(PROP_PATH_QUERY, path)); query.setBinding("sub", pathSource); return StreamSupport.stream(query.evaluate().spliterator(), false) .map(bindings -> bindings.getBinding("value").get().getValue()) .map(value -> { if (Literal.class.isAssignableFrom(value.getClass())) { return (Literal) value; } else { return vf.createLiteral(value.stringValue()); } }) .collect(Collectors.toList()); } else { return StreamSupport.stream(conn.getStatements(pathSource, attributeId, null).spliterator(), false) .map(Statement::getObject) .map(value -> value instanceof Literal ? (Literal) value : vf.createLiteral(value.stringValue())) .collect(Collectors.toList()); } } }  @Reference(target = "(id=system)") void setRepo(Repository repo); @Reference void setVf(ValueFactory vf); @Override List<Literal> findAttribute(AttributeDesignator attributeDesignator, Request request);  }
@Test public void findAttributeWithUnsupportedCategoryTest() { when(designator.category()).thenReturn(actionId); List<Literal> result = pip.findAttribute(designator, request); assertEquals(0, result.size()); }
@Test public void findAttributeSubjectDirectTest() throws Exception { List<Literal> result = pip.findAttribute(designator, request); assertEquals(1, result.size()); Literal value = result.get(0); assertEquals(VALUE_FACTORY.createIRI(XSD.BOOLEAN), value.getDatatype()); assertEquals(true, value.booleanValue()); }
@Test public void findAttributeSubjectPathTest() throws Exception { when(designator.attributeId()).thenReturn(pathId); List<Literal> result = pip.findAttribute(designator, request); assertEquals(1, result.size()); assertEquals("Title", result.get(0).stringValue()); }
@Test public void findAttributeSubjectMissingPropTest() throws Exception { when(designator.attributeId()).thenReturn(titleIRI); List<Literal> result = pip.findAttribute(designator, request); assertEquals(0, result.size()); }
@Test public void findAttributeResourceDirectTest() throws Exception { when(designator.category()).thenReturn(resourceCategory); List<Literal> result = pip.findAttribute(designator, request); assertEquals(2, result.size()); result.forEach(literal -> { assertEquals(VALUE_FACTORY.createIRI(XSD.INT), literal.getDatatype()); assertTrue(Arrays.asList(1, 2).contains(literal.intValue())); }); }
@Test public void findAttributeResourcePathTest() throws Exception { when(designator.category()).thenReturn(resourceCategory); when(designator.attributeId()).thenReturn(pathId); List<Literal> result = pip.findAttribute(designator, request); assertEquals(1, result.size()); assertEquals("Title", result.get(0).stringValue()); }
@Test public void findAttributeResourceMissingPropTest() throws Exception { when(designator.attributeId()).thenReturn(titleIRI); List<Literal> result = pip.findAttribute(designator, request); assertEquals(0, result.size()); }