FieldResultMapper { void map(ResultRow resultRow, Object target, UnitOfWork uow) { final Optional<Object> value = getVariableValue(resultRow); value.ifPresent( val -> EntityPropertiesUtils.setFieldValue(fieldSpec.getJavaField(), target, resolveValue(val))); } FieldResultMapper(FieldResult fieldResult, FieldSpecification<?, ?> fieldSpec);   FieldResultMapper(FieldSpecification<?, ?> fieldSpec);   }
@Test void mapThrowsResultMappingExceptionWhenValueCannotBeAssignedToField() throws Exception { final FieldResult fieldResult = WithMapping.getFieldResult(); final FieldSpecification fsMock = mock(FieldSpecification.class); when(fsMock.getJavaType()).thenReturn(Boolean.class); when(fsMock.getJavaField()).thenReturn(OWLClassM.getBooleanAttributeField()); when(resultRow.isBound(fieldResult.variable())).thenReturn(true); when(resultRow.getObject(fieldResult.variable())).thenReturn(117); final FieldResultMapper mapper = new FieldResultMapper(fieldResult, fsMock); final OWLClassM target = new OWLClassM(); final SparqlResultMappingException result = assertThrows(SparqlResultMappingException.class, () -> mapper.map(resultRow, target, uowMock)); assertThat(result.getMessage(), containsString( "Value " + resultRow .getObject(fieldResult.variable()) + " cannot be assigned (or transformed) to field of type " + fsMock.getJavaType())); assertNull(target.getBooleanAttribute()); }
@Test void mapExtractsValueFromResultSetAndSetsItOnTargetObjectField() throws Exception { final FieldResult fieldResult = WithMapping.getFieldResult(); final FieldSpecification fsMock = mock(FieldSpecification.class); when(fsMock.getJavaType()).thenReturn(String.class); when(fsMock.getJavaField()).thenReturn(OWLClassA.getStrAttField()); final String value = "stringValue"; when(resultRow.isBound(fieldResult.variable())).thenReturn(true); when(resultRow.getObject(fieldResult.variable())).thenReturn(value); final OWLClassA target = new OWLClassA(); final FieldResultMapper mapper = new FieldResultMapper(fieldResult, fsMock); mapper.map(resultRow, target, uowMock); verify(resultRow).getObject(fieldResult.variable()); assertEquals(value, target.getStringAttribute()); }
@Test void mapSkipsVariablesNotPresentInResultSet() throws Exception { final FieldResult fieldResult = WithMapping.getFieldResult(); final FieldSpecification fsMock = mock(FieldSpecification.class); when(fsMock.getJavaType()).thenReturn(Boolean.class); final OntoDriverException e = new OntoDriverException( "Result set does not contain column " + fieldResult.variable() + "."); when(resultRow.getObject(fieldResult.variable())).thenThrow(e); final FieldResultMapper mapper = new FieldResultMapper(fieldResult, fsMock); final OWLClassM target = new OWLClassM(); mapper.map(resultRow, target, uowMock); assertNull(target.getBooleanAttribute()); }
@Test void mapSkipsFieldForWhichVariableHasNoBindingInCurrentResultRow() throws Exception { final FieldResult fieldResult = WithMapping.getFieldResult(); final FieldSpecification fsMock = mock(FieldSpecification.class); when(fsMock.getJavaType()).thenReturn(Boolean.class); when(resultRow.getObject(fieldResult.variable())).thenReturn(null); final FieldResultMapper mapper = new FieldResultMapper(fieldResult, fsMock); final OWLClassM target = new OWLClassM(); mapper.map(resultRow, target, uowMock); assertNull(target.getBooleanAttribute()); }
@Test void mapSupportsTransformationFromLangStringToStringField() throws Exception { final FieldResult fieldResult = WithMapping.getFieldResult(); final FieldSpecification fsMock = mock(FieldSpecification.class); when(fsMock.getJavaType()).thenReturn(String.class); when(fsMock.getJavaField()).thenReturn(OWLClassA.getStrAttField()); final LangString value = new LangString("test", "en"); when(resultRow.isBound(fieldResult.variable())).thenReturn(true); when(resultRow.getObject(fieldResult.variable())).thenReturn(value); final FieldResultMapper mapper = new FieldResultMapper(fieldResult, fsMock); final OWLClassA target = new OWLClassA(); mapper.map(resultRow, target, uowMock); assertEquals(value.getValue(), target.getStringAttribute()); }