예제 #1
0
        public async Task <Result> Create(string username, string exchange, string vhost, Action <TopicPermissionsConfigurator> configurator,
                                          CancellationToken cancellationToken = default)
        {
            cancellationToken.RequestCanceled();

            var impl = new TopicPermissionsConfiguratorImpl();

            configurator?.Invoke(impl);

            impl.Validate();

            var errors = new List <Error>();

            errors.AddRange(impl.Errors.Value);

            if (string.IsNullOrWhiteSpace(exchange))
            {
                errors.Add(new ErrorImpl("Then name of the exchange is missing."));
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                errors.Add(new ErrorImpl("The username and/or password is missing."));
            }

            if (string.IsNullOrWhiteSpace(vhost))
            {
                errors.Add(new ErrorImpl("The name of the virtual host is missing."));
            }

            TopicPermissionsRequest request = new TopicPermissionsRequest(exchange, impl.WritePattern.Value, impl.ReadPattern.Value);

            Debug.Assert(request != null);

            string url = $"api/topic-permissions/{vhost.ToSanitizedName()}/{username}";

            if (errors.Any())
            {
                return(new FaultedResult(new DebugInfoImpl(url, request.ToJsonString(Deserializer.Options), errors)));
            }

            return(await PutRequest(url, request, cancellationToken).ConfigureAwait(false));
        }
예제 #2
0
        public async Task Verify_cannot_create_topic_permissions2()
        {
            var services = GetContainerBuilder().BuildServiceProvider();
            var result   = await services.GetService <IBrokerObjectFactory>()
                           .CreateTopicPermission(string.Empty, "E4", "HareDu", x =>
            {
                x.UsingReadPattern(".*");
                x.UsingWritePattern(".*");
            });

            Assert.Multiple(() =>
            {
                Assert.IsTrue(result.HasFaulted);
                Assert.IsNotNull(result.DebugInfo);
                Assert.AreEqual(1, result.DebugInfo.Errors.Count);

                TopicPermissionsRequest request = result.DebugInfo.Request.ToObject <TopicPermissionsRequest>();

                Assert.AreEqual("E4", request.Exchange);
                Assert.AreEqual(".*", request.Read);
                Assert.AreEqual(".*", request.Write);
            });
        }
예제 #3
0
        public async Task Verify_cannot_create_topic_permissions12()
        {
            var services = GetContainerBuilder().BuildServiceProvider();
            var result   = await services.GetService <IBrokerObjectFactory>()
                           .CreateTopicPermission(string.Empty, string.Empty, string.Empty, x =>
            {
                x.UsingReadPattern(string.Empty);
                x.UsingWritePattern(string.Empty);
            });

            Assert.Multiple(() =>
            {
                Assert.IsTrue(result.HasFaulted);
                Assert.IsNotNull(result.DebugInfo);
                Assert.AreEqual(5, result.DebugInfo.Errors.Count);

                TopicPermissionsRequest request = result.DebugInfo.Request.ToObject <TopicPermissionsRequest>(Deserializer.Options);

                Assert.That(request.Exchange, Is.Empty.Or.Null);
                Assert.That(request.Read, Is.Empty.Or.Null);
                Assert.That(request.Write, Is.Empty.Or.Null);
            });
        }