-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* IApplicationClient.cs added the method ExecutePostRequest<T> to perform post request and return a deserialized response. * added deactivate package command. --------- Co-authored-by: S.Liaskivskyi <[email protected]>
- Loading branch information
1 parent
a6218b5
commit b50d799
Showing
8 changed files
with
328 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using Clio.Command.PackageCommand; | ||
using Clio.Common; | ||
using Clio.Package; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Clio.Tests.Command; | ||
|
||
[TestFixture] | ||
public class DeactivatePackageCommandTestCase { | ||
|
||
#region Methods: Public | ||
|
||
[Test, Category("Unit")] | ||
public void Execute_DeactivatesPackage() | ||
{ | ||
var packageDeactivator = Substitute.For<IPackageDeactivator>(); | ||
var applicationClient = Substitute.For<IApplicationClient>(); | ||
var logger = Substitute.For<ILogger>(); | ||
var packageName = "TestPackageName"; | ||
packageDeactivator.Deactivate(packageName); | ||
var command = new DeactivatePackageCommand(packageDeactivator, applicationClient, new EnvironmentSettings(), logger); | ||
Assert.AreEqual(0, command.Execute(new DeactivatePkgOptions { PackageName = packageName })); | ||
logger.Received().WriteLine($"Start deactivation package: \"{packageName}\""); | ||
logger.Received().WriteLine($"Package \"{packageName}\" successfully deactivated."); | ||
} | ||
|
||
[Test, Category("Unit")] | ||
public void Execute_ShowsErrorMessage_WhenPackageWasNotDeactivated() | ||
{ | ||
var packageDeactivator = Substitute.For<IPackageDeactivator>(); | ||
var applicationClient = Substitute.For<IApplicationClient>(); | ||
var logger = Substitute.For<ILogger>(); | ||
var packageName = "TestPackageName"; | ||
var errorMessage = "SomeErrorMessage"; | ||
packageDeactivator.When(deactivator => deactivator.Deactivate(packageName)).Throw(new Exception(errorMessage)); | ||
var command = new DeactivatePackageCommand(packageDeactivator, applicationClient, new EnvironmentSettings(), logger); | ||
Assert.AreEqual(1, command.Execute(new DeactivatePkgOptions { PackageName = packageName})); | ||
logger.Received().WriteLine($"Start deactivation package: \"{packageName}\""); | ||
logger.Received().WriteLine(errorMessage); | ||
logger.DidNotReceive().WriteLine($"Package \"{packageName}\" successfully deactivated."); | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Clio.Common; | ||
using Clio.Common.Responses; | ||
using Clio.Package; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Clio.Tests.Package; | ||
|
||
[TestFixture] | ||
public class PackageDeactivatorTestCase | ||
{ | ||
#region Properties: Private | ||
|
||
private IApplicationPackageListProvider _applicationPackageListProvider; | ||
private IApplicationClient _applicationClient; | ||
private IServiceUrlBuilder _serviceUrlBuilder; | ||
private PackageDeactivator _packageDeactivator; | ||
|
||
#endregion | ||
|
||
#region Methods: Private | ||
|
||
private static PackageInfo CreatePackageInfo(string packageName, Guid? packageUId = null) | ||
{ | ||
return new PackageInfo(new PackageDescriptor { Name = packageName, UId = packageUId ?? Guid.NewGuid() }, | ||
string.Empty, | ||
Enumerable.Empty<string>()); | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods: Public | ||
|
||
[SetUp] | ||
public void Init() | ||
{ | ||
_applicationPackageListProvider = Substitute.For<IApplicationPackageListProvider>(); | ||
_applicationClient = Substitute.For<IApplicationClient>(); | ||
_serviceUrlBuilder = Substitute.For<IServiceUrlBuilder>(); | ||
_packageDeactivator = new PackageDeactivator(_applicationPackageListProvider, _applicationClient, | ||
_serviceUrlBuilder); | ||
} | ||
|
||
[Test, Category("Unit")] | ||
public void Deactivate_DeactivatesPackageByName() | ||
{ | ||
const string packageName = "TestPackageName"; | ||
Guid packageUId = Guid.NewGuid(); | ||
_applicationPackageListProvider.GetPackages("{}").Returns(new List<PackageInfo> | ||
{ | ||
CreatePackageInfo("SomePackage"), | ||
CreatePackageInfo(packageName, packageUId), | ||
CreatePackageInfo("SomePackage1") | ||
}); | ||
const string fullUrl = "TestUrl"; | ||
_serviceUrlBuilder.Build("/ServiceModel/PackageService.svc/DeactivatePackage").Returns(fullUrl); | ||
_applicationClient.ExecutePostRequest<BaseResponse>(fullUrl, | ||
Arg.Is<string>(data => data.Contains(packageUId.ToString()))) | ||
.Returns(new BaseResponse { Success = true }); | ||
Assert.DoesNotThrow(() => _packageDeactivator.Deactivate(packageName)); | ||
} | ||
|
||
[Test, Category("Unit")] | ||
[TestCase("")] | ||
[TestCase(null)] | ||
public void Deactivate_ThrowsException_WhenPackageByNameIsNotValid(string packageName) | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => _packageDeactivator.Deactivate(packageName)); | ||
} | ||
|
||
[Test, Category("Unit")] | ||
public void Deactivate_ThrowsException_WhenPackageNotFoundByName() | ||
{ | ||
const string packageName = "TestPackageName"; | ||
_applicationPackageListProvider.GetPackages("{}").Returns(new List<PackageInfo> | ||
{ | ||
CreatePackageInfo("SomePackage") | ||
}); | ||
Assert.Throws<Exception>(() => _packageDeactivator.Deactivate(packageName), | ||
$"Package with name {packageName} not found"); | ||
} | ||
|
||
[Test, Category("Unit")] | ||
public void Deactivate_ThrowsException_WhenPackageWasNotBeenDeactivated() | ||
{ | ||
const string packageName = "TestPackageName"; | ||
Guid packageUId = Guid.NewGuid(); | ||
const string errorMessage = "Some error"; | ||
_applicationPackageListProvider.GetPackages("{}").Returns(new List<PackageInfo> | ||
{ | ||
CreatePackageInfo(packageName, packageUId), | ||
}); | ||
_applicationClient.ExecutePostRequest<BaseResponse>(Arg.Any<string>(), | ||
Arg.Is<string>(data => data.Contains(packageUId.ToString()))) | ||
.Returns(new BaseResponse { Success = false, ErrorInfo = new ErrorInfo {Message = errorMessage}}); | ||
|
||
Assert.Throws<Exception>(() => _packageDeactivator.Deactivate(packageName), errorMessage); | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using Clio.Common; | ||
using Clio.Package; | ||
|
||
namespace Clio.Command.PackageCommand | ||
{ | ||
using CommandLine; | ||
|
||
[Verb("deactivate-pkg", HelpText = "Deactivate package from a web application. Will be available in 8.1.2")] | ||
internal class DeactivatePkgOptions : EnvironmentOptions { | ||
[Value(0, MetaName = "Name", Required = true, HelpText = "Package name")] | ||
public string PackageName { | ||
get; set; | ||
} | ||
} | ||
|
||
#region Class: GetPackageVersionCommand | ||
|
||
internal class DeactivatePackageCommand : RemoteCommand<DeactivatePkgOptions> { | ||
private readonly IPackageDeactivator _packageDeactivator; | ||
private readonly ILogger _logger; | ||
|
||
#region Constructors: Public | ||
|
||
public DeactivatePackageCommand(IPackageDeactivator packageDeactivator, IApplicationClient applicationClient, | ||
EnvironmentSettings environmentSettings, ILogger logger) | ||
: base(applicationClient, environmentSettings) { | ||
_packageDeactivator = packageDeactivator; | ||
_logger = logger; | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods: Public | ||
|
||
public override int Execute(DeactivatePkgOptions options) { | ||
try { | ||
string packageName = options.PackageName; | ||
_logger.WriteLine($"Start deactivation package: \"{packageName}\""); | ||
_packageDeactivator.Deactivate(packageName); | ||
_logger.WriteLine($"Package \"{packageName}\" successfully deactivated."); | ||
return 0; | ||
} | ||
catch (Exception e) { | ||
_logger.WriteLine(e.Message); | ||
return 1; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Clio.Package; | ||
|
||
#region Interface: IPackageDeactivator | ||
|
||
/// <summary> | ||
/// Provides interface for package deactivation. | ||
/// </summary> | ||
public interface IPackageDeactivator { | ||
|
||
#region Methods: Internal | ||
|
||
/// <summary> | ||
/// Deactivate package by UId. | ||
/// </summary> | ||
/// <param name="packageName"></param> | ||
void Deactivate(string packageName); | ||
|
||
#endregion | ||
} | ||
|
||
#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Linq; | ||
using Clio.Common; | ||
using Clio.Common.Responses; | ||
|
||
namespace Clio.Package; | ||
|
||
/// <inheritdoc cref="IPackageDeactivator"/> | ||
internal class PackageDeactivator: IPackageDeactivator | ||
{ | ||
#region Fields: Private | ||
|
||
private readonly IApplicationPackageListProvider _applicationPackageListProvider; | ||
private readonly IApplicationClient _applicationClient; | ||
private readonly IServiceUrlBuilder _serviceUrlBuilder; | ||
|
||
#endregion | ||
|
||
#region Constructors: Public | ||
|
||
public PackageDeactivator(IApplicationPackageListProvider applicationPackageListProvider, | ||
IApplicationClient applicationClient, IServiceUrlBuilder serviceUrlBuilder) { | ||
_applicationPackageListProvider = applicationPackageListProvider; | ||
_applicationClient = applicationClient; | ||
_serviceUrlBuilder = serviceUrlBuilder; | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods: Private | ||
|
||
private static string CreateRequestData(Guid packageUId) { | ||
return "\"" + packageUId + "\""; | ||
} | ||
private static void ThrowsErrorIfUnsuccessfulResponseReceived(BaseResponse deactivateResponse) { | ||
if (deactivateResponse.Success) { | ||
return; | ||
} | ||
throw new Exception(deactivateResponse.ErrorInfo.Message); | ||
} | ||
|
||
private BaseResponse SendDeactivateRequest(Guid packageUId) { | ||
return _applicationClient.ExecutePostRequest<BaseResponse>( | ||
_serviceUrlBuilder.Build("/ServiceModel/PackageService.svc/DeactivatePackage"), | ||
CreateRequestData(packageUId)); | ||
} | ||
|
||
private Guid GetPackageUId(string packageName) { | ||
PackageInfo packageInfo = | ||
_applicationPackageListProvider.GetPackages("{}") | ||
.FirstOrDefault(package => package.Descriptor.Name == packageName); | ||
if (packageInfo is null) { | ||
throw new Exception($"Package with name {packageName} not found"); | ||
} | ||
|
||
return packageInfo.Descriptor.UId; | ||
} | ||
|
||
#endregion | ||
|
||
#region Methods: Public | ||
|
||
/// <inheritdoc cref="IPackageDeactivator.Deactivate"/> | ||
public void Deactivate(string packageName) { | ||
packageName.CheckArgumentNullOrWhiteSpace(nameof(packageName)); | ||
Guid packageUId = GetPackageUId(packageName); | ||
BaseResponse deactivateResponse = SendDeactivateRequest(packageUId); | ||
ThrowsErrorIfUnsuccessfulResponseReceived(deactivateResponse); | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters