public void DistributeAsync_DistributesMultipleAtATimeByDefault()
        {
            var distributable1 = new FakeDistributable();
            var distributable2 = new FakeDistributable();
            var recipient = new FakeRecipient();
            var endpoint = new FakeEndpoint();

            var distributor = new Distributor<FakeDistributable, FakeRecipient>();

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100));

            var endpointRepository = new Mock<IEndpointRepository<FakeRecipient>>();
            endpointRepository.Setup(e => e.GetEndpointsForRecipient(recipient))
                .Returns(new[] { endpoint });

            distributor.RegisterDeliverer(deliverer);
            distributor.RegisterEndpointRepository(endpointRepository.Object);

            var task1 = distributor.DistributeAsync(distributable1, recipient);
            var task2 = distributor.DistributeAsync(distributable2, recipient);

            Task.WaitAll(task1, task2);

            var lastStartTime = deliverer.LogEntries.Max(e => e.StartDateTime);
            var firstEndTime = deliverer.LogEntries.Min(e => e.EndDateTime);

            lastStartTime.ShouldBeLessThan(firstEndTime);
        }
        public void GetSemaphoreForGroup_GetsANullSemaphoreWhenGroupingToNull()
        {
            var groupThrottler = new GroupThrottler<FakeEndpoint>(e => null, 1);

            var endpoint = new FakeEndpoint();

            var semaphore = groupThrottler.GetSemaphoreForGroup(endpoint);

            semaphore.ShouldBeNull();
        }
        public void GetSemaphoreForGroup_GetsSameSemaphoreForSameGrouping()
        {
            var groupThrottler = new GroupThrottler<FakeEndpoint>(e => e.Host, 1);

            var endpoint1 = new FakeEndpoint { Host = "1" };
            var endpoint2 = new FakeEndpoint { Host = "1" };

            var semaphore1 = groupThrottler.GetSemaphoreForGroup(endpoint1);
            var semaphore2 = groupThrottler.GetSemaphoreForGroup(endpoint2);

            semaphore1.ShouldBe(semaphore2);
        }
        public void Endpoint_with_external_documentation()
        {
            //Arrange
            var fakeEndpoint = new FakeEndpoint();

            //Act
            var endpoint = new Endpoint(fakeEndpoint.OperationName)
                           .WithExternalDocumentation(fakeEndpoint.ExternalDocsUrl, fakeEndpoint.ExternalDocs);

            //Assert
            Assert.Equal(fakeEndpoint.ExternalDocs, endpoint.ExternalDocs.Description);
            Assert.Equal(fakeEndpoint.ExternalDocsUrl, endpoint.ExternalDocs.Url);
        }
Beispiel #5
0
        public void Generate_openapi_metadata()
        {
            //Arrange
            var endpoint             = new FakeEndpoint();
            var fakeRouteDescription = new RouteDescription(endpoint.Operation, endpoint.Method, endpoint.Path, null);

            //Act
            var metadata = new OpenApiRouteMetadata(fakeRouteDescription);

            //Assert
            Assert.Equal(metadata.Path, fakeRouteDescription.Path);
            Assert.Equal(metadata.Method, fakeRouteDescription.Method.ToLowerInvariant());
            Assert.Equal(metadata.Name, fakeRouteDescription.Name);
        }
        public void DistributeAsync_DeliversToAnEndpointTypeWithNoDelivererThrowsAnError()
        {
            var distributable = new FakeDistributable();
            var recipient = new FakeRecipient();
            var endpoint = new FakeEndpoint();

            var endpointRepository = new Mock<IEndpointRepository<FakeRecipient>>();
            endpointRepository.Setup(e => e.GetEndpointsForRecipient(recipient))
                .Returns(new[] { endpoint });

            var distributor = new Distributor<FakeDistributable, FakeRecipient>();
            distributor.RegisterEndpointRepository(endpointRepository.Object);

            Should.ThrowAsync<InvalidOperationException>(distributor.DistributeAsync(distributable, recipient));
        }
        public void Endpoint_with_response()
        {
            //Arrange
            var fakeEndpoint = new FakeEndpoint();
            var fakeResponse = new FakeResponse();

            //Act
            var endpoint = new Endpoint(fakeEndpoint.OperationName)
                           .WithResponse(fakeResponse.StatusCode, fakeResponse.Description);

            //Assert
            Assert.Equal(fakeEndpoint.OperationName, endpoint.OperationId);
            Assert.NotNull(endpoint.ResponseInfos[fakeResponse.StatusCode]);
            Assert.Equal(fakeResponse.Description, endpoint.ResponseInfos[fakeResponse.StatusCode].Description);
        }
        public void DeliverAsync_DeliversMultipleAtATimeByDefault()
        {
            var endpoint = new FakeEndpoint();

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100));

            var task1 = ((IDeliverer)deliverer).DeliverAsync(new FakeDistributable(), endpoint);
            var task2 = ((IDeliverer)deliverer).DeliverAsync(new FakeDistributable(), endpoint);

            Task.WaitAll(task1, task2);

            var lastStartTime = deliverer.LogEntries.Max(e => e.StartDateTime);
            var firstEndTime = deliverer.LogEntries.Min(e => e.EndDateTime);

            lastStartTime.ShouldBeLessThan(firstEndTime);
        }
        public void Endpoint_with_default_response_model()
        {
            //Arrange
            var fakeEndpoint      = new FakeEndpoint();
            var fakeResponseModel = new FakeResponseModel();

            //Act
            var endpoint = new Endpoint(fakeEndpoint.OperationName)
                           .WithDefaultResponse(typeof(FakeResponseModel), fakeResponseModel.Description);

            //Assert
            Assert.Equal(fakeEndpoint.OperationName, endpoint.OperationId);
            Assert.NotNull(endpoint.ResponseInfos[fakeResponseModel.StatusCode]);
            Assert.Equal(fakeResponseModel.Description, endpoint.ResponseInfos[fakeResponseModel.StatusCode].Description);
            Assert.Contains(nameof(FakeResponseModel), endpoint.ResponseInfos[fakeResponseModel.StatusCode].Schema.Ref);
        }
Beispiel #10
0
        public void Endpoint_with_multiple_request_parameters()
        {
            //Arrange
            var    fakeEndpoint = new FakeEndpoint();
            var    fakeRequest  = new FakeRequest();
            string Param2       = "Param2";

            //Act
            var endpoint = new Endpoint(fakeEndpoint.Operation)
                           .WithRequestParameter
                           (
                fakeRequest.Name,
                fakeRequest.Type,
                fakeRequest.Format,
                fakeRequest.Required,
                fakeRequest.Description,
                fakeRequest.Loc,
                fakeRequest.Deprecated
                           )
                           .WithRequestParameter
                           (
                Param2,
                fakeRequest.Type,
                fakeRequest.Format,
                fakeRequest.Required,
                fakeRequest.Description,
                fakeRequest.Loc,
                fakeRequest.Deprecated
                           );

            //Assert
            Assert.Equal(fakeRequest.Description, endpoint.RequestParameters[0].Description);
            Assert.Null(endpoint.RequestParameters[0].Schema.Format);
            Assert.Equal(fakeRequest.Required, endpoint.RequestParameters[0].Required);
            Assert.Equal(fakeRequest.Name, endpoint.RequestParameters[0].Name);
            Assert.Equal(fakeRequest.Loc, endpoint.RequestParameters[0].In);
            Assert.Equal(fakeRequest.Type.Name.ToLowerInvariant(), endpoint.RequestParameters[0].Schema.Type);
            Assert.Equal(fakeRequest.Description, endpoint.RequestParameters[1].Description);
            Assert.Null(endpoint.RequestParameters[1].Schema.Format);
            Assert.Equal(fakeRequest.Required, endpoint.RequestParameters[1].Required);
            Assert.Equal(Param2, endpoint.RequestParameters[1].Name);
            Assert.Equal(fakeRequest.Loc, endpoint.RequestParameters[1].In);
            Assert.Equal(fakeRequest.Type.Name.ToLowerInvariant(), endpoint.RequestParameters[1].Schema.Type);
        }
        public void DeliverAsync_DeliversOneAtATimeWithConcurrencyLimitOne()
        {
            var distributable1 = new FakeDistributable();
            var distributable2 = new FakeDistributable();
            var endpoint = new FakeEndpoint();

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100));
            deliverer.MaximumConcurrentDeliveries(1);

            var task1 = ((IDeliverer)deliverer).DeliverAsync(distributable1, endpoint);
            var task2 = ((IDeliverer)deliverer).DeliverAsync(distributable2, endpoint);

            Task.WaitAll(task1, task2);

            var task1EndTime = deliverer.LogEntries.Single(e => e.Distributable == distributable1).EndDateTime;
            var task2StartTime = deliverer.LogEntries.Single(e => e.Distributable == distributable2).StartDateTime;

            task1EndTime.ShouldBeLessThanOrEqualTo(task2StartTime);
        }
        public void Endpoint_with_basic_authentication()
        {
            //Arrange
            var    fakeEndpoint = new FakeEndpoint();
            string scheme       = "basic";
            string type         = "http";
            string description  = "Basic Authentication Sample";

            //Act
            var  endpoint = new Endpoint(fakeEndpoint.Operation).WithBasicAuthentication(description);
            bool success  = SchemaCache.SecurityCache.TryGetValue(scheme, out SecurityScheme securityScheme);

            //Assert
            Assert.True(success);
            Assert.Equal(securityScheme.Name, scheme);
            Assert.Equal(securityScheme.Scheme, scheme);
            Assert.Equal(securityScheme.Type, type);
            Assert.Equal(securityScheme.Description, description);
        }
Beispiel #13
0
        public void Generate_method_description()
        {
            //Arrange
            var endpoint = new FakeEndpoint();

            var fakeRouteDescription = new RouteDescription(endpoint.Operation, endpoint.Method, endpoint.Path, null);

            //Act
            var metadata = new OpenApiRouteMetadata(fakeRouteDescription)
                           .With(i => i.WithResponseModel("200", typeof(FakeResponseModel), "Sample response")
                                 .WithSummary(endpoint.Summary));

            //Assert
            Assert.Equal(metadata.Path, fakeRouteDescription.Path);
            Assert.Equal(metadata.Method, fakeRouteDescription.Method.ToLowerInvariant());
            Assert.Equal(metadata.Name, fakeRouteDescription.Name);
            Assert.Equal(metadata.Info.OperationId, endpoint.Operation);
            Assert.Equal(metadata.Info.Summary, endpoint.Summary);
        }
        public void DistributeAsync_DeliversADistributableToARegisteredDeliverer()
        {
            var distributable = new FakeDistributable();
            var recipient = new FakeRecipient();
            var endpoint = new FakeEndpoint();

            var deliverer = new Mock<IDeliverer<FakeDistributable, FakeEndpoint>>();
            var endpointRepository = new Mock<IEndpointRepository<FakeRecipient>>();
            endpointRepository.Setup(e => e.GetEndpointsForRecipient(recipient))
                .Returns(new [] { endpoint });

            var distributor = new Distributor<FakeDistributable, FakeRecipient>();
            distributor.RegisterEndpointRepository(endpointRepository.Object);
            distributor.RegisterDeliverer(deliverer.Object);

            distributor.DistributeAsync(distributable, recipient).Wait();

            deliverer.Verify(eds => eds.DeliverAsync(distributable, (IEndpoint) endpoint), Times.Once);
        }
        public void DeliverAsync_DeliversOneAtATimeWhenLimitedToOneByGroup()
        {
            var distributable = new FakeDistributable();
            var endpoint1 = new FakeEndpoint { Host = "1" };
            var endpoint2 = new FakeEndpoint { Host = "1" };

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100),
                new Dictionary<Func<FakeEndpoint, object>, int> { { e => e.Host, 1 } });

            var task1 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint1);
            var task2 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint2);

            Task.WaitAll(task1, task2);

            var task1EndTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint1).EndDateTime;
            var task2StartTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint2).StartDateTime;

            task1EndTime.ShouldBeLessThanOrEqualTo(task2StartTime);
        }
Beispiel #16
0
        public void Endpoint_with_multiple_response()
        {
            //Arrange
            var    fakeEndpoint   = new FakeEndpoint();
            var    fakeResponse   = new FakeResponse();
            string badRequest     = "400";
            string badRequestDesc = "Bad Request Made";

            //Act
            var endpoint = new Endpoint(fakeEndpoint.Operation)
                           .WithResponse(fakeResponse.StatusCode, fakeResponse.Description)
                           .WithResponse(badRequest, badRequestDesc);

            //Assert
            Assert.Equal(fakeEndpoint.Operation, endpoint.OperationId);
            Assert.NotNull(endpoint.ResponseInfos[fakeResponse.StatusCode]);
            Assert.Equal(fakeResponse.Description, endpoint.ResponseInfos[fakeResponse.StatusCode].Description);
            Assert.NotNull(endpoint.ResponseInfos[badRequest]);
            Assert.Equal(badRequestDesc, endpoint.ResponseInfos[badRequest].Description);
        }
Beispiel #17
0
        public void Endpoint_with_description_and_existing_tag_exists()
        {
            //Arrange
            var fakeEndpoint = new FakeEndpoint();

            string[] sampleTag = new string[] { "Sample 1" };

            //Act
            var endpoint = new Endpoint(fakeEndpoint.Operation)
            {
                Tags = sampleTag
            };

            endpoint.WithDescription(fakeEndpoint.Description, fakeEndpoint.Tags);

            //Assert
            Assert.Equal(fakeEndpoint.Description, endpoint.Description);
            Assert.Equal(sampleTag, endpoint.Tags);
            Assert.NotEqual(fakeEndpoint.Tags, endpoint.Tags);
        }
        public void Endpoint_with_request_complex_model()
        {
            //Arrange
            var fakeEndpoint = new FakeEndpoint();
            var fakeRequest  = new FakeRequestComplexModel();

            //Act
            var endpoint = new Endpoint(fakeEndpoint.OperationName)
                           .WithRequestModel(
                typeof(FakeRequestComplexModel),
                fakeRequest.contentType,
                fakeRequest.Description,
                fakeRequest.Required);

            //Assert
            Assert.Equal(fakeRequest.Description, endpoint.RequestBody.Description);
            Assert.Equal(fakeRequest.Required, endpoint.RequestBody.Required);
            Assert.True(endpoint.RequestBody.Content.ContainsKey(fakeRequest.contentType));
            Assert.Contains(nameof(FakeRequestComplexModel), endpoint.RequestBody.Content[fakeRequest.contentType].Schema.Ref);
        }
Beispiel #19
0
        public void Endpoint_with_multiple_response_models()
        {
            //Arrange
            var    fakeEndpoint           = new FakeEndpoint();
            var    fakeResponseModel      = new FakeResponseModel();
            string NewResource            = "201";
            string NewResourceDescription = "New Resource Created";

            //Act
            var endpoint = new Endpoint(fakeEndpoint.Operation)
                           .WithResponseModel(fakeResponseModel.StatusCode, typeof(FakeResponseModel), fakeResponseModel.Description)
                           .WithResponseModel(NewResource, typeof(FakeResponseModel), NewResourceDescription);

            //Assert
            Assert.Equal(fakeEndpoint.Operation, endpoint.OperationId);
            Assert.NotNull(endpoint.ResponseInfos[fakeResponseModel.StatusCode]);
            Assert.NotNull(endpoint.ResponseInfos[NewResource]);
            Assert.Equal(fakeResponseModel.Description, endpoint.ResponseInfos[fakeResponseModel.StatusCode].Description);
            Assert.Equal(NewResourceDescription, endpoint.ResponseInfos[NewResource].Description);
            Assert.Contains(nameof(FakeResponseModel), endpoint.ResponseInfos[fakeResponseModel.StatusCode].Schema.Ref);
        }
        public void Endpoint_with_custom_authentication()
        {
            //Arrange
            var fakeEndpoint = new FakeEndpoint();

            //This is defined by the standard
            string name        = "my_custom_api_key";
            string type        = "apiKey";
            string location    = "cookie";
            string description = "Basic Custom Authentication Sample";

            //Act
            var  endpoint = new Endpoint(fakeEndpoint.Operation).WithApiKeyAuthentication(name, location, description);
            bool success  = SchemaCache.SecurityCache.TryGetValue(name, out SecurityScheme securityScheme);

            //Assert
            Assert.True(success);
            Assert.Equal(securityScheme.Name, name);
            Assert.Equal(securityScheme.In, location);
            Assert.Equal(securityScheme.Type, type);
            Assert.Equal(securityScheme.Description, description);
        }
        public void Endpoint_with_basic_authentication_twice()
        {
            //Arrange
            var    fakeEndpoint = new FakeEndpoint();
            string scheme       = "basic";
            string type         = "http";
            string description  = "Basic Authentication Sample";

            //Act
            SchemaCache.SecurityCache.Clear(); //This test requires that the size of the Schema Cache is 0 at the start.
            var endpoint = new Endpoint(fakeEndpoint.Operation)
                           .WithBasicAuthentication(description)
                           .WithBasicAuthentication(description);
            bool success = SchemaCache.SecurityCache.TryGetValue(scheme, out SecurityScheme securityScheme);

            //Assert
            Assert.True(success);
            Assert.Equal(1, SchemaCache.SecurityCache.Count);
            Assert.Equal(securityScheme.Name, scheme);
            Assert.Equal(securityScheme.Scheme, scheme);
            Assert.Equal(securityScheme.Type, type);
            Assert.Equal(securityScheme.Description, description);
        }
        public void DistributeAsync_DoesNotUseTheFirstDelivererWhenTwoAreRegistered()
        {
            var distributable = new FakeDistributable();
            var recipient = new FakeRecipient();
            var endpoint = new FakeEndpoint();

            var deliverer1 = new Mock<IDeliverer<FakeDistributable, FakeEndpoint>>();
            var deliverer2 = new Mock<IDeliverer<FakeDistributable, FakeEndpoint>>();
            var endpointRepository = new Mock<IEndpointRepository<FakeRecipient>>();
            endpointRepository.Setup(e => e.GetEndpointsForRecipient(recipient))
                .Returns(new[] { endpoint });

            var distributor = new Distributor<FakeDistributable, FakeRecipient>();
            distributor.RegisterEndpointRepository(endpointRepository.Object);
            distributor.RegisterDeliverer(deliverer1.Object);
            distributor.RegisterDeliverer(deliverer2.Object);

            distributor.DistributeAsync(distributable, recipient).Wait();

            deliverer1.Verify(eds => eds.DeliverAsync(distributable, (IEndpoint) endpoint), Times.Never);
        }
        public void DeliverAsync_DoesNotThrottleWhenMultipleGroupedToNull()
        {
            var distributable = new FakeDistributable();
            var endpoint1 = new FakeEndpoint();
            var endpoint2 = new FakeEndpoint();

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100),
                new Dictionary<Func<FakeEndpoint, object>, int> { { e => null, 1 } });

            var task1 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint1);
            var task2 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint2);

            Task.WaitAll(task1, task2);

            var task1EndTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint1).EndDateTime;
            var task2StartTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint2).StartDateTime;

            task2StartTime.ShouldBeLessThan(task1EndTime);
        }
        public void DeliverAsync_ThrottlesExceptWhenGroupedToNull()
        {
            var distributable = new FakeDistributable();
            var endpoint1 = new FakeEndpoint { Port = 0 };
            var endpoint2 = new FakeEndpoint { Port = 1 };
            var endpoint3 = new FakeEndpoint { Port = 1 };

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100),
                new Dictionary<Func<FakeEndpoint, object>, int> { { e => e.Port == 1 ? (int?)null : 1, 1 } });

            var task1 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint1);
            var task2 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint2);
            var task3 = ((IDeliverer)deliverer).DeliverAsync(distributable, endpoint3);

            Task.WaitAll(task1, task2, task3);

            var task1EndTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint1).EndDateTime;
            var task2StartTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint2).StartDateTime;
            var task2EndTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint2).EndDateTime;
            var task3StartTime = deliverer.LogEntries.Single(e => e.Endpoint == endpoint3).StartDateTime;

            task2StartTime.ShouldBeLessThan(task1EndTime);
            task3StartTime.ShouldBeLessThan(task1EndTime);
            task3StartTime.ShouldBeLessThan(task2EndTime);
        }