Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Nov 21, 2022
1 parent fb3dc0b commit 799a1d3
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 31 deletions.
141 changes: 135 additions & 6 deletions ServiceBusManager.UnitTests/ConnectViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@

using System;
using ServiceBusManager.Models;
using TinyMvvm;

namespace ServiceBusManager.UnitTests;
namespace ServiceBusManager.UnitTests;

public class ConnectViewModelTests
{
Expand Down Expand Up @@ -69,6 +64,27 @@ public async Task OnAppearingTest()
viewModel.ShowConnections.Should().BeTrue();
}

[Fact]
public async Task OnAppearing_NoSavedConnectionsTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();
connectionService.Get().Returns(Task.FromResult(new List<Models.ConnectionInfo>()));

var featureService = Substitute.For<IFeatureService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);

//Act
await viewModel.OnAppearing();

//Assert
viewModel.Connections.Should().HaveCount(0);
viewModel.ShowConnections.Should().BeFalse();
viewModel.ShowNew.Should().BeTrue();
}

[Fact]
public void OpenConnectionCommandTest()
{
Expand Down Expand Up @@ -121,4 +137,117 @@ public void SaveAndConnectToNewCommandTest()
await viewModel.Navigation.NavigateTo($"///{nameof(MainViewModel)}", connectionString);
});
}

[Fact]
public void ConnectToNewCommandTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var logService = Substitute.For<ILogService>();

TinyNavigation.Current = Substitute.For<TinyMvvm.INavigation>();

var name = "Test";
var connectionString = "Test";

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
viewModel.Name = name;
viewModel.ConnectionString = connectionString;

//Act
viewModel.SaveAndConnectToNewCommand.Execute(null);

//Assert
Received.InOrder(async () =>
{
await viewModel.Navigation.NavigateTo($"///{nameof(MainViewModel)}", connectionString);
});
}

[Fact]
public void ValidateSaveTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
viewModel.Name = string.Empty;
viewModel.ConnectionString = string.Empty;

//Act
viewModel.SaveAndConnectToNewCommand.Execute(null);

//Assert
viewModel.ShowSaveValidationMessage.Should().BeTrue();
viewModel.ShowConnectValidationMessage.Should().BeTrue();
}

[Fact]
public void ValidateConnectTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
viewModel.Name = "Test";
viewModel.ConnectionString = string.Empty;

//Act
viewModel.SaveAndConnectToNewCommand.Execute(null);

//Assert
viewModel.ShowSaveValidationMessage.Should().BeFalse();
viewModel.ShowConnectValidationMessage.Should().BeTrue();
}

[Fact]
public async Task RemoveCommandTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var logService = Substitute.For<ILogService>();


var toRemove = new ConnectionInfo()
{
Id = "test"
};

var toKeep = new ConnectionInfo()
{
Id = "keep"
};

connectionService.Get().Returns(Task.FromResult(new List<Models.ConnectionInfo>()
{
toKeep,
toRemove
}));


var viewModel = new ConnectViewModel(connectionService, featureService, logService);
await viewModel.OnAppearing();

//Act
viewModel.RemoveCommand.Execute(toRemove);

//Assert
viewModel.Connections.Should().HaveCount(1);
viewModel.Connections.First().Should().Be(toKeep);

Received.InOrder(async() => {
await connectionService.Remove(toRemove);
});
}
}

8 changes: 5 additions & 3 deletions ServiceBusManager.UnitTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
global using Xunit;
global using ServiceBusManager.ViewModels;
global using FluentAssertions;
global using NSubstitute;
global using ServiceBusManager.Models;
global using ServiceBusManager.Services;
global using FluentAssertions;
global using ServiceBusManager.ViewModels;
global using TinyMvvm;
global using Xunit;
1 change: 1 addition & 0 deletions ServiceBusManager/Models/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public record ConnectionInfo
{
public string? Id { get; set; }
public string? Name { get; set; }
public string? Value { get; set; }
}
17 changes: 10 additions & 7 deletions ServiceBusManager/ServiceBusManager.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFrameworks>net7.0;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks>net7.0-maccatalyst;net7.0;</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<OutputType Condition="'$(TargetFramework)' == 'net7.0-maccatalyst'">Exe</OutputType>
<OutputType Condition="'$(TargetFramework)' == 'net7.0'">Library</OutputType>
<RootNamespace>ServiceBusManager</RootNamespace>
<AssemblyName>AzServiceBus</AssemblyName>
<AssemblyName Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-maccatalyst|AnyCPU'">AzServiceBus</AssemblyName>
<AssemblyName Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">AzServiceBus Debug</AssemblyName>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableDefaultCssItems>false</EnableDefaultCssItems>
<!-- Display name -->
<ApplicationTitle>AzServiceBus</ApplicationTitle>
<ApplicationTitle Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-maccatalyst|AnyCPU'">AzServiceBus</ApplicationTitle>
<ApplicationTitle Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">AzServiceBus Debug</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>hindrikes.se.azservicebus</ApplicationId>
<ApplicationId Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">hindrikes.se.azservicebus.debug</ApplicationId>
<ApplicationId Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-maccatalyst|AnyCPU'">hindrikes.se.azservicebus</ApplicationId>
<ApplicationIdGuid>43681C5C-733E-4899-B69F-32C627F37BA2</ApplicationIdGuid>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>9</ApplicationVersion>
<ApplicationVersion>10</ApplicationVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
Expand All @@ -40,9 +43,9 @@
<EnablePackageSigning>true</EnablePackageSigning>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">
<EnableCodeSigning>True</EnableCodeSigning>
<EnableCodeSigning>False</EnableCodeSigning>
<CodesignKey>Apple Development: Created via API (C65YK5VCL5)</CodesignKey>
<CodesignProvision>AzServiceBusDev</CodesignProvision>
<CodesignProvision>AzServiceBusDebug</CodesignProvision>
<!--<CodesignEntitlements>Platforms\MacCatalyst\Entitlements.plist</CodesignEntitlements>-->
<CreatePackage>false</CreatePackage>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion ServiceBusManager/Services/DebugConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task Remove(ConnectionInfo connection)
{
var connections = await Get();

var current = connections.Single(x => x.Value == connection.Value);
var current = connections.Single(x => x.Id == connection.Id);

connections.Remove(current);

Expand Down
2 changes: 1 addition & 1 deletion ServiceBusManager/Services/SecureConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Remove(ConnectionInfo connection)
{
var connections = await Get();

var current = connections.Single(x => x.Value == connection.Value);
var current = connections.Single(x => x.Id == connection.Id);

connections.Remove(current);

Expand Down
56 changes: 51 additions & 5 deletions ServiceBusManager/ViewModels/ConnectViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public override async Task OnParameterSet()
[ObservableProperty]
private string? connectionString;

[ObservableProperty]
private bool showSaveValidationMessage;

[ObservableProperty]
private bool showConnectValidationMessage;

[RelayCommand]
private void ToggleNew()
{
Expand All @@ -92,12 +98,26 @@ private async Task SaveAndConnectToNew()
return;
}

if(string.IsNullOrWhiteSpace(name))
var error = false;

if (!ValidateSave())
{
ShowSaveValidationMessage = true;
error = true;
}

if (!ValidateConnect())
{
name = $"Connection-{DateTime.Now.ToString()}";
ShowConnectValidationMessage = true;
error = true;
}

await connectionService.Save(new ConnectionInfo() { Name = name, Value = connectionString });
if(error)
{
return;
}

await connectionService.Save(new ConnectionInfo() {Id = Guid.NewGuid().ToString(), Name = name, Value = connectionString });
}
catch (Exception ex)
{
Expand All @@ -120,7 +140,14 @@ private async Task OpenConnection()
return;
}

if (!ValidateConnect())
{
ShowConnectValidationMessage = true;
return;
}

await Navigation.NavigateTo($"///{nameof(MainViewModel)}", connectionString);

}

[RelayCommand]
Expand All @@ -130,9 +157,28 @@ private async Task ConnectToSaved(string? value)
await OpenConnection();
}

private bool Validate()
[RelayCommand]
private async Task Remove(ConnectionInfo info)
{
try
{
await connectionService.Remove(info);
Connections.Remove(info);
}
catch (Exception ex)
{
HandleException(ex);
}
}

private bool ValidateSave()
{
return !string.IsNullOrWhiteSpace(name);
}

private bool ValidateConnect()
{
return !string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(connectionString);
return !string.IsNullOrWhiteSpace(connectionString);
}

}
Expand Down
13 changes: 13 additions & 0 deletions ServiceBusManager/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ private void OpenSubscriptions(string? topic)
ShowDeadLetters = false;
ShowSubscriptions = true;
}

[RelayCommand]
public async Task ShowPremium()
{
RunAction("close_premium");
await Navigation.NavigateTo("///Premium");
}

[RelayCommand]
public void Dismiss()
{
RunAction("close_premium");
}
}


13 changes: 13 additions & 0 deletions ServiceBusManager/ViewModels/MessageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ private void ResendMessages()
return;
}

if(HasNotPremium && selectedMessages.Count > 1)
{
RunAction("open_premium");
return;
}


IsBusy = true;

Task.Run(async () =>
Expand Down Expand Up @@ -195,6 +202,12 @@ private void RemoveMessages()
return;
}

if (HasNotPremium && selectedMessages.Count > 1)
{
RunAction("open_premium");
return;
}

IsBusy = true;

Task.Run(async () =>
Expand Down
Loading

0 comments on commit 799a1d3

Please sign in to comment.