<commit_before>﻿using Nats.Services.Core;
using NFluent;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Xunit;

namespace Nats.Services.Tests
{
    public class NatsServiceSerializerTests
    {
        NatsServiceSerializer<DummyService> serializer;
        List<KeyValuePair<string, object>> args;
        AnotherDummyClass anotherArg;

        public interface DummyService
        {
            void Log(List<DummyClass> obj);
            AnotherDummyClass Create(double x, int n);
        }

        public NatsServiceSerializerTests()
        {
            serializer = new NatsServiceSerializer<DummyService>();
            args = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("dummy1", new DummyClass { Name = "aaaaa", Id = 11 }),
                new KeyValuePair<string, object>("dummy2", new DummyClass { Name = "bbbbb", Id = 22 })
            };

            anotherArg = new AnotherDummyClass { X = 1.25, N = 11 };
        }


        [Fact]
        public void SerializeMethodArgumentsTest()
        {
            byte[] payload = serializer.SerializeMethodArguments(args);
            string json = serializer.ToString(payload);
            string jsonExpected = "[{\"key\":\"dummy1\",\"value\":{\"__type\":\"NatsServiceSerializerTests.DummyClass:#Nats.Services.Tests\",\"Id\":11,\"Name\":\"aaaaa\"}},{\"key\":\"dummy2\",\"value\":{\"__type\":\"NatsServiceSerializerTests.DummyClass:#Nats.Services.Tests\",\"Id\":22,\"Name\":\"bbbbb\"}}]";
            Check.That(json).IsEqualTo(jsonExpected);

            var deserializedArgs = serializer.DeserializeMethodArguments(payload);
            Check.That(deserializedArgs.Count).IsEqualTo(2);
            Check.That(deserializedArgs.Count).IsEqualTo(args.Count);
            Check.That(deserializedArgs[0]).IsEqualTo(args[0]);
            Check.That(deserializedArgs[1]).IsEqualTo(args[1]);
        }

        [Fact]
        public void SerializeMethodArgumentsExceptionTest()
        {
            var serializer = new NatsServiceSerializer<DummyService>();

            var args = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("dummy1", anotherArg)
            };

            Check.ThatCode(() => serializer.SerializeMethodArguments(args)).Throws<SerializationException>(); ;
        }

        [Fact]
        public void SerializeReturnObjectTest()
        {
            byte[] payload = serializer.SerializeReturnObject(anotherArg);
            string json = serializer.ToString(payload);
            string jsonExpected = "{\"N\":11,\"X\":1.25}";
            Check.That(json).IsEqualTo(jsonExpected);

            var deserializedObj = serializer.DeserializeReturnObject(anotherArg.GetType(), payload);
            Check.That(deserializedObj).IsEqualTo(anotherArg);
        }
<commit_msg>Fix interface name to respect convention<commit_after>﻿using Nats.Services.Core;
using NFluent;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Xunit;

namespace Nats.Services.Tests
{
    public class NatsServiceSerializerTests
    {
        NatsServiceSerializer<IDummyService> serializer;
        List<KeyValuePair<string, object>> args;
        AnotherDummyClass anotherArg;

        public interface IDummyService
        {
            void Log(List<DummyClass> obj);
            AnotherDummyClass Create(double x, int n);
        }

        public NatsServiceSerializerTests()
        {
            serializer = new NatsServiceSerializer<IDummyService>();
            args = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("dummy1", new DummyClass { Name = "aaaaa", Id = 11 }),
                new KeyValuePair<string, object>("dummy2", new DummyClass { Name = "bbbbb", Id = 22 })
            };

            anotherArg = new AnotherDummyClass { X = 1.25, N = 11 };
        }


        [Fact]
        public void SerializeMethodArgumentsTest()
        {
            byte[] payload = serializer.SerializeMethodArguments(args);
            string json = serializer.ToString(payload);
            string jsonExpected = "[{\"key\":\"dummy1\",\"value\":{\"__type\":\"NatsServiceSerializerTests.DummyClass:#Nats.Services.Tests\",\"Id\":11,\"Name\":\"aaaaa\"}},{\"key\":\"dummy2\",\"value\":{\"__type\":\"NatsServiceSerializerTests.DummyClass:#Nats.Services.Tests\",\"Id\":22,\"Name\":\"bbbbb\"}}]";
            Check.That(json).IsEqualTo(jsonExpected);

            var deserializedArgs = serializer.DeserializeMethodArguments(payload);
            Check.That(deserializedArgs.Count).IsEqualTo(2);
            Check.That(deserializedArgs.Count).IsEqualTo(args.Count);
            Check.That(deserializedArgs[0]).IsEqualTo(args[0]);
            Check.That(deserializedArgs[1]).IsEqualTo(args[1]);
        }

        [Fact]
        public void SerializeMethodArgumentsExceptionTest()
        {
            var serializer = new NatsServiceSerializer<IDummyService>();

            var args = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("dummy1", anotherArg)
            };

            Check.ThatCode(() => serializer.SerializeMethodArguments(args)).Throws<SerializationException>(); ;
        }

        [Fact]
        public void SerializeReturnObjectTest()
        {
            byte[] payload = serializer.SerializeReturnObject(anotherArg);
            string json = serializer.ToString(payload);
            string jsonExpected = "{\"N\":11,\"X\":1.25}";
            Check.That(json).IsEqualTo(jsonExpected);

            var deserializedObj = serializer.DeserializeReturnObject(anotherArg.GetType(), payload);
            Check.That(deserializedObj).IsEqualTo(anotherArg);
        }

{'fraction_non_alphanumeric': 0.12957552844131295, 'fraction_numerical': 0.009623646674686372, 'mean_word_length': 2.5881627620221948, 'pattern_counts': {'":': 24, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '819107', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1563, 'n_words': 257}