Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Refactor, renaming, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger committed Mar 21, 2024
1 parent 8579f1c commit 342ca4c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.PowerShell;
using ActiveDirectoryQuerier.ViewModels;

namespace ActiveDirectoryQuerier.Tests;

public class AppConsoleTests : IDisposable
public class ConsoleViewModelTests : IDisposable
{
// TODO: Make sure this is how I should perform cleanups...
public void Dispose()
Expand All @@ -21,15 +22,15 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private static async Task<(AppConsole, PSOutput)> ExecuteCommandAsync(Command command)
private static async Task<(ConsoleViewModel, PSOutput)> ExecuteCommandAsync(Command command)
{
PSExecutor psExecutor = new();
AppConsole appConsole = new();
ConsoleViewModel consoleViewModel = new();
PSOutput result = await psExecutor.ExecuteAsync(command);

appConsole.Append(result.HadErrors ? result.StdErr : result.StdOut);
consoleViewModel.Append(result.HadErrors ? result.StdErr : result.StdOut);

return (appConsole, result);
return (consoleViewModel, result);
}

[Fact]
Expand All @@ -51,14 +52,14 @@ public async Task ClearConsole_ClearsConsole_SuccessfullyCleared()
public void Append_AppendsStringToConsole_SuccessfullyAppended()
{
// Arrange
AppConsole appConsole = new();
ConsoleViewModel consoleViewModel = new();
const string output = "Output";

// Act
appConsole.Append(output);
consoleViewModel.Append(output);

// Assert
Assert.Equal(output, appConsole.ConsoleOutput);
Assert.Equal(output, consoleViewModel.ConsoleOutput);
}

[Fact]
Expand All @@ -70,7 +71,7 @@ public async Task ExportToText_ExportToText_SuccessfullyExported()
var (appConsole, returnValues) = await ExecuteCommandAsync(command);

// Act
appConsole.ExportToText();
appConsole.ExportToTextFile();
string fileContents = await File.ReadAllTextAsync("output.txt");

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace ActiveDirectoryQuerier.ActiveDirectory;

// As a note, the fields are used instead of the property, as it would cause a Stack Overflow Exception.
// ReSharper disable once InconsistentNaming
public class ADCommandParameters
{
Expand Down Expand Up @@ -51,7 +52,7 @@ private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync)
_availableParameters.Add("No valid command provided");
return;
}

if (_availableParameters.Count == 0)
{
using var powerShell = System.Management.Automation.PowerShell.Create();
Expand Down
16 changes: 8 additions & 8 deletions ActiveDirectoryQuerier/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Menu>
</Border>
<!-- Grid for resizable content. -->
<Grid Grid.Row="1" Grid.ColumnSpan="2">
<Grid Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0">
<!-- Define Columns for resizable area. -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" MinWidth="200" />
Expand Down Expand Up @@ -79,15 +79,16 @@
</Grid.RowDefinitions>
<!-- Query Name TextBox. -->
<TextBlock Grid.Row="0" HorizontalAlignment="Left" Margin="20,10" FontWeight="Bold">Query Name</TextBlock>
<TextBox Grid.Column="0"
Grid.ColumnSpan="2"
<TextBox Grid.Column="0"
Grid.Row="0" Grid.ColumnSpan="2"
Text="{Binding QueryName}"
TextWrapping="Wrap"
VerticalAlignment="Center"
Height="50"
Margin="20,0,20,0"/>
<!-- Query Description RichTextBox -->
<TextBlock FontWeight="Bold" HorizontalAlignment="Left" Margin="20,3" Grid.Row="1">Query Description</TextBlock>
<TextBlock FontWeight="Bold" HorizontalAlignment="Left" Margin="20,3" Grid.Row="1"
Grid.Column="0">Query Description</TextBlock>
<RichTextBox Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Expand Down Expand Up @@ -121,7 +122,6 @@
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- TODO: Pieter refer to here regarding combobox setup -->
<TextBlock Grid.Row="0" Grid.Column="0" FontWeight="Bold" HorizontalAlignment="Center">Command</TextBlock>
<ComboBox ItemsSource="{Binding ADCommands}"
SelectedItem="{Binding SelectedCommandInQueryBuilder}"
Expand Down Expand Up @@ -166,7 +166,7 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- END of Dynamic ComboxBoxes for parameters and values-->
<!-- END of Dynamic ComboBoxes for parameters and values-->

<!-- START of Button ComboBox -->
<Menu Margin="0,4,0,6" Height="Auto" Grid.Row="2" Grid.Column="0"
Expand Down Expand Up @@ -198,7 +198,7 @@
<!-- ROW 0 END -->
<!-- GRID ROW 1 START -->
<!-- TextBox as Console Output -->
<TextBox Grid.Row="1" Text="{Binding ConsoleOutputInQueryBuilder.ConsoleOutput}"
<TextBox Grid.Row="1" Text="{Binding ConsoleViewModelOutputInQueryBuilder.ConsoleOutput}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="10" IsReadOnly="True" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />
Expand All @@ -220,7 +220,7 @@
SelectedItem="{Binding SelectedQueryInActiveDirectoryInfo}"
ItemsSource="{Binding AvailableQueriesInActiveDirectoryInfo.AvailableOptions.Keys }" />
</Grid>
<TextBox Grid.Row="1" Text="{Binding ConsoleOutputInActiveDirectoryInfo.ConsoleOutput}"
<TextBox Grid.Row="1" Text="{Binding ConsoleViewModelOutputInActiveDirectoryInfo.ConsoleOutput}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="10" IsReadOnly="True" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />
Expand Down
56 changes: 28 additions & 28 deletions ActiveDirectoryQuerier/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public sealed class MainWindowViewModel : INotifyPropertyChanged
private bool _isQueryEditingEnabled;
private string _queryName;
private string _queryDescription;
private AppConsole _consoleOutputInQueryBuilder;
private AppConsole _consoleOutputInActiveDirectoryInfo;
private ConsoleViewModel _consoleOutputInQueryBuilder;
private ConsoleViewModel _consoleOutputInActiveDirectoryInfo;
private Command? _selectedCommandInQueryBuilder;
private string? _selectedQueryInActiveDirectoryInfo;
private ObservableCollection<Button>? _buttons; // TODO: Rename to be more descriptive.
Expand All @@ -46,26 +46,26 @@ public sealed class MainWindowViewModel : INotifyPropertyChanged

public ObservableCollection<Button> QueryButtonStackPanel => _buttons ??= new ObservableCollection<Button>();

public AppConsole ConsoleOutputInQueryBuilder
public ConsoleViewModel ConsoleViewModelOutputInQueryBuilder
{
get => _consoleOutputInQueryBuilder;
set {
if (_consoleOutputInQueryBuilder != value)
{
_consoleOutputInQueryBuilder = value;
OnPropertyChanged(nameof(ConsoleOutputInQueryBuilder));
OnPropertyChanged(nameof(ConsoleViewModelOutputInQueryBuilder));
}
}
}

public AppConsole ConsoleOutputInActiveDirectoryInfo
public ConsoleViewModel ConsoleViewModelOutputInActiveDirectoryInfo
{
get => _consoleOutputInActiveDirectoryInfo;
set {
if (_consoleOutputInActiveDirectoryInfo != value)
{
_consoleOutputInActiveDirectoryInfo = value;
OnPropertyChanged(nameof(ConsoleOutputInActiveDirectoryInfo));
OnPropertyChanged(nameof(ConsoleViewModelOutputInActiveDirectoryInfo));
}
}
}
Expand Down Expand Up @@ -190,8 +190,8 @@ public MainWindowViewModel()
{
_queryName = string.Empty;
_queryDescription = string.Empty;
_consoleOutputInQueryBuilder = new AppConsole();
_consoleOutputInActiveDirectoryInfo = new AppConsole();
_consoleOutputInQueryBuilder = new ConsoleViewModel();
_consoleOutputInActiveDirectoryInfo = new ConsoleViewModel();
_psExecutor = new PSExecutor();
_queryManager = new QueryManager();
_currentQuery = new Query();
Expand Down Expand Up @@ -228,7 +228,7 @@ public MainWindowViewModel()
// [ Methods ] ----------------------------------------------------------------- //

// TODO: Hunter: Re-review this method and make any necessary changes.
private async Task ExecuteQueryAsync(AppConsole appConsole, Command? command = null)
private async Task ExecuteQueryAsync(ConsoleViewModel consoleOuput, Command? command = null)
{
if (SelectedCommandInQueryBuilder is null && command is null)
{
Expand Down Expand Up @@ -256,21 +256,21 @@ private async Task ExecuteQueryAsync(AppConsole appConsole, Command? command = n

if (result.HadErrors)
{
appConsole.Append(result.StdErr);
consoleOuput.Append(result.StdErr);
return;
}

appConsole.Append(result.StdOut);
consoleOuput.Append(result.StdOut);
}
catch (Exception ex)
{
appConsole.Append($"Error executing command: {ex.Message}" + ex.Message);
consoleOuput.Append($"Error executing command: {ex.Message}" + ex.Message);
}
}

private void ClearConsoleOutput(AppConsole appConsole)
private void ClearConsoleOutput(ConsoleViewModel consoleOuput)
{
if (appConsole.ConsoleOutput.Length == 0)
if (consoleOuput.ConsoleOutput.Length == 0)
{
MessageBox.Show("The console is already clear.",
"Information",
Expand All @@ -287,7 +287,7 @@ private void ClearConsoleOutput(AppConsole appConsole)

if (result == MessageBoxResult.Yes)
{
appConsole.Clear();
consoleOuput.Clear();
}
}

Expand All @@ -308,7 +308,7 @@ private async void ExecuteSelectedQueryInADInfo(object _)
{
PSOutput result = await method.Invoke();

ConsoleOutputInActiveDirectoryInfo.Append(result.HadErrors ? result.StdErr : result.StdOut);
ConsoleViewModelOutputInActiveDirectoryInfo.Append(result.HadErrors ? result.StdErr : result.StdOut);
}
// This is an internal error to ensure that if the selected option is not found, the program will not continue.
else
Expand Down Expand Up @@ -374,7 +374,7 @@ private async void ExecuteQueryFromQueryStackPanel(object queryButton)
}

var buttonQuery = (Query)currentButton.Tag;
await ExecuteQueryAsync(ConsoleOutputInQueryBuilder, buttonQuery.Command);
await ExecuteQueryAsync(ConsoleViewModelOutputInQueryBuilder, buttonQuery.Command);
}

private void DeleteQueryFromQueryStackPanel(object queryButton)
Expand Down Expand Up @@ -412,7 +412,7 @@ private void CreateNewQueryFile(object _)
{
QueryButtonStackPanel.Clear();
// File.WriteAllText(saveFileDialog.FileName, string.Empty);
_queryManager.QuerySaveLocation = saveFileDialog.FileName;
_queryManager.QueryFileSaveLocation = saveFileDialog.FileName;
}
}

Expand Down Expand Up @@ -449,7 +449,7 @@ private void ImportQueryFile(object _)
if (result == true)
{
// Open document
_queryManager.QuerySaveLocation = dialog.FileName;
_queryManager.QueryFileSaveLocation = dialog.FileName;

QueryButtonStackPanel.Clear();
LoadSavedQueriesFromFile();
Expand Down Expand Up @@ -524,11 +524,11 @@ private async void OutputExecutionResultsToTextFileAsync(object _)
Query buttonQuery;

buttonQuery = (Query)currentButton!.Tag;
await ExecuteQueryAsync(ConsoleOutputInQueryBuilder, buttonQuery.Command);
await ExecuteQueryAsync(ConsoleViewModelOutputInQueryBuilder, buttonQuery.Command);
}
else
{
await ExecuteQueryAsync(ConsoleOutputInQueryBuilder);
await ExecuteQueryAsync(ConsoleViewModelOutputInQueryBuilder);
}

// Filepath
Expand All @@ -546,7 +546,7 @@ private async void OutputExecutionResultsToTextFileAsync(object _)
{
// Open document
string filePath = saveFileDialog.FileName;
await File.WriteAllTextAsync(filePath, ConsoleOutputInQueryBuilder.ConsoleOutput);
await File.WriteAllTextAsync(filePath, ConsoleViewModelOutputInQueryBuilder.ConsoleOutput);
}
}

Expand All @@ -558,15 +558,15 @@ private async void OutputExecutionResultsToCsvFileAsync(object _)
Query buttonQuery;

buttonQuery = (Query)currentButton!.Tag;
await ExecuteQueryAsync(ConsoleOutputInQueryBuilder, buttonQuery.Command);
await ExecuteQueryAsync(ConsoleViewModelOutputInQueryBuilder, buttonQuery.Command);
}
else
{
await ExecuteQueryAsync(ConsoleOutputInQueryBuilder);
await ExecuteQueryAsync(ConsoleViewModelOutputInQueryBuilder);
}

var csv = new StringBuilder();
string[] output = ConsoleOutputInQueryBuilder.ConsoleOutput.Split(' ', '\n');
string[] output = ConsoleViewModelOutputInQueryBuilder.ConsoleOutput.Split(' ', '\n');

for (int i = 0; i < output.Length - 2; i++)
{
Expand Down Expand Up @@ -594,7 +594,7 @@ private async void OutputExecutionResultsToCsvFileAsync(object _)

private void ExportConsoleOutputToFile(object _)
{
if (ConsoleOutputInQueryBuilder.ConsoleOutput.Length == 0)
if (ConsoleViewModelOutputInQueryBuilder.ConsoleOutput.Length == 0)
{
MessageBox.Show("The console is empty.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
return;
Expand All @@ -607,7 +607,7 @@ private void ExportConsoleOutputToFile(object _)
if (result == true)
{
string filename = saveFileDialog.FileName;
ConsoleOutputInQueryBuilder.ExportToText(filename);
ConsoleViewModelOutputInQueryBuilder.ExportToTextFile(filename);
}
}

Expand Down Expand Up @@ -750,7 +750,7 @@ private void ClearQueryBuilder(object _)
MessageBoxImage.Warning,
MessageBoxResult.No);

// If the user selects yes, clear the console
// If the user selects yes, clear the consoleOuput
if (result == MessageBoxResult.Yes)
{
QueryName = "";
Expand Down
Loading

0 comments on commit 342ca4c

Please sign in to comment.