public void Online(IJsonSchema schema) { try { // TODO: Catch web exceptions and assert inconclusive. var localSchemaJson = schema.ToJson(null); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = JsonSchemaFactory.FromJson(onlineSchemaJson); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); Assert.AreEqual(onlineSchema, schema); onlineValidation.AssertValid(); localValidation.AssertValid(); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (WebException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any()) { Assert.Inconclusive(); } throw; } }
public void Online(JsonSchema schema) { try { var localSchemaJson = schema.ToJson(_serializer); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = new JsonSchema(); onlineSchema.FromJson(onlineSchemaJson, _serializer); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); Assert.AreEqual(onlineSchema, schema); onlineValidation.AssertValid(); localValidation.AssertValid(); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (WebException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any()) { Assert.Inconclusive(); } throw; } }
public void Online(string testName, JsonSchema schema) { try { var localSchemaJson = schema.ToJson(_serializer); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = new JsonSchema(); onlineSchema.FromJson(onlineSchemaJson, _serializer); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); try { Console.WriteLine("Asserting schema equality"); Assert.AreEqual(onlineSchema, schema); Console.WriteLine("Validating local against online"); onlineValidation.AssertValid(); Console.WriteLine("Validating online against local"); localValidation.AssertValid(); Console.WriteLine("Asserting json equality"); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (Exception) { Console.WriteLine("Online {0}", onlineSchemaJson); Console.WriteLine("Local {0}", localSchemaJson); throw; } } catch (WebException) { Assert.Inconclusive(); } catch (HttpRequestException) { Assert.Inconclusive(); } catch (SocketException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any() || e.InnerExceptions.OfType <HttpRequestException>().Any()) { Assert.Inconclusive(); } throw; } }
/// <summary> /// The object should match the defined schema. /// </summary> public AssertResult JsonSchema(PSObject inputObject, string uri) { // Guard parameters if (GuardNullParam(inputObject, nameof(inputObject), out AssertResult result) || GuardNullOrEmptyParam(uri, nameof(uri), out result)) { return(result); } // Get the schema if (!(TryReadJson(uri, out string schemaContent))) { return(Fail(ReasonStrings.JsonSchemaNotFound, uri)); } var s = new JsonSerializer(); var schema = s.Deserialize <JsonSchema>(JsonValue.Parse(schemaContent)); // Get the TargetObject var json = JsonValue.Parse(inputObject.ToJson()); // Validate var schemaOptions = new JsonSchemaOptions { OutputFormat = SchemaValidationOutputFormat.Basic }; var schemaResults = schema.Validate(json, schemaOptions); // Schema is valid if (schemaResults.IsValid) { return(Pass()); } // Handle schema invalid result = Fail(); if (!string.IsNullOrEmpty(schemaResults.ErrorMessage)) { result.AddReason(ReasonStrings.JsonSchemaInvalid, schemaResults.InstanceLocation.ToString(), schemaResults.ErrorMessage); } foreach (var r in schemaResults.NestedResults) { if (!string.IsNullOrEmpty(r.ErrorMessage)) { result.AddReason(ReasonStrings.JsonSchemaInvalid, r.InstanceLocation.ToString(), r.ErrorMessage); } } return(result); }