Example #1
0
        public void DeserializeTestNoAuthChain()
        {
            string id = "id";
            string serializedString = $"{{\"Identity\":\"{id}\"}}";
            BrokerServiceIdentity brokerServiceIdentity = JsonConvert.DeserializeObject <BrokerServiceIdentity>(serializedString);

            Assert.Equal(brokerServiceIdentity.Identity, id);
            Assert.False(brokerServiceIdentity.AuthChain.HasValue);
        }
Example #2
0
        public void SerializeTest()
        {
            string id        = "id";
            string authChain = "authChain";
            BrokerServiceIdentity brokerServiceIdentity = new BrokerServiceIdentity(id, Option.Some(authChain));
            var s = JsonConvert.SerializeObject(brokerServiceIdentity);

            Assert.Equal($"{{\"Identity\":\"{id}\",\"AuthChain\":\"{authChain}\"}}", s);
        }
Example #3
0
        public void DeserializeTest()
        {
            string id               = "id";
            string authChain        = "authChain";
            string serializedString = $"{{\"Identity\":\"{id}\",\"AuthChain\":\"{authChain}\"}}";
            BrokerServiceIdentity brokerServiceIdentity = JsonConvert.DeserializeObject <BrokerServiceIdentity>(serializedString);

            Assert.Equal(brokerServiceIdentity.Identity, id);
            Assert.Equal(brokerServiceIdentity.AuthChain.OrDefault(), authChain);
        }
Example #4
0
        public void CompareTest()
        {
            string id1        = "id";
            string id2        = "id";
            string id3        = "id3";
            string authChain1 = "authChain";
            string authChain2 = "authChain";
            string authChain3 = "authChain3";
            BrokerServiceIdentity brokerServiceIdentity1 = new BrokerServiceIdentity(id1, Option.Some(authChain1));
            BrokerServiceIdentity brokerServiceIdentity2 = new BrokerServiceIdentity(id2, Option.Some(authChain2));
            BrokerServiceIdentity brokerServiceIdentity3 = new BrokerServiceIdentity(id3, Option.Some(authChain3));

            Assert.Equal(brokerServiceIdentity1, brokerServiceIdentity2);
            Assert.NotEqual(brokerServiceIdentity1, brokerServiceIdentity3);
        }
Example #5
0
        public void PublishAddIdentitiesTest()
        {
            // Arrange
            var capture                    = new SendCapture();
            var connectionState            = new TaskCompletionSource <bool>();
            var connector                  = GetConnector(capture, connectionState);
            var deviceScopeIdentitiesCache = new Mock <IDeviceScopeIdentitiesCache>();
            var sut = new ScopeIdentitiesHandler(Task.FromResult(deviceScopeIdentitiesCache.Object));

            sut.SetConnector(connector.Object);
            var serviceIdentity  = new ServiceIdentity("d1", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity2 = new ServiceIdentity("d2", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity3 = new ServiceIdentity("d3", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);

            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain(It.IsAny <string>())).ReturnsAsync(Option.Some("testAuthChain"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAllIds()).ReturnsAsync(new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });
            BrokerServiceIdentity identity  = new BrokerServiceIdentity("d1", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity2 = new BrokerServiceIdentity("d2", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity3 = new BrokerServiceIdentity("d3", Option.Some("testAuthChain"));

            connectionState.SetResult(true);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string> {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });

            // Assert
            capture.WhenCaptured().Wait();

            Assert.Equal(Topic, capture.Topic);
            Assert.Equal(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new List <BrokerServiceIdentity>()
            {
                identity, identity2, identity3
            })), capture.Content);
        }
Example #6
0
        public void UpdateIdentitiesBeforeConnectTest()
        {
            // Arrange
            var capture   = new SendCapture();
            var connector = GetConnector(capture);
            var deviceScopeIdentitiesCache = new Mock <IDeviceScopeIdentitiesCache>();
            var serviceIdentity            = new ServiceIdentity("d1", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity2           = new ServiceIdentity("d2", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity3           = new ServiceIdentity("d3", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);

            BrokerServiceIdentity identity  = new BrokerServiceIdentity("d1", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity2 = new BrokerServiceIdentity("d2", Option.Some("testAuthChain2"));
            BrokerServiceIdentity identity3 = new BrokerServiceIdentity("d3", Option.Some("testAuthChain3"));

            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d1")).ReturnsAsync(Option.Some("testAuthChain"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d2")).ReturnsAsync(Option.Some("testAuthChain2"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d3")).ReturnsAsync(Option.Some("testAuthChain3"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAllIds()).ReturnsAsync(new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });

            var sut = new ScopeIdentitiesHandler(Task.FromResult(deviceScopeIdentitiesCache.Object));

            sut.SetConnector(connector.Object);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id
            });

            // Assert
            Assert.Null(capture.Topic);
            Assert.Null(capture.Content);

            // Act
            connector.Raise(c => c.OnConnected += null, null, null);

            // Assert
            Assert.Equal(Topic, capture.Topic);
            IList <BrokerServiceIdentity> brokerServiceIdentities = JsonConvert.DeserializeObject <IList <BrokerServiceIdentity> >(Encoding.UTF8.GetString(capture.Content));

            Assert.Equal(3, brokerServiceIdentities.Count);
            Assert.Contains(identity, brokerServiceIdentities);
            Assert.Contains(identity2, brokerServiceIdentities);
            Assert.Contains(identity3, brokerServiceIdentities);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id
            });

            // Assert
            Assert.Equal(Topic, capture.Topic);
            brokerServiceIdentities = JsonConvert.DeserializeObject <IList <BrokerServiceIdentity> >(Encoding.UTF8.GetString(capture.Content));
            Assert.Equal(2, brokerServiceIdentities.Count);
            Assert.Contains(identity, brokerServiceIdentities);
            Assert.Contains(identity2, brokerServiceIdentities);
        }