VariableResultMapper implements SparqlResultMapper { @Override public Object map(ResultRow resultRow, UnitOfWorkImpl uow) { try { if (!resultRow.isBound(name)) { return null; } final Object value = resultRow.getObject(name); if (!void.class.equals(targetType)) { return DatatypeTransformer.transform(value, targetType); } return value; } catch (OntoDriverException e) { throw new SparqlResultMappingException(e); } } VariableResultMapper(VariableResult mapping); @Override Object map(ResultRow resultRow, UnitOfWorkImpl uow);  }
@Test void mapReadsValueFromResultSetByVariableNameAndReturnsIt() throws OntoDriverException { final Integer value = 117; when(resultRow.isBound(NAME)).thenReturn(true); when(resultRow.getObject(NAME)).thenReturn(value); final VariableResultMapper mapper = new VariableResultMapper(NoType.getVariableMapping()); final Object result = mapper.map(resultRow, uowMock); assertEquals(value, result); verify(resultRow).getObject(NAME); }
@Test void mapCastsValueToTargetTypeWhenPossible() throws OntoDriverException { final Integer value = 117; when(resultRow.isBound(NAME)).thenReturn(true); when(resultRow.getObject(NAME)).thenReturn(value); final VariableResultMapper mapper = new VariableResultMapper(WithTypeCast.getVariableMapping()); final Object result = mapper.map(resultRow, uowMock); assertTrue(result instanceof Number); assertEquals(value, result); }
@Test void mapTransformsValueToTargetType() throws OntoDriverException { final String value = "http: when(resultRow.isBound(NAME)).thenReturn(true); when(resultRow.getObject(NAME)).thenReturn(value); final VariableResultMapper mapper = new VariableResultMapper(WithTypeTransform.getVariableMapping()); final Object result = mapper.map(resultRow, uowMock); assertTrue(result instanceof URI); assertEquals(URI.create(value), result); }
@Test void mapReturnsNullWhenVariableIsNotBoundInResultSetButIsNotRequired() throws Exception { when(resultRow.isBound(NAME)).thenReturn(false); when(resultRow.getObject(NAME)).thenThrow(new OntoDriverException("Error")); final VariableResultMapper mapper = new VariableResultMapper(WithTypeTransform.getVariableMapping()); final Object result = mapper.map(resultRow, uowMock); assertNull(result); }
@Test void mapTransformsLangStringToString() throws OntoDriverException { final LangString value = new LangString("test", "en"); when(resultRow.isBound(NAME)).thenReturn(true); when(resultRow.getObject(NAME)).thenReturn(value); final VariableResultMapper mapper = new VariableResultMapper(WithStringMapping.getVariableMapping()); final Object result = mapper.map(resultRow, uowMock); assertEquals(value.getValue(), result); }