We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Wednesday, February 12, 2025

Routing in Angular 7

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;

[TestClass]
public class LogExceptionToDatabaseMiddlewareTests
{
    private Mock<IExceptionTranslator> _mockExceptionTranslator;
    private Mock<IEliLogHelper> _mockEliLogHelper;
    private LogExceptionToDatabaseMiddleware _middleware;

    [TestInitialize]
    public void Setup()
    {
        _mockExceptionTranslator = new Mock<IExceptionTranslator>();
        _mockEliLogHelper = new Mock<IEliLogHelper>();
        _middleware = new LogExceptionToDatabaseMiddleware(_mockExceptionTranslator.Object, _mockEliLogHelper.Object);
    }

    [TestMethod]
    public void GenerateResponse_ShouldReturnErrorResponse_WhenGetMultipleErrorsHasErrors()
    {
        // Arrange
        var ex = new Exception("Test Exception");
        Guid clientCorrelationId = Guid.NewGuid();
        int statusCode;

        var errors = new List<Error>
        {
            new Error { Title = "Error 1" },
            new Error { Title = "Error 2" }
        };

        // Mock GetMultipleErrors to return errors
        var middlewareMock = new Mock<LogExceptionToDatabaseMiddleware>(_mockExceptionTranslator.Object, _mockEliLogHelper.Object);
        middlewareMock.Setup(m => m.GetMultipleErrors(It.IsAny<Exception>(), out statusCode))
                      .Returns(errors);

        // Act
        var result = middlewareMock.Object.GenerateResponse(ex, clientCorrelationId, out statusCode);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(2, result.Errors.Count);
        Assert.AreEqual(clientCorrelationId, result.Errors[0].Id);
    }

    [TestMethod]
    public void GenerateResponse_ShouldReturnTranslatedError_WhenGetMultipleErrorsIsEmpty()
    {
        // Arrange
        var ex = new Exception("Test Exception");
        Guid clientCorrelationId = Guid.NewGuid();
        int statusCode;
        var translatedError = new ErrorTranslation
        {
            HttpStatusCode = 400,
            Message = "Translated Error",
            ErrorCode = "ERR001",
            Detail = "Translated Error Detail",
            APMId = "APM123",
            EndPointName = "SomeService",
            Source = "API"
        };

        _mockExceptionTranslator.Setup(t => t.Translate(It.IsAny<Exception>()))
                                .Returns(translatedError);

        var middlewareMock = new Mock<LogExceptionToDatabaseMiddleware>(_mockExceptionTranslator.Object, _mockEliLogHelper.Object);
        middlewareMock.Setup(m => m.GetMultipleErrors(It.IsAny<Exception>(), out statusCode))
                      .Returns(new List<Error>()); // Return empty list

        // Act
        var result = middlewareMock.Object.GenerateResponse(ex, clientCorrelationId, out statusCode);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(1, result.Errors.Count);
        Assert.AreEqual("Translated Error", result.Errors[0].Title);
        Assert.AreEqual(400, statusCode);
    }

    [TestMethod]
    public void GenerateResponse_ShouldReturnDefaultError_WhenTranslationFails()
    {
        // Arrange
        var ex = new Exception("Test Exception");
        Guid clientCorrelationId = Guid.NewGuid();
        int statusCode;

        _mockExceptionTranslator.Setup(t => t.Translate(It.IsAny<Exception>()))
                                .Returns((ErrorTranslation)null);

        var middlewareMock = new Mock<LogExceptionToDatabaseMiddleware>(_mockExceptionTranslator.Object, _mockEliLogHelper.Object);
        middlewareMock.Setup(m => m.GetMultipleErrors(It.IsAny<Exception>(), out statusCode))
                      .Returns(new List<Error>()); // Return empty list

        middlewareMock.Setup(m => m.GetDefaultErrorResponse(It.IsAny<Guid>(), It.IsAny<string>()))
                      .Returns(new ErrorResponse { Errors = new List<Error> { new Error { Title = "Default Error" } } });

        // Act
        var result = middlewareMock.Object.GenerateResponse(ex, clientCorrelationId, out statusCode);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(1, result.Errors.Count);
        Assert.AreEqual("Default Error", result.Errors[0].Title);
        Assert.AreEqual(500, statusCode);
    }

    [TestMethod]
    public void GenerateResponse_ShouldHandleException_AndLogError()
    {
        // Arrange
        var ex = new Exception("Outer Exception");
        Guid clientCorrelationId = Guid.NewGuid();
        int statusCode;

        var middlewareMock = new Mock<LogExceptionToDatabaseMiddleware>(_mockExceptionTranslator.Object, _mockEliLogHelper.Object);
        middlewareMock.Setup(m => m.GetMultipleErrors(It.IsAny<Exception>(), out statusCode))
                      .Throws(new Exception("Inner Exception"));

        middlewareMock.Setup(m => m.GetDefaultErrorResponse(It.IsAny<Guid>(), It.IsAny<string>()))
                      .Returns(new ErrorResponse { Errors = new List<Error> { new Error { Title = "Exception Handled" } } });

        // Act
        var result = middlewareMock.Object.GenerateResponse(ex, clientCorrelationId, out statusCode);

        // Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(1, result.Errors.Count);
        Assert.AreEqual("Exception Handled", result.Errors[0].Title);
        Assert.AreEqual(500, statusCode);

        // Verify LogError was called
        _mockEliLogHelper.Verify(l => l.LogError(It.IsAny<Exception>()), Times.Once);
    }
}

public class ErrorTranslation { public int HttpStatusCode { get; set; } // Example: 400, 500, etc. public string Message { get; set; } // Example: "Bad Request" public string ErrorCode { get; set; } // Example: "ERR001" public string Detail { get; set; } // More descriptive details about the error public bool UseExceptionMessage { get; set; } // Whether to use `ex.Message` or `Detail` // Additional metadata (from your method) public string APMId { get; set; } public string EndPointName { get; set; } public string Source { get; set; } }

No comments: