Skip to content

Commit

Permalink
Throw MissingTypeInfoException when instance has no type info, i.e., no
Browse files Browse the repository at this point in the history
@owlclass annotation and no values in @types (or no such attribute at all).
  • Loading branch information
ledsoft committed Sep 25, 2018
1 parent b056599 commit d2ba21f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cz.cvut.kbss.jsonld.exception;

/**
* Indicates that no type info was found when serializing an object.
*/
public class MissingTypeInfoException extends JsonLdSerializationException {

public MissingTypeInfoException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/**
* Copyright (C) 2017 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.serialization.traversal;

import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
import cz.cvut.kbss.jsonld.exception.BeanProcessingException;
import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException;

import java.lang.reflect.Field;
import java.util.Collection;
Expand Down Expand Up @@ -54,6 +53,10 @@ Set<String> resolveTypes(Object instance) {
runtimeTypes.forEach(t -> declaredTypes.add(t.toString()));
}
});
if (declaredTypes.isEmpty()) {
throw new MissingTypeInfoException("No type info found on instance " + instance +
". Either annotate the class with @OWLClass or provide a non-empty @Types field.");
}
return declaredTypes;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
/**
* Copyright (C) 2017 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.serialization.traversal;

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.jopa.model.annotations.Types;
import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder;
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.MissingIdentifierException;
import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException;
import org.hamcrest.core.StringStartsWith;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -37,6 +38,7 @@
import java.util.Map;
import java.util.Set;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -225,7 +227,7 @@ public void traversePutsVisitedInstanceTogetherWithIdentifierIntoKnownInstancesO

@Test
public void traverseGeneratesBlankNodeIdentifierWhenPuttingInstanceWithoutIdentifierIntoKnownInstances() throws
Exception {
Exception {
final Person person = Generator.generatePerson();
person.setUri(null);
traverser.traverse(person);
Expand All @@ -252,7 +254,8 @@ public void traverseInvokesVisitWhenInstanceIsPlainIdentifierPropertyValue() thr
traverser.traverse(employee);
final InOrder inOrder = inOrder(visitor);
inOrder.verify(visitor).openInstance(employee);
inOrder.verify(visitor).visitField(EmployeeWithUriEmployer.class.getDeclaredField("employer"), employee.employer);
inOrder.verify(visitor)
.visitField(EmployeeWithUriEmployer.class.getDeclaredField("employer"), employee.employer);
inOrder.verify(visitor).openInstance(employee.employer);
inOrder.verify(visitor).visitIdentifier(employee.employer.toString(), employee.employer);
}
Expand All @@ -275,4 +278,31 @@ public void traverseThrowsMissingIdentifierExceptionWhenIdentifierIsRequiredAndM
traverser.setRequireId(true);
traverser.traverse(person);
}

@Test
public void traverseThrowsMissingTypeInfoExceptionWhenObjectHasNoTypesAndIsNotOwlClass() {
final NoType instance = new NoType();
instance.uri = Generator.generateUri();
thrown.expect(MissingTypeInfoException.class);
thrown.expectMessage(containsString("@OWLClass"));
thrown.expectMessage(containsString("@Types"));
traverser.traverse(instance);
}

@Test
public void traverseSupportsInstanceOfClassWithoutOWLClassAnnotationButWithNonEmptyTypes() {
final NoType instance = new NoType();
instance.uri = Generator.generateUri();
instance.types = Collections.singleton(Vocabulary.PERSON);
traverser.traverse(instance);
verify(visitor).visitTypes(instance.types, instance);
}

private static class NoType {
@Id
private URI uri;

@Types
private Set<String> types;
}
}

0 comments on commit d2ba21f

Please sign in to comment.