<commit_before>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Automation.Tests.Helpers;
using Microsoft.Azure.Management.Automation;
using Microsoft.Azure.Management.Automation.Models;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Rest.Azure;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Newtonsoft.Json;
using HttpStatusCode = System.Net.HttpStatusCode;

namespace Automation.Tests.TestSupport
{
    public class AutomationTestBase : TestBase, IDisposable
    {
        private const string ResourceGroup = "automation-sdk-test";
        private const string AutomationAccount = "automation-sdk-test-account";
        private const string Location = "East US 2";

        public AutomationTestBase(MockContext context)
        {
            var handler = new RecordedDelegatingHandler();
            AutomationClient = ResourceGroupHelper.GetAutomationClient(context, handler);
            var resourcesClient = ResourceGroupHelper.GetResourcesClient(context, handler);

            try
            {
                resourcesClient.ResourceGroups.CreateOrUpdate(ResourceGroup,
                    new ResourceGroup
                    {
                        Location = Location
                    });

                AutomationClient.AutomationAccount.CreateOrUpdate(
                    ResourceGroup, AutomationAccount, new AutomationAccountCreateOrUpdateParameters
                    {
                        Name = AutomationAccount,
                        Location = Location,
                        Sku = new Sku {Name = "Free", Family = "Test", Capacity = 1}
                    });
            }
            catch (CloudException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.Conflict) throw;
            }

            // Clean up the automation account, delete runbooks, schedules, variables, etc.
            CleanUpCredentials();
            CleanUpRunbooks();
            CleanUpSchedules();
            CleanUpVariables();
            CleanUpWebhooks();
        }

        public AutomationClient AutomationClient { get; private set; }

        public void CleanUpRunbooks()
        {
            var runbooks = AutomationClient.Runbook.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var rb in runbooks)
            {
                DeleteRunbook(rb.Name);
            }
        }

        public void CleanUpSchedules()
        {
            var schedules = AutomationClient.Schedule.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var schedule in schedules)
            {
                DeleteSchedule(schedule.Name);
            }
        }

        public void CleanUpVariables()
        {
            var variables = AutomationClient.Variable.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var variable in variables)
            {
                DeleteVariable(variable.Name);
            }
        }

        public void CleanUpWebhooks()
        {
            var webhooks =
                AutomationClient.Webhook.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var webhook in webhooks)
            {
                DeleteWebhook(webhook.Name);
            }
        }

        public Credential CreateCredential(string credentialName, string userName, string password,
            string description = null)
        {
            var credential = AutomationClient.Credential.CreateOrUpdate(ResourceGroup, AutomationAccount,
                credentialName,
                new CredentialCreateOrUpdateParameters(credentialName,
                    userName,
                    password,
                    description));
            return credential;
        }

        public Webhook CreateWebhook(string webhookName, string runbookName, string uri, string description = null)
        {
            var webhook = AutomationClient.Webhook.CreateOrUpdate(ResourceGroup, AutomationAccount, webhookName,
                new WebhookCreateOrUpdateParameters
                {
                    Name = webhookName,
                    Runbook = new RunbookAssociationProperty {Name = runbookName},
                    Uri = uri,
                    IsEnabled = true,
                    ExpiryTime = DateTime.Now.AddYears(1)
                });
            return webhook;
        }

        public string GenerateUriForWebhook()
        {
            return AutomationClient.Webhook.GenerateUri(ResourceGroup, AutomationAccount);
        }

        public void CreateRunbook(string runbookName, string runbookContent, string description = null)
        {
            AutomationClient.Runbook.CreateOrUpdate(ResourceGroup, AutomationAccount, runbookName,
                new RunbookCreateOrUpdateParameters
                {
                    Name = runbookName,
                    Description = description,
                    Location = Location,
                    RunbookType = RunbookTypeEnum.PowerShellWorkflow,
                    Draft = new RunbookDraft()
                });

            this.UpdateRunbookContent(runbookName, runbookContent);
        }

        public Schedule CreateHourlySchedule(string scheduleName, DateTimeOffset startTime,
            DateTimeOffset expiryTime, string description = null, byte hourInterval = 1)
        {
            var schedule = AutomationClient.Schedule.CreateOrUpdate(ResourceGroup, AutomationAccount, scheduleName,
                new ScheduleCreateOrUpdateParameters
                {
                    Name = scheduleName,
                    StartTime = startTime.DateTime,
                    ExpiryTime = expiryTime.DateTime,
                    Description = description,
                    Interval = hourInterval,
                    Frequency = ScheduleFrequency.Hour<commit_msg>Adding a dummy checkin to kickoff Travis Build<commit_after>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Automation.Tests.Helpers;
using Microsoft.Azure.Management.Automation;
using Microsoft.Azure.Management.Automation.Models;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Rest.Azure;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Newtonsoft.Json;
using HttpStatusCode = System.Net.HttpStatusCode;

namespace Automation.Tests.TestSupport
{
    public class AutomationTestBase : TestBase, IDisposable
    {
        private const string ResourceGroup = "automation-sdk-test";
        private const string AutomationAccount = "automation-sdk-test-account";
        private const string Location = "East US 2";

        public AutomationTestBase(MockContext context)
        {
            var handler = new RecordedDelegatingHandler();
            AutomationClient = ResourceGroupHelper.GetAutomationClient(context, handler);
            var resourcesClient = ResourceGroupHelper.GetResourcesClient(context, handler);

            try
            {
                resourcesClient.ResourceGroups.CreateOrUpdate(ResourceGroup,
                    new ResourceGroup
                    {
                        Location = Location
                    });

                AutomationClient.AutomationAccount.CreateOrUpdate(
                    ResourceGroup, AutomationAccount, new AutomationAccountCreateOrUpdateParameters
                    {
                        Name = AutomationAccount,
                        Location = Location,
                        Sku = new Sku {Name = "Free", Family = "Test", Capacity = 1}
                    });
            }
            catch (CloudException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.Conflict) throw;
            }

            // Clean up the automation account, delete runbooks, schedules, variables, etc.
            CleanUpCredentials();
            CleanUpRunbooks();
            CleanUpSchedules();
            CleanUpVariables();
            CleanUpWebhooks();
        }

        public AutomationClient AutomationClient { get; private set; }

        public void CleanUpRunbooks()
        {
            var runbooks = AutomationClient.Runbook.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var rb in runbooks)
            {
                DeleteRunbook(rb.Name);
            }
        }

        public void CleanUpSchedules()
        {
            var schedules = AutomationClient.Schedule.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var schedule in schedules)
            {
                DeleteSchedule(schedule.Name);
            }
        }

        public void CleanUpVariables()
        {
            var variables = AutomationClient.Variable.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var variable in variables)
            {
                DeleteVariable(variable.Name);
            }
        }

        public void CleanUpWebhooks()
        {
            var webhooks =
                AutomationClient.Webhook.ListByAutomationAccount(ResourceGroup, AutomationAccount);

            foreach (var webhook in webhooks)
            {
                DeleteWebhook(webhook.Name);
            }
        }

        public Credential CreateCredential(string credentialName, string userName, string password,
            string description = null)
        {
            var credential = AutomationClient.Credential.CreateOrUpdate(ResourceGroup, AutomationAccount,
                credentialName,
                new CredentialCreateOrUpdateParameters(credentialName,
                    userName,
                    password,
                    description));
            return credential;
        }

        public Webhook CreateWebhook(string webhookName, string runbookName, string uri, string description = null)
        {
            var webhook = AutomationClient.Webhook.CreateOrUpdate(ResourceGroup, AutomationAccount, webhookName,
                new WebhookCreateOrUpdateParameters
                {
                    Name = webhookName,
                    Runbook = new RunbookAssociationProperty {Name = runbookName},
                    Uri = uri,
                    IsEnabled = true,
                    ExpiryTime = DateTime.Now.AddYears(1)
                });
            return webhook;
        }

        public string GenerateUriForWebhook()
        {
            return AutomationClient.Webhook.GenerateUri(ResourceGroup, AutomationAccount);
        }

        public void CreateRunbook(string runbookName, string runbookContent, string description = null)
        {
            AutomationClient.Runbook.CreateOrUpdate(ResourceGroup, AutomationAccount, runbookName,
                new RunbookCreateOrUpdateParameters
                {
                    Name = runbookName,
                    Description = description,
                    Location = Location,
                    RunbookType = RunbookTypeEnum.PowerShellWorkflow,
                    Draft = new RunbookDraft()
                });

            UpdateRunbookContent(runbookName, runbookContent);
        }

        public Schedule CreateHourlySchedule(string scheduleName, DateTimeOffset startTime,
            DateTimeOffset expiryTime, string description = null, byte hourInterval = 1)
        {
            var schedule = AutomationClient.Schedule.CreateOrUpdate(ResourceGroup, AutomationAccount, scheduleName,
                new ScheduleCreateOrUpdateParameters
                {
                    Name = scheduleName,
                    StartTime = startTime.DateTime,
                    ExpiryTime = expiryTime.DateTime,
                    Description = description,
                    Interval = hourInterval,
                    Frequency = ScheduleFrequency.Hour
{'fraction_non_alphanumeric': 0.06246953696181966, 'fraction_numerical': 0.0006498781478472786, 'mean_word_length': 1.8477908859588248, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5218617', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2657, 'n_words': 632}