コード例 #1
0
        public void OwnerDeserializeTest()
        {
            DateTime now = TestUtils.GetNowIgnoringMilis();

            string json =
                "{\"username\":\"test\"," +
                "\"x_password\":\"password\"," +
                "\"x_registration_date\":\"" + now.ToString(EventSerializerTest.DatetimeFormat) + "\"," +
                "\"age\": 89," +
                "\"weight\": 125.5," +
                "\"married\": true," +
                "\"counter\": -13582," +
                "\"list_owner\": [\"val1\",\"val2\",\"val3\"]," +
                "\"event_id\":\"46aabccd-4442-6665-a1f0-49889330eaf3\"}";

            var attributes = ImmutableDictionary.CreateBuilder <string, object>();

            attributes.Add("age", 89);
            attributes.Add("weight", 125.5);
            attributes.Add("married", true);
            attributes.Add("counter", -13582);
            attributes.Add("list_owner", new string[] { "val1", "val2", "val3" });

            Owner owner = OwnerSerializer.DeserializeOwner(json);

            Assert.AreEqual(owner.Username, "test");
            Assert.AreEqual(owner.EventId.ToString(), "46aabccd-4442-6665-a1f0-49889330eaf3");
            Assert.AreEqual(owner.Password, "password");
            Assert.AreEqual(owner.RegistrationDate.Value.ToString(EventSerializerTest.DatetimeFormat),
                            now.ToString(EventSerializerTest.DatetimeFormat));
            CollectionAssert.AreEqual(owner.Attributes, attributes.ToImmutable());
        }
コード例 #2
0
        public void OwnerSerializeTest()
        {
            DateTime now = TestUtils.GetNowIgnoringMilis();

            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("string", "stringValue");
            attributes.Add("double", 10d);
            attributes.Add("float", 10.5f);
            attributes.Add("boolean", false);

            Owner owner = new Owner.Builder()
            {
                Username         = "******",
                Password         = "******",
                RegistrationDate = now,
                Attributes       = attributes,
                EventId          = Guid.Parse("9ab392d8-a865-48da-9035-0dc0a728b454")
            };

            string json = OwnerSerializer.SerializeOwner(owner);

            Assert.AreEqual(
                "{\"username\":\"username\"," +
                "\"x_registration_date\":\"" + now.ToString(EventSerializerTest.DatetimeFormat) + "\"," +
                "\"x_password\":\"password\"," +
                "\"event_id\":\"9ab392d8-a865-48da-9035-0dc0a728b454\"," +
                "\"boolean\":false," +
                "\"float\":10.5," +
                "\"double\":10.0," +
                "\"string\":\"stringValue\"}",
                json);
        }
コード例 #3
0
        public void OwnerSerializeTestDeprecatedDate()
        {
            DateTimeOffset now = new DateTimeOffset(2017, 06, 12, 18, 05, 05, TimeSpan.FromHours(5d));

            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("string", "stringValue");
            attributes.Add("double", 10d);
            attributes.Add("float", 10.5f);
            attributes.Add("boolean", false);

            Owner owner = new Owner.Builder()
            {
                Username         = "******",
                Password         = "******",
                RegistrationDate = now.UtcDateTime,
                Attributes       = attributes,
            };

            string json = OwnerSerializer.SerializeOwner(owner);

            TestUtils.AssertJsonEquals(json, new List <string>
            {
                "\"username\":\"username\"",
                "\"x_registration_date\":\"2017-06-12T13:05:05Z\"",
                "\"x_password\":\"password\"",
                "\"float\":10.5",
                "\"boolean\":false",
                "\"double\":10.0",
                "\"string\":\"stringValue\""
            });
        }
コード例 #4
0
        public void OwnerSerializeTestWithEmptyOwner()
        {
            Owner owner = new Owner.Builder().Build();

            string json = OwnerSerializer.SerializeOwner(owner);

            Assert.AreEqual("{}", json);
        }
コード例 #5
0
        public void OwnerDeserializeTestWrongRegistrationTimeType()
        {
            string json = "{\"x_registration_date\":\"6565g\"}";

            Assert.That(() => OwnerSerializer.DeserializeOwner(json),
                        Throws.TypeOf <InvalidOperationException>()
                        .With.Message.EqualTo("Field 'x_registration_date' does not match TYPE 'DATETIME'"));
        }
コード例 #6
0
        public void OwnerDeserializeTestWrongUsernameType()
        {
            string json = "{\"username\":false}";

            Assert.That(() => OwnerSerializer.DeserializeOwner(json),
                        Throws.TypeOf <InvalidOperationException>()
                        .With.Message.EqualTo("Field 'username' does not match TYPE 'TEXT'"));
        }
コード例 #7
0
        public void OwnerDeserializeTestWrongEventIdType()
        {
            string json = "{\"username\":\"test\",\"event_id\":\"54545c5454-054-54\",\"string\":\"stringValue\"}";

            Assert.That(() => OwnerSerializer.DeserializeOwner(json),
                        Throws.TypeOf <InvalidOperationException>()
                        .With.Message.EqualTo("Field 'x_event_id' does not match TYPE 'GUID'"));
        }
コード例 #8
0
        public void OwnerDeserializeTestWrongPasswordType()
        {
            string json = "{\"x_password\":9898.3}";

            Assert.That(() => OwnerSerializer.DeserializeOwner(json),
                        Throws.TypeOf <InvalidOperationException>()
                        .With.Message.EqualTo("Field 'x_password' does not match TYPE 'TEXT'"));
        }
コード例 #9
0
        public static List <Owner> DeserializeOwners(string json)
        {
            List <Owner> owners = new List <Owner>();

            foreach (var jsonObject in JArray.Parse(json))
            {
                owners.Add(OwnerSerializer.DeserializeOwner(jsonObject.ToString(Formatting.None)));
            }
            return(owners);
        }
コード例 #10
0
        public void OwnerDeserializeTestCheckNull()
        {
            string json = "{}";

            Owner owner = OwnerSerializer.DeserializeOwner(json);

            Assert.IsNull(owner.Password);
            Assert.IsNull(owner.Username);
            Assert.IsNull(owner.RegistrationDate);
            Assert.IsNotNull(owner.Attributes);
            Assert.AreEqual(owner.Attributes.Count, 0);
        }
コード例 #11
0
        public void OwnerSerializeTestAttributeList()
        {
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("list", new List <string>()
            {
                "1", "2"
            });

            Owner owner = new Owner.Builder()
            {
                Attributes = attributes
            };

            string json = OwnerSerializer.SerializeOwner(owner);

            Assert.AreEqual("{\"list\":[\"1\",\"2\"]}", json);
        }
コード例 #12
0
        public SucceedAPIsMockModule()
        {
            Post(BasePath + "objects", x => {
                TestUtils.AssertObjectEquals(TestUtils.CreateTestObject(), ObjectSerializer.DeserializeObject(NancyUtils.BodyAsString(this.Request)));
                return(201);
            });

            Put(BasePath + "objects", x => {
                List <SmartObject> objects = TestUtils.DeserializeObjects(NancyUtils.BodyAsString(this.Request));
                TestUtils.AssertObjectsEqual(TestUtils.CreateObjects(objects.Count <SmartObject>()), objects);

                List <Result> results = new List <Result>();
                foreach (SmartObject anObject in objects)
                {
                    results.Add(new Result(anObject.DeviceId, Result.ResultStates.Success, null));
                }

                return(JsonConvert.SerializeObject(results));
            });

            Put(BasePath + "objects/{deviceId}", x => {
                TestUtils.AssertObjectEquals(TestUtils.CreateObjectUpdateAttribute(), ObjectSerializer.DeserializeObject(NancyUtils.BodyAsString(this.Request)));
                Assert.AreEqual(TestUtils.DeviceId, (string)x.deviceId);
                return(200);
            });

            Delete(BasePath + "objects/{deviceId}", x => {
                Assert.AreEqual(TestUtils.DeviceId, (string)x.deviceId);
                return(200);
            });

            Get(BasePath + "objects/exists/{deviceId}", x => {
                var deviceId = (string)x.deviceId;
                Assert.AreEqual(TestUtils.DeviceId, deviceId);
                IDictionary <string, bool> result = new Dictionary <string, bool>()
                {
                    { deviceId, true }
                };
                return(JsonConvert.SerializeObject(result));
            });

            Post(BasePath + "owners", x => {
                TestUtils.AssertOwnerEquals(TestUtils.CreateTestOwner(), OwnerSerializer.DeserializeOwner(NancyUtils.BodyAsString(this.Request)));
                return(201);
            });

            Put(BasePath + "owners", x => {
                List <Owner> owners = TestUtils.DeserializeOwners(NancyUtils.BodyAsString(this.Request));
                TestUtils.AssertOwnersEqual(TestUtils.CreateOwners(owners.Count <Owner>()), owners);

                List <Result> results = new List <Result>();
                foreach (Owner owner in owners)
                {
                    results.Add(new Result(owner.Username, Result.ResultStates.Success, null));
                }

                return(JsonConvert.SerializeObject(results));
            });

            Put(BasePath + "owners/{username}", x => {
                TestUtils.AssertOwnerEquals(TestUtils.CreateOwnerUpdateAttribute(), OwnerSerializer.DeserializeOwner(NancyUtils.BodyAsString(this.Request)));
                Assert.AreEqual(TestUtils.Username, (string)x.username);
                return(200);
            });

            Put(BasePath + "owners/{username}/password", x => {
                Dictionary <string, string> body = JsonConvert.DeserializeObject <Dictionary <string, string> >(NancyUtils.BodyAsString(this.Request));
                Assert.IsTrue(body.ContainsKey("x_password"));
                String password = "";
                body.TryGetValue("x_password", out password);
                Assert.AreEqual(TestUtils.Password, password);
                Assert.AreEqual(TestUtils.Username, (string)x.username);
                return(200);
            });

            Post(BasePath + "owners/{username}/objects/{deviceId}/claim", x => {
                Assert.AreEqual(TestUtils.DeviceId, (string)x.deviceId);
                Assert.AreEqual(TestUtils.Username, (string)x.username);
                return(200);
            });

            Post(BasePath + "owners/{username}/objects/{deviceId}/unclaim", x => {
                Assert.AreEqual(TestUtils.DeviceId, (string)x.deviceId);
                Assert.AreEqual(TestUtils.Username, (string)x.username);
                return(200);
            });

            Delete(BasePath + "owners/{username}", x => {
                Assert.AreEqual(TestUtils.Username, (string)x.username);
                return(200);
            });

            Get(BasePath + "owners/exists/{username}", x => {
                string username = (string)x.username;
                Assert.AreEqual(TestUtils.Username, username);
                IDictionary <string, bool> result = new Dictionary <string, bool>()
                {
                    { username, true }
                };
                return(JsonConvert.SerializeObject(result));
            });

            Post(BasePath + "events", x => {
                bool reportResults = (bool)this.Request.Query["report_results"];

                Assert.AreEqual(true, reportResults);
                List <EventResult> results = TestUtils.EventsToSuccesfullResults(NancyUtils.BodyAsString(this.Request));
                return(JsonConvert.SerializeObject(results));
            });

            Get(BasePath + "events/exists/{eventId}", x => {
                IDictionary <string, bool> result = new Dictionary <string, bool>()
                {
                    { (string)x.eventId, true }
                };
                return(JsonConvert.SerializeObject(result));
            });

            Get(BasePath + "search/datasets", x => {
                return(JsonConvert.SerializeObject(TestUtils.CreateDatasets()));
            });

            Post(BasePath + "search/basic", x => {
                Assert.AreEqual(TestUtils.CreateQuery(), NancyUtils.BodyAsString(this.Request));
                return(TestUtils.CreateExpectedSearchResult());
            });

            // test HttpClient itself
            Post(BasePath + "compressed", x => {
                if (!NancyUtils.IsGzipCompressed(this.Request))
                {
                    return(FailedAPIsMockModule.badRequest());
                }

                if (!this.Request.Headers.AcceptEncoding.Contains("gzip"))
                {
                    return(FailedAPIsMockModule.badRequest());
                }

                var body = NancyUtils.BodyAsString(this.Request);
                if (body != TestJsonString)
                {
                    return(FailedAPIsMockModule.badRequest());
                }

                var data     = Encoding.UTF8.GetBytes(body);
                var response = new Response();
                response.Headers.Add("Content-Encoding", "gzip");
                response.Headers.Add("Content-Type", "application/json");
                response.Contents = stream =>
                {
                    using (var gz = new GZipStream(stream, CompressionMode.Compress))
                    {
                        gz.Write(data, 0, data.Length);
                        gz.Flush();
                    }
                };
                return(response);
            });

            Post(BasePath + "decompressed", x => {
                if (NancyUtils.IsGzipCompressed(this.Request))
                {
                    return(FailedAPIsMockModule.badRequest());
                }

                var body = NancyUtils.BodyAsString(this.Request);
                if (body != TestJsonString)
                {
                    return(FailedAPIsMockModule.badRequest());
                }

                return(TestJsonString);
            });

            Post(BasePath + "tokencheck", x => {
                return(this.Request.Headers.Authorization);
            });
        }