<commit_before>﻿using System;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using ApiClientShared;
using Digipost.Api.Client.Action;
using Digipost.Api.Client.Api;
using Digipost.Api.Client.Domain;
using Digipost.Api.Client.Domain.Enums;
using Digipost.Api.Client.Handlers;
using Digipost.Api.Client.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Digipost.Api.Client.Tests.Integration
{
    [TestClass]
    public class DigipostApiTests
    {
        private static ResourceUtility _resourceUtility;
        private static ClientConfig _clientConfig;
        private static string _uri;
        private static X509Certificate2 _certificate;

        [ClassInitialize]
        public static void Init(TestContext context)
        {
            _resourceUtility = new ResourceUtility("Digipost.Api.Client.Tests.Resources");
            _clientConfig = new ClientConfig("1337");
            _uri = "identification";
            _certificate = TestProperties.Certificate();

        }

        /// <summary>
        /// This integration test assures that the connection between handlers is correct and that a message is built and sent. 
        /// The ActionFactory is mocked to prevent actual HTTP-request to Digipost. 
        /// </summary>
        [TestMethod]
        public void DigipostApi_SendMessageRequest_Integration()
        {
            var message = new Message(
                new Recipient(IdentificationChoice.PersonalidentificationNumber, "00000000000"),
                new Document("Integrasjonstjest", "txt", _resourceUtility.ReadAllBytes(true,"Vedlegg.txt"))
                );

            try
            {
                var fakehandler = new FakeMessageResponseHandler();
                var loggingHandler = new LoggingHandler(fakehandler);
                var authenticationHandler = new AuthenticationHandler(_clientConfig, _certificate, _uri, loggingHandler);


                //Setup - init mock of ActionFactory to inject fake identification response handler
                var mockFacktory = new Mock<DigipostActionFactory>();
                mockFacktory.Setup(
                    f => f.CreateClass(typeof(Message), It.IsAny<ClientConfig>(), It.IsAny<X509Certificate2>(), It.IsAny<string>()))
                    .Returns(new MessageAction(_clientConfig, _certificate, _uri)
                    {
                        HttpClient = new HttpClient(authenticationHandler) { BaseAddress = new Uri("http://tull") }
                    });

                var dpApi = new DigipostApi(_clientConfig, _certificate) { DigipostActionFactory = mockFacktory.Object };

                dpApi.SendMessage(message);
            }
            catch
            {
                Assert.Fail();
            }
        }
        
        /// <summary>
        /// This integration test assures that the connection between handlers is correct and that a message is built and sent. 
        /// The ActionFactory is mocked to prevent actual HTTP-request to Digipost. 
        /// </summary>
        [TestMethod]
        public void DigipostApi_SendIdentifyRequest_Integration()
        {
            var identification = new Identification(IdentificationChoice.PersonalidentificationNumber, "00000000000");

            try
            {
                var fakehandler = new FakeIdentificationResponseHandler();
                var loggingHandler = new LoggingHandler(fakehandler);
                var authenticationHandler = new AuthenticationHandler(_clientConfig, _certificate, _uri, loggingHandler);
                

                //Setup - init mock of ActionFactory to inject fake identification response handler
                var mockFacktory = new Mock<DigipostActionFactory>();
                mockFacktory.Setup(
                    f => f.CreateClass(typeof(Identification),It.IsAny<ClientConfig>(),It.IsAny<X509Certificate2>(),It.IsAny<string>()))
                    .Returns(new IdentificationAction(_clientConfig, _certificate, _uri)
                    {
                        HttpClient = new HttpClient(authenticationHandler) { BaseAddress = new Uri("http://tull") }
                    });

                var dpApi = new DigipostApi(_clientConfig, _certificate) {DigipostActionFactory = mockFacktory.Object};

                dpApi.Identify(identification);
            }
            catch
            {
                Assert.Fail();
            }

        }


    }
}<commit_msg>Ryddet i ApiIntegrasjons-tester til nytt patterm<commit_after>﻿using System;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using ApiClientShared;
using Digipost.Api.Client.Action;
using Digipost.Api.Client.Api;
using Digipost.Api.Client.Domain;
using Digipost.Api.Client.Domain.Enums;
using Digipost.Api.Client.Handlers;
using Digipost.Api.Client.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Digipost.Api.Client.Tests.Integration
{
    [TestClass]
    public class DigipostApiIntegrationTests
    {
        protected ResourceUtility ResourceUtility;
        protected ClientConfig ClientConfig;
        protected string Uri;
        protected X509Certificate2 Certificate;

        [TestInitialize]
        public void Init()
        {
            ResourceUtility = new ResourceUtility("Digipost.Api.Client.Tests.Resources");
            ClientConfig = new ClientConfig("1337");
            Uri = "identification";
            Certificate = TestProperties.Certificate();
        }

        [TestClass]
        public class SendMessageMethod : DigipostApiIntegrationTests
        {
            /// <summary>
            /// This integration test assures that the connection between handlers is correct and that a message is built and sent. 
            /// The ActionFactory is mocked to prevent actual HTTP-request to Digipost. 
            /// </summary>
            [TestMethod]
            public void ProperRequestSent()
            {
                var message = new Message(
                    new Recipient(IdentificationChoice.PersonalidentificationNumber, "00000000000"),
                    new Document("Integrasjonstjest", "txt", ResourceUtility.ReadAllBytes(true, "Vedlegg.txt"))
                    );

                try
                {
                    var fakehandler = new FakeMessageResponseHandler();
                    var loggingHandler = new LoggingHandler(fakehandler);
                    var authenticationHandler = new AuthenticationHandler(ClientConfig, Certificate, Uri, loggingHandler);


                    //Setup - init mock of ActionFactory to inject fake identification response handler
                    var mockFacktory = new Mock<DigipostActionFactory>();
                    mockFacktory.Setup(
                        f => f.CreateClass(typeof(Message), It.IsAny<ClientConfig>(), It.IsAny<X509Certificate2>(), It.IsAny<string>()))
                        .Returns(new MessageAction(ClientConfig, Certificate, Uri)
                        {
                            HttpClient = new HttpClient(authenticationHandler) { BaseAddress = new Uri("http://tull") }
                        });

                    var dpApi = new DigipostApi(ClientConfig, Certificate) { DigipostActionFactory = mockFacktory.Object };

                    dpApi.SendMessage(message);
                }
                catch
                {
                    Assert.Fail();
                }
            }    
        }

        [TestClass]
        public class SendIdentifyMethod : DigipostApiIntegrationTests
        {
            /// <summary>
            /// This integration test assures that the connection between handlers is correct and that a message is built and sent. 
            /// The ActionFactory is mocked to prevent actual HTTP-request to Digipost. 
            /// </summary>
            [TestMethod]
            public void ProperRequestSent()
            {
                var identification = new Identification(IdentificationChoice.PersonalidentificationNumber, "00000000000");

                try
                {
                    var fakehandler = new FakeIdentificationResponseHandler();
                    var loggingHandler = new LoggingHandler(fakehandler);
                    var authenticationHandler = new AuthenticationHandler(ClientConfig, Certificate, Uri, loggingHandler);


                    //Setup - init mock of ActionFactory to inject fake identification response handler
                    var mockFacktory = new Mock<DigipostActionFactory>();
                    mockFacktory.Setup(
                        f => f.CreateClass(typeof(Identification), It.IsAny<ClientConfig>(), It.IsAny<X509Certificate2>(), It.IsAny<string>()))
                        .Returns(new IdentificationAction(ClientConfig, Certificate, Uri)
                        {
                            HttpClient = new HttpClient(authenticationHandler) { BaseAddress = new Uri("http://tull") }
                        });

                    var dpApi = new DigipostApi(ClientConfig, Certificate) { DigipostActionFactory = mockFacktory.Object };

                    dpApi.Identify(identification);
                }
                catch
                {
                    Assert.Fail();
                }

            }
        }
       


    }
}
{'fraction_non_alphanumeric': 0.07939891292763508, 'fraction_numerical': 0.008739209208142385, 'mean_word_length': 1.941692789968652, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226830', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2167, 'n_words': 531}