public async Task CreateAndGetDefaultChannel()
        {
            using TestData data = await BuildDefaultAsync();

            string channelName    = "TEST-CHANNEL-LIST-REPOSITORIES";
            string classification = "TEST-CLASSIFICATION";
            string repository     = "FAKE-REPOSITORY";
            string branch         = "FAKE-BRANCH";

            Channel channel1, channel2;
            {
                var result = await data.ChannelsController.CreateChannel($"{channelName}-1", classification);

                channel1 = (Channel)((ObjectResult)result).Value;
                result   = await data.ChannelsController.CreateChannel($"{channelName}-2", classification);

                channel2 = (Channel)((ObjectResult)result).Value;
            }

            DefaultChannel defaultChannel;
            {
                DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
                {
                    Branch     = branch,
                    ChannelId  = channel2.Id,
                    Enabled    = true,
                    Repository = repository
                };
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            DefaultChannel singleChannelGetDefaultChannel;

            {
                IActionResult result = await data.DefaultChannelsController.Get(defaultChannel.Id);

                result.Should().BeAssignableTo <ObjectResult>();
                var objResult = (ObjectResult)result;
                objResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
                objResult.Value.Should().BeAssignableTo <DefaultChannel>();
                singleChannelGetDefaultChannel = ((DefaultChannel)objResult.Value);
            }
            singleChannelGetDefaultChannel.Id.Should().Be(defaultChannel.Id);

            List <DefaultChannel> listOfInsertedDefaultChannels;

            {
                IActionResult result = data.DefaultChannelsController.List(repository, branch, channel2.Id);
                result.Should().BeAssignableTo <ObjectResult>();
                var objResult = (ObjectResult)result;
                objResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
                objResult.Value.Should().BeAssignableTo <IEnumerable <DefaultChannel> >();
                listOfInsertedDefaultChannels = ((IEnumerable <DefaultChannel>)objResult.Value).ToList();
            }

            listOfInsertedDefaultChannels.Should().ContainSingle();
            listOfInsertedDefaultChannels.Single().Channel.Id.Should().Be(channel2.Id, "Only fake channel #2's id should show up as a default channel");
        }
        public async Task TryToGetOrUpdateNonExistentChannel()
        {
            string channelName    = "TEST-CHANNEL-TO-UPDATE";
            string classification = "TEST-CLASSIFICATION";
            string repository     = "FAKE-NON-EXISTENT-REPOSITORY-MISSING-CHANNEL-UPDATE";
            string branch         = "FAKE-BRANCH-MISSING-CHANNEL-UPDATE";

            using TestData data = await BuildDefaultAsync();

            DefaultChannelUpdateData defaultChannelThatDoesntExistUpdateData = new DefaultChannelUpdateData()
            {
                Branch     = branch,
                ChannelId  = 404,
                Enabled    = false,
                Repository = repository
            };
            // First: non-existent default channel
            var expectedFailResult = await data.DefaultChannelsController.Update(404, defaultChannelThatDoesntExistUpdateData);

            expectedFailResult.Should().BeOfType <NotFoundResult>("Asking for a non-existent channel should give a not-found type result");

            // Second: Extant default, non-existent channel.
            Channel channel;
            {
                var result = await data.ChannelsController.CreateChannel(channelName, classification);

                channel = (Channel)((ObjectResult)result).Value;
            }

            DefaultChannel defaultChannel;
            {
                DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
                {
                    Branch     = branch,
                    ChannelId  = channel.Id,
                    Enabled    = true,
                    Repository = repository
                };
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            DefaultChannelUpdateData defaultChannelUpdateData = new DefaultChannelUpdateData()
            {
                Branch     = $"{branch}-UPDATED",
                ChannelId  = 404,
                Enabled    = false,
                Repository = $"NEW-{repository}"
            };
            var secondExpectedFailResult = await data.DefaultChannelsController.Update(defaultChannel.Id, defaultChannelUpdateData);

            secondExpectedFailResult.Should().BeOfType <NotFoundObjectResult>("Updating a default channel for a non-existent channel should give a not-found type result");
            // Try to get a default channel that just doesn't exist at all.
            var thirdExpectedFailResult = await data.DefaultChannelsController.Get(404);

            thirdExpectedFailResult.Should().BeOfType <NotFoundResult>("Getting a default channel for a non-existent default channel should give a not-found type result");
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Adds a default channel association.
        /// </summary>
        /// <param name="repository">Repository receiving the default association</param>
        /// <param name="branch">Branch receiving the default association</param>
        /// <param name="channel">Name of channel that builds of 'repository' on 'branch' should automatically be applied to.</param>
        /// <returns>Async task.</returns>
        public async Task AddDefaultChannelAsync(string repository, string branch, string channel)
        {
            // Look up channel to translate to channel id.
            Channel foundChannel = await GetChannel(channel);

            var defaultChannelsData = new DefaultChannelCreateData(repository: repository, branch: branch, channelId: foundChannel.Id);

            await _barClient.DefaultChannels.CreateAsync(defaultChannelsData);
        }
Exemplo n.º 4
0
 public async Task CreateAsync(
     DefaultChannelCreateData body,
     CancellationToken cancellationToken = default
     )
 {
     using (await CreateInternalAsync(
                body,
                cancellationToken
                ).ConfigureAwait(false))
     {
         return;
     }
 }
Exemplo n.º 5
0
        public async Task CreateAsync(
            DefaultChannelCreateData body,
            CancellationToken cancellationToken = default
            )
        {
            if (body == default(DefaultChannelCreateData))
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (!body.IsValid)
            {
                throw new ArgumentException("The parameter is not valid", nameof(body));
            }

            const string apiVersion = "2019-01-16";

            var _baseUri = Client.Options.BaseUri;
            var _url     = new RequestUriBuilder();

            _url.Reset(_baseUri);
            _url.AppendPath(
                "/api/default-channels",
                false);

            _url.AppendQuery("api-version", Client.Serialize(apiVersion));


            using (var _req = Client.Pipeline.CreateRequest())
            {
                _req.Uri    = _url;
                _req.Method = RequestMethod.Post;

                if (body != default(DefaultChannelCreateData))
                {
                    _req.Content = RequestContent.Create(Encoding.UTF8.GetBytes(Client.Serialize(body)));
                    _req.Headers.Add("Content-Type", "application/json; charset=utf-8");
                }

                using (var _res = await Client.SendAsync(_req, cancellationToken).ConfigureAwait(false))
                {
                    if (_res.Status < 200 || _res.Status >= 300)
                    {
                        await OnCreateFailed(_req, _res).ConfigureAwait(false);
                    }


                    return;
                }
            }
        }
        public async Task AddDuplicateDefaultChannels()
        {
            using TestData data = await BuildDefaultAsync();

            string channelName    = "TEST-CHANNEL-DUPLICATE-ENTRY-SCENARIO";
            string classification = "TEST-CLASSIFICATION";
            string repository     = "FAKE-REPOSITORY";
            string branch         = "FAKE-BRANCH";

            Channel channel;
            {
                var result = await data.ChannelsController.CreateChannel(channelName, classification);

                channel = (Channel)((ObjectResult)result).Value;
            }

            DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
            {
                Branch     = branch,
                ChannelId  = channel.Id,
                Enabled    = true,
                Repository = repository
            };

            DefaultChannel defaultChannel;

            {
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            defaultChannel.Should().NotBeNull();

            DefaultChannel defaultChannelDuplicateAdd;

            {
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannelDuplicateAdd = (DefaultChannel)((ObjectResult)result).Value;
            }

            // Adding the same thing twice should succeed, as well as provide the correct object in return.
            defaultChannelDuplicateAdd.Should().BeEquivalentTo(defaultChannel);
        }
        public async Task TryToAddNonExistentChannel()
        {
            using TestData data = await BuildDefaultAsync();

            string repository = "FAKE-REPOSITORY";
            string branch     = "FAKE-BRANCH";

            DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
            {
                Branch     = branch,
                ChannelId  = 404,
                Enabled    = true,
                Repository = repository
            };
            var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

            result.Should().BeOfType <NotFoundObjectResult>("Asking for a non-existent channel should give a not-found-object type result");
        }
        public async Task UpdateDefaultChannel()
        {
            using TestData data = await BuildDefaultAsync();

            string channelName    = "TEST-CHANNEL-TO-UPDATE";
            string classification = "TEST-CLASSIFICATION";
            string repository     = "FAKE-REPOSITORY";
            string branch         = "FAKE-BRANCH";

            Channel channel1, channel2;
            {
                var result = await data.ChannelsController.CreateChannel($"{channelName}-1", classification);

                channel1 = (Channel)((ObjectResult)result).Value;
                result   = await data.ChannelsController.CreateChannel($"{channelName}-2", classification);

                channel2 = (Channel)((ObjectResult)result).Value;
            }

            DefaultChannel defaultChannel;
            {
                DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
                {
                    Branch     = branch,
                    ChannelId  = channel1.Id,
                    Enabled    = true,
                    Repository = repository
                };
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            DefaultChannel updatedDefaultChannel;
            {
                DefaultChannelUpdateData defaultChannelUpdateData = new DefaultChannelUpdateData()
                {
                    Branch     = $"{branch}-UPDATED",
                    ChannelId  = channel2.Id,
                    Enabled    = false,
                    Repository = $"NEW-{repository}"
                };
                var result = await data.DefaultChannelsController.Update(defaultChannel.Id, defaultChannelUpdateData);

                updatedDefaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            List <DefaultChannel> defaultChannels;

            {
                IActionResult result = data.DefaultChannelsController.List($"NEW-{repository}", $"{branch}-UPDATED", channel2.Id, false);
                result.Should().BeAssignableTo <ObjectResult>();
                var objResult = (ObjectResult)result;
                objResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
                objResult.Value.Should().BeAssignableTo <IEnumerable <DefaultChannel> >();
                defaultChannels = ((IEnumerable <DefaultChannel>)objResult.Value).ToList();
            }

            defaultChannels.Should().ContainSingle();
            defaultChannels.Single().Channel.Id.Should().Be(channel2.Id, "Only fake channel #2's id should show up as a default channel");
        }
        public async Task DefaultChannelRegularExpressionMatching()
        {
            using TestData data = await BuildDefaultAsync();

            string channelName    = "TEST-CHANNEL-REGEX-FOR-DEFAULT";
            string classification = "TEST-CLASSIFICATION";
            string repository     = "FAKE-REPOSITORY";
            string branch         = "-regex:FAKE-BRANCH-REGEX-.*";

            Channel channel;
            {
                var result = await data.ChannelsController.CreateChannel($"{channelName}", classification);

                channel = (Channel)((ObjectResult)result).Value;
            }

            DefaultChannel defaultChannel;

            {
                DefaultChannelCreateData testDefaultChannelData = new DefaultChannelCreateData()
                {
                    Branch     = branch,
                    ChannelId  = channel.Id,
                    Enabled    = true,
                    Repository = repository
                };
                var result = await data.DefaultChannelsController.Create(testDefaultChannelData);

                defaultChannel = (DefaultChannel)((ObjectResult)result).Value;
            }

            string[] branchesThatMatch     = new string[] { "FAKE-BRANCH-REGEX-", "FAKE-BRANCH-REGEX-RELEASE-BRANCH-1", "FAKE-BRANCH-REGEX-RELEASE-BRANCH-2" };
            string[] branchesThatDontMatch = new string[] { "I-DONT-MATCH", "REAL-BRANCH-REGEX" };

            foreach (string branchName in branchesThatMatch)
            {
                List <DefaultChannel> defaultChannels;
                {
                    IActionResult result = data.DefaultChannelsController.List(repository, branchName, channel.Id);
                    result.Should().BeAssignableTo <ObjectResult>();
                    var objResult = (ObjectResult)result;
                    objResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
                    objResult.Value.Should().BeAssignableTo <IEnumerable <DefaultChannel> >();
                    defaultChannels = ((IEnumerable <DefaultChannel>)objResult.Value).ToList();
                }
                defaultChannels.Should().ContainSingle();
                defaultChannels.Single().Channel.Id.Should().Be(channel.Id);
            }

            foreach (string branchName in branchesThatDontMatch)
            {
                List <DefaultChannel> defaultChannels;
                {
                    IActionResult result = data.DefaultChannelsController.List(repository, branchName, channel.Id);
                    result.Should().BeAssignableTo <ObjectResult>();
                    var objResult = (ObjectResult)result;
                    objResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
                    objResult.Value.Should().BeAssignableTo <IEnumerable <DefaultChannel> >();
                    defaultChannels = ((IEnumerable <DefaultChannel>)objResult.Value).ToList();
                }
                defaultChannels.Should().BeEmpty();
            }
        }
Exemplo n.º 10
0
        internal async Task <HttpOperationResponse> CreateInternalAsync(
            DefaultChannelCreateData body,
            CancellationToken cancellationToken = default
            )
        {
            if (body == default)
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (!body.IsValid)
            {
                throw new ArgumentException("The parameter is not valid", nameof(body));
            }

            const string apiVersion = "2019-01-16";

            var _path = "/api/default-channels";

            var _query = new QueryBuilder();

            _query.Add("api-version", Client.Serialize(apiVersion));

            var _uriBuilder = new UriBuilder(Client.BaseUri);

            _uriBuilder.Path  = _uriBuilder.Path.TrimEnd('/') + _path;
            _uriBuilder.Query = _query.ToString();
            var _url = _uriBuilder.Uri;

            HttpRequestMessage  _req = null;
            HttpResponseMessage _res = null;

            try
            {
                _req = new HttpRequestMessage(HttpMethod.Post, _url);

                string _requestContent = null;
                if (body != default)
                {
                    _requestContent = Client.Serialize(body);
                    _req.Content    = new StringContent(_requestContent, Encoding.UTF8)
                    {
                        Headers =
                        {
                            ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"),
                        },
                    };
                }

                if (Client.Credentials != null)
                {
                    await Client.Credentials.ProcessHttpRequestAsync(_req, cancellationToken).ConfigureAwait(false);
                }

                _res = await Client.SendAsync(_req, cancellationToken).ConfigureAwait(false);

                if (!_res.IsSuccessStatusCode)
                {
                    await OnCreateFailed(_req, _res);
                }
                string _responseContent = await _res.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(new HttpOperationResponse
                {
                    Request = _req,
                    Response = _res,
                });
            }
            catch (Exception)
            {
                _req?.Dispose();
                _res?.Dispose();
                throw;
            }
        }