public IStorageDeviceReading AddDeviceReading(DateTime time, IDictionary <string, object> values)
        {
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();

            return(this.AddDeviceReading(device, time, values));
        }
Пример #2
0
        private IList <IStorageChannel> CreateSensorChannels()
        {
            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousDevice(TestData.device_02_id.UrnToId(),
                                                TestData.device_02_token);
            //OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            //IStorageDevice = storage.GetCurrentUser();

            IStorageAlert alert_1 = new IStorageAlert()
            {
                SensorType  = "I0",
                IntMaxValue = 100,
                IntMinValue = -10
            };
            IStorageAlert alert_2 = new IStorageAlert()
            {
                SensorType  = "I0",
                IntMaxValue = 40,
                IntMinValue = 0
            };

            List <IStorageAlert> alerts = new List <IStorageAlert>()
            {
                alert_1, alert_2
            };

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            IStorageChannel channel = storage.AddChannel("xUnit_IngestTests_Channel_01",
                                                         "A test channel for xUnit AzureStorageIngestTests", "I0", "Test integer units",
                                                         alerts);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            alert_1 = new IStorageAlert()
            {
                SensorType  = "S0",
                StringValue = "Alert 1!"
            };
            alert_2 = new IStorageAlert()
            {
                SensorType  = "S0",
                StringValue = "Alert 1!"
            };

            alerts = new List <IStorageAlert>()
            {
                alert_1, alert_2
            };
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            IStorageChannel channel_2 = storage.AddChannel("xUnit_IngestTests_Channel_03",
                                                           "A test channel for xUnit AzureStorageIngestTests", "S0", "Test stringr units",
                                                           alerts);

            return(new List <IStorageChannel>()
            {
                channel, channel_2
            });
        }
Пример #3
0
        public static void AddDeviceReading(DateTime time, IDictionary <string, object> values)
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
            storage.AddDeviceReading(time, values);
            Log.AddDeviceReadingSuccess("Added device readings.");
        }
Пример #4
0
        public void CanAddUser()
        {
            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id,
                                                         TestData.user_01_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            AzureStorage storage = new AzureStorage();
            IStorageUser user    = storage.AddUser("xUnit_CanAddUserTest_Name", "xUnit_CanAddUserTest_Token", null);

            Assert.NotNull(user.Id);
            Assert.Equal("xUnit_CanAddUserTest_Name", user.UserName);
            Assert.Equal("xUnit_CanAddUserTest_Token", user.Token);
        }
Пример #5
0
        public void CanFindUser()
        {
            //InitialiseTestData();
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(), TestData.user_02_token);

            Assert.NotNull(user);
            Assert.Equal(user.UserName, TestData.user_02_name);
            Assert.Equal(user.Token, TestData.user_02_token);
        }
        public void CanAuthorizeDeleteUser()
        {
            OverlordIdentity.InitializeUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser("d155074f-4e85-4cb5-a597-8bfecb0dfc04".ToGuid(), "admin");

            Assert.Throws(typeof(System.Security.SecurityException), () => storage.DeleteUser(user));
            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            Assert.Throws(typeof(System.Security.SecurityException), () => storage.DeleteUser(user));
        }
Пример #7
0
        public void CanAddDeviceReading()
        {
            Assert.True(Api.AuthenticateDevice(TestData.device_01_id, TestData.device_01_token));
            OverlordIdentity.AddClaim(Resource.Api, ApiAction.AddReading);
            DateTime time = TestData.GenerateRandomTime(null, null, null,
                                                        DateTime.Now.Hour);
            IDictionary <string, object> sensor_values = new Dictionary <string, object>()
            {
                { TestData.sensor_01_name, TestData.GenerateRandomString(43) }
            };

            Api.AddDeviceReading(time, sensor_values);
        }
Пример #8
0
        public void CanAddSensor()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeAnonymousIdentity();
            storage.AuthenticateAnonymousDevice(TestData.device_02_id.UrnToId(),
                                                TestData.device_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
            IStorageSensor sensor = storage.AddSensor(TestData.sensor_01_name, TestData.sensor_01_unit, null, null);

            Assert.NotNull(sensor);
            Assert.Equal(sensor.Name, TestData.sensor_01_name);
            Assert.Equal(sensor.Unit, TestData.sensor_01_unit);
        }
Пример #9
0
        private void InitialiseTestData()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id, TestData.user_01_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_01 = storage.AddUser(TestData.user_01_name, TestData.user_01_token, null, TestData.user_01_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_02 = storage.AddUser(TestData.user_02_name, TestData.user_02_token, null, TestData.user_02_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_03 = storage.AddUser(TestData.user_03_name, TestData.user_03_token, null, TestData.user_03_id);
        }
Пример #10
0
        public void CanFindDevice()
        {
            //InitialiseTestData();
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id, TestData.user_02_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_01_id.UrnToGuid(), TestData.user_01_token);

            Assert.NotNull(user);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = storage.FindDevice(TestData.device_01_id.UrnToGuid(), TestData.device_01_token);

            Assert.NotNull(device);
        }
Пример #11
0
        public void CanAddDeviceReadingParallel()
        {
            Dictionary <int, IStorageDevice> devices = new Dictionary <int, IStorageDevice>();

            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousUser(TestData.user_02_id.UrnToId(),
                                              TestData.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.GetCurrentUser();

            for (int i = 0; i <= 5; i++)

            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
                IStorageDevice device = storage.AddDevice(user, "xUnit_CanAddDevicereadingParallel_Device_" + i.ToString(),
                                                          "xUnit_CanAddDevicereadingParallel_Device_Token", null, null);
                devices.Add(i, device);
            }
            OverlordIdentity.InitializeAnonymousIdentity();
            Parallel.For(0, devices.Count, d =>
            {
                Log.Partition();
                storage.AuthenticateAnonymousDevice(devices[d].Id.ToUrn(), devices[0].Token);
                IDictionary <string, object> sensor_values = TestData.GenerateRandomSensorData(10);
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         sensor_values);
                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values));

                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values));
            });
        }
Пример #12
0
        public IStorageSensor AddSensor(string sensor_name, string sensor_units,
                                        IList <Guid> sensor_channels, IList <IStorageAlert> sensor_alerts)
        {
            if (!sensor_name.IsVaildSensorName())
            {
                throw new ArgumentException(
                          string.Format("Invalid sensor name: {0}", sensor_name));
            }
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();
            IStorageSensor sensor = new IStorageSensor()
            {
                DeviceId = device.Id,
                Name     = sensor_name,
                Unit     = sensor_units,
                Channels = sensor_channels,
                Alerts   = sensor_alerts
            };

            if (device.Sensors.Keys.Contains(sensor_name))
            {
                device.Sensors.Remove(sensor_name);
            }
            device.Sensors.Add(sensor_name, sensor);
            try
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.UpdateDevice);
                this.UpdateDevice(device);
                Log.WriteTableSuccess(string.Format("Added sensor {0} to device entity: Id: {1}, Token: {2}",
                                                    sensor.Name, device.Id.ToUrn(), device.Token));
                return(sensor);
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to read table for device: Id: {0}, Token: {1}.",
                                                   device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddSensor);
            }
        }
Пример #13
0
        public void CanAddDeviceReading()
        {
            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousDevice(TestData.device_01_id.UrnToId(),
                                                TestData.device_01_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = storage.GetCurrentDevice();
            IDictionary <string, object> sensor_values = TestData.GenerateRandomSensorData(10);

            foreach (KeyValuePair <string, object> s in sensor_values)
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
                storage.AddSensor(s.Key, "CanAddDeviceReading_Test", null, null);
            }
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
            storage.AddDeviceReading(DateTime.Now, sensor_values);
        }
        public void CanAuthorizeAddSensor()
        {
            AzureStorage storage = new AzureStorage();

            Assert.Throws(typeof(System.Security.SecurityException), () =>
                          storage.AddSensor("foo", "bar", null, null));

            //Throws security exception even if correct identity.
            OverlordIdentity.InitializeDeviceIdentity(TestData.device_01_id.UrnToId(), TestData.device_01_token, new string[0]);
            Assert.Throws(typeof(System.Security.SecurityException), () =>
                          storage.AddSensor("foo", "bar", null, null));

            //Doesn't throw security exception when proper permission is present.
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
            IStorageSensor s = storage.AddSensor(TestData.sensor_01_name, "bar", null, null);

            Assert.NotNull(s);
            Assert.True(s.Name == TestData.sensor_01_name);
        }
Пример #15
0
        public void CanAddDevice()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id,
                                                    TestData.user_02_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(),
                                                 TestData.user_02_token);

            Assert.NotNull(user);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device = storage.AddDevice(user, "xUnit_CanAddDeviceTest_Name",
                                                      "xUnit_CanAddDeviceTest_Token", null);

            Assert.NotNull(device.Id);
            Assert.Equal("xUnit_CanAddDeviceTest_Name", device.Name);
            Assert.Equal("xUnit_CanAddDeviceTest_Token", device.Token);
            Assert.True(user.Devices.Contains(device.Id));
        }
Пример #16
0
        private void InitialiseTestData()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id, TestData.user_02_token, new string[0]);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(), TestData.user_02_token);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_01 = storage.AddDevice(user, TestData.device_01_name, TestData.device_01_token, null, TestData.device_01_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            //user = storage.FindUser(AzureStorageTests.user_02_id.UrnToGuid(), AzureStorageTests.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_02 = storage.AddDevice(user, TestData.device_02_name, TestData.device_02_token, null, TestData.device_02_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            //user = storage.FindUser(AzureStorageTests.user_02_id.UrnToGuid(), AzureStorageTests.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_03 = storage.AddDevice(user, TestData.device_03_name, TestData.device_03_token, null, TestData.device_03_id);
        }
Пример #17
0
        public IStorageChannel AddChannel(string channel_name, string channel_description,
                                          string sensor_type, string channel_units, List <IStorageAlert> alerts)
        {
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();

            IStorageChannel channel = new IStorageChannel()
            {
                Id          = Guid.NewGuid(),
                Name        = channel_name,
                Description = channel_description,
                SensorType  = sensor_type,
                Alerts      = alerts
            };

            try
            {
                TableOperation insert_channel_operation = TableOperation
                                                          .Insert(AzureStorage.CreateChannelTableEntity(channel));
                TableResult result;
                result = this.ChannelsTable.Execute(insert_channel_operation);
                Log.WriteTableSuccess(string.Format("Added Channel entity: {0}, Id: {1}.",
                                                    channel.Name, channel.Id.ToUrn()));
                return(channel);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add Channel entity: {0}, Id: {1}.", channel.Name,
                                                    channel.Id), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddChannel);
            }
        }
Пример #18
0
 public IStorageUser GetCurrentUser()
 {
     OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
     return(this.FindUser(OverlordIdentity.CurrentUserId.UrnToGuid(), OverlordIdentity.CurrentUserToken));
 }
Пример #19
0
        private void CreateDeviceReadings()
        {
            IList <IStorageChannel>          channels = CreateSensorChannels();
            Dictionary <int, IStorageDevice> devices  = new Dictionary <int, IStorageDevice>();

            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousUser(TestData.user_02_id.UrnToId(),
                                              TestData.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.GetCurrentUser();

            for (int i = 0; i <= 5; i++)
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
                IStorageDevice device = storage.AddDevice(user, "xUnit_IngestTests_Device_" + i.ToString(),
                                                          "xUnit_IngestTests_Device_Token", null, null);
                devices.Add(i, device);
            }
            IDictionary <int, IDictionary <string, object> > sensor_values = new Dictionary <int, IDictionary <string, object> >(6);

            for (int d = 0; d <= 5; d++)
            {
                sensor_values[d] = TestData.GenerateRandomSensorData(10);
                foreach (string s in sensor_values[d].Keys)
                {
                    IStorageSensor sensor = new IStorageSensor()
                    {
                        DeviceId = devices[d].Id,
                        Name     = s,
                        Channels = new List <Guid>()
                    };
                    List <IStorageAlert> alerts = new List <IStorageAlert>();
                    foreach (IStorageChannel channel in channels.Where(c => c.SensorType.ToSensorType() ==
                                                                       sensor.Name.ToSensorType()))
                    {
                        sensor.Channels.Add(channel.Id);
                    }
                    OverlordIdentity.AddClaim(Resource.Storage, StorageAction.UpdateDevice);
                    devices[d].Sensors.Add(new KeyValuePair <string, IStorageSensor>(s, sensor));
                    devices[d] = storage.UpdateDevice(devices[d]);
                }
            }
            OverlordIdentity.InitializeAnonymousIdentity();
            storage.AuthenticateAnonymousDevice(devices[0].Id.ToUrn(), devices[0].Token);
            for (int d = 0; d < devices.Count(); d++)
            {
                //Log.Partition();
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(devices[d], TestData.GenerateRandomTime(null, null, null, null),
                                         sensor_values[d]);
                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(devices[d], TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values[d]));

                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(devices[d], TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values[d]));
            }
        }