public void Enumerable_fields_should_not_contain_null_values() {
            using (var server = TestServer.Create<TestStartup>()) {
                // arrange
                var schema = new MetaType { Name = "simple_schema" };
                schema.Fields.Add(new EnumerableField { Name = "listOfStrings", EnumerableOf = new TextField() });

                using (var session = Storage.GetAsyncSession()) {
                    session.StoreAsync(schema).Wait();
                    session.SaveChangesAsync().Wait();
                }

                // act
                var model = new Model { };
                model["listOfStrings"] = new object[] { null };


                var json = JsonConvert.SerializeObject(model, this.JsonConverter);
                var response =
                    server.CreateRequest("api/models/{0}".FormatString(schema.Name))
                          .And(message => message.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                          .PostAsync()
                          .Result;

                // verify
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                var content = response.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<ApiError>(content);
                Assert.Equal(ApiErrorCode.SchemaMismatch, result.ErrorCode);
            }
        }
        public void Should_not_be_able_create_a_model_without_all_fields_defined_on_MetaType(string[] fieldNames) {
            using (var server = TestServer.Create<TestStartup>()) {
                // arrange
                var schema = new MetaType { Name = "simple_schema" };
                foreach (var fieldName in fieldNames) {
                    schema.Fields.Add(new TextField { Name = fieldName });
                }

                using (var session = Storage.GetAsyncSession()) {
                    session.StoreAsync(schema).Wait();
                    session.SaveChangesAsync().Wait();
                }

                // act
                var someFields = fieldNames.Take(3);
                var model = new Model { };
                foreach (var fieldName in someFields) {
                    model[fieldName] = "Some Text";
                }

                var json = JsonConvert.SerializeObject(model, this.JsonConverter);
                var response =
                    server.CreateRequest("api/models/{0}".FormatString(schema.Name))
                          .And(message => message.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                          .PostAsync()
                          .Result;

                // verify
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                var content = response.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<ApiError>(content);
                Assert.Equal(ApiErrorCode.SchemaMismatch, result.ErrorCode);
            }
        }
        public void Creating_a_Model_should_return_the_object_with_expected_metatype_info_embedded(string metaTypeName, string displayName) {
            using (var server = TestServer.Create<TestStartup>()) {

                // arrange
                var metaType = new MetaType { Name = metaTypeName, DisplayName = displayName, };
                using (var session = Storage.GetAsyncSession()) {
                    session.StoreAsync(metaType).Wait();
                    session.SaveChangesAsync().Wait();
                }

                var model = new Model { };
                var json = JsonConvert.SerializeObject(model, this.JsonConverter);

                // act
                var response =
                    server.CreateRequest("api/models/{0}".FormatString(metaTypeName))
                          .And(message => message.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                          .PostAsync()
                          .Result;

                // verify
                Assert.Equal(HttpStatusCode.Created, response.StatusCode);

                var result = response.Content.ReadAsStringAsync().Result;
                var returnedModel = JsonConvert.DeserializeObject<Model>(result, this.JsonConverter);
                Assert.Equal(metaType.Id, returnedModel._shurikenMeta.MetaTypeId);
                Assert.Equal(metaType.Name, returnedModel._shurikenMeta.MetaTypeName);
                Assert.Equal(metaType.DisplayName, returnedModel._shurikenMeta.MetaTypeDisplayName);
            }
        }
        public void Should_not_be_able_update_Models_with_fields_not_specified_on_the_MetaType(
            string fieldName,
            string fieldNameNotInSchema) {
            using (var server = TestServer.Create<TestStartup>()) {
                // arrange
                var schema = new MetaType { Name = "simple_schema" };
                var model = new Model { };
                model[fieldName] = "Some Text";
                schema.Fields.Add(new TextField { Name = fieldName });

                using (var session = Storage.GetAsyncSession()) {
                    session.StoreAsync(schema).Wait();
                    model.SaveAsync(schema.Name, session).Wait();
                    session.SaveChangesAsync().Wait();
                }

                // act
                model[fieldNameNotInSchema] = "Some Text";

                var json = JsonConvert.SerializeObject(model, this.JsonConverter);
                var response =
                    server.CreateRequest("api/models/{0}".FormatString(schema.Name))
                          .And(message => message.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                          .SendAsync("PUT")
                          .Result;

                // verify
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                var content = response.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<ApiError>(content);
                Assert.Equal(ApiErrorCode.SchemaMismatch, result.ErrorCode);
            }
        }
예제 #5
0
 private void WriteModelMetaData(JsonWriter writer, Model.MetaData obj) {
     var t = obj.GetType();
     var properties = t.GetProperties();
     writer.WriteStartObject();
     foreach (var property in properties) {
         writer.WritePropertyName(property.Name);
         writer.WriteValue(property.GetValue(obj));
     }
     writer.WriteEndObject();
 }
예제 #6
0
        public virtual bool Equals(Model other) {
            if (ReferenceEquals(null, other)) {
                return false;
            }

            if (ReferenceEquals(this, other)) {
                return true;
            }

            return this.Id.Equals(other.Id);
        }
예제 #7
0
        private Model ReadModel(JsonReader reader) {
            while (reader.TokenType == JsonToken.Comment) {
                if (!reader.Read()) throw new JsonSerializationException("Unexpected Token when converting Model object");
            }

            if (reader.TokenType != JsonToken.StartObject) {
                throw new JsonSerializationException("Unexpected Token when converting Model object");
            }

            var obj = new Model { };

            while (reader.Read()) {
                switch (reader.TokenType) {
                    case JsonToken.PropertyName:
                        var propertyName = reader.Value.ToString();

                        if (!reader.Read()) {
                            throw new JsonSerializationException("Unexpected end when reading Model object");
                        }

                        if (propertyName == "Id") {
                            obj.Id = (string)this.ReadValue(reader);
                            break;
                        }

                        if (propertyName == "_shurikenMeta") {
                            obj._shurikenMeta = this.ReadMetaData(reader);
                            break;
                        }

                        var v = this.ReadValue(reader);

                        obj[propertyName] = v;
                        break;
                    case JsonToken.Comment:
                        break;
                    case JsonToken.EndObject:
                        return obj;
                }
            }

            throw new JsonSerializationException("Unexpected end when reading Model object");
        }
        public void Should_not_be_able_to_create_models_with_ArbitraryObject_fields_N_levels_deep_with_an_empty_object_value(int n) {
            using (var server = TestServer.Create<TestStartup>()) {
                // arrange
                var schema = new MetaType { Name = "simple_schema" };
                var i = 0;
                var fields = schema.Fields;
                while (i < n) {
                    var obj = new ArbitraryObjectField { Name = "obj{0}".FormatString(i) };
                    fields.Add(obj);
                    fields = obj.Fields;
                    i++;
                }
                using (var session = Storage.GetAsyncSession()) {
                    session.StoreAsync(schema).Wait();
                    session.SaveChangesAsync().Wait();
                }

                // act
                i = 0;
                var model = new Model { };
                var o = model as Dough;
                while (i < n) {
                    var fieldName = "obj{0}".FormatString(i);
                    o[fieldName] = new Dough { };
                    o = o[fieldName] as Dough;
                    i++;
                }
                var json = JsonConvert.SerializeObject(model, this.JsonConverter);
                var response =
                    server.CreateRequest("api/models/{0}".FormatString(schema.Name))
                          .And(message => message.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                          .PostAsync()
                          .Result;

                // verify
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                var content = response.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<ApiError>(content);
                Assert.Equal(ApiErrorCode.SchemaMismatch, result.ErrorCode);
            }
        }