public void GivenDeployAProxy_WhenFirstDeployment_ThenSuccess()
        {
            var baseUri = new Uri("https://api.enterprise.apigee.com");
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;
            fixture.FakeResponseHandler.AddFakeResponse(
                new Uri(baseUri, $"v1/o/org/environments/dev/apis/apiName/revisions/1/deployments?override=True&delay=15"),
                new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(
                    ResourceHelper.GetResourceAsString("DeployResponseForSingleRevision.json"))
            });

            // Act
            ApigeeAliases.DeployProxy(fixture.ContextMock.Object, "org", "dev", "apiName", "1", new DeployProxySettings());
        }
        public void GivenInstallNodePackagedModules_WhenModulesRestored_ThenModulesAndVersionsReturned()
        {
            // Arrange
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;

            fixture.UseSuccessfulNpmInstallResponse();

            // Act
            var modules = ApigeeAliases.InstallNodePackagedModules(fixture.ContextMock.Object, "org", "proxy", "123");

            // Assert
            Assert.Equal(14, modules.Length);
            Assert.Equal("express-xml-bodyparser", modules[0].Name);
            Assert.Equal("0.3.0", modules[0].Version);
        }
        public void GivenImportProxy_WhenImportFails_ThenUsefulErrorShown()
        {
            // Arrange
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;

            fixture.UseFailedImportResponse();

            // Act
            try
            {
                ApigeeAliases.ImportProxy(fixture.ContextMock.Object, "org", "proxy", fixture.GetProxyZipFilePath());
            }
            catch (Exception ex)
            {
                Assert.StartsWith("Apigee returned BadRequest", ex.Message);
            }
        }
        public void GivenDeleteAllUndeployedApiProxyRevisions_WhenAllRevisionsDeleted_ThenSucceed()
        {
            // Arrange
            Uri baseUri = new Uri("https://api.enterprise.apigee.com");
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;
            fixture.FakeResponseHandler.AddFakeResponse(new Uri(baseUri, $"v1/organizations/org/apis/proxy"), new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(ResourceHelper.GetResourceAsString("GetApiProxyResponse.json"))
            });
            for (var revision = 1; revision < 10; revision++)
            {
                fixture.FakeResponseHandler.AddFakeResponse(new Uri(baseUri, $"v1/organizations/org/apis/proxy/revisions/" + revision), new HttpResponseMessage(HttpStatusCode.OK));
            }

            // Act
            ApigeeAliases.DeleteAllUndeployedApiProxyRevisions(fixture.ContextMock.Object, "org", "proxy");
        }
        public void GivenImportProxy_WhenCredentialsSupplied_ThenCredentialsAddedToAuthorizationHeader()
        {
            // Arrange
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;

            fixture.UseSuccessfulImportResponse();

            // Act
            var credentials = new Credentials {
                Username = "******", Password = "******"
            };

            ApigeeAliases.ImportProxy(fixture.ContextMock.Object, "org", "proxy", fixture.GetProxyZipFilePath(), new ImportProxySettings {
                Credentials = credentials
            });

            // Assert
            Assert.Equal("Basic", fixture.FakeResponseHandler.Requests.First().Headers.Authorization.Scheme);
        }
        public void GivenImportProxy_WhenImportFailsDueToValidation_ThenValidationErrorIsLoggedAndExceptionThrown()
        {
            // Arrange
            var fixture = new ApigeeAliasesFixture(this.output);

            ApigeeAliases.ApigeeProxyManagementService = fixture.ApigeeProxyManagementService;

            fixture.UseValidationFailedImportResponse();

            // Act
            try
            {
                ApigeeAliases.ImportProxy(fixture.ContextMock.Object, "org", "proxy", fixture.GetProxyZipFilePath());
            }
            catch (Exception ex)
            {
                Assert.StartsWith("Apigee returned BadRequest", ex.Message);
            }

            Assert.Contains(fixture.LogMessages, message => message.Contains("BadElement"));
        }