using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using Centron.BusinessLogic; using Centron.BusinessLogic.Administration.FileManagement; using Centron.BusinessLogic.WebServices.Administration.FileManagement; using Centron.Interfaces.BL; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Administration.FileManagement; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.System.Indexing.Documents; public class DocumentFulltextIndexingTests : EndToEndTest { public DocumentFulltextIndexingTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { using var session = new BLSession(); var newDocs = InsertExampleDocumentsIntoDatabase(session); // Check if we have the index stats for the documents string sql = @$"SELECT DocumentI3D, IsUpdateRequested FROM DocumentFulltextIndexStats WHERE DocumentI3D IN ({string.Join(",", newDocs)}) ORDER BY DocumentI3D"; Verifier.VerifySql("DocumentFulltextIndexingTests_NewDoc_Stats", sql); // Verify the created indexes sql = @$"SELECT DocumentI3D, TextValue FROM DocumentFulltextIndex WHERE DocumentI3D IN ({string.Join(",", newDocs)}) ORDER BY DocumentI3D, TextValue"; Verifier.VerifySql("DocumentFulltextIndexingTests_NewDoc_Indexes", sql); // Search after the new documents var searchResult = session.GetBL().SearchDocumentsThroughPaging( base.GetLoggedInUser(), new DocumentFilter() { SearchText = "DE10034252384" }, 1, int.MaxValue).ThrowIfError(); var foundDocumentNames = searchResult.Result.Select(s => s.Name).ToList(); Verifier.Verify("DocumentFulltextIndexingTests_DocSearch", foundDocumentNames); // Test the service session.DAOSession.GetSession().Clear(); base.Sql("TRUNCATE TABLE DocumentFulltextIndexStats"); base.Sql("TRUNCATE TABLE DocumentFulltextIndex"); session.GetBL().CreateMissingIndexes(isUnitTest:true); // Check if we have the index stats for the documents sql = @$"SELECT DocumentI3D, IsUpdateRequested FROM DocumentFulltextIndexStats WHERE DocumentI3D IN ({string.Join(",", newDocs)}) ORDER BY DocumentI3D"; Verifier.VerifySql("DocumentFulltextIndexingTests_NewDoc_Stats_after_truncate", sql); // Check if we have our indexes again sql = @$"SELECT DocumentI3D, TextValue FROM DocumentFulltextIndex WHERE DocumentI3D IN ({string.Join(",", newDocs)}) ORDER BY DocumentI3D, TextValue"; Verifier.VerifySql("DocumentFulltextIndexingTests_NewDoc_Indexes_after_truncate", sql); } private List InsertExampleDocumentsIntoDatabase(BLSession session) { var fileList = GetExampleDocumentsFromResource(); var documentBL = session.GetBL(); var newDocI3Ds = new List(); foreach (var file in fileList) { // Directory I3D "3265" is from the folder "Rechnung 40171". Just picked one randomly. We want to test the indexing. var newDoc = documentBL.AddFileToDirectory(base.GetLoggedInUser(), 3265, file.Value, file.Key, file.Value.Length, true); if (newDoc == null || newDoc.I3D <= 0) throw new Exception("Could create the example document!"); newDocI3Ds.Add(newDoc.I3D); } return newDocI3Ds; } private List> GetExampleDocumentsFromResource() { var exampleFileList = new List { "ExampleFile_PDF_RE936953.pdf", "ExampleFile_RTF_RE936953.rtf", "ExampleFile_WORD_RE936953.docx", "ExampleFile_XLSX_RE936953.xlsx", "ExampleFile_MSG.msg" }; var fileList = new List>(); foreach (var file in exampleFileList) { using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"Centron.Tests.EndToEnd.Tests.System.Indexing.Documents.{file}"); if (stream == null) throw new Exception($"No resource file '{file}' available!"); using var memStream = new MemoryStream(); stream.CopyTo(memStream); fileList.Add(new KeyValuePair(file, memStream.ToArray())); } return fileList; } }