public void TestGetCurrentSubscriptionByName()
        {
            var globalComponents = GlobalComponents.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, null, Data.ValidPublishSettings.First());

            var importSubscriptionCommand = new ImportAzurePublishSettingsCommand();
            importSubscriptionCommand.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            var currentSubscription = importSubscriptionCommand.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionName, Data.Subscription1);
            Assert.IsTrue(currentSubscription.IsDefault);

            // Test the get for all subscription (null name)
            var getSubscriptionCommand = new GetSubscriptionCommandStub();
            getSubscriptionCommand.GetSubscriptionProcess("ByName", null, null);

            Assert.AreEqual(6, getSubscriptionCommand.Messages.Count);

            // Test the get for a specific susbcription
            getSubscriptionCommand = new GetSubscriptionCommandStub();
            getSubscriptionCommand.GetSubscriptionProcess("ByName", currentSubscription.SubscriptionName, null);

            Assert.AreEqual(1, getSubscriptionCommand.Messages.Count);
            Assert.AreEqual(currentSubscription.SubscriptionName, getSubscriptionCommand.Messages.First().SubscriptionName);
            Assert.AreEqual(currentSubscription.SubscriptionId, getSubscriptionCommand.Messages.First().SubscriptionId);

            globalComponents.DeleteGlobalComponents();
        }
        public void TestImportPublishSettingsWithMultiplePublishSettingsFilesFound()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.SubscriptionClient = CreateMockSubscriptionClient();
            string directoryName = "testdir2";
            string fileName1 = "myfile1.publishsettings";
            string fileName2 = "myfile2.publishsettings";
            string filePath1 = Path.Combine(directoryName, fileName1);
            string filePath2 = Path.Combine(directoryName, fileName2);
            Directory.CreateDirectory(directoryName);
            File.WriteAllText(filePath1, File.ReadAllText(Data.ValidPublishSettings.First()));
            File.WriteAllText(filePath2, File.ReadAllText(Data.ValidPublishSettings.First()));
            cmdlet.PublishSettingsFile = directoryName;

            cmdlet.ExecuteCmdlet();

            SubscriptionData currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionName, Data.Subscription1);
            Assert.IsTrue(currentSubscription.IsDefault);
            Assert.AreEqual<string>(filePath1, mockCommandRuntime.OutputPipeline[0].ToString());
            Assert.AreEqual<string>(string.Format(Resources.MultiplePublishSettingsFilesFoundMessage, filePath1), mockCommandRuntime.WarningStream[0]);
        }
        public void TestImportPublishSettingsWithNoPublishSettingsFilesFound()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.SubscriptionClient = CreateMockSubscriptionClient();
            string directoryName = "testdir3";
            string originalDirectory = Directory.GetCurrentDirectory();
            Directory.CreateDirectory(directoryName);
            Directory.SetCurrentDirectory(Path.GetFullPath(directoryName));

            Testing.AssertThrows<Exception>(
                () => cmdlet.ExecuteCmdlet(),
                string.Format(Resources.NoPublishSettingsFilesFoundMessage, Directory.GetCurrentDirectory()));
            Directory.SetCurrentDirectory(originalDirectory);
            Assert.AreEqual<string>(originalDirectory, Directory.GetCurrentDirectory());
        }
        public void TestImportPublishSettingsWithoutPassingDirectory()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.SubscriptionClient = CreateMockSubscriptionClient();
            string directoryName = "testdir";
            string fileName = "myfile.publishsettings";
            Directory.CreateDirectory(directoryName);
            string originalDirectory = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(Path.GetFullPath(directoryName));
            File.WriteAllText(fileName, File.ReadAllText(Data.ValidPublishSettings.First()));

            cmdlet.ExecuteCmdlet();

            SubscriptionData currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionName, Data.Subscription1);
            Assert.IsTrue(currentSubscription.IsDefault);
            Assert.AreEqual<string>(Path.GetFullPath(fileName), mockCommandRuntime.OutputPipeline[0].ToString());
            Directory.SetCurrentDirectory(originalDirectory);
            Assert.AreEqual<string>(originalDirectory, Directory.GetCurrentDirectory());
        }
        public void TestImportSubscriptionPublishSettingsSecondVersionOnlyProcess()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.SubscriptionClient = CreateMockSubscriptionClient();
            cmdlet.ImportSubscriptionFile(
                Data.ValidPublishSettings2.First(),
                null);

            var currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(Data.SampleSubscription1, currentSubscription.SubscriptionName);
            Assert.AreEqual("https://newmanagement.core.windows.net/", currentSubscription.ServiceEndpoint);
            Assert.IsNotNull(currentSubscription.Certificate);
            Assert.IsTrue(currentSubscription.IsDefault);
        }
        public void TestImportSubscriptionPublishSettingsOnlyProcess()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            var currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(Data.Subscription1, currentSubscription.SubscriptionName);
            Assert.IsTrue(currentSubscription.IsDefault);
        }
        public void TestImportSubscriptionPublishSettingsOnlyMultipleTimesProcess()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            cmdlet.SubscriptionClient = CreateMockSubscriptionClient();
            cmdlet.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            var subscriptions = cmdlet.GetSubscriptions(null);

            SubscriptionData currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(Data.Subscription1, currentSubscription.SubscriptionName);
            Assert.IsTrue(currentSubscription.IsDefault);

            SubscriptionData newCurrentSubscription = subscriptions.Values.FirstOrDefault(s => !s.SubscriptionId.Equals(currentSubscription.SubscriptionId));
            cmdlet.SetCurrentSubscription(newCurrentSubscription);

            cmdlet.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionId, newCurrentSubscription.SubscriptionId);
        }
        public void TestImportSubscriptionProcess()
        {
            MockCommandRuntime mockCommandRuntime;
            ImportAzurePublishSettingsCommand cmdlet;
            mockCommandRuntime = new MockCommandRuntime();
            cmdlet = new ImportAzurePublishSettingsCommand();
            cmdlet.CommandRuntime = mockCommandRuntime;
            var globalComponents = GlobalComponents.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, null, Data.ValidPublishSettings.First());

            cmdlet.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            var currentSubscription = cmdlet.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionName, Data.Subscription1);
            Assert.IsTrue(currentSubscription.IsDefault);

            globalComponents.DeleteGlobalComponents();
        }
        public void TestGetCurrentSubscriptionCurrent()
        {
            var globalSettingsManager = GlobalSettingsManager.CreateFromPublishSettings(GlobalPathInfo.GlobalSettingsDirectory, null, Data.ValidPublishSettings.First());

            var importSubscriptionCommand = new ImportAzurePublishSettingsCommand();
            importSubscriptionCommand.ImportSubscriptionFile(
                Data.ValidPublishSettings.First(),
                null);

            var currentSubscription = importSubscriptionCommand.GetCurrentSubscription();
            Assert.AreEqual(currentSubscription.SubscriptionName, Data.Subscription1);
            Assert.IsTrue(currentSubscription.IsDefault);

            // Test the get for the current subscription
            var getSubscriptionCommand = new GetSubscriptionCommandStub();
            getSubscriptionCommand.GetSubscriptionProcess("Current", null, null);

            Assert.AreEqual(1, getSubscriptionCommand.Messages.Count);
            Assert.AreEqual(currentSubscription.SubscriptionName, getSubscriptionCommand.Messages.First().SubscriptionName);
            Assert.AreEqual(currentSubscription.SubscriptionId, getSubscriptionCommand.Messages.First().SubscriptionId);

            globalSettingsManager.DeleteGlobalSettingsManager();
        }
        public void TestInitialize()
        {
            CmdletSubscriptionExtensions.SessionManager = new InMemorySessionManager();

            serviceName = Path.GetRandomFileName();
            GlobalPathInfo.GlobalSettingsDirectory = Data.AzureSdkAppDir;
            service = new AzureServiceWrapper(Directory.GetCurrentDirectory(), Path.GetRandomFileName(), null);
            service.CreateVirtualCloudPackage();
            packagePath = service.Paths.CloudPackage;
            configPath = service.Paths.CloudConfiguration;
            settings = ServiceSettingsTestData.Instance.Data[ServiceSettingsState.Default];
            mockCommandRuntime = new MockCommandRuntime();
            importCmdlet = new ImportAzurePublishSettingsCommand();
            importCmdlet.CommandRuntime = mockCommandRuntime;
            importCmdlet.ImportSubscriptionFile(Data.ValidPublishSettings.First(), null);
            importCmdlet.SubscriptionClient = CreateMockSubscriptionClient();
        }