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");
        }
Esempio n. 2
0
        public async Task <DefaultChannel> UpdateAsync(
            int id,
            DefaultChannelUpdateData body       = default,
            CancellationToken cancellationToken = default
            )
        {
            if (id == default(int))
            {
                throw new ArgumentNullException(nameof(id));
            }

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

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

            _url.Reset(_baseUri);
            _url.AppendPath(
                "/api/default-channels/{id}".Replace("{id}", Uri.EscapeDataString(Client.Serialize(id))),
                false);

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


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

                if (body != default(DefaultChannelUpdateData))
                {
                    _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 OnUpdateFailed(_req, _res).ConfigureAwait(false);
                    }

                    if (_res.ContentStream == null)
                    {
                        await OnUpdateFailed(_req, _res).ConfigureAwait(false);
                    }

                    using (var _reader = new StreamReader(_res.ContentStream))
                    {
                        var _content = await _reader.ReadToEndAsync().ConfigureAwait(false);

                        var _body = Client.Deserialize <DefaultChannel>(_content);
                        return(_body);
                    }
                }
            }
        }
Esempio n. 3
0
 public async Task <DefaultChannel> UpdateAsync(
     int id,
     DefaultChannelUpdateData body       = default,
     CancellationToken cancellationToken = default
     )
 {
     using (var _res = await UpdateInternalAsync(
                id,
                body,
                cancellationToken
                ).ConfigureAwait(false))
     {
         return(_res.Body);
     }
 }
Esempio n. 4
0
        /// <summary>
        ///     Updates a default channel with new information.
        /// </summary>
        /// <param name="id">Id of default channel.</param>
        /// <param name="repository">New repository</param>
        /// <param name="branch">New branch</param>
        /// <param name="channel">New channel</param>
        /// <param name="enabled">Enabled/disabled status</param>
        /// <returns>Async task</returns>
        public async Task UpdateDefaultChannelAsync(int id, string repository = null, string branch = null,
                                                    string channel            = null, bool?enabled  = null)
        {
            Channel foundChannel = null;

            if (!string.IsNullOrEmpty(channel))
            {
                foundChannel = await GetChannel(channel);
            }

            DefaultChannelUpdateData updateData = new DefaultChannelUpdateData
            {
                Branch     = branch,
                ChannelId  = foundChannel?.Id,
                Enabled    = enabled,
                Repository = repository
            };

            await _barClient.DefaultChannels.UpdateAsync(id, updateData);
        }
        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");
        }
Esempio n. 6
0
        internal async Task <HttpOperationResponse <DefaultChannel> > UpdateInternalAsync(
            int id,
            DefaultChannelUpdateData body       = default,
            CancellationToken cancellationToken = default
            )
        {
            if (id == default)
            {
                throw new ArgumentNullException(nameof(id));
            }

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

            var _path = "/api/default-channels/{id}";

            _path = _path.Replace("{id}", Client.Serialize(id));

            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(new HttpMethod("PATCH"), _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 OnUpdateFailed(_req, _res);
                }
                string _responseContent = await _res.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(new HttpOperationResponse <DefaultChannel>
                {
                    Request = _req,
                    Response = _res,
                    Body = Client.Deserialize <DefaultChannel>(_responseContent),
                });
            }
            catch (Exception)
            {
                _req?.Dispose();
                _res?.Dispose();
                throw;
            }
        }