示例#1
0
        public MachinesControllerTests()
        {
            // repository mock
            _repositoryFactoryMock.Setup(m => m.CreateRepositoryAsync <Machine>(It.IsAny <CancellationToken>()))
            .ReturnsAsync(_machineRepositoryMock.Object);

            _validatorMock.Setup(m => m.Validate(It.IsAny <object>()))
            .Returns(new ValidationResult());

            // validator mock
            _validatorLocatorMock.Setup(m => m.GetValidator(It.IsAny <Type>()))
            .Returns(_validatorMock.Object);

            // logger factory mock
            var loggerFactoryMock = new Mock <ILoggerFactory>();

            loggerFactoryMock.Setup(m => m.Get(It.IsAny <LogTopic>()))
            .Returns(_loggerMock.Object);

            _controller = new MachinesController(
                _repositoryFactoryMock.Object,
                _validatorLocatorMock.Object,
                loggerFactoryMock.Object,
                _eventServiceMock.Object,
                _cloudShimMock.Object,
                _tenantApiMock.Object);
        }
        public MachinesModule(
            IMachinesController machineController,
            IMetadataRegistry metadataRegistry,
            IPolicyEvaluator policyEvaluator,
            ILoggerFactory loggerFactory)
            : base(ServiceInformation.ServiceNameShort, metadataRegistry, policyEvaluator, loggerFactory)
        {
            _machinesController = machineController;

            this.RequiresAuthentication();

            CreateRoute("CreateMachine", HttpMethod.Post, Routing.Machines, CreateMachineAsync)
            .Description("Create a new machine resource")
            .StatusCodes(HttpStatusCode.Created, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.BadRequest, HttpStatusCode.InternalServerError)
            .RequestFormat(Machine.Example())
            .ResponseFormat(Machine.Example());

            CreateRoute("GetMachineById", HttpMethod.Get, Routing.MachinesWithId, GetMachineByIdAsync)
            .Description("Gets a machine by its unique identifier")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .ResponseFormat(Machine.Example());

            CreateRoute("GetMachineByKey", HttpMethod.Get, Routing.Machines, GetMachineByKeyAsync, c => c.Request.Query.ContainsKey("machinekey"))
            .Description("Get a machine by machine key")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .ResponseFormat(Machine.Example());

            CreateRoute("UpdateMachine", HttpMethod.Put, Routing.MachinesWithId, UpdateMachineAsync)
            .Description("Update a Principal resource.")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .RequestFormat(Machine.Example())
            .ResponseFormat(Machine.Example());

            CreateRoute("DeleteMachine", HttpMethod.Delete, Routing.MachinesWithId, DeleteMachineAsync)
            .Description("Deletes a machine")
            .StatusCodes(HttpStatusCode.NoContent, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError);

            CreateRoute("ChangeMachineTenant", HttpMethod.Put, Routing.ChangeMachineTenant, ChangeMachineTenantAsync)
            .Description("Changes a machine's tenant")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .RequestFormat(Machine.Example())
            .ResponseFormat(Machine.Example());

            CreateRoute("GetTenantMachines", HttpMethod.Get, Routing.Machines, GetTenantMachinesAsync, c => !c.Request.Query.ContainsKey("machinekey"))
            .Description("Retrieves a list of machines for the tenant")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .ResponseFormat(new List <Machine> {
                Machine.Example()
            });
        }