Projekt

Obecné

Profil

Stáhnout (5.97 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2
using Models.Enums;
3
using Core.Entities;
4
using Serilog;
5

    
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Core.MapperProfiles;
14
using Core.Contexts;
15
using Microsoft.Extensions.Configuration;
16
using Models.Documents;
17
using Core.Services;
18

    
19
namespace BackendTesting
20
{
21
    //class responsible for testing user and backend autentication on login
22
    [TestClass]
23
    public class DocumentManagementTesting
24
    {
25
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
26
        public DatabaseContext ctx;
27
        public Core.Services.DocumentService.IDocumentService DS;
28
        public DocumentAddRequest docList;
29
        public User user;
30

    
31
        public DocumentManagementTesting()
32
        {
33
            //creating new database
34
            this.ctx = new DatabaseContext(configuration);
35
            this.DS = new Core.Services.DocumentService.DocumentServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
36
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
37

    
38
            //ensure the database is fresh without unwanted users from previous testings
39
            ctx.Database.EnsureDeleted();
40
            ctx.Database.EnsureCreated();
41

    
42
            user = new() { Id = Guid.NewGuid(), Name = "aa", Surname = "aa", Password = "aa", Role = ERole.ADMINISTRATOR, Username = "aa" };
43
            ctx.Users.Add(user);
44
            ctx.SaveChanges();
45

    
46
            docList = MakeRequest();
47
        }
48

    
49
        //method for testing Documents addition
50
        //if document is really in databse is tested in next method where its requested from there (GetDocumentsTest())
51
        [TestMethod]
52
        public void AddDocumentsTest()
53
        {
54
            try
55
            {
56
                DS.AddDocuments(docList, user.Id);
57
            }
58
            catch (Exception ex)
59
            {
60
                Assert.Fail(ex.Message);
61
            }
62
        }
63

    
64
        [TestMethod]
65
        [DataRow(2, 1)]
66
        [DataRow(0, 2)]
67
        [DataRow(3, 1)]
68
        //method for GetDocuments() testing
69
        //recieves same paramaters as GetDocuments()
70
        //numberOfDocs is number representing lenght of list got from method (in documents)
71
        //index represents index of those lists -> 2nd list (index 1) of lenght 2 contains two documents with indexes 2 and 3 (or so is expected)
72
        public void GetDocumentsTest(int index, int numberOfDocs)
73
        {
74
            //vlastne telo addDocument metody - potrebuju do databaze dostat data
75
            try
76
            {
77
                DS.AddDocuments(docList, user.Id);
78
            }
79
            catch (Exception ex)
80
            {
81
                Assert.Fail("Mistake in AddDocument - test AddDocumentsTest");
82
            }
83

    
84

    
85
            var documents = DS.GetDocuments(); //= DS.GetDocuments(index, numberOfDocs);
86
            Assert.IsNotNull(documents);
87
            Assert.AreEqual(documents.Documents.Count, numberOfDocs);
88
            for (int i = 0; i < numberOfDocs; i++)
89
            {
90
                var doc1 = documents.Documents[i];
91
                var name = docList.Documents[numberOfDocs * index + i].Name;
92
                Assert.AreEqual(doc1.Name, name);
93
            }
94
        }
95

    
96

    
97
        //Method for preparing data for database creating DatabaseAssRequest from DocumentAddInfo witch contains documents strings
98
        private DocumentAddRequest MakeRequest()
99
        {
100
            docList = new DocumentAddRequest() { Documents = new() };
101
            var dc = new DocumentContent();
102
            string content = "aaa <html> bbb";
103
            content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));
104
            dc.Content = content;
105
            var dt = new DateTime(2022, 4, 11);
106
            DocumentAddInfo info = new DocumentAddInfo();
107
            info.Name = "a";
108
            info.Content = content;
109
            info.Format = EAddDocumentFormat.TEXTFILE;
110
            docList.Documents.Add(info);
111

    
112
            content = "aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb";
113
            dc.Content = content;
114
            dt = new DateTime(2022, 4, 12);
115
            docList.Documents.Add(new DocumentAddInfo() { Name = "b", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
116

    
117
            content = "aaa <html> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?>" +
118
                " bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc>" +
119
                " nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html>" +
120
                " <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?>" +
121
                " bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb ";
122
            dc.Content = content;
123
            dt = new DateTime(2022, 4, 13);
124
            docList.Documents.Add(new DocumentAddInfo() { Name = "c", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
125

    
126
            content = "aaa";
127
            dc.Content = content;
128
            dt = new DateTime(2022, 4, 14);
129
            docList.Documents.Add(new DocumentAddInfo() { Name = "d", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
130

    
131
            content = "aaa <html> edfgckvhbjno§mpoizuzcjtjczkutvlzibůuoni§piuoůbzivlutkczjxzcktuvlziůuoi§ouůizvlutckzxjzckguvl h.gjcutfgýíáéphuoůizltukridýžáflizv bbb";
132
            dc.Content = content;
133
            dt = new DateTime(2022, 4, 15);
134
            docList.Documents.Add(new DocumentAddInfo() { Name = "e", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
135

    
136
            return docList;
137
        }
138
    }
139
}
(3-3/6)