Ejemplo n.º 1
0
        public void ParseTermInChild()
        {
            // Arrange
            var doc = new JObject(new JProperty("foo", "bar"),
                                  new JProperty("child",
                                                new JObject(new JProperty("Ick", "much"), new JProperty("Ock", "wow"))));
            var localObject = new LocalType();

            // Act
            var root  = new VocabTerm <LocalType>();
            var child = new VocabTerm <LocalChildType>("child");

            child.MapProperty <string>("Ick", (s, o) => s.Ick = o);
            child.MapProperty <string>("Ock", (s, o) => s.Ock = o);

            root.MapObject <LocalChildType>(child, (s) =>
            {
                var subject   = s;
                subject.Child = new LocalChildType();
                return(subject.Child);
            });

            JsonStreamingParser.ParseStream(doc.ToMemoryStream(), localObject, root);

            // Assert
            Assert.Equal("much", localObject.Child.Ick);
            Assert.Equal("wow", localObject.Child.Ock);
        }
        public void ParseTermInChild()
        {
            var x = new Vocabulary();

            x.AddTermHandler("child", (s, o) => {
                var subject   = (LocalType)s.Subject;
                subject.Child = new LocalChildType();
                return(new Context {
                    Subject = subject.Child
                });
            });

            x.AddTermHandler <LocalChildType, string>("Ick", (s, o) => {
                s.Ick = o;
            });

            x.AddTermHandler <LocalChildType, string>("Ock", (s, o) => {
                s.Ock = o;
            });

            var doc = new JObject(new JProperty("foo", "bar"),
                                  new JProperty("child", new JObject(new JProperty("Ick", "much"), new JProperty("Ock", "wow"))));

            var parser = new JsonStreamingParser();

            var localObject = new LocalType();

            JsonStreamingParser.ParseStream(doc.ToMemoryStream(), localObject, x);

            Assert.Equal("much", localObject.Child.Ick);
            Assert.Equal("wow", localObject.Child.Ock);
        }
Ejemplo n.º 3
0
        public void ParseCompleteSwagger()
        {
            var vocab = OpenApiVocab.Create();

            var stream = this.GetType().Assembly
                         .GetManifestResourceStream(this.GetType(), "forecast.io.swagger.json");

            var swaggerDoc = new OpenApiDocument();

            JsonStreamingParser.ParseStream(stream, swaggerDoc, vocab);

            Assert.Equal("2.0", swaggerDoc.Version);
            Assert.Equal(1, swaggerDoc.Schemes.Count);
            Assert.Equal("https", swaggerDoc.Schemes.First());

            Assert.Equal(1, swaggerDoc.Paths.Count);
            Assert.Equal(swaggerDoc.Paths.Keys.First(), "/forecast/{apiKey}/{latitude},{longitude}");
            var path = swaggerDoc.Paths.Values.First();

            Assert.Equal(1, path.Operations.Count);
            var operation = path.Operations.Values.First();

            Assert.Equal("Forecast", operation.Id);

            Assert.False(String.IsNullOrEmpty(swaggerDoc.Info.Description));
            Assert.False(String.IsNullOrEmpty(swaggerDoc.Info.Title));
            Assert.True(String.IsNullOrEmpty(swaggerDoc.Info.Version));
        }
Ejemplo n.º 4
0
        public void ParseAPIsJsonDocumentMinimal()
        {
            var stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType(), "apis.json");

            var rootMap = new VocabTerm <ApisJson>(null);

            rootMap.MapProperty <string>("name", (s, o) => s.Name = o);
            rootMap.MapProperty <string>("url", (s, o) => s.Url   = new Uri(o));

            var apiMap = new VocabTerm <ApisJsonApi>("apis");

            apiMap.MapProperty <string>("name", (s, o) => s.Name        = o);
            apiMap.MapProperty <string>("description", (s, o) => s.Name = o);
            apiMap.MapProperty <string>("baseUrl", (s, o) => s.BaseUrl  = new Uri(o));

            rootMap.MapObject <ApisJsonApi>(apiMap, (s) =>
            {
                if (s.Apis == null)
                {
                    s.Apis = new List <ApisJsonApi>();
                }
                var api = new ApisJsonApi();
                s.Apis.Add(api);
                return(api);
            });

            var apis = new ApisJson();

            JsonStreamingParser.ParseStream(stream, apis, rootMap);

            Assert.Equal("API Evangelist", apis.Name);
            Assert.Equal(2, apis.Apis.Count());
        }
Ejemplo n.º 5
0
        public void ParseSwaggerPaths()
        {
            var opsTerm = new VocabTerm <Operation>();

            var pathTerm = new VocabTerm <Path>();

            pathTerm.MapAnyObject <Operation>(opsTerm, (s, p) => {
                return(s.AddOperation(p, Guid.NewGuid().ToString()));
            });

            var pathsTerm = new VocabTerm <OpenApiDocument>("paths");

            pathsTerm.MapAnyObject <Path>(pathTerm, (s, p) => {
                return(s.AddPath(p));
            });

            var rootTerm = new VocabTerm <OpenApiDocument>();

            rootTerm.MapObject <OpenApiDocument>(pathsTerm, (s) =>
            {
                return(s);
            });



            var stream = this.GetType().Assembly
                         .GetManifestResourceStream(this.GetType(), "forecast.io.swagger.json");

            var swaggerDoc = new OpenApiDocument();

            JsonStreamingParser.ParseStream(stream, swaggerDoc, rootTerm);

            Assert.Equal(1, swaggerDoc.Paths.Count);
            Assert.Equal(1, swaggerDoc.Paths.First().Value.Operations.Count());
        }
Ejemplo n.º 6
0
        public void ParseSwaggerPaths()
        {
            var swaggerDoc = new OpenApiDocument();

            swaggerDoc.AddPath("foo", p => {
                p.AddOperation("get", "fooget");
            });

            swaggerDoc.AddPath("bar", p => {
                p.AddOperation("get", "barget");
            });

            swaggerDoc.AddPath("baz", p => {
                p.AddOperation("get", "bazget");
                p.AddOperation("put", "bazput");
            });


            var swaggerstring = swaggerDoc.ToString();

            _Output.WriteLine(swaggerstring);

            var newDoc = new OpenApiDocument();

            JsonStreamingParser.ParseStream(swaggerstring.ToMemoryStream(), newDoc, OpenApiVocab.Create());
        }
Ejemplo n.º 7
0
        public void ParseSimpleSwagger()
        {
            var jObject = new JObject(new JProperty("swagger", "2.0"),
                                      new JProperty("x-unknown", "blah"),
                                      new JProperty("info",
                                                    new JObject(
                                                        new JProperty("title", "This is the title"),
                                                        new JProperty("description", "This is the description"),
                                                        new JProperty("version", "1.1")
                                                        ))
                                      );

            var doc = new OpenApiDocument();

            JsonStreamingParser.ParseStream(jObject.ToMemoryStream(), doc, OpenApiVocab.Create());
        }
Ejemplo n.º 8
0
        public void ParseTerm()
        {
            // Arrange
            var doc         = new JObject(new JProperty("foo", "bar"), new JProperty("baz", 22));
            var localObject = new LocalType();

            // Act
            var rootMap = new VocabTerm <LocalType>();

            rootMap.MapProperty <string>("foo", (s, o) => s.Foo = o);
            rootMap.MapProperty <int>("baz", (s, o) => s.Baz    = o);

            JsonStreamingParser.ParseStream(doc.ToMemoryStream(), localObject, rootMap);

            // Assert
            Assert.Equal("bar", localObject.Foo);
            Assert.Equal(22, localObject.Baz);
        }
Ejemplo n.º 9
0
        public void ParseProblemDocument()
        {
            var stream = this.GetType().Assembly.GetManifestResourceStream("Hapikit.net.Tests.problem.json");
            Func <string, Uri> typeProducer = (o) => new Uri(o);
            var vocab = new VocabTerm <ProblemDocument>();

            vocab.MapProperty <string>("type", (s, o) => { s.ProblemType = typeProducer(o); });
            vocab.MapProperty <string>("title", (s, o) => { s.Title = o; });
            vocab.MapProperty <string>("detail", (s, o) => { s.Detail = o; });
            vocab.MapProperty <string>("instance", (s, o) => { s.ProblemInstance = new Uri(o, UriKind.RelativeOrAbsolute); });


            var problem = new Tavis.ProblemDocument();

            JsonStreamingParser.ParseStream(stream, problem, vocab);

            Assert.Equal("https://example.com/probs/out-of-credit", problem.ProblemType.OriginalString);
            Assert.Equal("You do not have enough credit.", problem.Title);
        }
Ejemplo n.º 10
0
        public void ParseTerm()
        {
            var x = new Vocabulary();

            x.AddTermHandler <LocalType, string>("foo", (s, o) => {
                s.Foo = o;
            });
            x.AddTermHandler <LocalType, int>("baz", (s, o) => {
                s.Baz = o;
            });

            var doc = new JObject(new JProperty("foo", "bar"), new JProperty("baz", 22));

            var parser = new JsonStreamingParser();

            var localObject = new LocalType();

            JsonStreamingParser.ParseStream(doc.ToMemoryStream(), localObject, x);

            Assert.Equal("bar", localObject.Foo);
            Assert.Equal(22, localObject.Baz);
        }
Ejemplo n.º 11
0
        //   [Fact]
        public void ParseVeryLargeSwaggerPaths()
        {
            var opsTerm = new VocabTerm <Operation>();

            var pathTerm = new VocabTerm <Path>();

            pathTerm.MapAnyObject <Operation>(opsTerm, (s, p) =>
            {
                return(s.AddOperation(p, Guid.NewGuid().ToString()));
            });

            var pathsTerm = new VocabTerm <OpenApiDocument>("paths");

            pathsTerm.MapAnyObject <Path>(pathTerm, (s, p) =>
            {
                return(s.AddPath(p));
            });

            var rootTerm = new VocabTerm <OpenApiDocument>();

            rootTerm.MapObject <OpenApiDocument>(pathsTerm, (s) =>
            {
                return(s);
            });

            var swaggerDoc = new OpenApiDocument();
            var sw         = new Stopwatch();

            using (var stream = File.OpenRead(@"C:\Users\Darrel\Documents\Swagger\WebSites.json"))
            {
                sw.Start();

                JsonStreamingParser.ParseStream(stream, swaggerDoc, rootTerm);
                sw.Stop();
            };

            _output.WriteLine(swaggerDoc.Paths.Count.ToString());
            _output.WriteLine("That took : " + sw.ElapsedMilliseconds);
        }
Ejemplo n.º 12
0
        public void ParseCompleteSwagger()
        {
            DiagnosticListener listener = new DiagnosticListener("Testing");

            JsonStreamingParser.DiagSource = listener;
            listener.Subscribe(new ConsoleLogger(this.output));

            Vocabulary vocab = OpenApiVocab.Create();

            var stream = typeof(ParsingTests).Assembly
                         .GetManifestResourceStream(typeof(ParsingTests), "forecast.io.swagger.json");

            var swaggerDoc = new OpenApiDocument();

            JsonStreamingParser.ParseStream(stream, swaggerDoc, vocab);

            Assert.Equal("2.0", swaggerDoc.Version);
            Assert.Equal(1, swaggerDoc.Schemes.Count);
            Assert.Equal("https", swaggerDoc.Schemes.First());
            Assert.True(swaggerDoc.Paths.Count > 0);
            Assert.False(String.IsNullOrEmpty(swaggerDoc.Info.Description));
            Assert.False(String.IsNullOrEmpty(swaggerDoc.Info.Title));
            Assert.True(String.IsNullOrEmpty(swaggerDoc.Info.Version));
        }
Ejemplo n.º 13
0
        public void ParseAPIsJsonDocument()
        {
            var stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType(), "apis.json");

            var vocab = ApisJsonVocab.Create();

            //new VocabTerm<ApisJson>(null);

            //vocab.MapProperty<string>("name", (s, o) => { s.Name = o; });
            //vocab.MapProperty<string>("url", (s, o) => { s.Url = new Uri(o); });
            //vocab.MapProperty<string>("image", (s, o) => { s.Image = new Uri(o); });
            //vocab.MapProperty<string>("modified", (s, o) => { s.Modified = DateTime.Parse(o); });
            //vocab.MapProperty<string>("created", (s, o) => { s.Created = DateTime.Parse(o); });
            //vocab.MapProperty<string>("tags", (s, o) => {
            //    if (s.Tags == null) s.Tags = new List<string>();
            //    s.Tags.Add(o); });
            //vocab.MapProperty<string>("specificationVersion", (s, o) => { s.SpecificationVersion = o; });


            //var apivocab = new VocabTerm<ApisJsonApi>("apis");
            //apivocab.MapProperty<string>("name", (s, o) => { s.Name = o; });
            //apivocab.MapProperty<string>("description", (s, o) => { s.Name = o; });
            //apivocab.MapProperty<string>("humanUrl", (s, o) => { s.HumanUrl = new Uri(o); });
            //apivocab.MapProperty<string>("baseUrl", (s, o) => { s.BaseUrl = new Uri(o); });
            //apivocab.MapProperty<string>("image", (s, o) => { s.Image = new Uri(o); });
            //apivocab.MapProperty<string>("tags", (s, o) => {
            //    if (s.Tags == null) s.Tags = new List<string>();
            //    s.Tags.Add(o);
            //});

            //vocab.MapObject<ApisJsonApi>(apivocab, (s) =>
            //{
            //    if (s.Apis == null)
            //    {
            //        s.Apis = new List<ApisJsonApi>();
            //    }
            //    var api = new ApisJsonApi();
            //    s.Apis.Add(api);
            //    return api;
            //});

            //var propertyTerm = new VocabTerm<ApisJsonProperty>("properties");
            //propertyTerm.MapProperty<string>("url", (s, o) => { s.Url = new Uri(o);});
            //propertyTerm.MapProperty<string>("type", (s, o) => { s.Type = o;});


            //apivocab.MapObject<ApisJsonProperty>(propertyTerm, (s) =>
            //{
            //    if (s.Properties == null)
            //    {
            //        s.Properties = new List<ApisJsonProperty>();
            //    }
            //    var property = new ApisJsonProperty();
            //    s.Properties.Add(property);
            //    return property;
            //});


            var apis = new ApisJson();

            JsonStreamingParser.ParseStream(stream, apis, vocab);

            Assert.Equal("API Evangelist", apis.Name);
            Assert.Equal(2, apis.Apis.Count());
        }
Ejemplo n.º 14
0
        public void ParseEmbeddedSwagger()
        {
            var stream = typeof(ParsingTests).Assembly.GetManifestResourceStream(typeof(ParsingTests), "forecast.io.swagger.json");

            JsonStreamingParser.ParseStream(stream, new OpenApiDocument(), OpenApiVocab.Create());
        }