public void Test_SubscriptionOperations_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    SubscriptionQuotasGetResponse subscriptionQuotasGetResponse = client.Subscriptions.GetQuotas(serviceLocation);
                    Assert.Equal(HttpStatusCode.OK, subscriptionQuotasGetResponse.StatusCode);
                    Assert.Equal(0, subscriptionQuotasGetResponse.Value.FirstOrDefault().Properties.CurrentCount);
                    Assert.Equal(50, subscriptionQuotasGetResponse.Value.FirstOrDefault().Properties.MaxCount);
                }
                finally
                {
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public async Task<Microsoft.Azure.Management.Resources.Models.ResourceGroup> CreateResourceGroup(
                        [Metadata("Name", "The name of the resource group")]string name,
                        [Metadata("Location", "The location of the resource group")]string location
            )
        {
            var client = await ResourceUtilities.GetClient().ConfigureAwait(continueOnCapturedContext: false);

            var parameters = new ResourceGroup()
            {
                Location = location
            };
            var result = await client.ResourceGroups.CreateOrUpdateAsync(name, parameters, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);

            return result.ResourceGroup;
        }
Example #3
0
        private void CreateResourceGroup()
        {
            azureLogin.Authenticate();
            using (var resourceManagementClient = new ResourceManagementClient(azureLogin.Credentials))
            using (var resourceManagementClient2 = new ResourceManagementClient(azureLogin.Credentials))
            using (var resourceManagementClient3 = new ResourceManagementClient(azureLogin.Credentials))
            {
                var rg = new ResourceGroup()
                {
                    Location = demo.Location,
                };
                HandleResult($"CreateOrUpdate ResourceGroup {demo.Group} @ {demo.Location}",
                    () => resourceManagementClient.ResourceGroups.CreateOrUpdate(demo.Group, rg));

                Parallel.Invoke(
                    () => HandleResult("Register Microsoft.Storage", () => resourceManagementClient.Providers.Register("Microsoft.Storage")),
                    () => HandleResult("Register Microsoft.Network", () => resourceManagementClient2.Providers.Register("Microsoft.Network")),
                    () => HandleResult("Register Microsoft.Compute", () => resourceManagementClient3.Providers.Register("Microsoft.Compute"))
                );
            }
        }
Example #4
0
        public void OAuthAuthorizationSessionTest()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                const string oAuthType = "AzureDataLake";
                string resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string factoryName = TestUtilities.GenerateName("DataFactory");
                string serverLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetDataFactoryManagementClient(handler);

                ResourceGroup resourceGroup = new ResourceGroup() { Location = serverLocation };
                resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                // create a data factory
                var df = new Microsoft.Azure.Management.DataFactories.Models.DataFactory()
                {
                    Name = factoryName,
                    Location = serverLocation
                };

                client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                {
                    DataFactory = df,
                });

                AuthorizationSessionGetResponse response = client.OAuth.Get(resourceGroupName, factoryName, oAuthType);
                
                Assert.True(response.StatusCode == HttpStatusCode.OK);
                Assert.NotNull(response.AuthorizationSession.Endpoint);
                Assert.NotNull(response.AuthorizationSession.SessionId);
            }
        }
        public void Test_OutputOperations_Blob()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    string outputName = TestUtilities.GenerateName("outputtest");
                    StorageAccount storageAccount = new StorageAccount();
                    storageAccount.AccountName = TestHelper.AccountName;
                    storageAccount.AccountKey = TestHelper.AccountKey;
                    BlobOutputDataSource blobOutputDataSource = new BlobOutputDataSource()
                    {
                        Properties = new BlobOutputDataSourceProperties()
                        {
                            StorageAccounts = new[] { storageAccount },
                            Container = "state",
                            PathPattern = "{date}",
                            DateFormat = "yyyy/MM/dd"
                        }
                    };
                    JsonSerialization jsonSerialization = new JsonSerialization()
                    {
                        Properties = new JsonSerializationProperties()
                        {
                            Encoding = "UTF8",
                            Format = Format.LineSeparated
                        }
                    };
                    outputProperties.DataSource = blobOutputDataSource;
                    outputProperties.Serialization = jsonSerialization;
                    Output output1 = new Output(outputName)
                    {
                        Properties = outputProperties
                    };

                    // Add an output
                    OutputCreateOrUpdateParameters outputCreateOrUpdateParameters = new OutputCreateOrUpdateParameters();
                    outputCreateOrUpdateParameters.Output = output1;
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(outputName, outputCreateOrUpdateResponse.Output.Name);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.DataSource is BlobOutputDataSource);
                    BlobOutputDataSource blobOutputDataSourceInResponse1 = (BlobOutputDataSource)outputCreateOrUpdateResponse.Output.Properties.DataSource;
                    Assert.Equal("{date}", blobOutputDataSourceInResponse1.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobOutputDataSourceInResponse1.Properties.DateFormat);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Get the output
                    OutputGetResponse outputGetResponse = client.Outputs.Get(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, outputGetResponse.StatusCode);
                    Assert.Equal(outputName, outputGetResponse.Output.Name);
                    Assert.True(outputGetResponse.Output.Properties.DataSource is BlobOutputDataSource);
                    BlobOutputDataSource blobOutputDataSourceInResponse2 = (BlobOutputDataSource)outputGetResponse.Output.Properties.DataSource;
                    Assert.Equal("{date}", blobOutputDataSourceInResponse2.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobOutputDataSourceInResponse2.Properties.DateFormat);
                    Assert.Equal(outputCreateOrUpdateResponse.Output.Properties.Etag, outputGetResponse.Output.Properties.Etag);

                    // Test output connectivity
                    DataSourceTestConnectionResponse response = client.Outputs.TestConnection(resourceGroupName, resourceName, outputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the output
                    blobOutputDataSource.Properties.PathPattern = "test.csv";
                    outputProperties.DataSource = blobOutputDataSource;
                    outputProperties.Etag = outputCreateOrUpdateResponse.Output.Properties.Etag;
                    OutputPatchParameters outputPatchParameters = new OutputPatchParameters(outputProperties);
                    OutputPatchResponse outputPatchResponse = client.Outputs.Patch(resourceGroupName, resourceName, outputName, outputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, outputPatchResponse.StatusCode);
                    Assert.True(outputPatchResponse.Properties.DataSource is BlobOutputDataSource);
                    BlobOutputDataSource blobOutputDataSourceInResponse3 = (BlobOutputDataSource)outputPatchResponse.Properties.DataSource;
                    Assert.Equal("test.csv", blobOutputDataSourceInResponse3.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobOutputDataSourceInResponse3.Properties.DateFormat);
                    Assert.NotNull(outputPatchResponse.Properties.Etag);
                    Assert.NotEqual(outputCreateOrUpdateResponse.Output.Properties.Etag, outputPatchResponse.Properties.Etag);

                    // Delete the output
                    AzureOperationResponse deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    // Check that there are 0 outputs in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Outputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void TestCreateTable()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string factoryName = TestUtilities.GenerateName("DataFactory");
                string linkedServiceName = "foo2"; // need to hard coded as it is referenced in table json
                string tableName = TestUtilities.GenerateName("table");
                string serverLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetDataPipelineManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serverLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // create a data factory
                    DataFactory df = new DataFactory() { Name = factoryName, Location = serverLocation };
                    client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                    {
                        DataFactory = df,
                    });

                    // verify data factory
                    var dfResponse = client.DataFactories.Get(resourceGroupName, factoryName);
                    Assert.True(dfResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(dfResponse.DataFactory.Name == df.Name);
                    Assert.True(dfResponse.DataFactory.Location == df.Location);

                    // create a linked service 
                    string content = File.ReadAllText(@"Resources\linkedService.json");
                    client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedServiceName, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                    {
                        Content = content,
                    });

                    // verify linked service
                    var lsResponse = client.LinkedServices.Get(resourceGroupName, factoryName, linkedServiceName);
                    Assert.True(lsResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(lsResponse.LinkedService.Name == linkedServiceName);

                    // create a table
                    content = File.ReadAllText(@"Resources\table.json");
                    client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, tableName, new TableCreateOrUpdateWithRawJsonContentParameters()
                    {
                        Content = content,
                    });

                    // verify table
                    var tResponse = client.Tables.Get(resourceGroupName, factoryName, tableName);
                    Assert.True(tResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(tResponse.Table.Name == tableName);
                }
                finally
                {
                    client.DataFactories.Delete(resourceGroupName, factoryName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        } 
        public void WikipediaPipelineTest()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string factoryName = TestUtilities.GenerateName("DataFactory");
                string serverLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetDataPipelineManagementClient(handler);

                // Linked Service names are hard-coded as they are referenced in Data Artifacts. 
                // Data Artifact names are hard-coded as they are referenced in Data Pipeline.
                string linkedService_ClickEvent = "LinkedService-WikipediaClickEvents";
                string linkedService_CuratedData = "LinkedService-CuratedWikiData";
                string linkedService_AggregatedData = "LinkedService-WikiAggregatedData";
                string linkedService_Hdibyoc = "HDILinkedService";

                string dataArtifact_ClickEvent = "DA_WikipediaClickEvents";
                string dataArtifact_CuratedData = "DA_CuratedWikiData";
                string dataArtifact_AggregatedData = "DA_WikiAggregatedData";

                string pipelineName = TestUtilities.GenerateName("DP_Wikipedia");

                ResourceGroup resourceGroup = new ResourceGroup() { Location = serverLocation };
                resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                // create a data factory
                DataFactory df = new DataFactory() { Name = factoryName, Location = serverLocation };
                client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                {
                    DataFactory = df,
                });

                // verify data factory
                var dfResponse = client.DataFactories.Get(resourceGroupName, factoryName);
                Assert.True(dfResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(dfResponse.DataFactory.Name == df.Name);
                Assert.True(dfResponse.DataFactory.Location == df.Location);

                // create linked services
                string content = File.ReadAllText(@"Resources\LinkedService_WikipediaClickEvents.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedService_ClickEvent, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\LinkedService_CuratedWikiData.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedService_CuratedData, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\LinkedService_WikiAggregatedData.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedService_AggregatedData, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\LinkedService_HDIBYOC.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedService_Hdibyoc, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                // Create Tables
                content = File.ReadAllText(@"Resources\DA_WikiAggregatedData.json");
                client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, dataArtifact_AggregatedData, new TableCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\DA_CuratedWikiData.json");
                client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, dataArtifact_CuratedData, new TableCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\DA_WikipediaClickEvents.json");
                client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, dataArtifact_ClickEvent, new TableCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                // create pipeline
                content = File.ReadAllText(@"Resources\DP_Wikisamplev2json.json");
                client.Pipelines.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, pipelineName, new PipelineCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                DateTime now = DateTime.Parse("2014-10-08T12:00:00");
                DateTime start = new DateTime(now.Year, now.Month, now.Day, now.Hour - 4, 0, 0);
                string startTime = start.ToString("yyyy-MM-ddTHH:mm:ss");
                string endTime = start.AddHours(2).ToString("yyyy-MM-ddTHH:mm:ss");

                client.Pipelines.SetActivePeriod(resourceGroupName, factoryName, pipelineName, new PipelineSetActivePeriodParameters()
                {
                    ActivePeriodStartTime = startTime,
                    ActivePeriodEndTime = endTime,
                });

                // verify linked services
                string[] linkedServices = new string[] { linkedService_ClickEvent, linkedService_CuratedData, linkedService_AggregatedData, linkedService_Hdibyoc };
                foreach (string s in linkedServices)
                {
                    var lsResponse = client.LinkedServices.Get(resourceGroupName, factoryName, s);
                    Assert.True(lsResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(lsResponse.LinkedService.Name == s);
                }

                // verify tables
                string[] tableNames = new string[] { dataArtifact_ClickEvent, dataArtifact_CuratedData, dataArtifact_AggregatedData };
                foreach (string tableName in tableNames)
                {
                    var tResponse = client.Tables.Get(resourceGroupName, factoryName, tableName);
                    Assert.True(tResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(tResponse.Table.Name == tableName);
                }

                // verify slice
                var sliceResponse = client.DataSlices.List(resourceGroupName, factoryName, dataArtifact_AggregatedData, startTime, endTime);
                Assert.True(sliceResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(sliceResponse.DataSlices.Count == 2);
            }
        }
        public void Test_JobOperationsWithJsonContent_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    Job job = new Job();
                    job.Location = serviceLocation;

                    string inputName = TestUtilities.GenerateName("input");
                    string outputName = TestUtilities.GenerateName("output");
                    string transformationName = TestUtilities.GenerateName("transformation");
                    string functionName = TestUtilities.GenerateName("function");

                    // Create a streaming job
                    string content = File.ReadAllText(@"Resources\JobDefinition.json");
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse =
                        client.StreamingJobs.CreateOrUpdateWithRawJsonContent(resourceGroupName, resourceName,
                            new JobCreateOrUpdateWithRawJsonContentParameters()
                            {
                                Content = content
                            });
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(jobCreateOrUpdateResponse.Job.Properties.Etag);

                    // Create an input for the streaming job
                    content = File.ReadAllText(@"Resources\InputDefinition.json");
                    InputCreateOrUpdateResponse inputCreateOrUpdateResponse =
                        client.Inputs.CreateOrUpdateWithRawJsonContent(resourceGroupName, resourceName, inputName,
                            new InputCreateOrUpdateWithRawJsonContentParameters()
                            {
                                Content = content
                            });
                    Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(inputCreateOrUpdateResponse.Input.Properties.Etag);

                    // Create an output for the streaming job
                    content = File.ReadAllText(@"Resources\OutputDefinition.json");
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse =
                        client.Outputs.CreateOrUpdateWithRawJsonContent(resourceGroupName, resourceName, outputName,
                            new OutputCreateOrUpdateWithRawJsonContentParameters()
                            {
                                Content = content
                            });
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Create a tranformation for the streaming job
                    content = File.ReadAllText(@"Resources\TransformationDefinition.json");
                    TransformationCreateOrUpdateResponse transformationCreateOrUpdateResponse =
                        client.Transformations.CreateOrUpdateWithRawJsonContent(resourceGroupName, resourceName,
                            transformationName, new TransformationCreateOrUpdateWithRawJsonContentParameters()
                            {
                                Content = content
                            });
                    Assert.Equal(HttpStatusCode.OK, transformationCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(transformationCreateOrUpdateResponse.Transformation.Properties.Etag);

                    content = File.ReadAllText(@"Resources\FunctionDefinition.json");
                    FunctionCreateOrUpdateResponse functionCreateOrUpdateResponse =
                        client.Functions.CreateOrUpdateWithRawJsonContent(resourceGroupName, resourceName, functionName,
                            new FunctionCreateOrUpdateWithRawJsonContentParameters()
                            {
                                Content = content
                            });
                    Assert.Equal(HttpStatusCode.OK, functionCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(functionCreateOrUpdateResponse.Function.Properties.Etag);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters("inputs,transformation,outputs,functions");
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);
                    Assert.Equal(inputName, jobGetResponse.Job.Properties.Inputs.SingleOrDefault().Name);
                    Assert.Equal(outputName, jobGetResponse.Job.Properties.Outputs.SingleOrDefault().Name);
                    Assert.Equal(transformationName, jobGetResponse.Job.Properties.Transformation.Name);
                    Assert.Equal(functionName, jobGetResponse.Job.Properties.Functions.SingleOrDefault().Name);
                    Assert.NotNull(jobGetResponse.Job.Properties.Etag);
                    Assert.NotEqual(jobCreateOrUpdateResponse.Job.Properties.Etag, jobGetResponse.Job.Properties.Etag);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void GetsAllResourceGroupsWithDetails()
        {
            ResourceGroup resourceGroup1 = new ResourceGroup() { Name = resourceGroupName + 1, Location = resourceGroupLocation };
            ResourceGroup resourceGroup2 = new ResourceGroup() { Name = resourceGroupName + 2, Location = resourceGroupLocation };
            ResourceGroup resourceGroup3 = new ResourceGroup() { Name = resourceGroupName + 3, Location = resourceGroupLocation };
            ResourceGroup resourceGroup4 = new ResourceGroup() { Name = resourceGroupName + 4, Location = resourceGroupLocation };
            resourceGroupMock.Setup(f => f.ListAsync(null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupListResult
                {
                    ResourceGroups = new List<ResourceGroup>() { resourceGroup1, resourceGroup2, resourceGroup3, resourceGroup4 }
                }));
            SetupListForResourceGroupAsync(resourceGroup1.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup2.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup3.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup4.Name, new List<Resource>() { new Resource() { Name = "resource" } });

            List<PSResourceGroup> actual = resourcesClient.FilterResourceGroups(null, null, true);

            Assert.Equal(4, actual.Count);
            Assert.Equal(resourceGroup1.Name, actual[0].ResourceGroupName);
            Assert.Equal(resourceGroup2.Name, actual[1].ResourceGroupName);
            Assert.Equal(resourceGroup3.Name, actual[2].ResourceGroupName);
            Assert.Equal(resourceGroup4.Name, actual[3].ResourceGroupName);
            Assert.Equal(1, actual[0].Resources.Count());
            Assert.Equal(1, actual[1].Resources.Count());
            Assert.Equal(1, actual[2].Resources.Count());
            Assert.Equal(1, actual[3].Resources.Count());
        }
        private async Task<string> CreateResourceGroupAsync(TokenCloudCredentials credential, string rgName)
        {
            ResourceGroup resourceGroup = new ResourceGroup {Location = this.settings.Region};

            using (ResourceManagementClient resourceManagementClient = new ResourceManagementClient(credential))
            {
                ResourceGroupExistsResult exists = await resourceManagementClient.ResourceGroups.CheckExistenceAsync(rgName);

                if (exists.Exists)
                {
                    return "Exists";
                }

                ResourceGroupCreateOrUpdateResult rgResult = await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync(rgName, resourceGroup);

                return rgResult.StatusCode.ToString();
            }
        }
        public void Test_InputOperations_IoTHub()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Input
                    string sharedAccessPolicyName = "owner";
                    InputProperties inputProperties = new StreamInputProperties()
                    {
                        Serialization = new CsvSerialization()
                        {
                            Properties = new CsvSerializationProperties()
                            {
                                FieldDelimiter = ",",
                                Encoding = "UTF8"
                            }
                        },
                        DataSource = new IoTHubStreamInputDataSource()
                        {
                            Properties = new IoTHubStreamInputDataSourceProperties()
                            {
                                IotHubNamespace = TestHelper.IoTHubNamespace,
                                SharedAccessPolicyName = sharedAccessPolicyName,
                                SharedAccessPolicyKey = TestHelper.IotHubSharedAccessPolicyKey
                            }
                        }
                    };
                    string inputName = TestUtilities.GenerateName("inputtest");
                    Input input1 = new Input(inputName)
                    {
                        Properties = inputProperties
                    };

                    // Add an input
                    InputCreateOrUpdateParameters inputCreateOrUpdateParameters = new InputCreateOrUpdateParameters();
                    inputCreateOrUpdateParameters.Input = input1;
                    InputCreateOrUpdateResponse inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(inputName, inputCreateOrUpdateResponse.Input.Name);
                    Assert.Equal("Stream", inputCreateOrUpdateResponse.Input.Properties.Type);
                    Assert.True(inputCreateOrUpdateResponse.Input.Properties.Serialization is CsvSerialization);
                    CsvSerialization csvSerializationInResponse1 = (CsvSerialization)inputCreateOrUpdateResponse.Input.Properties.Serialization;
                    Assert.Equal(",", csvSerializationInResponse1.Properties.FieldDelimiter);
                    Assert.True(inputCreateOrUpdateResponse.Input.Properties is StreamInputProperties);
                    StreamInputProperties streamInputPropertiesInResponse = (StreamInputProperties)inputCreateOrUpdateResponse.Input.Properties;
                    Assert.True(streamInputPropertiesInResponse.DataSource is IoTHubStreamInputDataSource);
                    IoTHubStreamInputDataSource iotHubInputDataSourceInResponse1 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse.DataSource;
                    Assert.Equal(sharedAccessPolicyName, iotHubInputDataSourceInResponse1.Properties.SharedAccessPolicyName);
                    Assert.NotNull(streamInputPropertiesInResponse.Etag);

                    // Get the input
                    InputGetResponse inputGetResponse = client.Inputs.Get(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, inputGetResponse.StatusCode);
                    Assert.Equal(inputName, inputGetResponse.Input.Name);
                    Assert.True(inputGetResponse.Input.Properties is StreamInputProperties);
                    StreamInputProperties streamInputPropertiesInResponse2 = (StreamInputProperties)inputGetResponse.Input.Properties;
                    Assert.True(streamInputPropertiesInResponse2.DataSource is IoTHubStreamInputDataSource);
                    IoTHubStreamInputDataSource iotHubInputDataSourceInResponse2 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse2.DataSource;
                    Assert.Equal(sharedAccessPolicyName, iotHubInputDataSourceInResponse2.Properties.SharedAccessPolicyName);
                    Assert.Equal(inputCreateOrUpdateResponse.Input.Properties.Etag, inputGetResponse.Input.Properties.Etag);

                    // Test input connectivity
                    DataSourceTestConnectionResponse response = client.Inputs.TestConnection(resourceGroupName, resourceName, inputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the input
                    string newSharedPolicyName = TestUtilities.GenerateName("owner");
                    inputProperties = new StreamInputProperties()
                    {
                        Serialization = new CsvSerialization()
                        {
                            Properties = new CsvSerializationProperties()
                            {
                                FieldDelimiter = "|",
                                Encoding = "UTF8"
                            }
                        },
                        DataSource = new IoTHubStreamInputDataSource()
                        {
                            Properties = new IoTHubStreamInputDataSourceProperties()
                            {
                                IotHubNamespace = TestHelper.IoTHubNamespace,
                                SharedAccessPolicyName = newSharedPolicyName,
                                SharedAccessPolicyKey = TestHelper.IotHubSharedAccessPolicyKey
                            }
                        }
                    };
                    inputProperties.Etag = inputCreateOrUpdateResponse.Input.Properties.Etag;
                    InputPatchParameters inputPatchParameters = new InputPatchParameters(inputProperties);
                    InputPatchResponse inputPatchResponse = client.Inputs.Patch(resourceGroupName, resourceName, inputName, inputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, inputPatchResponse.StatusCode);
                    Assert.True(inputPatchResponse.Properties.Serialization is CsvSerialization);
                    CsvSerialization csvSerializationInResponse2 = (CsvSerialization)inputPatchResponse.Properties.Serialization;
                    Assert.Equal("|", csvSerializationInResponse2.Properties.FieldDelimiter);
                    Assert.True(inputPatchResponse.Properties is StreamInputProperties);
                    StreamInputProperties streamInputPropertiesInResponse3 = (StreamInputProperties)inputPatchResponse.Properties;
                    Assert.True(streamInputPropertiesInResponse3.DataSource is IoTHubStreamInputDataSource);
                    IoTHubStreamInputDataSource iotHubInputDataSourceInResponse3 = (IoTHubStreamInputDataSource)streamInputPropertiesInResponse3.DataSource;
                    Assert.Equal(newSharedPolicyName, iotHubInputDataSourceInResponse3.Properties.SharedAccessPolicyName);
                    Assert.NotNull(inputPatchResponse.Properties.Etag);
                    Assert.NotEqual(streamInputPropertiesInResponse.Etag, inputPatchResponse.Properties.Etag);

                    // Delete the input
                    AzureOperationResponse deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode);

                    // Check that there are 0 inputs in the job
                    jobGetParameters = new JobGetParameters("inputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Inputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_TransformationOperations_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    Job job = new Job();
                    job.Name = resourceName;
                    job.Location = serviceLocation;

                    // Construct the general properties for JobProperties
                    JobProperties jobProperties = new JobProperties();
                    jobProperties.Sku = new Sku()
                    {
                        Name = "standard"
                    };
                    jobProperties.EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Drop;
                    jobProperties.EventsOutOfOrderMaxDelayInSeconds = 0;

                    job.Properties = jobProperties;

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters();
                    jobCreateOrUpdateParameters.Job = job;

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Transformation
                    string transformationName = TestUtilities.GenerateName("transformationtest");
                    int numberOfStreamingUnits = 1;
                    Transformation transformation = new Transformation()
                    {
                        Name = transformationName,
                        Properties = new TransformationProperties()
                        {
                            Query = "Select Id, Name from inputtest",
                            StreamingUnits = numberOfStreamingUnits
                        }
                    };

                    // Add an Transformation
                    TransformationCreateOrUpdateParameters transformationCreateOrUpdateParameters = new TransformationCreateOrUpdateParameters();
                    transformationCreateOrUpdateParameters.Transformation = transformation;
                    TransformationCreateOrUpdateResponse transformationCreateOrUpdateResponse = client.Transformations.CreateOrUpdate(resourceGroupName, resourceName, transformationCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, transformationCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(numberOfStreamingUnits, transformationCreateOrUpdateResponse.Transformation.Properties.StreamingUnits);
                    Assert.NotNull(transformationCreateOrUpdateResponse.Transformation.Properties.Etag);

                    // Update the Transformation
                    transformation.Properties.StreamingUnits = 3;
                    transformation.Properties.Etag = transformationCreateOrUpdateResponse.Transformation.Properties.Etag;
                    TransformationPatchParameters transformationPatchParameters = new TransformationPatchParameters(transformation.Properties);
                    TransformationPatchResponse transformationPatchResponse = client.Transformations.Patch(resourceGroupName, resourceName, transformationName, transformationPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, transformationPatchResponse.StatusCode);
                    Assert.Equal(3, transformationPatchResponse.Properties.StreamingUnits);
                    Assert.NotNull(transformationPatchResponse.Properties.Etag);
                    Assert.NotEqual(transformationCreateOrUpdateResponse.Transformation.Properties.Etag, transformationPatchResponse.Properties.Etag);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void WikipediaPipelineE2ETest()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            // Linked Service names are hard-coded as they are referenced in Tables. 
            // Table names are hard-coded as they are referenced in the Pipeline.
            const string LinkedServiceNameClickEvents = "LinkedService-WikipediaClickEvents";
            const string LinkedServiceNameCuratedWikiData = "LinkedService-CuratedWikiData";
            const string LinkedServiceNameAggregatedData = "LinkedService-WikiAggregatedData";
            const string LinkedServiceNameHDInsightByoc = "HDILinkedService";

            const string TableNameClickEvents = "DA_WikipediaClickEvents";
            const string TableNameCuratedWikiData = "DA_CuratedWikiData";
            const string TableNameAggregatedData = "DA_WikiAggregatedData";

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string factoryName = TestUtilities.GenerateName("DataFactory");
                string serverLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetDataFactoryManagementClient(handler);
                
                string pipelineName = TestUtilities.GenerateName("DP_Wikipedia");

                ResourceGroup resourceGroup = new ResourceGroup() { Location = serverLocation };
                resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                // create a data factory
                var df = new Microsoft.Azure.Management.DataFactories.Models.DataFactory()
                             {
                                 Name = factoryName,
                                 Location = serverLocation
                             };

                client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                {
                    DataFactory = df,
                });

                // verify data factory
                var dfResponse = client.DataFactories.Get(resourceGroupName, factoryName);
                Assert.True(dfResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(dfResponse.DataFactory.Name == df.Name);
                Assert.True(dfResponse.DataFactory.Location == df.Location);

                // create linked services
                string content = File.ReadAllText(@"Resources\LinkedService_WikipediaClickEvents.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    LinkedServiceNameClickEvents,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        LinkedServiceCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                content = File.ReadAllText(@"Resources\LinkedService_CuratedWikiData.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    LinkedServiceNameCuratedWikiData,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        LinkedServiceCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                content = File.ReadAllText(@"Resources\LinkedService_WikiAggregatedData.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    LinkedServiceNameAggregatedData,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        LinkedServiceCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                content = File.ReadAllText(@"Resources\LinkedService_HDIBYOC.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    LinkedServiceNameHDInsightByoc,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        LinkedServiceCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                // Create Datasets
                content = File.ReadAllText(@"Resources\DA_WikiAggregatedData.json");
                client.Datasets.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    TableNameAggregatedData,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        DatasetCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                content = File.ReadAllText(@"Resources\DA_CuratedWikiData.json");
                client.Datasets.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    TableNameCuratedWikiData,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        DatasetCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                content = File.ReadAllText(@"Resources\DA_WikipediaClickEvents.json");
                client.Datasets.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    TableNameClickEvents,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        DatasetCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                // create pipeline
                content = File.ReadAllText(@"Resources\DP_Wikisamplev2json.json");
                client.Pipelines.CreateOrUpdateWithRawJsonContent(
                    resourceGroupName,
                    factoryName,
                    pipelineName,
                    new Microsoft.Azure.Management.DataFactories.Core.Models.
                        PipelineCreateOrUpdateWithRawJsonContentParameters() { Content = content, });

                DateTime now = DateTime.Parse("2014-10-08T12:00:00");
                DateTime start = new DateTime(now.Year, now.Month, now.Day, now.Hour - 4, 0, 0);
                string startTime = start.ToString("yyyy-MM-ddTHH:mm:ss");
                string endTime = start.AddHours(2).ToString("yyyy-MM-ddTHH:mm:ss");

                client.Pipelines.SetActivePeriod(resourceGroupName, factoryName, pipelineName,
                    new PipelineSetActivePeriodParameters()
                    {
                        ActivePeriodStartTime = startTime,
                        ActivePeriodEndTime = endTime,
                    });

                // verify linked services
                string[] linkedServices = new string[]
                {
                    LinkedServiceNameClickEvents, LinkedServiceNameCuratedWikiData, LinkedServiceNameAggregatedData,
                    LinkedServiceNameHDInsightByoc
                };

                foreach (string s in linkedServices)
                {
                    var lsResponse = client.LinkedServices.Get(resourceGroupName, factoryName, s);
                    Assert.True(lsResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(lsResponse.LinkedService.Name == s);
                }

                // verify tables
                string[] tableNames = new string[] { TableNameClickEvents, TableNameCuratedWikiData, TableNameAggregatedData };
                foreach (string tableName in tableNames)
                {
                    var tResponse = client.Datasets.Get(resourceGroupName, factoryName, tableName);
                    Assert.True(tResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(tResponse.Dataset.Name == tableName);
                }

                // verify slice
                var sliceResponse = client.DataSlices.List(resourceGroupName, factoryName, TableNameAggregatedData,
                    new DataSliceListParameters(startTime, endTime));
                Assert.True(sliceResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(sliceResponse.DataSlices.Count == 2);

                // verify list and get slice run
                foreach (var slice in sliceResponse.DataSlices)
                {
                    var listSliceRunResponse = client.DataSliceRuns.List(resourceGroupName, factoryName,
                        TableNameAggregatedData, new DataSliceRunListParameters(slice.Start.ConvertToISO8601DateTimeString()));
                    Assert.True(listSliceRunResponse.StatusCode == HttpStatusCode.OK);

                    foreach (var dataSliceRun in listSliceRunResponse.DataSliceRuns)
                    {
                        var getSliceRunResponse = client.DataSliceRuns.Get(resourceGroupName, factoryName,
                            dataSliceRun.Id);

                        Assert.True(getSliceRunResponse.StatusCode == HttpStatusCode.OK);
                    }
                }
            }
        }
        public void Test_FunctionOperations_Full_Scalar_AzureMLWebService()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Retrieve default definition of the function
                    string functionName = TestUtilities.GenerateName("functiontest");
                    var retrieveDefaultDefinitionParameters = new AzureMachineLearningWebServiceFunctionRetrieveDefaultDefinitionParameters
                        ()
                    {
                        BindingRetrievalProperties = new AzureMachineLearningWebServiceFunctionBindingRetrievalProperties()
                        {
                            ExecuteEndpoint = TestHelper.ExecuteEndpoint,
                            UdfType = "Scalar"
                        }
                    };
                    FunctionRetrieveDefaultDefinitionResponse retrieveDefaultDefinitionResponse =
                        client.Functions.RetrieveDefaultDefinition(resourceGroupName, resourceName, functionName,
                            retrieveDefaultDefinitionParameters);
                    Assert.Equal(functionName, retrieveDefaultDefinitionResponse.Function.Name);

                    // Add the function
                    ScalarFunctionProperties scalarFunctionProperties =
                        (ScalarFunctionProperties) retrieveDefaultDefinitionResponse.Function.Properties;
                    AzureMachineLearningWebServiceFunctionBinding azureMachineLearningWebServiceFunctionBinding =
                        (AzureMachineLearningWebServiceFunctionBinding) scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint);
                    Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize);
                    Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey);
                    azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey = TestHelper.ApiKey;
                    FunctionCreateOrUpdateParameters functionCreateOrUpdateParameters =
                        new FunctionCreateOrUpdateParameters {Function = retrieveDefaultDefinitionResponse.Function};
                    FunctionCreateOrUpdateResponse functionCreateOrUpdateResponse = client.Functions.CreateOrUpdate(resourceGroupName, resourceName, functionCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, functionCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(functionName, functionCreateOrUpdateResponse.Function.Name);
                    scalarFunctionProperties =
                        (ScalarFunctionProperties)functionCreateOrUpdateResponse.Function.Properties;
                    azureMachineLearningWebServiceFunctionBinding =
                        (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint);
                    Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize);
                    Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey);
                    Assert.NotNull(functionCreateOrUpdateResponse.Function.Properties.Etag);

                    // Get the function
                    FunctionGetResponse functionGetResponse = client.Functions.Get(resourceGroupName, resourceName, functionName);
                    Assert.Equal(HttpStatusCode.OK, functionGetResponse.StatusCode);
                    Assert.Equal(functionName, functionGetResponse.Function.Name);
                    scalarFunctionProperties =
                        (ScalarFunctionProperties)functionGetResponse.Function.Properties;
                    azureMachineLearningWebServiceFunctionBinding =
                        (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint);
                    Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize);
                    Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey);
                    Assert.Equal(functionCreateOrUpdateResponse.Function.Properties.Etag, functionGetResponse.Function.Properties.Etag);

                    // List functions
                    FunctionListResponse functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName);
                    Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode);
                    Assert.Equal(1, functionListResponse.Value.Count);

                    // Check that there is 1 function in the job
                    jobGetParameters = new JobGetParameters("functions");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(1, jobGetResponse.Job.Properties.Functions.Count);

                    // Test function connectivity
                    ResourceTestConnectionResponse response = client.Functions.TestConnection(resourceGroupName, resourceName, functionName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(ResourceTestStatus.TestSucceeded, response.ResourceTestStatus);

                    // Update the function
                    ScalarFunctionProperties scalarFunctionPropertiesForPatch = new ScalarFunctionProperties()
                    {
                        Properties = new ScalarFunctionConfiguration()
                        {
                            Binding = new AzureMachineLearningWebServiceFunctionBinding()
                            {
                                Properties = new AzureMachineLearningWebServiceFunctionBindingProperties()
                                {
                                    BatchSize = 256
                                }
                            }
                        }
                    };
                    FunctionPatchParameters functionPatchParameters = new FunctionPatchParameters(scalarFunctionPropertiesForPatch);
                    FunctionPatchResponse functionPatchResponse = client.Functions.Patch(resourceGroupName, resourceName, functionName, functionPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, functionPatchResponse.StatusCode);
                    scalarFunctionProperties =
                        (ScalarFunctionProperties)functionPatchResponse.Properties;
                    azureMachineLearningWebServiceFunctionBinding =
                        (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint);
                    Assert.Equal(256, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize);
                    Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey);
                    Assert.NotNull(functionPatchResponse.Properties.Etag);
                    Assert.NotEqual(functionCreateOrUpdateResponse.Function.Properties.Etag, functionPatchResponse.Properties.Etag);

                    // Add second function
                    string functionName2 = TestUtilities.GenerateName("functiontest");
                    Function function2 = new Function(functionName2)
                    {
                        Properties = retrieveDefaultDefinitionResponse.Function.Properties
                    };
                    functionCreateOrUpdateParameters.Function = function2;
                    functionCreateOrUpdateResponse = client.Functions.CreateOrUpdate(resourceGroupName, resourceName, functionCreateOrUpdateParameters);

                    // List functions
                    functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName);
                    Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode);
                    Assert.Equal(2, functionListResponse.Value.Count);

                    // Check that there are 2 functions in the job
                    jobGetParameters = new JobGetParameters("functions");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(2, jobGetResponse.Job.Properties.Functions.Count);

                    // Delete the functions
                    AzureOperationResponse deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName, resourceName, functionName);
                    Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode);

                    deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName, resourceName, functionName2);
                    Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode);

                    // Check that there are 0 functions in the job
                    jobGetParameters = new JobGetParameters("functions");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Functions.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_FunctionOperations_RetrieveDefaultDefinitionWithRawJsonContent()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Retrieve default definition of the function
                    string functionName = TestUtilities.GenerateName("functiontest");
                    string content = File.ReadAllText(@"Resources\RetrieveDefaultFunctionDefinitionRequest.json");
                    var retrieveDefaultDefinitionParameters = new FunctionRetrieveDefaultDefinitionWithRawJsonContentParameters
                        ()
                    {
                        Content = content
                    };
                    FunctionRetrieveDefaultDefinitionResponse retrieveDefaultDefinitionResponse =
                        client.Functions.RetrieveDefaultDefinitionWithRawJsonContent(resourceGroupName, resourceName, functionName,
                            retrieveDefaultDefinitionParameters);
                    Assert.Equal(functionName, retrieveDefaultDefinitionResponse.Function.Name);
                    ScalarFunctionProperties scalarFunctionProperties =
                        (ScalarFunctionProperties)retrieveDefaultDefinitionResponse.Function.Properties;
                    AzureMachineLearningWebServiceFunctionBinding azureMachineLearningWebServiceFunctionBinding =
                        (AzureMachineLearningWebServiceFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(TestHelper.ExecuteEndpoint, azureMachineLearningWebServiceFunctionBinding.Properties.Endpoint);
                    Assert.Equal(1000, azureMachineLearningWebServiceFunctionBinding.Properties.BatchSize);
                    Assert.Null(azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey);
                    azureMachineLearningWebServiceFunctionBinding.Properties.ApiKey = TestHelper.ApiKey;
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void SqlCopyPipelineTest()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string factoryName = TestUtilities.GenerateName("DataFactory");
                string serverLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetDataPipelineManagementClient(handler);

                string pipelineName = "DataPipeline-Sql2SqlTest";
                string linkedServiceName = "LinkedService-AzureSql";
                string sourceTableName = "Table-Sql2Sql-Source";
                string sinkTableName = "Table-Sql2Sql-Sink";

                ResourceGroup resourceGroup = new ResourceGroup() { Location = serverLocation };
                resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                // create a data factory
                DataFactory df = new DataFactory() { Name = factoryName, Location = serverLocation };
                client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                {
                    DataFactory = df,
                });

                // verify data factory
                var dfResponse = client.DataFactories.Get(resourceGroupName, factoryName);
                Assert.True(dfResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(dfResponse.DataFactory.Name == df.Name);
                Assert.True(dfResponse.DataFactory.Location == df.Location);
                
                // create linked service
                string content = File.ReadAllText(@"Resources\SqlStoredProcedureParameters\LinkedService_SqlAzure.json");
                client.LinkedServices.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, linkedServiceName, new LinkedServiceCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                // Create Tables
                content = File.ReadAllText(@"Resources\SqlStoredProcedureParameters\Table_SourceTable.json");
                client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, sourceTableName, new TableCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                content = File.ReadAllText(@"Resources\SqlStoredProcedureParameters\Table_SinkTable.json");
                client.Tables.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, sinkTableName, new TableCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                // create pipeline
                content = File.ReadAllText(@"Resources\SqlStoredProcedureParameters\Pipeline_Copy.json");
                client.Pipelines.CreateOrUpdateWithRawJsonContent(resourceGroupName, factoryName, pipelineName, new PipelineCreateOrUpdateWithRawJsonContentParameters()
                {
                    Content = content,
                });

                string startTime = "2015-02-18T12:00:00Z";
                string endTime = "2015-02-18T14:00:00Z";

                client.Pipelines.SetActivePeriod(resourceGroupName, factoryName, pipelineName, new PipelineSetActivePeriodParameters()
                {
                    ActivePeriodStartTime = startTime,
                    ActivePeriodEndTime = endTime,
                });

                // verify linked services
                string[] linkedServices = new string[] { linkedServiceName };
                foreach (string s in linkedServices)
                {
                    var lsResponse = client.LinkedServices.Get(resourceGroupName, factoryName, s);
                    Assert.True(lsResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(lsResponse.LinkedService.Name == s);
                }

                // verify tables
                string[] tableNames = new string[] { sourceTableName, sinkTableName };
                foreach (string tableName in tableNames)
                {
                    var tResponse = client.Tables.Get(resourceGroupName, factoryName, tableName);
                    Assert.True(tResponse.StatusCode == HttpStatusCode.OK);
                    Assert.True(tResponse.Table.Name == tableName);
                }

                // verify slice
                var sliceResponse = client.DataSlices.List(resourceGroupName, factoryName, sinkTableName, startTime, endTime);
                Assert.True(sliceResponse.StatusCode == HttpStatusCode.OK);
                Assert.True(sliceResponse.DataSlices.Count == 2);
            }
        }
        public void Test_OutputOperations_EventHub()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    string outputName = TestUtilities.GenerateName("outputtest");
                    string partitionKey = "partitionKey";
                    EventHubOutputDataSource eventHubOutputDataSource = new EventHubOutputDataSource()
                    {
                        Properties = new EventHubOutputDataSourceProperties()
                        {
                            ServiceBusNamespace = TestHelper.ServiceBusNamespace,
                            EventHubName = TestHelper.EventHubName,
                            SharedAccessPolicyName = TestHelper.SharedAccessPolicyName,
                            SharedAccessPolicyKey = TestHelper.SharedAccessPolicyKey,
                            PartitionKey = partitionKey
                        }
                    };
                    JsonSerialization jsonSerialization = new JsonSerialization()
                    {
                        Properties = new JsonSerializationProperties()
                        {
                            Encoding = "UTF8",
                            Format = Format.LineSeparated
                        }
                    };
                    outputProperties.DataSource = eventHubOutputDataSource;
                    outputProperties.Serialization = jsonSerialization;
                    Output output1 = new Output(outputName)
                    {
                        Properties = outputProperties
                    };

                    // Add an output
                    OutputCreateOrUpdateParameters outputCreateOrUpdateParameters = new OutputCreateOrUpdateParameters();
                    outputCreateOrUpdateParameters.Output = output1;
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(outputName, outputCreateOrUpdateResponse.Output.Name);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.Serialization is JsonSerialization);
                    JsonSerialization jsonSerializationInResponse1 = (JsonSerialization)outputCreateOrUpdateResponse.Output.Properties.Serialization;
                    Assert.Equal(Format.LineSeparated, jsonSerializationInResponse1.Properties.Format);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.DataSource is EventHubOutputDataSource);
                    EventHubOutputDataSource eventHubOutputDataSourceInResponse1 = (EventHubOutputDataSource)outputCreateOrUpdateResponse.Output.Properties.DataSource;
                    Assert.Equal(partitionKey, eventHubOutputDataSourceInResponse1.Properties.PartitionKey);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Get the output
                    OutputGetResponse outputGetResponse = client.Outputs.Get(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, outputGetResponse.StatusCode);
                    Assert.Equal(outputName, outputGetResponse.Output.Name);
                    Assert.True(outputGetResponse.Output.Properties.Serialization is JsonSerialization);
                    JsonSerialization jsonSerializationInResponse2 = (JsonSerialization)outputGetResponse.Output.Properties.Serialization;
                    Assert.Equal(Format.LineSeparated, jsonSerializationInResponse2.Properties.Format);
                    Assert.True(outputGetResponse.Output.Properties.DataSource is EventHubOutputDataSource);
                    EventHubOutputDataSource eventHubOutputDataSourceInResponse2 = (EventHubOutputDataSource)outputGetResponse.Output.Properties.DataSource;
                    Assert.Equal(partitionKey, eventHubOutputDataSourceInResponse2.Properties.PartitionKey);

                    // Test output connectivity
                    ResourceTestConnectionResponse response = client.Outputs.TestConnection(resourceGroupName, resourceName, outputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(ResourceTestStatus.TestSucceeded, response.ResourceTestStatus);

                    // Update the output
                    jsonSerialization = new JsonSerialization()
                    {
                        Properties = new JsonSerializationProperties()
                        {
                            Encoding = "UTF8",
                            Format = Format.Array
                        }
                    };
                    string newPartitionKey = TestUtilities.GenerateName("NewPartitionKey");
                    eventHubOutputDataSource.Properties.PartitionKey = newPartitionKey;
                    outputProperties.DataSource = eventHubOutputDataSource;
                    outputProperties.Serialization = jsonSerialization;
                    outputProperties.Etag = outputCreateOrUpdateResponse.Output.Properties.Etag;
                    OutputPatchParameters outputPatchParameters = new OutputPatchParameters(outputProperties);
                    OutputPatchResponse outputPatchResponse = client.Outputs.Patch(resourceGroupName, resourceName, outputName, outputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, outputPatchResponse.StatusCode);
                    Assert.True(outputPatchResponse.Properties.Serialization is JsonSerialization);
                    JsonSerialization jsonSerializationInResponse3 = (JsonSerialization)outputPatchResponse.Properties.Serialization;
                    // TODO : Re-enable check once service bug is fixed
                    //Assert.Equal(Format.Array, jsonSerializationInResponse3.Properties.Format);
                    Assert.True(outputPatchResponse.Properties.DataSource is EventHubOutputDataSource);
                    EventHubOutputDataSource eventHubOutputDataSourceInResponse3 = (EventHubOutputDataSource)outputPatchResponse.Properties.DataSource;
                    Assert.Equal(newPartitionKey, eventHubOutputDataSourceInResponse3.Properties.PartitionKey);
                    Assert.NotNull(outputPatchResponse.Properties.Etag);
                    Assert.NotEqual(outputCreateOrUpdateResponse.Output.Properties.Etag, outputPatchResponse.Properties.Etag);

                    // Delete the output
                    AzureOperationResponse deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    // Check that there are 0 outputs in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Outputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_FunctionOperations_Scalar_JavaScript()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse =
                        client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Retrieve default definition of the function
                    string functionName = TestUtilities.GenerateName("functiontest");
                    string javaScriptFunctionCode = @"function (x, y) { return x + y; }";
                    var retrieveDefaultDefinitionParameters = new JavaScriptFunctionRetrieveDefaultDefinitionParameters
                        ()
                    {
                        BindingRetrievalProperties = new JavaScriptFunctionBindingRetrievalProperties()
                        {
                            Script = javaScriptFunctionCode,
                            UdfType = "Scalar"
                        }
                    };

                    CloudException cloudException =
                        Assert.Throws<CloudException>(
                            () =>
                                client.Functions.RetrieveDefaultDefinition(resourceGroupName, resourceName, functionName,
                                    retrieveDefaultDefinitionParameters));
                    Assert.Equal(HttpStatusCode.InternalServerError, cloudException.Response.StatusCode);
                    Assert.Contains("not supported", cloudException.Error.Message, StringComparison.InvariantCulture);

                    // Add the function
                    Function javaScriptFunction = new Function(functionName)
                    {
                        Properties = new ScalarFunctionProperties()
                        {
                            Properties = new ScalarFunctionConfiguration()
                            {
                                Inputs = new List<FunctionInput>(){ new FunctionInput() { DataType = "Any"} },
                                Output = new FunctionOutput()
                                {
                                    DataType = "Any"
                                },
                                Binding = new JavaScriptFunctionBinding()
                                {
                                    Properties = new JavaScriptFunctionBindingProperties()
                                    {
                                        Script = javaScriptFunctionCode
                                    }
                                }
                            }
                        }
                    };
                    FunctionCreateOrUpdateParameters functionCreateOrUpdateParameters =
                        new FunctionCreateOrUpdateParameters { Function = javaScriptFunction };
                    FunctionCreateOrUpdateResponse functionCreateOrUpdateResponse =
                        client.Functions.CreateOrUpdate(resourceGroupName, resourceName,
                            functionCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, functionCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(functionName, functionCreateOrUpdateResponse.Function.Name);
                    var scalarFunctionProperties =
                        (ScalarFunctionProperties)functionCreateOrUpdateResponse.Function.Properties;
                    var javaScriptFunctionBinding =
                        (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script);
                    Assert.NotNull(functionCreateOrUpdateResponse.Function.Properties.Etag);

                    // Get the function
                    FunctionGetResponse functionGetResponse = client.Functions.Get(resourceGroupName, resourceName, functionName);
                    Assert.Equal(HttpStatusCode.OK, functionGetResponse.StatusCode);
                    Assert.Equal(functionName, functionGetResponse.Function.Name);
                    scalarFunctionProperties =
                        (ScalarFunctionProperties)functionGetResponse.Function.Properties;
                    javaScriptFunctionBinding =
                        (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script);
                    Assert.Equal(functionCreateOrUpdateResponse.Function.Properties.Etag, functionGetResponse.Function.Properties.Etag);

                    // List functions
                    FunctionListResponse functionListResponse = client.Functions.ListFunctionsInJob(resourceGroupName, resourceName);
                    Assert.Equal(HttpStatusCode.OK, functionListResponse.StatusCode);
                    Assert.Equal(1, functionListResponse.Value.Count);

                    // Check that there is 1 function in the job
                    jobGetParameters = new JobGetParameters("functions");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(1, jobGetResponse.Job.Properties.Functions.Count);

                    // Test function connectivity
                    ResourceTestConnectionResponse response = client.Functions.TestConnection(resourceGroupName,
                        resourceName, functionName);
                    Assert.Equal(OperationStatus.Failed, response.Status);
                    Assert.Equal(ResourceTestStatus.TestFailed, response.ResourceTestStatus);
                    Assert.NotNull(response.Error);
                    Assert.Contains("not supported", response.Error.Message, StringComparison.InvariantCulture);

                    // Update the function
                    string newJavaScriptFunctionCode = @"function (x, y) { return x * y; }";
                    ScalarFunctionProperties scalarFunctionPropertiesForPatch = new ScalarFunctionProperties()
                    {
                        Properties = new ScalarFunctionConfiguration()
                        {
                            Binding = new JavaScriptFunctionBinding()
                            {
                                Properties = new JavaScriptFunctionBindingProperties()
                                {
                                    Script = newJavaScriptFunctionCode
                                }
                            }
                        }
                    };
                    FunctionPatchParameters functionPatchParameters = new FunctionPatchParameters(scalarFunctionPropertiesForPatch);
                    FunctionPatchResponse functionPatchResponse = client.Functions.Patch(resourceGroupName, resourceName,
                        functionName, functionPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, functionPatchResponse.StatusCode);
                    scalarFunctionProperties =
                        (ScalarFunctionProperties)functionPatchResponse.Properties;
                    javaScriptFunctionBinding =
                        (JavaScriptFunctionBinding)scalarFunctionProperties.Properties.Binding;
                    Assert.Equal(newJavaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script);
                    Assert.NotEqual(javaScriptFunctionCode, javaScriptFunctionBinding.Properties.Script);
                    Assert.NotNull(functionPatchResponse.Properties.Etag);
                    Assert.NotEqual(functionCreateOrUpdateResponse.Function.Properties.Etag, functionPatchResponse.Properties.Etag);

                    // Delete the functions
                    AzureOperationResponse deleteFunctionOperationResponse = client.Functions.Delete(resourceGroupName,
                        resourceName, functionName);
                    Assert.Equal(HttpStatusCode.OK, deleteFunctionOperationResponse.StatusCode);

                    // Check that there are 0 functions in the job
                    jobGetParameters = new JobGetParameters("functions");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Functions.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_InputOperations_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);
                    
                    Job job = new Job();
                    job.Name = resourceName;
                    job.Location = serviceLocation;

                    // Construct the general properties for JobProperties
                    JobProperties jobProperties = new JobProperties();
                    jobProperties.Sku = new Sku()
                    {
                        Name = "standard"
                    };
                    jobProperties.EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Drop;
                    jobProperties.EventsOutOfOrderMaxDelayInSeconds = 0;

                    job.Properties = jobProperties;

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters();
                    jobCreateOrUpdateParameters.Job = job;

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Input
                    StorageAccount storageAccount = new StorageAccount();
                    storageAccount.AccountName = TestHelper.AccountName;
                    storageAccount.AccountKey = TestHelper.AccountKey;
                    InputProperties inputProperties = new StreamInputProperties()
                    {
                        Serialization = new CsvSerialization()
                        {
                            Properties = new CsvSerializationProperties()
                            {
                                FieldDelimiter = ",",
                                Encoding = "UTF8"
                            }
                        },
                        DataSource = new BlobStreamInputDataSource()
                        {
                            Properties = new BlobStreamInputDataSourceProperties()
                            {
                                StorageAccounts = new[] { storageAccount },
                                Container = "state",
                                PathPattern = ""
                            }
                        }
                    };
                    string inputName = TestUtilities.GenerateName("inputtest");
                    Input input1 = new Input(inputName)
                    {
                        Properties = inputProperties
                    };

                    // Add an input
                    InputCreateOrUpdateParameters inputCreateOrUpdateParameters = new InputCreateOrUpdateParameters();
                    inputCreateOrUpdateParameters.Input = input1;
                    InputCreateOrUpdateResponse inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(inputName, inputCreateOrUpdateResponse.Input.Name);
                    Assert.Equal("Stream", inputCreateOrUpdateResponse.Input.Properties.Type);
                    Assert.True(inputCreateOrUpdateResponse.Input.Properties.Serialization is CsvSerialization);
                    CsvSerialization csvSerializationInResponse1 = (CsvSerialization)inputCreateOrUpdateResponse.Input.Properties.Serialization;
                    Assert.Equal(",", csvSerializationInResponse1.Properties.FieldDelimiter);
                    Assert.NotNull(inputCreateOrUpdateResponse.Input.Properties.Etag);

                    // Get the input
                    InputGetResponse inputGetResponse = client.Inputs.Get(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, inputGetResponse.StatusCode);
                    Assert.Equal(inputName, inputGetResponse.Input.Name);
                    Assert.True(inputGetResponse.Input.Properties.Serialization is CsvSerialization);
                    CsvSerialization csvSerializationInResponse2 = (CsvSerialization)inputGetResponse.Input.Properties.Serialization;
                    Assert.Equal(",", csvSerializationInResponse2.Properties.FieldDelimiter);
                    Assert.Equal(inputCreateOrUpdateResponse.Input.Properties.Etag, inputGetResponse.Input.Properties.Etag);

                    // List inputs
                    InputListResponse inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName, new InputListParameters());
                    Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode);
                    Assert.Equal(1, inputListResponse.Value.Count);

                    // Check that there is 1 input in the job
                    jobGetParameters = new JobGetParameters("inputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(1, jobGetResponse.Job.Properties.Inputs.Count);

                    // Test input connectivity
                    DataSourceTestConnectionResponse response = client.Inputs.TestConnection(resourceGroupName, resourceName, inputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the input
                    Serialization csvSerialization = new CsvSerialization()
                    {
                        Properties = new CsvSerializationProperties()
                        {
                            FieldDelimiter = "|",
                            Encoding = "UTF8"
                        }
                    };
                    inputProperties.Serialization = csvSerialization;
                    inputProperties.Etag = inputCreateOrUpdateResponse.Input.Properties.Etag;
                    InputPatchParameters inputPatchParameters = new InputPatchParameters(inputProperties);
                    InputPatchResponse inputPatchResponse = client.Inputs.Patch(resourceGroupName, resourceName, inputName, inputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, inputPatchResponse.StatusCode);
                    Assert.True(inputPatchResponse.Properties.Serialization is CsvSerialization);
                    CsvSerialization csvSerializationInResponse3 = (CsvSerialization)inputPatchResponse.Properties.Serialization;
                    Assert.Equal("|", csvSerializationInResponse3.Properties.FieldDelimiter);
                    Assert.NotNull(inputPatchResponse.Properties.Etag);
                    Assert.NotEqual(inputCreateOrUpdateResponse.Input.Properties.Etag, inputPatchResponse.Properties.Etag);
                    
                    // Add second input
                    string inputName2 = TestUtilities.GenerateName("inputtest");
                    Input input2 = new Input(inputName2)
                    {
                        Properties = inputProperties
                    };
                    inputCreateOrUpdateParameters.Input = input2;
                    inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters);

                    // List inputs
                    inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName, new InputListParameters());
                    Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode);
                    Assert.Equal(2, inputListResponse.Value.Count);

                    // Check that there are 2 inputs in the job
                    jobGetParameters = new JobGetParameters("inputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(2, jobGetResponse.Job.Properties.Inputs.Count);

                    // Delete the inputs
                    AzureOperationResponse deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode);

                    deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName2);
                    Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode);

                    // Check that there are 0 inputs in the job
                    jobGetParameters = new JobGetParameters("inputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Inputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_OutputOperations_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    string outputName = TestUtilities.GenerateName("outputtest");
                    string tableName = "StateInfo";
                    SqlAzureOutputDataSource sqlAzureOutputDataSource = new SqlAzureOutputDataSource()
                    {
                        Properties = new SqlAzureOutputDataSourceProperties()
                        {
                            Server = TestHelper.Server,
                            Database = TestHelper.Database,
                            User = TestHelper.User,
                            Password = TestHelper.Password,
                            Table = tableName
                        }
                    };
                    outputProperties.DataSource = sqlAzureOutputDataSource;
                    Output output1 = new Output(outputName)
                    {
                        Properties = outputProperties
                    };

                    // Add an output
                    OutputCreateOrUpdateParameters outputCreateOrUpdateParameters = new OutputCreateOrUpdateParameters();
                    outputCreateOrUpdateParameters.Output = output1;
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(outputName, outputCreateOrUpdateResponse.Output.Name);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.DataSource is SqlAzureOutputDataSource);
                    SqlAzureOutputDataSource sqlAzureOutputDataSourceInResponse1 = (SqlAzureOutputDataSource)outputCreateOrUpdateResponse.Output.Properties.DataSource;
                    Assert.Equal(tableName, sqlAzureOutputDataSourceInResponse1.Properties.Table);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Get the output
                    OutputGetResponse outputGetResponse = client.Outputs.Get(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, outputGetResponse.StatusCode);
                    Assert.Equal(outputName, outputGetResponse.Output.Name);
                    Assert.True(outputGetResponse.Output.Properties.DataSource is SqlAzureOutputDataSource);
                    SqlAzureOutputDataSource sqlAzureOutputDataSourceInResponse2 = (SqlAzureOutputDataSource)outputGetResponse.Output.Properties.DataSource;
                    Assert.Equal(tableName, sqlAzureOutputDataSourceInResponse2.Properties.Table);

                    // List outputs
                    OutputListResponse outputListResponse = client.Outputs.ListOutputInJob(resourceGroupName, resourceName, new OutputListParameters());
                    Assert.Equal(HttpStatusCode.OK, outputListResponse.StatusCode);
                    Assert.Equal(1, outputListResponse.Value.Count);

                    // Check that there is 1 output in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(1, jobGetResponse.Job.Properties.Outputs.Count);

                    // Test output connectivity
                    DataSourceTestConnectionResponse response = client.Outputs.TestConnection(resourceGroupName, resourceName, outputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the output
                    string newTableName = TestUtilities.GenerateName("NewTableName");
                    sqlAzureOutputDataSource.Properties.Table = newTableName;
                    outputProperties.DataSource = sqlAzureOutputDataSource;
                    outputProperties.Etag = outputCreateOrUpdateResponse.Output.Properties.Etag;
                    OutputPatchParameters outputPatchParameters = new OutputPatchParameters(outputProperties);
                    OutputPatchResponse outputPatchResponse = client.Outputs.Patch(resourceGroupName, resourceName, outputName, outputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, outputPatchResponse.StatusCode);
                    Assert.True(outputPatchResponse.Properties.DataSource is SqlAzureOutputDataSource);
                    SqlAzureOutputDataSource sqlAzureOutputDataSourceInResponse3 = (SqlAzureOutputDataSource)outputPatchResponse.Properties.DataSource;
                    Assert.Equal(newTableName, sqlAzureOutputDataSourceInResponse3.Properties.Table);
                    Assert.NotNull(outputPatchResponse.Properties.Etag);
                    Assert.NotEqual(outputCreateOrUpdateResponse.Output.Properties.Etag, outputPatchResponse.Properties.Etag);

                    // Add second output
                    string outputName2 = TestUtilities.GenerateName("outputtest");
                    Output output2 = new Output(outputName2)
                    {
                        Properties = outputProperties
                    };
                    outputCreateOrUpdateParameters.Output = output2;
                    outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);

                    // List outputs
                    outputListResponse = client.Outputs.ListOutputInJob(resourceGroupName, resourceName, new OutputListParameters());
                    Assert.Equal(HttpStatusCode.OK, outputListResponse.StatusCode);
                    Assert.Equal(2, outputListResponse.Value.Count);

                    // Check that there are 2 outputs in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(2, jobGetResponse.Job.Properties.Outputs.Count);

                    // Delete the outputs
                    AzureOperationResponse deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName2);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    // Check that there are 0 outputs in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Outputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_InputOperations_ReferenceBlob()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Input
                    StorageAccount storageAccount = new StorageAccount();
                    storageAccount.AccountName = TestHelper.AccountName;
                    storageAccount.AccountKey = TestHelper.AccountKey;
                    InputProperties inputProperties = new ReferenceInputProperties()
                    {
                        Serialization = new CsvSerialization()
                        {
                            Properties = new CsvSerializationProperties()
                            {
                                FieldDelimiter = ",",
                                Encoding = "UTF8"
                            }
                        },
                        DataSource = new BlobReferenceInputDataSource()
                        {
                            Properties = new BlobReferenceInputDataSourceProperties()
                            {
                                StorageAccounts = new[] { storageAccount },
                                Container = "state",
                                PathPattern = "{date}",
                                DateFormat = "yyyy/MM/dd"
                            }
                        }
                    };
                    string inputName = TestUtilities.GenerateName("inputtest");
                    Input input1 = new Input(inputName)
                    {
                        Properties = inputProperties
                    };

                    // Add an input
                    InputCreateOrUpdateParameters inputCreateOrUpdateParameters = new InputCreateOrUpdateParameters();
                    inputCreateOrUpdateParameters.Input = input1;
                    InputCreateOrUpdateResponse inputCreateOrUpdateResponse = client.Inputs.CreateOrUpdate(resourceGroupName, resourceName, inputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, inputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(inputName, inputCreateOrUpdateResponse.Input.Name);
                    Assert.Equal("Reference", inputCreateOrUpdateResponse.Input.Properties.Type);
                    Assert.True(inputCreateOrUpdateResponse.Input.Properties is ReferenceInputProperties);
                    ReferenceInputProperties referenceInputPropertiesInResponse1 = (ReferenceInputProperties) inputCreateOrUpdateResponse.Input.Properties;
                    Assert.True(referenceInputPropertiesInResponse1.DataSource is BlobReferenceInputDataSource);
                    BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse1 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse1.DataSource;
                    Assert.Equal("{date}", blobReferenceInputDataSourceInResponse1.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse1.Properties.DateFormat);
                    Assert.NotNull(inputCreateOrUpdateResponse.Input.Properties.Etag);

                    // Get the input
                    InputGetResponse inputGetResponse = client.Inputs.Get(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, inputGetResponse.StatusCode);
                    Assert.Equal(inputName, inputGetResponse.Input.Name);
                    Assert.True(inputGetResponse.Input.Properties is ReferenceInputProperties);
                    ReferenceInputProperties referenceInputPropertiesInResponse2 = (ReferenceInputProperties)inputGetResponse.Input.Properties;
                    Assert.True(referenceInputPropertiesInResponse2.DataSource is BlobReferenceInputDataSource);
                    BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse2 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse2.DataSource;
                    Assert.Equal("{date}", blobReferenceInputDataSourceInResponse2.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse2.Properties.DateFormat);
                    Assert.Equal(inputCreateOrUpdateResponse.Input.Properties.Etag, inputGetResponse.Input.Properties.Etag);

                    // List inputs
                    InputListResponse inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName, new InputListParameters());
                    Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode);
                    Assert.Equal(1, inputListResponse.Value.Count);

                    // Test input connectivity
                    DataSourceTestConnectionResponse response = client.Inputs.TestConnection(resourceGroupName, resourceName, inputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the input
                    BlobReferenceInputDataSource blobReferenceInputDataSource = new BlobReferenceInputDataSource()
                    {
                        Properties = new BlobReferenceInputDataSourceProperties()
                        {
                            StorageAccounts = new[] { storageAccount },
                            Container = "state",
                            PathPattern = "test.csv",
                            DateFormat = "yyyy/MM/dd"
                        }
                    };
                    ((ReferenceInputProperties)inputProperties).DataSource = blobReferenceInputDataSource;
                    inputProperties.Etag = inputCreateOrUpdateResponse.Input.Properties.Etag;
                    InputPatchParameters inputPatchParameters = new InputPatchParameters(inputProperties);
                    InputPatchResponse inputPatchResponse = client.Inputs.Patch(resourceGroupName, resourceName, inputName, inputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, inputPatchResponse.StatusCode);
                    Assert.True(inputPatchResponse.Properties is ReferenceInputProperties);
                    ReferenceInputProperties referenceInputPropertiesInResponse3 = (ReferenceInputProperties)inputPatchResponse.Properties;
                    Assert.True(referenceInputPropertiesInResponse3.DataSource is BlobReferenceInputDataSource);
                    BlobReferenceInputDataSource blobReferenceInputDataSourceInResponse3 = (BlobReferenceInputDataSource)referenceInputPropertiesInResponse3.DataSource;
                    Assert.Equal("test.csv", blobReferenceInputDataSourceInResponse3.Properties.PathPattern);
                    Assert.Equal("yyyy/MM/dd", blobReferenceInputDataSourceInResponse3.Properties.DateFormat);
                    Assert.NotNull(inputPatchResponse.Properties.Etag);
                    Assert.NotEqual(inputCreateOrUpdateResponse.Input.Properties.Etag, inputPatchResponse.Properties.Etag);

                    // Delete the inputs
                    AzureOperationResponse deleteInputOperationResponse = client.Inputs.Delete(resourceGroupName, resourceName, inputName);
                    Assert.Equal(HttpStatusCode.OK, deleteInputOperationResponse.StatusCode);

                    // Check that there are 0 inputs in the job
                    jobGetParameters = new JobGetParameters("inputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Inputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void Test_OutputOperations_ServiceBusTopic()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    string outputName = TestUtilities.GenerateName("outputtest");
                    string topicName = "sdktopic";
                    ServiceBusTopicOutputDataSource serviceBusTopicOutputDataSource = new ServiceBusTopicOutputDataSource()
                    {
                        Properties = new ServiceBusTopicOutputDataSourceProperties()
                        {
                            ServiceBusNamespace = TestHelper.ServiceBusNamespace,
                            TopicName = topicName,
                            SharedAccessPolicyName = TestHelper.SharedAccessPolicyName,
                            SharedAccessPolicyKey = TestHelper.SharedAccessPolicyKey,
                        }
                    };
                    JsonSerialization jsonSerialization = new JsonSerialization()
                    {
                        Properties = new JsonSerializationProperties()
                        {
                            Encoding = "UTF8",
                            Format = Format.LineSeparated
                        }
                    };
                    outputProperties.DataSource = serviceBusTopicOutputDataSource;
                    outputProperties.Serialization = jsonSerialization;
                    Output output1 = new Output(outputName)
                    {
                        Properties = outputProperties
                    };

                    // Add an output
                    OutputCreateOrUpdateParameters outputCreateOrUpdateParameters = new OutputCreateOrUpdateParameters();
                    outputCreateOrUpdateParameters.Output = output1;
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(outputName, outputCreateOrUpdateResponse.Output.Name);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.DataSource is ServiceBusTopicOutputDataSource);
                    ServiceBusTopicOutputDataSource serviceBusTopicOutputDataSourceInResponse1 = (ServiceBusTopicOutputDataSource)outputCreateOrUpdateResponse.Output.Properties.DataSource;
                    Assert.Equal(topicName, serviceBusTopicOutputDataSourceInResponse1.Properties.TopicName);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Get the output
                    OutputGetResponse outputGetResponse = client.Outputs.Get(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, outputGetResponse.StatusCode);
                    Assert.Equal(outputName, outputGetResponse.Output.Name);
                    Assert.True(outputGetResponse.Output.Properties.DataSource is ServiceBusTopicOutputDataSource);
                    ServiceBusTopicOutputDataSource serviceBusTopicOutputDataSourceInResponse2 = (ServiceBusTopicOutputDataSource)outputGetResponse.Output.Properties.DataSource;
                    Assert.Equal(topicName, serviceBusTopicOutputDataSourceInResponse2.Properties.TopicName);

                    // Test output connectivity
                    DataSourceTestConnectionResponse response = client.Outputs.TestConnection(resourceGroupName, resourceName, outputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the output
                    string newTopicName = TestUtilities.GenerateName("NewTopicName");
                    serviceBusTopicOutputDataSource.Properties.TopicName = newTopicName;
                    outputProperties.DataSource = serviceBusTopicOutputDataSource;
                    outputProperties.Etag = outputCreateOrUpdateResponse.Output.Properties.Etag;
                    OutputPatchParameters outputPatchParameters = new OutputPatchParameters(outputProperties);
                    OutputPatchResponse outputPatchResponse = client.Outputs.Patch(resourceGroupName, resourceName, outputName, outputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, outputPatchResponse.StatusCode);
                    Assert.True(outputPatchResponse.Properties.DataSource is ServiceBusTopicOutputDataSource);
                    ServiceBusTopicOutputDataSource serviceBusTopicOutputDataSourceInResponse3 = (ServiceBusTopicOutputDataSource)outputPatchResponse.Properties.DataSource;
                    Assert.Equal(newTopicName, serviceBusTopicOutputDataSourceInResponse3.Properties.TopicName);
                    Assert.NotNull(outputPatchResponse.Properties.Etag);
                    Assert.NotEqual(outputCreateOrUpdateResponse.Output.Properties.Etag, outputPatchResponse.Properties.Etag);

                    // Delete the output
                    AzureOperationResponse deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    // Check there is no output any more
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Outputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
Example #23
0
 /// <summary>
 /// Ensures that a resource group with the specified name exists. If it does not, will attempt to create one.
 /// </summary>
 /// <param name="resourceManagementClient">The resource manager client.</param>
 /// <param name="resourceGroupName">The name of the resource group.</param>
 /// <param name="resourceGroupLocation">The resource group location. Required when creating a new resource group.</param>
 private static void EnsureResourceGroupExists(ResourceManagementClient resourceManagementClient, string resourceGroupName, string resourceGroupLocation)
 {
     if (resourceManagementClient.ResourceGroups.CheckExistence(resourceGroupName) != true)
     {
         Console.WriteLine(string.Format("Creating resource group '{0}' in location '{1}'", resourceGroupName, resourceGroupLocation));
         var resourceGroup = new ResourceGroup();
         resourceGroup.Location = resourceGroupLocation;
         resourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);
     }
     else
     {
         Console.WriteLine(string.Format("Using existing resource group '{0}'", resourceGroupName));
     }
 }
        public void Test_OutputOperations_DocumentDB()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");

                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() { Location = serviceLocation };
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters =
                        new JobCreateOrUpdateParameters(TestHelper.GetDefaultJob(resourceName, serviceLocation));

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters(string.Empty);
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    string outputName = TestUtilities.GenerateName("outputtest");
                    string database = "db01";
                    DocumentDbOutputDataSource documentDbOutputDataSource = new DocumentDbOutputDataSource()
                    {
                        Properties = new DocumentDbOutputDataSourceProperties()
                        {
                            AccountId = TestHelper.DocDbAccountId,
                            AccountKey = TestHelper.DocDbAccountKey,
                            Database = database,
                            PartitionKey = "key",
                            CollectionNamePattern = "collection"
                        }
                    };
                    outputProperties.DataSource = documentDbOutputDataSource;
                    Output output1 = new Output(outputName)
                    {
                        Properties = outputProperties
                    };

                    // Add an output
                    OutputCreateOrUpdateParameters outputCreateOrUpdateParameters = new OutputCreateOrUpdateParameters();
                    outputCreateOrUpdateParameters.Output = output1;
                    OutputCreateOrUpdateResponse outputCreateOrUpdateResponse = client.Outputs.CreateOrUpdate(resourceGroupName, resourceName, outputCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, outputCreateOrUpdateResponse.StatusCode);
                    Assert.Equal(outputName, outputCreateOrUpdateResponse.Output.Name);
                    Assert.True(outputCreateOrUpdateResponse.Output.Properties.DataSource is DocumentDbOutputDataSource);
                    DocumentDbOutputDataSource documentDbOutputDataSourceInResponse1 = (DocumentDbOutputDataSource)outputCreateOrUpdateResponse.Output.Properties.DataSource;
                    Assert.Equal(database, documentDbOutputDataSourceInResponse1.Properties.Database);
                    Assert.NotNull(outputCreateOrUpdateResponse.Output.Properties.Etag);

                    // Get the output
                    OutputGetResponse outputGetResponse = client.Outputs.Get(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, outputGetResponse.StatusCode);
                    Assert.Equal(outputName, outputGetResponse.Output.Name);
                    Assert.True(outputGetResponse.Output.Properties.DataSource is DocumentDbOutputDataSource);
                    DocumentDbOutputDataSource documentDbOutputDataSourceInResponse2 = (DocumentDbOutputDataSource)outputGetResponse.Output.Properties.DataSource;
                    Assert.Equal(database, documentDbOutputDataSourceInResponse2.Properties.Database);

                    // Test output connectivity
                    DataSourceTestConnectionResponse response = client.Outputs.TestConnection(resourceGroupName, resourceName, outputName);
                    Assert.Equal(OperationStatus.Succeeded, response.Status);
                    Assert.Equal(DataSourceTestStatus.TestSucceeded, response.DataSourceTestStatus);

                    // Update the output
                    string newDatabase = TestUtilities.GenerateName("NewDatabase");
                    documentDbOutputDataSource.Properties.Database = newDatabase;
                    outputProperties.DataSource = documentDbOutputDataSource;
                    outputProperties.Etag = outputCreateOrUpdateResponse.Output.Properties.Etag;
                    OutputPatchParameters outputPatchParameters = new OutputPatchParameters(outputProperties);
                    OutputPatchResponse outputPatchResponse = client.Outputs.Patch(resourceGroupName, resourceName, outputName, outputPatchParameters);
                    Assert.Equal(HttpStatusCode.OK, outputPatchResponse.StatusCode);
                    Assert.True(outputPatchResponse.Properties.DataSource is DocumentDbOutputDataSource);
                    DocumentDbOutputDataSource documentDbOutputDataSourceInResponse3 = (DocumentDbOutputDataSource)outputPatchResponse.Properties.DataSource;
                    Assert.Equal(newDatabase, documentDbOutputDataSourceInResponse3.Properties.Database);
                    Assert.NotNull(outputPatchResponse.Properties.Etag);
                    Assert.NotEqual(outputCreateOrUpdateResponse.Output.Properties.Etag, outputPatchResponse.Properties.Etag);

                    // Delete the output
                    AzureOperationResponse deleteOutputOperationResponse = client.Outputs.Delete(resourceGroupName, resourceName, outputName);
                    Assert.Equal(HttpStatusCode.OK, deleteOutputOperationResponse.StatusCode);

                    // Check that there are 0 outputs in the job
                    jobGetParameters = new JobGetParameters("outputs");
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(0, jobGetResponse.Job.Properties.Outputs.Count);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void GetsSpecificResourceGroup()
        {
            string name = resourceGroupName;
            Resource resource1 = new Resource() { Id = "/subscriptions/abc123/resourceGroups/group1/providers/Microsoft.Test/servers/r12345sql/db/r45678db", Location = resourceGroupLocation, Name = resourceName };
            Resource resource2 = new Resource() { Id = "/subscriptions/abc123/resourceGroups/group1/providers/Microsoft.Test/servers/r12345sql/db/r45678db", Location = resourceGroupLocation, Name = resourceName + "2" };
            ResourceGroup resourceGroup = new ResourceGroup() { Name = name, Location = resourceGroupLocation, ProvisioningState = "Succeeded" };
            resourceGroupMock.Setup(f => f.GetAsync(name, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupGetResult
                {
                    ResourceGroup = resourceGroup,
                }));
            SetupListForResourceGroupAsync(name, new List<Resource>() { resource1, resource2 });

            List<PSResourceGroup> actual = resourcesClient.FilterResourceGroups(name, null, true);

            Assert.Equal(1, actual.Count);
            Assert.Equal(name, actual[0].ResourceGroupName);
            Assert.Equal(resourceGroupLocation, actual[0].Location);
            Assert.Equal(2, actual[0].Resources.Count);
            Assert.Equal("Succeeded", actual[0].ProvisioningState);
            Assert.True(!string.IsNullOrEmpty(actual[0].ResourcesTable));
        }
 /// <summary>
 /// Create a resource group.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceGroupOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group to be created or updated.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the create or update resource
 /// group service operation.
 /// </param>
 /// <returns>
 /// Resource group information.
 /// </returns>
 public static Task<ResourceGroupCreateOrUpdateResult> CreateOrUpdateAsync(this IResourceGroupOperations operations, string resourceGroupName, ResourceGroup parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, parameters, CancellationToken.None);
 }
        public void GetsResourceGroupsFilteredByTagsWithDetails()
        {
            Dictionary<string, string> tag1 = new Dictionary<string, string> { { "tag1", "val1" }, { "tag2", "val2" } };
            Dictionary<string, string> tag2 = new Dictionary<string, string> { { "tag1", "valx" } };
            Dictionary<string, string> tag3 = new Dictionary<string, string> { { "tag2", "" } };

            ResourceGroup resourceGroup1 = new ResourceGroup() { Name = resourceGroupName + 1, Location = resourceGroupLocation, Tags = tag1 };
            ResourceGroup resourceGroup2 = new ResourceGroup() { Name = resourceGroupName + 2, Location = resourceGroupLocation, Tags = tag2 };
            ResourceGroup resourceGroup3 = new ResourceGroup() { Name = resourceGroupName + 3, Location = resourceGroupLocation, Tags = tag3 };
            ResourceGroup resourceGroup4 = new ResourceGroup() { Name = resourceGroupName + 4, Location = resourceGroupLocation };
            resourceGroupMock.Setup(f => f.ListAsync(null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupListResult
                {
                    ResourceGroups = new List<ResourceGroup>() { resourceGroup1, resourceGroup2, resourceGroup3, resourceGroup4 }
                }));
            SetupListForResourceGroupAsync(resourceGroup1.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup2.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup3.Name, new List<Resource>() { new Resource() { Name = "resource" } });
            SetupListForResourceGroupAsync(resourceGroup4.Name, new List<Resource>() { new Resource() { Name = "resource" } });

            List<PSResourceGroup> groups1 = resourcesClient.FilterResourceGroups(null,
                new Hashtable(new Dictionary<string, string> { { "Name", "tag1" } }), true);

            Assert.Equal(2, groups1.Count);
            Assert.Equal(resourceGroup1.Name, groups1[0].ResourceGroupName);
            Assert.Equal(resourceGroup2.Name, groups1[1].ResourceGroupName);
            Assert.Equal(1, groups1[0].Resources.Count());
            Assert.Equal(1, groups1[1].Resources.Count());

            List<PSResourceGroup> groups2 = resourcesClient.FilterResourceGroups(null,
                new Hashtable(new Dictionary<string, string> { { "Name", "tag2" } }), true);

            Assert.Equal(2, groups2.Count);
            Assert.Equal(resourceGroup1.Name, groups2[0].ResourceGroupName);
            Assert.Equal(resourceGroup3.Name, groups2[1].ResourceGroupName);
            Assert.Equal(1, groups2[0].Resources.Count());
            Assert.Equal(1, groups2[1].Resources.Count());

            List<PSResourceGroup> groups3 = resourcesClient.FilterResourceGroups(null,
                new Hashtable(new Dictionary<string, string> { { "Name", "tag3" } }), true);

            Assert.Equal(0, groups3.Count);

            List<PSResourceGroup> groups4 = resourcesClient.FilterResourceGroups(null,
                new Hashtable(new Dictionary<string, string> { { "Name", "TAG1" }, { "Value", "val1" } }), true);

            Assert.Equal(1, groups4.Count);
            Assert.Equal(resourceGroup1.Name, groups4[0].ResourceGroupName);
            Assert.Equal(1, groups4[0].Resources.Count());
        }
 /// <summary>
 /// Resource groups can be updated through a simple PATCH operation to
 /// a group address. The format of the request is the same as that for
 /// creating a resource groups, though if a field is unspecified
 /// current value will be carried over.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceGroupOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group to be created or updated.
 /// The name is case insensitive.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the update state resource group
 /// service operation.
 /// </param>
 /// <returns>
 /// Resource group information.
 /// </returns>
 public static ResourceGroupPatchResult Patch(this IResourceGroupOperations operations, string resourceGroupName, ResourceGroup parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IResourceGroupOperations)s).PatchAsync(resourceGroupName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void Test_JobOperations_E2E()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                string resourceGroupName = TestUtilities.GenerateName("StreamAnalytics");
                string resourceName = TestUtilities.GenerateName("MyStreamingJobSubmittedBySDK");
                
                string serviceLocation = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetStreamAnalyticsManagementClient(handler);

                try
                {
                    ResourceGroup resourceGroup = new ResourceGroup() {Location = serviceLocation};
                    resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                    Job job = new Job();
                    job.Name = resourceName;
                    job.Location = serviceLocation;

                    // Construct the general properties for JobProperties
                    JobProperties jobProperties = new JobProperties();
                    jobProperties.Sku = new Sku()
                    {
                        Name = "standard"
                    };
                    jobProperties.EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Drop;
                    jobProperties.EventsOutOfOrderMaxDelayInSeconds = 0;

                    // Construct the Input
                    StorageAccount storageAccount = new StorageAccount
                    {
                        AccountName = TestHelper.AccountName,
                        AccountKey = TestHelper.AccountKey
                    };
                    InputProperties inputProperties = new StreamInputProperties()
                    {
                        Serialization = new JsonSerialization()
                        {
                            Properties = new JsonSerializationProperties()
                            {
                                Encoding = "UTF8"
                            }
                        },
                        DataSource = new BlobStreamInputDataSource()
                        {
                            Properties = new BlobStreamInputDataSourceProperties()
                            {
                                StorageAccounts = new[] { storageAccount },
                                Container = "state",
                                PathPattern = ""
                            }
                        }
                    };
                    Input input1 = new Input("inputtest")
                    {
                        Properties = inputProperties
                    };
                    jobProperties.Inputs = new[] { input1 };

                    // Construct the Output
                    OutputProperties outputProperties = new OutputProperties();
                    SqlAzureOutputDataSource sqlAzureOutputDataSource = new SqlAzureOutputDataSource()
                    {
                        Properties = new SqlAzureOutputDataSourceProperties()
                        {
                            Server = TestHelper.Server,
                            Database = TestHelper.Database,
                            User = TestHelper.User,
                            Password = TestHelper.Password,
                            Table = "StateInfo"
                        }
                    };
                    outputProperties.DataSource = sqlAzureOutputDataSource;
                    Output output1 = new Output("outputtest")
                    {
                        Properties = outputProperties
                    };
                    jobProperties.Outputs = new Output[] { output1 };

                    // Construct the transformation
                    Transformation transformation = new Transformation()
                    {
                        Name = "transformationtest",
                        Properties = new TransformationProperties()
                        {
                            Query = "Select Id, Name from inputtest",
                            StreamingUnits = 1
                        }
                    };
                    jobProperties.Transformation = transformation;

                    job.Properties = jobProperties;

                    // Construct the JobCreateProperties
                    JobCreateOrUpdateParameters jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters();
                    jobCreateOrUpdateParameters.Job = job;

                    // Create a streaming job
                    JobCreateOrUpdateResponse jobCreateOrUpdateResponse = client.StreamingJobs.CreateOrUpdate(resourceGroupName, jobCreateOrUpdateParameters);
                    Assert.Equal(HttpStatusCode.OK, jobCreateOrUpdateResponse.StatusCode);
                    Assert.NotNull(jobCreateOrUpdateResponse.Job.Properties.Etag);

                    // Get a streaming job to check
                    JobGetParameters jobGetParameters = new JobGetParameters("inputs,transformation,outputs");
                    JobGetResponse jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(serviceLocation, jobGetResponse.Job.Location);
                    Assert.Equal(resourceName, jobGetResponse.Job.Name);
                    Assert.True(jobGetResponse.Job.Properties.Inputs[0].Properties is StreamInputProperties);
                    StreamInputProperties streamInputProperties = jobGetResponse.Job.Properties.Inputs[0].Properties as StreamInputProperties;
                    Assert.Equal("Stream", jobGetResponse.Job.Properties.Inputs[0].Properties.Type);
                    Assert.Equal("Microsoft.Storage/Blob", streamInputProperties.DataSource.Type);
                    Assert.Equal("Json", streamInputProperties.Serialization.Type);
                    Assert.Equal(EventsOutOfOrderPolicy.Drop, jobGetResponse.Job.Properties.EventsOutOfOrderPolicy);
                    Assert.NotNull(jobGetResponse.Job.Properties.Etag);
                    Assert.Equal(jobCreateOrUpdateResponse.Job.Properties.Etag, jobGetResponse.Job.Properties.Etag);

                    // Patch the streaming job
                    JobPatchParameters jobPatchParameters = new JobPatchParameters()
                    {
                        JobPatchRequest = new JobPatchRequest()
                        {
                            Properties = new JobProperties()
                            {
                                EventsOutOfOrderPolicy = EventsOutOfOrderPolicy.Adjust
                            }
                        }
                    };
                    var jobPatchResponse = client.StreamingJobs.Patch(resourceGroupName, resourceName, jobPatchParameters);
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobPatchResponse.StatusCode);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(EventsOutOfOrderPolicy.Adjust, jobPatchResponse.Job.Properties.EventsOutOfOrderPolicy);
                    Assert.Equal(EventsOutOfOrderPolicy.Adjust, jobGetResponse.Job.Properties.EventsOutOfOrderPolicy);

                    JobListParameters parameters = new JobListParameters(string.Empty);
                    JobListResponse response = client.StreamingJobs.ListJobsInResourceGroup(resourceGroupName, parameters);
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                    // Start a streaming job
                    JobStartParameters jobStartParameters = new JobStartParameters()
                    {
                        OutputStartMode = OutputStartMode.LastOutputEventTime
                    };
                    CloudException cloudException = Assert.Throws<CloudException>(() => client.StreamingJobs.Start(resourceGroupName, resourceName, jobStartParameters));
                    Assert.Equal("LastOutputEventTime must be available when OutputStartMode is set to LastOutputEventTime. Please make sure at least one output event has been processed. ", cloudException.Error.Message);

                    jobStartParameters.OutputStartMode = OutputStartMode.CustomTime;
                    jobStartParameters.OutputStartTime = new DateTime(2012, 12, 12, 12, 12, 12, DateTimeKind.Utc);
                    AzureOperationResponse jobStartOperationResponse = client.StreamingJobs.Start(resourceGroupName, resourceName, jobStartParameters);
                    Assert.Equal(HttpStatusCode.OK, jobStartOperationResponse.StatusCode);

                    // Get a streaming job to check
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.True(IsRunning(jobGetResponse.Job.Properties.JobState));

                    // Check diagnostics
                    InputListResponse inputListResponse = client.Inputs.ListInputInJob(resourceGroupName, resourceName,
                        new InputListParameters("*"));
                    Assert.Equal(HttpStatusCode.OK, inputListResponse.StatusCode);
                    Assert.NotEqual(0, inputListResponse.Value.Count);
                    Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics);
                    Assert.NotEqual(0, inputListResponse.Value[0].Properties.Diagnostics.Conditions.Count);
                    Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics.Conditions[0].Code);
                    Assert.NotNull(inputListResponse.Value[0].Properties.Diagnostics.Conditions[0].Message);

                    // Stop a streaming job
                    AzureOperationResponse jobStopOperationResponse = client.StreamingJobs.Stop(resourceGroupName, resourceName);
                    Assert.Equal(HttpStatusCode.OK, jobStopOperationResponse.StatusCode);

                    // Get a streaming job to check
                    jobGetResponse = client.StreamingJobs.Get(resourceGroupName, resourceName, jobGetParameters);
                    Assert.Equal(HttpStatusCode.OK, jobGetResponse.StatusCode);
                    Assert.Equal(JobRunningState.Stopped, jobGetResponse.Job.Properties.JobState);

                    // Delete a streaming job
                    AzureOperationResponse jobDeleteOperationResponse = client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    Assert.Equal(HttpStatusCode.OK, jobDeleteOperationResponse.StatusCode);
                }
                finally
                {
                    client.StreamingJobs.Delete(resourceGroupName, resourceName);
                    resourceClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
 /// <summary>
 /// Resource groups can be updated through a simple PATCH operation to
 /// a group address. The format of the request is the same as that for
 /// creating a resource groups, though if a field is unspecified
 /// current value will be carried over.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceGroupOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group to be created or updated.
 /// The name is case insensitive.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the update state resource group
 /// service operation.
 /// </param>
 /// <returns>
 /// Resource group information.
 /// </returns>
 public static Task<ResourceGroupPatchResult> PatchAsync(this IResourceGroupOperations operations, string resourceGroupName, ResourceGroup parameters)
 {
     return operations.PatchAsync(resourceGroupName, parameters, CancellationToken.None);
 }