Skip to content

Commit

Permalink
Merge branch 'master' into pr/337
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-nikonov committed Dec 26, 2024
2 parents 0c09a21 + 605c277 commit 9bfcdf4
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 4 deletions.
82 changes: 82 additions & 0 deletions clio.tests/Command/UploadLicensesCommand.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace Clio.Tests.Command
{
using Clio.Command;
using Clio.Command.PackageCommand;
using Clio.Common;
using NSubstitute;
using NUnit.Framework;

[TestFixture]
public class UploadLicensesCommandTestCase
{
private UploadLicensesCommandTestable _command;

[SetUp]
public void SetUp() {
_command = new UploadLicensesCommandTestable(Substitute.For<IApplicationClient>(),
Substitute.For<EnvironmentSettings>());
}

[Test]
public void TestProceedResponse_SuccessResponse_DoesNotThrow() {
var response = "{\"success\": true}";
var options = new UploadLicensesOptions();
Assert.DoesNotThrow(() => _command.TestProceedResponse(response, options));
}

[Test]
public void TestProceedResponse_ErrorResponseWithErrorInfo_ThrowsException() {
var response = @"
{
""success"": false,
""errorInfo"": {
""message"": ""Invalid license key"",
""errorCode"": ""INVALID_KEY""
}
}";
var options = new UploadLicensesOptions();
var ex = Assert.Throws<LicenseInstallationException>(() =>
_command.TestProceedResponse(response, options));
Assert.That(ex.Message,
Is.EqualTo("License not installed. ErrorCode: INVALID_KEY, Message: Invalid license key"));
}

[Test]
public void TestProceedResponse_ErrorResponseWithoutErrorInfo_ThrowsException() {
var response = "{\"success\": false}";
var options = new UploadLicensesOptions();
var ex = Assert.Throws<LicenseInstallationException>(() =>
_command.TestProceedResponse(response, options));
Assert.That(ex.Message, Is.EqualTo("License not installed: Unknown error details"));
}

[Test]
public void TestProceedResponse_ResponseWithoutSuccessProperty_DoesNotThrow() {
var response = "{\"errorInfo\": { \"message\": \"Error occurred\" }}";
var options = new UploadLicensesOptions();
Assert.DoesNotThrow(() => _command.TestProceedResponse(response, options));
}

[Test]
public void TestProceedResponse_AuthenticationFailed_ThrowsLicenseInstallationException() {
var response = "{\"Message\":\"Authentication failed.\",\"StackTrace\":null,\"ExceptionType\":\"System.InvalidOperationException\"}";
var options = new UploadLicensesOptions();
var ex = Assert.Throws<LicenseInstallationException>(() =>
_command.TestProceedResponse(response, options));
Assert.That(ex.Message, Is.EqualTo("License not installed: Authentication failed."));
}
}

public class UploadLicensesCommandTestable : UploadLicensesCommand
{
public UploadLicensesCommandTestable(IApplicationClient applicationClient, EnvironmentSettings settings)
: base(applicationClient, settings) {
}

public void TestProceedResponse(string response, UploadLicensesOptions options) {
ProceedResponse(response, options);
}
}


}
6 changes: 6 additions & 0 deletions clio/Command/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public virtual bool ShowDefaultEnvironment() {
[Option("force", Required = false, HelpText = "Force restore")]
public bool Force { get; set; }

[Option("callback-process", Required = false, HelpText = "Callback process name")]
public string CallbackProcess {
get; set;
}

internal virtual bool RequiredEnvironment => true;

public void CopyFromEnvironmentSettings(EnvironmentOptions source)
Expand Down Expand Up @@ -156,6 +161,7 @@ public void CopyFromEnvironmentSettings(EnvironmentOptions source)
this.DbWorknigFolder = source.DbWorknigFolder;
this.DbName = source.DbName;
this.Force = source.Force;
this.CallbackProcess = source.CallbackProcess;
}

internal bool IsEmpty() {
Expand Down
56 changes: 53 additions & 3 deletions clio/Command/PushWorkspaceCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Clio.Command
{
using System;
using System.Text.Json;
using Clio.Command.StartProcess;
using Clio.Common;
using Clio.Workspaces;
using CommandLine;
Expand All @@ -10,6 +12,8 @@ namespace Clio.Command
[Verb("push-workspace", Aliases = new string[] { "pushw" }, HelpText = "Push workspace to selected environment")]
public class PushWorkspaceCommandOptions : EnvironmentOptions
{
[Option("unlock", Required = false, HelpText = "Unlock workspace package after install workspace to the environment")]
public bool NeedUnlockPackage { get; set; }
}

#endregion
Expand All @@ -22,32 +26,78 @@ public class PushWorkspaceCommand : Command<PushWorkspaceCommandOptions>
#region Fields: Private

private readonly IWorkspace _workspace;
private UnlockPackageCommand _unlockPackageCommand;
public IApplicationClientFactory _applicationClientFactory;
private readonly EnvironmentSettings _environmentSettings;
private readonly IServiceUrlBuilder _serviceUrlBuilder;

#endregion

#region Constructors: Public

public PushWorkspaceCommand(IWorkspace workspace) {
public PushWorkspaceCommand(IWorkspace workspace, UnlockPackageCommand unlockPackageCommand,
IApplicationClientFactory applicationClientFactory, EnvironmentSettings environmentSettings,
IServiceUrlBuilder serviceUrlBuilder) {
workspace.CheckArgumentNull(nameof(workspace));
_workspace = workspace;
_unlockPackageCommand = unlockPackageCommand;
_applicationClientFactory = applicationClientFactory;
_environmentSettings = environmentSettings;
_serviceUrlBuilder = serviceUrlBuilder;
}


#endregion

#region Methods: Public

public override int Execute(PushWorkspaceCommandOptions options) {
try
{
try {
Console.WriteLine("Push workspace...");
CallbackInfo(options.CallbackProcess, "Push workspace...");
_workspace.Install();
if (options.NeedUnlockPackage) {
var unlockPackageCommandOptions = new UnlockPackageOptions();
unlockPackageCommandOptions.CopyFromEnvironmentSettings(options);
unlockPackageCommandOptions.Name = string.Join(',', _workspace.WorkspaceSettings.Packages);
Console.WriteLine("Unlock packages...");
CallbackInfo(options.CallbackProcess, "Unlock packages...");
_unlockPackageCommand.Execute(unlockPackageCommandOptions);
}
Console.WriteLine("Done");
CallbackInfo(options.CallbackProcess, "Workspace was successfully restored");
return 0;
} catch (Exception e) {
Console.WriteLine(e.Message);
CallbackInfo(options.CallbackProcess, e.Message);
return 1;
}
}

private void CallbackInfo(string callbackProcess, string message) {
if (!string.IsNullOrEmpty(callbackProcess)) {
var applicationClient = _applicationClientFactory.CreateClient(_environmentSettings);
var runProcessUri = _serviceUrlBuilder.Build(ServiceUrlBuilder.KnownRoute.RunProcess);
ProcessStartArgs runProcessArgs = new() {
SchemaName = callbackProcess,
Values = [
new ProcessStartArgs.ParameterValues {
Name = "Message",
Value = message
},
new ProcessStartArgs.ParameterValues {
Name = "Title",
Value = "CLIO"
}
]
};
Console.WriteLine($"Run callback process {callbackProcess}");
var processRunResponseJson = applicationClient.ExecutePostRequest(runProcessUri, JsonSerializer.Serialize(runProcessArgs));
var response = JsonSerializer.Deserialize<ProcessStartResponse>(processRunResponseJson);
Console.WriteLine($"Run process id {response.ProcessId}");
}
}

#endregion

}
Expand Down
65 changes: 65 additions & 0 deletions clio/Command/StartProcess/ProcessArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Clio.Command.StartProcess;

[JsonObject]
public record ProcessStartArgs
{

[JsonObject]
public class ParameterValues
{
[JsonPropertyName("name")]
[JsonProperty("name")]
public string Name { get; init; }

[JsonPropertyName("value")]
[JsonProperty("value")]
public string Value { get; init; }
}

[JsonProperty("schemaName")]
[JsonPropertyName("schemaName")]
public string SchemaName { get; init; }

[JsonProperty("parameterValues")]
[JsonPropertyName("parameterValues")]
public ParameterValues[] Values { get; init; }

[JsonProperty("resultParameterNames")]
[JsonPropertyName("resultParameterNames")]
public string[] Result { get; init; }

}


[JsonObject]
public record ProcessStartResponse
{
[JsonPropertyName("processId")]
[JsonProperty("processId")]
public Guid ProcessId { get; init; }

[JsonPropertyName("processStatus")]
[JsonProperty("processStatus")]
public int ProcessStatus { get; init; }

[JsonPropertyName("resultParameterValues")]
[JsonProperty("resultParameterValues")]
public Dictionary<string, object> ResultParameterValues { get; init; }

[JsonPropertyName("executionData")]
[JsonProperty("executionData")]
public object ExecutionData { get; init; }

[JsonPropertyName("success")]
[JsonProperty("success")]
public bool Success { get; init; }

[JsonPropertyName("errorInfo")]
[JsonProperty("errorInfo")]
public object ErrorInfo { get; init; }
}
29 changes: 29 additions & 0 deletions clio/Command/UploadLicensesCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Clio.Command.PackageCommand
{
using System;
using System.IO;
using System.Text.Json;
using Clio.Common;

public class UploadLicensesCommand : RemoteCommand<UploadLicensesOptions>
Expand All @@ -18,5 +20,32 @@ protected override string GetRequestData(UploadLicensesOptions options) {
return "{\"licData\":\"" + fileBody + "\"}";
}

protected override void ProceedResponse(string response, UploadLicensesOptions options) {
var json = JsonDocument.Parse(response);
if (json.RootElement.TryGetProperty("success", out var successProperty) &&
successProperty.GetBoolean() == false) {
if (json.RootElement.TryGetProperty("errorInfo", out var errorInfo)) {
var errorMessage = errorInfo.TryGetProperty("message", out var messageProperty)
? messageProperty.GetString()
: "Unknown error message";
var errorCode = errorInfo.TryGetProperty("errorCode", out var codeProperty)
? codeProperty.GetString()
: "UNKNOWN_CODE";
throw new LicenseInstallationException(
$"License not installed. ErrorCode: {errorCode}, Message: {errorMessage}");
}
throw new LicenseInstallationException("License not installed: Unknown error details");
}
if (response.ToLower().Contains("authentication failed")) {
throw new LicenseInstallationException("License not installed: Authentication failed.");
}
base.ProceedResponse(response, options);
}
}

public class LicenseInstallationException : Exception
{
public LicenseInstallationException(string message) : base(message) { }
}

}
2 changes: 1 addition & 1 deletion clio/clio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Authors>creatio rnd team</Authors>
<PackageTags>cli ATF clio creatio</PackageTags>
<NeutralLanguage>en</NeutralLanguage>
<AssemblyVersion>8.0.1.7</AssemblyVersion>
<AssemblyVersion>8.0.1.10</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<Version>$(AssemblyVersion)</Version>
<Description>CLI interface for Creatio</Description>
Expand Down

0 comments on commit 9bfcdf4

Please sign in to comment.