public async Task <DeploymentApiModel> PostAsync([FromBody] DeploymentApiModel deployment)
        {
            if (string.IsNullOrWhiteSpace(deployment.Name))
            {
                throw new InvalidInputException("Name must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.DeviceGroupId))
            {
                throw new InvalidInputException("DeviceGroupId must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.DeviceGroupQuery))
            {
                throw new InvalidInputException("DeviceGroupQuery must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.PackageContent))
            {
                throw new InvalidInputException("PackageContent must be provided");
            }

            if (deployment.Priority < 0)
            {
                throw new InvalidInputException($"Invalid priority provided of {deployment.Priority}. " +
                                                "It must be non-negative");
            }

            return(new DeploymentApiModel(await this.deployments.CreateAsync(deployment.ToServiceModel())));
        }
Ejemplo n.º 2
0
        public async Task PostDeploymentTest(string name, string deviceGroupId,
                                             string deviceGroupQuery, string packageContent,
                                             int priority, bool throwsException)
        {
            // Arrange
            var deploymentId = "test-deployment";

            this.deploymentsMock.Setup(x => x.CreateAsync(Match.Create <DeploymentServiceModel>(model =>
                                                                                                model.DeviceGroupId == deviceGroupId &&
                                                                                                model.PackageContent == packageContent &&
                                                                                                model.Priority == priority &&
                                                                                                model.Name == name &&
                                                                                                model.Type == DeploymentType.EdgeManifest)))
            .ReturnsAsync(new DeploymentServiceModel()
            {
                Name             = name,
                DeviceGroupId    = deviceGroupId,
                DeviceGroupQuery = deviceGroupQuery,
                PackageContent   = packageContent,
                Priority         = priority,
                Id   = deploymentId,
                Type = DeploymentType.EdgeManifest,
                CreatedDateTimeUtc = DateTime.UtcNow
            });

            var depApiModel = new DeploymentApiModel()
            {
                Name             = name,
                DeviceGroupId    = deviceGroupId,
                DeviceGroupQuery = deviceGroupQuery,
                PackageContent   = packageContent,
                Type             = DeploymentType.EdgeManifest,
                Priority         = priority
            };

            // Act
            if (throwsException)
            {
                await Assert.ThrowsAsync <InvalidInputException>(async() => await this.deploymentsController.PostAsync(depApiModel));
            }
            else
            {
                var result = await this.deploymentsController.PostAsync(depApiModel);

                // Assert
                Assert.Equal(deploymentId, result.DeploymentId);
                Assert.Equal(name, result.Name);
                Assert.Equal(deviceGroupId, result.DeviceGroupId);
                Assert.Equal(deviceGroupQuery, result.DeviceGroupQuery);
                Assert.Equal(packageContent, result.PackageContent);
                Assert.Equal(priority, result.Priority);
                Assert.Equal(DeploymentType.EdgeManifest, result.Type);
                Assert.True((DateTimeOffset.UtcNow - result.CreatedDateTimeUtc).TotalSeconds < 5);
            }
        }
Ejemplo n.º 3
0
        public async Task <DeploymentApiModel> PostAsync([FromBody] DeploymentApiModel deployment)
        {
            if (string.IsNullOrWhiteSpace(deployment.Name))
            {
                throw new InvalidInputException("Name must be provided");
            }

            // If DeviceGroupId is provided, fill the DeviceGroup details if they are not provided
            if (!string.IsNullOrWhiteSpace(deployment.DeviceGroupId) && string.IsNullOrWhiteSpace(deployment.DeviceGroupQuery))
            {
                await this.HydrateDeploymentWithDeviceGroupDetails(deployment);
            }

            // If PackageId is provided, fill the package details if they are not provided
            if (!string.IsNullOrWhiteSpace(deployment.PackageId) && string.IsNullOrWhiteSpace(deployment.PackageContent))
            {
                await this.HydrateDeploymentWithPackageDetails(deployment);
            }

            if (string.IsNullOrWhiteSpace(deployment.DeviceGroupId))
            {
                throw new InvalidInputException("DeviceGroupId must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.DeviceGroupName))
            {
                throw new InvalidInputException("DeviceGroupName must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.DeviceGroupQuery) && (deployment.DeviceIds == null || (deployment.DeviceIds != null && deployment.DeviceIds.Count() == 0)))
            {
                throw new InvalidInputException("DeviceGroupQuery must be provided");
            }

            if (string.IsNullOrWhiteSpace(deployment.PackageContent))
            {
                throw new InvalidInputException("PackageContent must be provided");
            }

            if (deployment.PackageType.Equals(PackageType.DeviceConfiguration) &&
                string.IsNullOrEmpty(deployment.ConfigType))
            {
                throw new InvalidInputException("Configuration type must be provided");
            }

            if (deployment.Priority < 0)
            {
                throw new InvalidInputException($"Invalid priority provided of {deployment.Priority}. " +
                                                "It must be non-negative");
            }

            return(new DeploymentApiModel(await this.deployments.CreateAsync(deployment.ToServiceModel(), this.GetClaimsUserDetails(), this.GetTenantId())));
        }
Ejemplo n.º 4
0
        private async Task HydrateDeploymentWithDeviceGroupDetails(DeploymentApiModel deployment)
        {
            var deviceGroup = await this.deployments.GetDeviceGroupAsync(deployment.DeviceGroupId);

            if (deviceGroup == null)
            {
                throw new ResourceNotFoundException($"No Device Group found with DeviceGroupId:{deployment.DeviceGroupId}");
            }

            deployment.DeviceGroupName  = deviceGroup.DisplayName;
            deployment.DeviceGroupQuery = JsonConvert.SerializeObject(deviceGroup.Conditions);
        }
Ejemplo n.º 5
0
        private async Task HydrateDeploymentWithPackageDetails(DeploymentApiModel deployment)
        {
            var package = await this.deployments.GetPackageAsync(deployment.PackageId);

            if (package == null)
            {
                throw new ResourceNotFoundException($"No Package found with packageId:{deployment.PackageId}");
            }

            deployment.PackageType     = package.PackageType;
            deployment.ConfigType      = package.ConfigType;
            deployment.PackageContent  = package.Content;
            deployment.PackageName     = package.Name;
            deployment.DeviceGroupId   = string.IsNullOrWhiteSpace(deployment.DeviceGroupId) ? Guid.Empty.ToString() : deployment.DeviceGroupId;
            deployment.DeviceGroupName = string.IsNullOrWhiteSpace(deployment.DeviceGroupName) ? deployment.DeviceIds != null && deployment.DeviceIds.Any() ? "DirectToDevices: " + string.Join(',', deployment.DeviceIds) : "DirectToDevices" : deployment.DeviceGroupName;
        }
Ejemplo n.º 6
0
        private IHttpResponse CreateDeployment(string name, string deviceGroupName = "", string packageName = "")
        {
            var input = new DeploymentApiModel
            {
                Name             = name,
                DeviceGroupId    = this.deviceGroupId,
                DeviceGroupName  = deviceGroupName,
                DeviceGroupQuery = this.deviceGroupQuery,
                PackageContent   = this.packageContent,
                PackageName      = packageName,
                Priority         = this.priority,
                Type             = DeploymentType.EdgeManifest
            };

            return(this.Request.Post(JsonConvert.SerializeObject(input, Formatting.None)));
        }
Ejemplo n.º 7
0
        public async Task PostInvalidDeploymentTest(string name, string deviceGroupId,
                                                    string deviceGroupQuery, string packageContent,
                                                    int priority)
        {
            // Arrange
            var depApiModel = new DeploymentApiModel()
            {
                Name             = name,
                DeviceGroupId    = deviceGroupId,
                DeviceGroupQuery = deviceGroupQuery,
                PackageContent   = packageContent,
                PackageType      = PackageType.DeviceConfiguration,
                ConfigType       = string.Empty,
                Priority         = priority
            };

            // Act
            await Assert.ThrowsAsync <InvalidInputException>(async() => await this.deploymentsController.PostAsync(depApiModel));
        }
        public async Task PostDeploymentTest(
            string name,
            string deviceGroupId,
            string deviceGroupQuery,
            string packageContent,
            int priority,
            bool throwsException)
        {
            // Arrange
            var          deploymentId    = "test-deployment";
            const string deviceGroupName = "DeviceGroup";

            this.deploymentsMock.Setup(x => x.CreateAsync(
                                           Match.Create <DeploymentServiceModel>(model =>
                                                                                 model.DeviceGroupId == deviceGroupId &&
                                                                                 model.PackageContent == packageContent &&
                                                                                 model.Priority == priority &&
                                                                                 model.DeviceGroupName == deviceGroupName &&
                                                                                 model.Name == name &&
                                                                                 model.PackageType == PackageType.EdgeManifest &&
                                                                                 model.ConfigType == ConfigurationType),
                                           It.IsAny <string>(),
                                           It.IsAny <string>()))
            .ReturnsAsync(new DeploymentServiceModel()
            {
                Name             = name,
                DeviceGroupId    = deviceGroupId,
                DeviceGroupName  = deviceGroupName,
                DeviceGroupQuery = deviceGroupQuery,
                PackageContent   = packageContent,
                Priority         = priority,
                Id                 = deploymentId,
                PackageType        = PackageType.EdgeManifest,
                ConfigType         = ConfigurationType,
                CreatedDateTimeUtc = DateTime.UtcNow,
            });

            this.deploymentsMock.Setup(c => c.GetDeviceGroupAsync(It.IsAny <string>()))
            .ReturnsAsync(new Common.Services.Models.DeviceGroup()
            {
                Id         = deviceGroupId,
                Conditions = new List <DeviceGroupCondition>(),
            });

            this.deploymentsMock.Setup(c => c.GetPackageAsync(It.IsAny <string>()))
            .ReturnsAsync(new PackageApiModel()
            {
                ConfigType  = ConfigurationType,
                PackageType = PackageType.EdgeManifest,
                Content     = packageContent,
                Name        = "PackageName",
            });

            var depApiModel = new DeploymentApiModel()
            {
                Name             = name,
                DeviceGroupId    = deviceGroupId,
                DeviceGroupQuery = deviceGroupQuery,
                DeviceGroupName  = deviceGroupName,
                PackageContent   = packageContent,
                PackageType      = PackageType.EdgeManifest,
                ConfigType       = ConfigurationType,
                Priority         = priority,
            };

            // Act
            if (throwsException)
            {
                await Assert.ThrowsAsync <InvalidInputException>(async() => await this.controller.PostAsync(depApiModel));
            }
            else
            {
                var result = await this.controller.PostAsync(depApiModel);

                // Assert
                Assert.Equal(deploymentId, result.DeploymentId);
                Assert.Equal(name, result.Name);
                Assert.Equal(deviceGroupId, result.DeviceGroupId);
                Assert.Equal(deviceGroupQuery, result.DeviceGroupQuery);
                Assert.Equal(packageContent, result.PackageContent);
                Assert.Equal(priority, result.Priority);
                Assert.Equal(PackageType.EdgeManifest, result.PackageType);
                Assert.Equal(ConfigurationType, result.ConfigType);
                Assert.True((DateTimeOffset.UtcNow - result.CreatedDateTimeUtc).TotalSeconds < 5);
            }
        }