AxiomSaver { void saveAxioms(AxiomValueDescriptor descriptor) { final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString()); final Map<String, List<Statement>> statements = new HashMap<>(); for (Assertion a : descriptor.getAssertions()) { final URI context = descriptor.getAssertionContext(a); final String strContext = context != null ? context.toString() : null; statements.putIfAbsent(strContext, new ArrayList<>()); statements.get(strContext).addAll(transformToStatements(a, descriptor.getAssertionValues(a), subject)); } statements.forEach((ctx, toAdd) -> connector.add(toAdd, ctx)); } AxiomSaver(StorageConnector connector);   }
@Test public void saveAxiomsAddsClassAssertionToStorage() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion a = Assertion.createClassAssertion(false); final NamedResource type = NamedResource.create(Generator.generateUri()); descriptor.addAssertionValue(a, new Value<>(type)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createResource(SUBJECT.toString()), arg.get(0).getSubject()); assertEquals(ResourceFactory.createProperty(Vocabulary.RDF_TYPE), arg.get(0).getPredicate()); assertEquals(ResourceFactory.createResource(type.getIdentifier().toString()), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsResourceStatementsForObjectPropertyAssertionAxioms() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createObjectPropertyAssertion(Generator.generateUri(), false); final NamedResource value = NamedResource.create(Generator.generateUri()); descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createProperty(assertion.getIdentifier().toString()), arg.get(0).getPredicate()); assertEquals(ResourceFactory.createResource(value.getIdentifier().toString()), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsLiteralStatementsForDatatypePropertyAssertionAxioms() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), "en", false); final Integer value = 117; descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createTypedLiteral(value), arg.get(0).getObject()); }
@Test public void saveAxiomsCreatesLanguageTaggedLiteralsForDatatypePropertyAssertionsWithLanguageTag() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final String lang = "en"; final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), lang, false); final String value = "english text"; descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createLangLiteral(value, lang), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsResourceStatementsForAnnotationPropertyAxiomsWithIriValue() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createAnnotationPropertyAssertion(Generator.generateUri(), false); final String value = Generator.generateUri().toString(); descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createResource(value), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsLiteralStatementForAnnotationPropertyAxiomWithLiteralValue() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createAnnotationPropertyAssertion(Generator.generateUri(), false); final Integer value = 117; descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createTypedLiteral(value), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsCorrectDateTimeLiteralForDateDataPropertyAxiomValue() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false); final Date value = new Date(); descriptor.addAssertionValue(assertion, new Value<>(value)); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); final GregorianCalendar cal = new GregorianCalendar(); cal.setTime(value); assertEquals(ResourceFactory.createTypedLiteral(cal), arg.get(0).getObject()); }
@Test public void saveAxiomsAddsStatementsIntoCorrectContextWhenAssertionHasContext() { final URI context = Generator.generateUri(); final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false); final Integer value = 117; descriptor.addAssertionValue(assertion, new Value<>(value)); descriptor.setAssertionContext(assertion, context); saver.saveAxioms(descriptor); verify(connectorMock).add(anyList(), eq(context.toString())); }
@Test public void saveAxiomsSkipsNullValues() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createObjectPropertyAssertion(Generator.generateUri(), false); descriptor.addAssertionValue(assertion, Value.nullValue()); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); assertTrue(captor.getValue().isEmpty()); }
@Test void saveAxiomsSavesOntoDriverLangStringAxiomValuesAsRDFLangStringValues() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false); descriptor.addAssertionValue(assertion, new Value<>(new LangString("test", "en"))); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createLangLiteral("test", "en"), arg.get(0).getObject()); }
@Test void saveAxiomsSavesOntoDriverLangStringAxiomWithoutLanguageAsSimpleLiteral() { final AxiomValueDescriptor descriptor = new AxiomValueDescriptor(SUBJECT); final Assertion assertion = Assertion.createDataPropertyAssertion(Generator.generateUri(), false); descriptor.addAssertionValue(assertion, new Value<>(new LangString("test"))); saver.saveAxioms(descriptor); final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); verify(connectorMock).add(captor.capture(), eq(null)); final List<Statement> arg = captor.getValue(); assertEquals(1, arg.size()); assertEquals(ResourceFactory.createPlainLiteral("test"), arg.get(0).getObject()); }