diff --git a/UnitTest/Input/main.cpp b/UnitTest/Input/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bbf9904455864ca5d5c4984eb6d66689d43625d --- /dev/null +++ b/UnitTest/Input/main.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include <string> + +namespace MyNamespace { + class Person { + public: + std::string name; + int age; + + void displayInfo() { + std::cout << "Name: " << name << ", Age: " << age << std::endl; + } + }; +} + +int main() { + MyNamespace::Person person1; + person1.name = "John"; + person1.age = 25; + person1.displayInfo(); + + return 0; +} \ No newline at end of file diff --git a/UnitTest/UnitTest.csproj b/UnitTest/UnitTest.csproj index 07a023dd86000062e9d8089316894637b6ed05e4..65c9a55c08a9693486fb35b16a96a7ac19e85d49 100644 --- a/UnitTest/UnitTest.csproj +++ b/UnitTest/UnitTest.csproj @@ -23,4 +23,10 @@ <ProjectReference Include="..\WebAPI\WebAPI.csproj" /> </ItemGroup> + <ItemGroup> + <Content Include="Input\main.cpp"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + </ItemGroup> + </Project> diff --git a/UnitTest/WebApiTests.cs b/UnitTest/WebApiTests.cs index 58ad007feb4acccf0eb628f9e34d07fe7ce89b6d..2ec6ad8fe3f63b94e00a7c2b07d39147aaa04a48 100644 --- a/UnitTest/WebApiTests.cs +++ b/UnitTest/WebApiTests.cs @@ -1,3 +1,4 @@ +using System.Text; using Database; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; @@ -256,7 +257,7 @@ public class WebApiTests [TestCase(true)] [TestCase(false)] - public void UploadCode(bool getValidToken) + public void UploadCodeAndTransform(bool getValidToken) { string token = string.Empty; if (getValidToken) @@ -269,10 +270,12 @@ public class WebApiTests } var path = Path.Join(Directory.GetCurrentDirectory(), "users.json"); ApiController apiController = new ApiController(null, null, path); + var code = File.ReadAllText("Input/main.cpp"); + var codeBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(code)); var model = new UploadCodeModel() { Token = token, - Code = "TestCode", + Code = codeBase64 }; var result = apiController.UploadCode(model); if (getValidToken) @@ -288,6 +291,8 @@ public class WebApiTests Assert.IsTrue(idInt == 1); ListAll(true, 1); ListOne(true, 1, true); + ConvertToActivityDiagram(true, 1, true); + ConvertToClassDiagram(true, 1, true); } else { @@ -341,7 +346,103 @@ public class WebApiTests Assert.IsTrue(okResult.Value as string == "Invalid token"); } } + #endregion + #region Code Transformation Tests + [TestCase(true, -1, false)] + [TestCase(false, -1, false)] + public void ConvertToActivityDiagram(bool getValidToken, int id, bool codeExists) + { + string token = string.Empty; + if (getValidToken) + { + token = GetToken(TestUserAuthenticationProcedure()); + } + else + { + token = "WrongToken"; + } + var path = Path.Join(Directory.GetCurrentDirectory(), "users.json"); + ApiController apiController = new ApiController(null, null, path); + var result = apiController.ConvertToActivityDiagram(token, id, string.Empty,"main"); + if (getValidToken) + { + if (codeExists) + { + Assert.IsTrue(result is FileStreamResult); + var fileStreamResult = result as FileStreamResult; + Assert.IsTrue(fileStreamResult.ContentType == "application/xml"); + Assert.IsTrue(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName)); + + //check if filestream is not empty + var stream = fileStreamResult.FileStream; + Assert.IsTrue(stream.Length > 0); + } + else + { + Assert.IsTrue(result is NotFoundObjectResult); + var okResult = result as NotFoundObjectResult; + Assert.IsTrue(okResult.Value is string); + Assert.IsTrue(okResult.Value as string == "Code does not exist"); + } + + } + else + { + Assert.IsTrue(result is BadRequestObjectResult); + var okResult = result as BadRequestObjectResult; + Assert.IsTrue(okResult.Value is string); + Assert.IsTrue(okResult.Value as string == "Invalid token"); + } + } + + //ConvertToClassDiagram + [TestCase(true, -1, false)] + [TestCase(false, -1, false)] + public void ConvertToClassDiagram(bool getValidToken, int id, bool codeExists) + { + string token = string.Empty; + if (getValidToken) + { + token = GetToken(TestUserAuthenticationProcedure()); + } + else + { + token = "WrongToken"; + } + var path = Path.Join(Directory.GetCurrentDirectory(), "users.json"); + ApiController apiController = new ApiController(null, null, path); + var result = apiController.ConvertToClassDiagram(token, id); + if (getValidToken) + { + if (codeExists) + { + Assert.IsTrue(result is FileStreamResult); + var fileStreamResult = result as FileStreamResult; + Assert.IsTrue(fileStreamResult.ContentType == "application/xml"); + Assert.IsTrue(!string.IsNullOrEmpty(fileStreamResult.FileDownloadName)); + + //check if filestream is not empty + var stream = fileStreamResult.FileStream; + Assert.IsTrue(stream.Length > 0); + } + else + { + Assert.IsTrue(result is NotFoundObjectResult); + var okResult = result as NotFoundObjectResult; + Assert.IsTrue(okResult.Value is string); + Assert.IsTrue(okResult.Value as string == "Code does not exist"); + } + + } + else + { + Assert.IsTrue(result is BadRequestObjectResult); + var okResult = result as BadRequestObjectResult; + Assert.IsTrue(okResult.Value is string); + Assert.IsTrue(okResult.Value as string == "Invalid token"); + } + } #endregion