Skip to content
Snippets Groups Projects
Commit 38584297 authored by Jakub Konvička's avatar Jakub Konvička
Browse files

feat: remove code smells WiP

parent 2747b052
Branches
1 merge request!15merge: develop into main
Pipeline #959 passed with stages
in 6 minutes and 32 seconds
......@@ -59,7 +59,7 @@ public class ActivityDiagramTests
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmi);
XmlNodeList nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Activity')]");
Assert.IsTrue(nodes.Count == numberOfActivities,
Assert.That(nodes.Count == numberOfActivities,
$"Number of activities added to XMI should be {numberOfActivities}");
RemoveGeneratedFile();
}
......@@ -87,7 +87,7 @@ public class ActivityDiagramTests
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmi);
XmlNodeList nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Decision')]");
Assert.IsTrue(nodes.Count == decisionCount * 2 - 1,
Assert.That(nodes.Count == decisionCount * 2 - 1,
$"Number of decisions added to XMI should be {decisionCount * 2 - 1}");
RemoveGeneratedFile();
}
......@@ -165,15 +165,15 @@ public class ActivityDiagramTests
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmi);
XmlNodeList nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Decision')]");
Assert.IsTrue(nodes.Count == decisionCount, $"Number of decisions added to XMI should be {decisionCount}");
Assert.That(nodes.Count == decisionCount, $"Number of decisions added to XMI should be {decisionCount}");
//test if number of activitises added to xmi <node name="Activity
nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Activity')]");
Assert.IsTrue(nodes.Count == decisionCount * actionsCount,
Assert.That(nodes.Count == decisionCount * actionsCount,
$"Number of activities added to XMI should be {decisionCount * actionsCount}");
if (addMerge)
{
nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Merge')]");
Assert.IsTrue(nodes.Count == decisionCount-1, $"Number of merges added to XMI should be {decisionCount}");
Assert.That(nodes.Count == decisionCount-1, $"Number of merges added to XMI should be {decisionCount}");
}
RemoveGeneratedFile();
......@@ -233,12 +233,12 @@ public class ActivityDiagramTests
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmi);
XmlNodeList nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Decision')]");
Assert.IsTrue(nodes.Count == 10, $"Number of decisions added to XMI should be 10");
Assert.That(nodes.Count == 10, $"Number of decisions added to XMI should be 10");
//test if number of activitises added to xmi <node name="Activity
nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Activity')]");
Assert.IsTrue(nodes.Count == 100, $"Number of activities added to XMI should be 100");
Assert.That(nodes.Count == 100, $"Number of activities added to XMI should be 100");
nodes = xmlDoc.SelectNodes("//node[contains(@name, 'Loop')]");
Assert.IsTrue(nodes.Count == 10, $"Number of loops added to XMI should be 10");
Assert.That(nodes.Count == 10, $"Number of loops added to XMI should be 10");
}
void ValidateSchema()
......@@ -266,7 +266,7 @@ public class ActivityDiagramTests
}
// Assert that there are no errors
Assert.IsTrue(_errors.Count == 0, "There should be no errors in the XMI file");
Assert.That(_errors.Count == 0, "There should be no errors in the XMI file");
foreach ((string severity, string message) in _errors)
{
Console.WriteLine($"{severity}: {message}");
......@@ -284,7 +284,7 @@ public class ActivityDiagramTests
private void AssertCommonAssertions(string xmi)
{
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
if (!string.IsNullOrEmpty(generatedFileName))
......
......@@ -32,7 +32,7 @@ public class ClassDiagramTests
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
// Additional assertions for XMI structure
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
File.WriteAllText(generatedFileName, xmi);
......@@ -66,7 +66,7 @@ public class ClassDiagramTests
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
// Additional assertions for XMI structure
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
File.WriteAllText(generatedFileName, xmi);
......@@ -93,7 +93,7 @@ public class ClassDiagramTests
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
// Additional assertions for XMI structure
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
File.WriteAllText(generatedFileName, xmi);
......@@ -124,7 +124,7 @@ public class ClassDiagramTests
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
// Additional assertions for XMI structure
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
File.WriteAllText(generatedFileName, xmi);
......@@ -269,7 +269,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
......@@ -286,7 +286,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
......@@ -304,7 +304,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
......@@ -322,7 +322,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
public void BuildDiagramWithGeneralizationAndAssociationAndOperationAndProperty()
......@@ -340,7 +340,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
public void BuildDiagramWithGeneralizationAndAssociationAndOperationAndPropertyAndPackage()
......@@ -366,7 +366,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
public void BuildDiagramWithGeneralizationAndAssociationAndOperationAndPropertyAndPackageAndMultipleClasses()
......@@ -400,7 +400,7 @@ public class ClassDiagramTests
AssertCommonAssertions(xmi);
// Add specific assertions for generalization
Assert.IsTrue(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
Assert.That(xmi.Contains("general=\"TestPackage-Class1\""), "XMI should contain Generalization");
}
[Test]
......@@ -470,7 +470,7 @@ public class ClassDiagramTests
}
// Assert that there are no errors
Assert.IsTrue(_errors.Count == 0, "There should be no errors in the XMI file");
Assert.That(_errors.Count == 0, "There should be no errors in the XMI file");
foreach ((string severity, string message) in _errors)
{
Console.WriteLine($"{severity}: {message}");
......@@ -488,7 +488,7 @@ public class ClassDiagramTests
private void AssertCommonAssertions(string xmi)
{
Assert.IsFalse(string.IsNullOrEmpty(xmi), "XMI should not be null or empty");
Assert.IsTrue(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
Assert.That(xmi.Contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"), "XMI should contain the XML header");
// Add more assertions based on the expected structure of your XMI
if (!string.IsNullOrEmpty(generatedFileName))
......
using System.Text;
using Database;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WebAPI.Controllers;
using WebAPI.DTO;
......@@ -41,6 +42,7 @@ public class WebApiTests
public void AuthenticationTest(string username = "TestUser", string password = "TestPassword")
{
var path = Path.Join(Directory.GetCurrentDirectory(), "users.json");
//create logger
ApiController apiController = new ApiController(null, null, path);
var model = new AuthenticateUserModel()
{
......@@ -49,13 +51,13 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
var token = okResult.Value as string;
Assert.IsTrue(!string.IsNullOrEmpty(token));
Assert.That(!string.IsNullOrEmpty(token));
}
public string TestUserAuthenticationProcedure(string username = "TestUser", string password = "TestPassword")
private string TestUserAuthenticationProcedure(string username = "TestUser", string password = "TestPassword")
{
var path = Path.Join(Directory.GetCurrentDirectory(), "users.json");
ApiController apiController = new ApiController(null, null, path);
......@@ -66,10 +68,10 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
var token = okResult.Value as string;
Assert.IsTrue(!string.IsNullOrEmpty(token));
Assert.That(!string.IsNullOrEmpty(token));
return okResult.Value as string;
}
......@@ -85,7 +87,7 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
}
[Test]
......@@ -100,7 +102,7 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
}
[Test]
......@@ -115,7 +117,7 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
}
[Test]
......@@ -130,7 +132,7 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
}
[Test]
......@@ -145,7 +147,7 @@ public class WebApiTests
};
var result = apiController.AuthenticateUserPassword(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
}
#endregion
......@@ -165,10 +167,10 @@ public class WebApiTests
};
var result = apiController.RegisterUser(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
Assert.IsTrue(!string.IsNullOrEmpty(okResult.Value as string));
Assert.IsTrue((okResult.Value as string) == "User created");
Assert.That(!string.IsNullOrEmpty(okResult.Value as string));
Assert.That((okResult.Value as string) == "User created");
}
[Test]
......@@ -185,10 +187,10 @@ public class WebApiTests
};
var result = apiController.RegisterUser(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(!string.IsNullOrEmpty(okResult.Value as string));
Assert.IsTrue((okResult.Value as string) == "User already exists");
Assert.That(!string.IsNullOrEmpty(okResult.Value as string));
Assert.That((okResult.Value as string) == "User already exists");
}
[Test]
......@@ -204,10 +206,10 @@ public class WebApiTests
};
var result = apiController.RegisterUser(model);
//assert that returns Ok and parse token
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(!string.IsNullOrEmpty(okResult.Value as string));
Assert.IsTrue((okResult.Value as string) == "Invalid token");
Assert.That(!string.IsNullOrEmpty(okResult.Value as string));
Assert.That((okResult.Value as string) == "Invalid token");
}
[Test]
......@@ -237,18 +239,18 @@ public class WebApiTests
if (getValidToken)
{
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
Assert.IsTrue(okResult.Value is List<SourceCodeExt>);
Assert.That(okResult.Value is List<SourceCodeExt>);
var list = okResult.Value as List<SourceCodeExt>;
Assert.IsTrue(list.Count == count);
Assert.That(list.Count == count);
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
......@@ -278,15 +280,15 @@ public class WebApiTests
var result = apiController.UploadCode(model);
if (getValidToken)
{
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.That(okResult.Value is string);
//parse Code uploaded, id: 1
var value = okResult.Value as string;
var id = value.Split("Code uploaded, id:")[1].Trim();
Assert.IsTrue(!string.IsNullOrEmpty(id));
Assert.IsTrue(int.TryParse(id, out int idInt));
Assert.IsTrue(idInt == 1);
Assert.That(!string.IsNullOrEmpty(id));
Assert.That(int.TryParse(id, out int idInt));
Assert.That(idInt == 1);
ListAll(true, 1);
ListOne(true, 1, true);
ConvertToActivityDiagram(true, 1, true);
......@@ -297,10 +299,10 @@ public class WebApiTests
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
......@@ -324,27 +326,27 @@ public class WebApiTests
{
if (codeExists)
{
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
Assert.IsTrue(okResult.Value is SourceCodeExt);
Assert.That(okResult.Value is SourceCodeExt);
var code = okResult.Value as SourceCodeExt;
Assert.IsTrue(code.Id == id);
Assert.That(code.Id == id);
}
else
{
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
var okResult = result as NotFoundObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Code does not exist");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Code does not exist");
}
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
#endregion
......@@ -370,30 +372,30 @@ public class WebApiTests
{
if (codeExists)
{
Assert.IsTrue(result is FileStreamResult);
Assert.That(result is FileStreamResult);
var fileStreamResult = result as FileStreamResult;
Assert.IsTrue(fileStreamResult.ContentType == "application/xml");
Assert.IsTrue(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName));
Assert.That(fileStreamResult.ContentType == "application/xml");
Assert.That(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName));
//check if filestream is not empty
var stream = fileStreamResult.FileStream;
Assert.IsTrue(stream.Length > 0);
Assert.That(stream.Length > 0);
}
else
{
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
var okResult = result as NotFoundObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Code does not exist");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Code does not exist");
}
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
......@@ -418,30 +420,30 @@ public class WebApiTests
{
if (codeExists)
{
Assert.IsTrue(result is FileStreamResult);
Assert.That(result is FileStreamResult);
var fileStreamResult = result as FileStreamResult;
Assert.IsTrue(fileStreamResult.ContentType == "application/xml");
Assert.IsTrue(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName));
Assert.That(fileStreamResult.ContentType == "application/xml");
Assert.That(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName));
//check if filestream is not empty
var stream = fileStreamResult.FileStream;
Assert.IsTrue(stream.Length > 0);
Assert.That(stream.Length > 0);
}
else
{
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
var okResult = result as NotFoundObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Code does not exist");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Code does not exist");
}
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
......@@ -474,26 +476,26 @@ public class WebApiTests
{
if (codeExists)
{
Assert.IsTrue(result is OkObjectResult);
Assert.That(result is OkObjectResult);
var okResult = result as OkObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Code removed");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Code removed");
}
else
{
Assert.IsTrue(result is NotFoundObjectResult);
Assert.That(result is NotFoundObjectResult);
var okResult = result as NotFoundObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Code does not exist");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Code does not exist");
}
}
else
{
Assert.IsTrue(result is BadRequestObjectResult);
Assert.That(result is BadRequestObjectResult);
var okResult = result as BadRequestObjectResult;
Assert.IsTrue(okResult.Value is string);
Assert.IsTrue(okResult.Value as string == "Invalid token");
Assert.That(okResult.Value is string);
Assert.That(okResult.Value as string == "Invalid token");
}
}
#endregion
......
......@@ -19,11 +19,11 @@ namespace WebAPI.Controllers
[Route("api/")]
public class ApiController : ControllerBase
{
private readonly ILogger<ApiController> _logger;
private readonly IConfiguration _configuration;
private readonly ILogger<ApiController>? _logger;
private readonly IConfiguration? _configuration;
private int tokenValidityMinutes => _configuration?.GetValue<int>("UserManagement:TokenValidityMinutes") ?? 30;
public ApiController(ILogger<ApiController> logger, IConfiguration configuration, string dbPath = "default-users.json")
public ApiController(ILogger<ApiController>? logger, IConfiguration? configuration, string dbPath = "default-users.json")
{
_logger = logger;
_configuration = configuration;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment