Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task <OneOf <MaterialManufacturingResponseModel, EntityNotFound> > CreateMaterialManufacturingAsync(
            CreateMaterialManufacturingCommand command,
            CancellationToken cancellationToken)
        {
            var smartContract =
                await _blockchainRepository.GetSmartContractByTypeAsync(ContractType, cancellationToken);

            if (smartContract == null)
            {
                return(new EntityNotFound {
                    Message = $"Smart-contract with type: {ContractType} not found"
                });
            }

            var deployedContractAddress = await _ethereumService.DeployAsync(command, smartContract);

            var deployedSmartContractModel = new DeployedSmartContract(
                deployedContractAddress,
                ContractType,
                smartContract.Abi);

            var deployedSmartContract = await _blockchainRepository.AddDeployedSmartContractAsync(deployedSmartContractModel, cancellationToken);

            return(new MaterialManufacturingResponseModel(deployedSmartContract.Id, command.Name));
        }
Exemplo n.º 2
0
        public async Task CreateMaterialManufacturingAsync_ShouldReturn_CreatedEntity_When_Ok()
        {
            // arrange
            var command = new CreateMaterialManufacturingCommand()
            {
                Name = "test name"
            };
            var contractAddress = "contract address";
            var smartContract   = new SmartContract
            {
                Id         = 1,
                Abi        = "abi",
                ByteCode   = "byte code",
                ApiVersion = "v1",
                Type       = ContractType
            };
            var deployedSmartContract = new DeployedSmartContract(
                contractAddress,
                ContractType,
                smartContract.Abi)
            {
                Id = 1
            };
            var expectedResult = new MaterialManufacturingResponseModel(deployedSmartContract.Id, command.Name);

            _blockchainRepositoryMock.Setup(
                x => x.GetSmartContractByTypeAsync(
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .ReturnsAsync(smartContract);
            _blockchainRepositoryMock.Setup(
                x => x.AddDeployedSmartContractAsync(
                    It.IsAny <DeployedSmartContract>(),
                    CancellationToken.None))
            .ReturnsAsync(deployedSmartContract);

            _ethereumServiceMock.Setup(
                x => x.DeployAsync(
                    It.IsAny <CreateMaterialManufacturingCommand>(),
                    It.IsAny <SmartContract>()))
            .ReturnsAsync(contractAddress);

            // act
            var result = await _sub
                         .CreateMaterialManufacturingAsync(command, CancellationToken.None);

            // assert
            result.Should().NotBeNull();
            result.Should().BeOfType <OneOf <MaterialManufacturingResponseModel, EntityNotFound> >();
            result.IsT0.Should().BeTrue();
            result.Value.Should().BeEquivalentTo(expectedResult);
        }
Exemplo n.º 3
0
        public void GetPropertiesInfo_ShouldReturn_ObjectPropertiesList()
        {
            // arrange
            var command            = new CreateMaterialManufacturingCommand();
            var expectedProperties = command.GetType().GetProperties();

            // act
            var result = command.GetPropertiesInfo();

            // assert
            result.Should().NotBeNull();
            result.Should().Equal(expectedProperties);
        }
Exemplo n.º 4
0
        public void CreateMaterialManufacturingValidator_ShouldReturnTrue_If_ModelIsValid()
        {
            // arrange
            var command = new CreateMaterialManufacturingCommand {
                Name = "Test Material"
            };
            var validator = new CreateMaterialManufacturingCommandValidator();

            // act
            var result = validator.Validate(command);

            // assert
            result.IsValid.Should().Be(true);
        }
Exemplo n.º 5
0
        public async Task CreateMaterialManufacturingAsync_ShouldReturn_EntityNotFound_When_SmartContractNotFound()
        {
            // arrange
            var command = new CreateMaterialManufacturingCommand();

            _blockchainRepositoryMock.Setup(
                x => x.GetSmartContractByTypeAsync(
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()))
            .ReturnsAsync((SmartContract)null);

            // act
            var result = await _sub
                         .CreateMaterialManufacturingAsync(command, CancellationToken.None);

            // assert
            result.Should().NotBeNull();
            result.Should().BeOfType <OneOf <MaterialManufacturingResponseModel, EntityNotFound> >();
            result.IsT1.Should().BeTrue();
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddMaterialManufacturing(
            [FromBody] CreateMaterialManufacturingCommand command,
            CancellationToken cancellationToken)
        {
            var validationResult = await new CreateMaterialManufacturingCommandValidator()
                                   .ValidateAsync(command, cancellationToken);

            if (!validationResult.IsValid)
            {
                var error = new ErrorResponseModel(
                    validationResult.ToString(","),
                    nameof(EntityNotValid));
                return(BadRequest(error));
            }

            var result = await _materialManufacturingService
                         .CreateMaterialManufacturingAsync(command, cancellationToken);

            return(result.Match <IActionResult>(
                       entity
                       => Created($"api/v1.0/material-manufacturing/{entity.Id}", entity),
                       notFound => BadRequest(new ErrorResponseModel(notFound.Message, nameof(EntityNotFound)))
                       ));
        }