public async Task BadArgShouldThrowExceptionsAsync()
        {
            using (var mock = AutoMock.GetLoose(builder => {
                builder.RegisterType <NewtonSoftJsonConverters>().As <IJsonSerializerConverterProvider>();
                builder.RegisterType <NewtonSoftJsonSerializer>().As <IJsonSerializer>();
                builder.RegisterType <IoTHubServices>().As <IIoTHubTwinServices>();
                builder.RegisterType <ApplicationTwins>().As <IApplicationRepository>();
            })) {
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.RegisterApplicationAsync(null));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.GetApplicationAsync(null, false));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.GetApplicationAsync("", false));

                await Assert.ThrowsAsync <ResourceNotFoundException>(
                    () => service.GetApplicationAsync("abc", false));

                await Assert.ThrowsAsync <ResourceNotFoundException>(
                    () => service.GetApplicationAsync(Guid.NewGuid().ToString(), false));
            }
        }
        public void RejectNewApplication()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose()) {
                var hub = IoTHubServices.Create(devices);
                mock.Provide <IIoTHubTwinServices>(hub);
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                var app = apps.First();
                service.RejectApplicationAsync(app.ApplicationId, false, null).Wait();
                var registration = service.GetApplicationAsync(app.ApplicationId, false).Result;

                Assert.Equal(ApplicationState.Rejected, registration.Application.State);

                // reject approved app should fail
                Assert.ThrowsAsync <ResourceInvalidStateException>(
                    () => service.ApproveApplicationAsync(app.ApplicationId, false, null)).Wait();

                service.ApproveApplicationAsync(app.ApplicationId, true, null).Wait();
                registration = service.GetApplicationAsync(app.ApplicationId, false).Result;

                Assert.Equal(ApplicationState.Approved, registration.Application.State);
            }
        }
        public async Task BadArgShouldThrowExceptions()
        {
            using (var mock = AutoMock.GetLoose()) {
                var hub = new IoTHubServices();
                mock.Provide <IIoTHubTwinServices>(hub);
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.RegisterApplicationAsync(null));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.RejectApplicationAsync(null, false, null));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.RejectApplicationAsync("", false, null));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.GetApplicationAsync(null, false));

                await Assert.ThrowsAsync <ArgumentNullException>(
                    () => service.GetApplicationAsync("", false));

                await Assert.ThrowsAsync <ResourceNotFoundException>(
                    () => service.GetApplicationAsync("abc", false));

                await Assert.ThrowsAsync <ResourceNotFoundException>(
                    () => service.GetApplicationAsync(Guid.NewGuid().ToString(), false));
            }
        }
Exemplo n.º 4
0
        public void DisableEnableApplication()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose()) {
                var hub = IoTHubServices.Create(devices);
                mock.Provide <IIoTHubTwinServices>(hub);
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                var app = apps.First();
                service.DisableApplicationAsync(app.ApplicationId, null).Wait();
                var registration = service.GetApplicationAsync(app.ApplicationId, false).Result;
                Assert.NotNull(registration.Application.NotSeenSince);
                service.EnableApplicationAsync(app.ApplicationId, null).Wait();
                registration = service.GetApplicationAsync(app.ApplicationId, false).Result;
                Assert.Null(registration.Application.NotSeenSince);
            }
        }
 /// <summary>
 /// Find application.
 /// </summary>
 /// <param name="service"></param>
 /// <param name="applicationId"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static async Task <ApplicationRegistrationModel> FindApplicationAsync(
     this IApplicationRegistry service, string applicationId,
     CancellationToken ct = default)
 {
     try {
         return(await service.GetApplicationAsync(applicationId, false, ct));
     }
     catch (ResourceNotFoundException) {
         return(null);
     }
 }
        public void DisableEnableApplication()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose(builder => {
                var hub = IoTHubServices.Create(devices);
                builder.RegisterType <NewtonSoftJsonConverters>().As <IJsonSerializerConverterProvider>();
                builder.RegisterType <NewtonSoftJsonSerializer>().As <IJsonSerializer>();
                builder.RegisterInstance(hub).As <IIoTHubTwinServices>();
                builder.RegisterType <ApplicationTwins>().As <IApplicationRepository>();
            })) {
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                var app = apps.First();
                service.DisableApplicationAsync(app.ApplicationId, null).Wait();
                var registration = service.GetApplicationAsync(app.ApplicationId, false).Result;
                Assert.NotNull(registration.Application.NotSeenSince);
                service.EnableApplicationAsync(app.ApplicationId, null).Wait();
                registration = service.GetApplicationAsync(app.ApplicationId, false).Result;
                Assert.Null(registration.Application.NotSeenSince);
            }
        }
        /// <summary>
        /// List all application registrations
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        public static async Task <List <ApplicationRegistrationModel> > ListAllRegistrationsAsync(
            this IApplicationRegistry service)
        {
            var registrations = new List <ApplicationRegistrationModel>();
            var infos         = await service.ListAllApplicationsAsync();

            foreach (var info in infos)
            {
                var registration = await service.GetApplicationAsync(info.ApplicationId);

                registrations.Add(registration);
            }
            return(registrations);
        }
        /// <summary>
        /// List all application registrations
        /// </summary>
        /// <param name="service"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public static async Task <List <ApplicationRegistrationModel> > ListAllRegistrationsAsync(
            this IApplicationRegistry service, CancellationToken ct = default)
        {
            var registrations = new List <ApplicationRegistrationModel>();
            var infos         = await service.ListAllApplicationsAsync(ct);

            foreach (var info in infos)
            {
                var registration = await service.GetApplicationAsync(info.ApplicationId, false, ct);

                registrations.Add(registration);
            }
            return(registrations);
        }
        public void DoubleApproveApplicationDoesNothing()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose()) {
                var hub = IoTHubServices.Create(devices);
                mock.Provide <IIoTHubTwinServices>(hub);
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                var app = apps.First();
                service.ApproveApplicationAsync(app.ApplicationId, false, null).Wait();
                service.ApproveApplicationAsync(app.ApplicationId, false, null).Wait();
                var registration = service.GetApplicationAsync(app.ApplicationId, false).Result;
                Assert.Equal(ApplicationState.Approved, registration.Application.State);
            }
        }
Exemplo n.º 10
0
        public void GetApplicationThatDoesNotExist()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose()) {
                mock.Provide <IIoTHubTwinServices>(IoTHubServices.Create(devices));
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                // Run
                var t = service.GetApplicationAsync("test", false);

                // Assert
                Assert.NotNull(t.Exception);
                Assert.IsType <AggregateException>(t.Exception);
                Assert.IsType <ResourceNotFoundException>(t.Exception.InnerException);
            }
        }
Exemplo n.º 11
0
        public void GetApplicationThatExists()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);
            var first = apps.First();

            using (var mock = AutoMock.GetLoose()) {
                mock.Provide <IIoTHubTwinServices>(IoTHubServices.Create(devices));
                mock.Provide <IApplicationRepository, ApplicationTwins>();
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                // Run
                var result = service.GetApplicationAsync(
                    ApplicationInfoModelEx.CreateApplicationId(site,
                                                               first.ApplicationUri, first.ApplicationType), false).Result;

                // Assert
                Assert.True(result.Application.IsSameAs(apps.First()));
                Assert.True(result.Endpoints.Count == 0);
            }
        }
        public void GetApplicationThatDoesNotExist()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);

            using (var mock = AutoMock.GetLoose(builder => {
                var hub = IoTHubServices.Create(devices);
                builder.RegisterType <NewtonSoftJsonConverters>().As <IJsonSerializerConverterProvider>();
                builder.RegisterType <NewtonSoftJsonSerializer>().As <IJsonSerializer>();
                builder.RegisterInstance(hub).As <IIoTHubTwinServices>();
                builder.RegisterType <ApplicationTwins>().As <IApplicationRepository>();
            })) {
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                // Run
                var t = service.GetApplicationAsync("test", false);

                // Assert
                Assert.NotNull(t.Exception);
                Assert.IsType <AggregateException>(t.Exception);
                Assert.IsType <ResourceNotFoundException>(t.Exception.InnerException);
            }
        }
        public void GetApplicationThatExists()
        {
            CreateAppFixtures(out var site, out var super, out var apps, out var devices);
            var first = apps.First();

            using (var mock = AutoMock.GetLoose(builder => {
                var hub = IoTHubServices.Create(devices);
                builder.RegisterType <NewtonSoftJsonConverters>().As <IJsonSerializerConverterProvider>();
                builder.RegisterType <NewtonSoftJsonSerializer>().As <IJsonSerializer>();
                builder.RegisterInstance(hub).As <IIoTHubTwinServices>();
                builder.RegisterType <ApplicationTwins>().As <IApplicationRepository>();
            })) {
                IApplicationRegistry service = mock.Create <ApplicationRegistry>();

                // Run
                var result = service.GetApplicationAsync(
                    ApplicationInfoModelEx.CreateApplicationId(site,
                                                               first.ApplicationUri, first.ApplicationType), false).Result;

                // Assert
                Assert.True(result.Application.IsSameAs(apps.First()));
                Assert.True(result.Endpoints.Count == 0);
            }
        }