Skip to content

Commit

Permalink
[Fix] Handle target objects without identifier attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jul 17, 2020
1 parent 6d23c84 commit ff3d820
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.deserialization;

import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.deserialization.util.DataTypeTransformer;
import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;

import java.lang.reflect.Field;
import java.util.Map;
Expand Down Expand Up @@ -56,8 +55,15 @@ Class<T> getInstanceType() {
*/
void setIdentifierValue(String value) {
final Field idField = getFieldForProperty(JsonLd.ID);
assert idField != null;
if (isBlankNodeIdentifier(value) && !idField.getType().equals(String.class)) {
final boolean blankNode = isBlankNodeIdentifier(value);
if (idField == null) {
if (blankNode) {
return;
} else {
throw UnknownPropertyException.create(JsonLd.ID, getInstanceType());
}
}
if (blankNode && !idField.getType().equals(String.class)) {
return;
}
setFieldValue(idField, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ void processValue(Map<?, ?> value) {
}

private void openObject(Map<?, ?> value) {
if (property != null) {
instanceBuilder.openObject(getId(value), property, getObjectTypes(value));
} else {
assert targetClass != null;
final Class<?> cls = resolveTargetClass(value, targetClass);
assert targetClass.isAssignableFrom(cls);
instanceBuilder.openObject(getId(value), cls);
try {
if (property != null) {
instanceBuilder.openObject(getId(value), property, getObjectTypes(value));
} else {
assert targetClass != null;
final Class<?> cls = resolveTargetClass(value, targetClass);
assert targetClass.isAssignableFrom(cls);
instanceBuilder.openObject(getId(value), cls);
}
} catch (UnknownPropertyException e) {
if (!configuration().is(ConfigParam.IGNORE_UNKNOWN_PROPERTIES)) {
throw e;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.deserialization;

import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.IdentifierUtil;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.*;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
import org.junit.jupiter.api.Test;

import java.net.URI;
Expand Down Expand Up @@ -194,4 +194,20 @@ void setIdentifierValueStoresIdentifierToBeAccessibleByGetter() {
sut.setIdentifierValue(identifier);
assertEquals(identifier, sut.getIdentifier());
}

@Test
void setIdentifierValueDoesNothingWhenIdentifierIsBlankNodeAndTargetObjectHasNoIdentifierField() {
final SingularObjectContext<Object> sut = new SingularObjectContext<>(new Object(), Collections.emptyMap(),
new HashMap<>());
sut.setIdentifierValue(IdentifierUtil.generateBlankNodeId());
assertNull(sut.getIdentifier());
assertNotNull(sut.getInstance());
}

@Test
void setIdentifierValueThrowsUnknownPropertyExceptionWhenIdentifierIsNotBlankNodeAndTargetObjectHasNoIdentifierField() {
final SingularObjectContext<Object> sut = new SingularObjectContext<>(new Object(), Collections.emptyMap(),
new HashMap<>());
assertThrows(UnknownPropertyException.class, () -> sut.setIdentifierValue(Generator.generateUri().toString()));
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.deserialization.expanded;

import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.Configuration;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder;
Expand All @@ -29,6 +28,7 @@
import cz.cvut.kbss.jsonld.environment.model.Study;
import cz.cvut.kbss.jsonld.environment.model.User;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
import org.hamcrest.core.StringStartsWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -44,8 +44,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class ObjectDeserializerTest {
Expand All @@ -60,7 +59,7 @@ class ObjectDeserializerTest {

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
}

@Test
Expand Down Expand Up @@ -184,4 +183,33 @@ void processValueSkipsPropertyMappedToFieldWithReadOnlyAccess() throws Exception
sut.processValue((Map<?, ?>) input.get(0));
verify(instanceBuilderMock, never()).addValue(eq(Vocabulary.NUMBER_OF_PEOPLE_INVOLVED), any());
}

@Test
void processValueIgnoresMissingIdFieldWhenIgnoreUnknownPropertiesIsConfigured() throws Exception {
doReturn(Object.class).when(tcResolverMock).getTargetClass(eq(Object.class), anyCollection());
when(instanceBuilderMock.isPropertyDeserializable(any())).thenReturn(false);
doThrow(UnknownPropertyException.class).when(instanceBuilderMock).openObject(anyString(), any(Class.class));
when(instanceBuilderMock.isPropertyDeserializable(any())).thenReturn(false);
final Configuration config = new Configuration();
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.TRUE.toString());
this.sut = new ObjectDeserializer(instanceBuilderMock,
new DeserializerConfig(config, tcResolverMock), Object.class);
final List<?> input = (List<?>) TestUtil.readAndExpand("objectWithDataProperties.json");
sut.processValue((Map<?, ?>) input.get(0));
}

@Test
void processValueThrowsUnknownPropertyExceptionWhenObjectIsMissingIdField() throws Exception {
doReturn(Object.class).when(tcResolverMock).getTargetClass(eq(Object.class), anyCollection());
when(instanceBuilderMock.isPropertyDeserializable(any())).thenReturn(false);
final UnknownPropertyException ex = UnknownPropertyException.create(JsonLd.ID, Object.class);
doThrow(ex).when(instanceBuilderMock).openObject(anyString(), any(Class.class));
when(instanceBuilderMock.isPropertyDeserializable(any())).thenReturn(false);
this.sut = new ObjectDeserializer(instanceBuilderMock,
new DeserializerConfig(new Configuration(), tcResolverMock), Object.class);
final List<?> input = (List<?>) TestUtil.readAndExpand("objectWithDataProperties.json");
final UnknownPropertyException result = assertThrows(UnknownPropertyException.class,
() -> sut.processValue((Map<?, ?>) input.get(0)));
assertEquals(ex, result);
}
}

0 comments on commit ff3d820

Please sign in to comment.