-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
229 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Nancy.Rdf.Tests/ModelBinding/JsonldBodyDeserializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.IO; | ||
using System.Text; | ||
using FakeItEasy; | ||
using FluentAssertions; | ||
using JsonLD.Entities; | ||
using Nancy.ModelBinding; | ||
using Nancy.Rdf.ModelBinding; | ||
using Newtonsoft.Json.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace Nancy.Rdf.Tests.ModelBinding | ||
{ | ||
public class JsonldBodyDeserializerTests | ||
{ | ||
[Test] | ||
public void Should_support_jsonld() | ||
{ | ||
// given | ||
var entitySerializer = A.Fake<IEntitySerializer>(); | ||
var deserializer = new JsonldBodyDeserializer(entitySerializer); | ||
|
||
// then | ||
deserializer.CanDeserialize(RdfSerialization.JsonLd.MediaType, new BindingContext()) | ||
.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Deserialize_should_deserialize_JSONLD_proper_type_using_entity_serializer() | ||
{ | ||
// given | ||
var entitySerializer = A.Fake<IEntitySerializer>(); | ||
var binder = new JsonldBodyDeserializer(entitySerializer); | ||
var bindingContext = new BindingContext { DestinationType = typeof(TestModel) }; | ||
var body = new MemoryStream(Encoding.UTF8.GetBytes("{}")); | ||
|
||
// when | ||
binder.Deserialize(RdfSerialization.JsonLd.MediaType, body, bindingContext); | ||
|
||
// then | ||
A.CallTo(() => entitySerializer.Deserialize<TestModel>(A<JToken>._)).MustHaveHappened(); | ||
} | ||
|
||
private class TestModel | ||
{ | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Nancy.Rdf.Tests/ModelBinding/NonJsonLdRdfBodyDeserializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using FakeItEasy; | ||
using FluentAssertions; | ||
using JsonLD.Entities; | ||
using Nancy.ModelBinding; | ||
using Nancy.Rdf.ModelBinding; | ||
using Nancy.Rdf.Responses; | ||
using NUnit.Framework; | ||
using VDS.RDF; | ||
using VDS.RDF.Parsing; | ||
|
||
namespace Nancy.Rdf.Tests.ModelBinding | ||
{ | ||
public class NonJsonLdRdfBodyDeserializerTests | ||
{ | ||
private readonly BindingContext bindingContext = new BindingContext { DestinationType = typeof(TestModel) }; | ||
|
||
[TestCaseSource(nameof(NonJsonLdSerializations))] | ||
public void Should_support_serialization(RdfSerialization serialization) | ||
{ | ||
// given | ||
var deserializer = new NonJsonLdRdfBodyDeserializer(A.Fake<IEntitySerializer>()); | ||
|
||
// when | ||
deserializer.CanDeserialize(serialization.MediaType, new BindingContext()); | ||
} | ||
|
||
[Test] | ||
public void Deserialize_should_deserialize_directly_from_ntriples() | ||
{ | ||
// given | ||
var entitySerializer = A.Fake<IEntitySerializer>(); | ||
var binder = new NonJsonLdRdfBodyDeserializer(entitySerializer); | ||
const string bodyString = "some nquads"; | ||
var body = new MemoryStream(Encoding.UTF8.GetBytes(bodyString)); | ||
|
||
// when | ||
binder.Deserialize(RdfSerialization.NTriples.MediaType, body, this.bindingContext); | ||
|
||
// then | ||
A.CallTo(() => entitySerializer.Deserialize<TestModel>(bodyString)).MustHaveHappened(); | ||
} | ||
|
||
[Test, Sequential] | ||
public void Deserialize_should_convert_to_ntriples( | ||
[ValueSource(nameof(NonJsonLdSerializations))] RdfSerialization serialization, | ||
[Values(typeof(Notation3Parser), typeof(TurtleParser), typeof(RdfXmlParser))] Type serializerType) | ||
{ | ||
// given | ||
var converter = A.Fake<IRdfConverter>(); | ||
var deserializer = new NonJsonLdRdfBodyDeserializer(A.Fake<IEntitySerializer>(), converter); | ||
var body = new MemoryStream(); | ||
|
||
// when | ||
deserializer.Deserialize(serialization.MediaType, body, this.bindingContext); | ||
|
||
// then | ||
A.CallTo(() => converter.ConvertToNtriples(body, A<IRdfReader>.That.Matches(rr => rr.GetType() == serializerType))).MustHaveHappened(); | ||
} | ||
|
||
public IEnumerable<RdfSerialization> NonJsonLdSerializations() | ||
{ | ||
yield return RdfSerialization.Notation3; | ||
yield return RdfSerialization.Turtle; | ||
yield return RdfSerialization.RdfXml; | ||
} | ||
|
||
private class TestModel | ||
{ | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.IO; | ||
using VDS.RDF; | ||
|
||
namespace Nancy.Rdf.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Converts RDF serializations to n-triples | ||
/// </summary> | ||
public interface IRdfConverter | ||
{ | ||
/// <summary> | ||
/// Converts to nquads. | ||
/// </summary> | ||
/// <returns>input rdf serialized as n-triples</returns> | ||
string ConvertToNtriples(Stream rdf, IRdfReader reader); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 39 additions & 26 deletions
65
src/Nancy.Rdf/ModelBinding/NonJsonLdRdfBodyDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,78 @@ | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using JsonLD.Entities; | ||
using Nancy.ModelBinding; | ||
using Nancy.Responses.Negotiation; | ||
using VDS.RDF; | ||
using VDS.RDF.Parsing; | ||
using VDS.RDF.Writing; | ||
using StringWriter = System.IO.StringWriter; | ||
|
||
namespace Nancy.Rdf.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Converts body, first converting it to NQuads | ||
/// Deserializers request body, first converting it to n-triples | ||
/// </summary> | ||
public abstract class NonJsonLdRdfBodyDeserializer : RdfBodyDeserializer | ||
public class NonJsonLdRdfBodyDeserializer : RdfBodyDeserializer | ||
{ | ||
private static readonly MethodInfo DeserializeNquadsMethod = typeof(IEntitySerializer).GetMethod("Deserialize", new[] { typeof(string) }); | ||
|
||
private static readonly IRdfWriter RdfWriter = new NTriplesWriter(NTriplesSyntax.Rdf11); | ||
private readonly IRdfReader reader; | ||
private readonly IDictionary<RdfSerialization, IRdfReader> readers; | ||
private readonly IRdfConverter converter; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NonJsonLdRdfBodyDeserializer"/> class. | ||
/// </summary> | ||
protected NonJsonLdRdfBodyDeserializer( | ||
RdfSerialization serialization, | ||
IEntitySerializer serializer, | ||
IRdfReader reader) | ||
: base(serialization, serializer) | ||
public NonJsonLdRdfBodyDeserializer(IEntitySerializer serializer) | ||
: this(serializer, new RdfConverter()) | ||
{ | ||
this.reader = reader; | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize the request body to a model | ||
/// Initializes a new instance of the <see cref="NonJsonLdRdfBodyDeserializer"/> class. | ||
/// </summary> | ||
public override object Deserialize(MediaRange contentType, Stream body, BindingContext context) | ||
public NonJsonLdRdfBodyDeserializer( | ||
IEntitySerializer serializer, | ||
IRdfConverter converter) | ||
: this(serializer, RdfSerialization.RdfXml, RdfSerialization.NTriples, RdfSerialization.Notation3, RdfSerialization.Turtle) | ||
{ | ||
var deserialize = DeserializeNquadsMethod.MakeGenericMethod(context.DestinationType); | ||
this.converter = converter; | ||
} | ||
|
||
return deserialize.Invoke(this.Serializer, new object[] { this.GetNquads(body) }); | ||
private NonJsonLdRdfBodyDeserializer(IEntitySerializer serializer, params RdfSerialization[] serializations) | ||
: base(serializer, serializations) | ||
{ | ||
this.readers = new Dictionary<RdfSerialization, IRdfReader> | ||
{ | ||
{ RdfSerialization.Notation3, new Notation3Parser() }, | ||
{ RdfSerialization.RdfXml, new RdfXmlParser() }, | ||
{ RdfSerialization.Turtle, new TurtleParser() } | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Converts body to N-Triples | ||
/// Deserialize the request body to a model | ||
/// </summary> | ||
protected virtual string GetNquads(Stream body) | ||
public override object Deserialize(MediaRange contentType, Stream body, BindingContext context) | ||
{ | ||
// todo: implement actual parsers for json-ld.net so that it's not necessary to parse and write to ntriples | ||
IGraph g = new Graph(); | ||
var deserialize = DeserializeNquadsMethod.MakeGenericMethod(context.DestinationType); | ||
string nquads; | ||
|
||
using (var streamReader = new StreamReader(body)) | ||
if (contentType.Matches(RdfSerialization.NTriples.MediaType)) | ||
{ | ||
this.reader.Load(g, streamReader); | ||
using (var bodyReader = new StreamReader(body)) | ||
{ | ||
nquads = bodyReader.ReadToEnd(); | ||
} | ||
} | ||
|
||
using (var stringWriter = new StringWriter()) | ||
else | ||
{ | ||
RdfWriter.Save(g, stringWriter); | ||
return stringWriter.ToString(); | ||
var reader = this.readers.First(r => contentType.Matches(r.Key.MediaType)).Value; | ||
nquads = this.converter.ConvertToNtriples(body, reader); | ||
} | ||
|
||
return deserialize.Invoke(this.Serializer, new object[] { nquads }); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.