|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Cli.Tests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests for Export Command in CLI. |
| 8 | +/// </summary> |
| 9 | +[TestClass] |
| 10 | +public class ExporterTests |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Tests the ExportGraphQLFromDabService method to ensure it logs correctly when the HTTPS endpoint works. |
| 14 | + /// </summary> |
| 15 | + [TestMethod] |
| 16 | + public void ExportGraphQLFromDabService_LogsWhenHttpsWorks() |
| 17 | + { |
| 18 | + // Arrange |
| 19 | + Mock<ILogger> mockLogger = new(); |
| 20 | + Mock<Exporter> mockExporter = new(); |
| 21 | + RuntimeConfig runtimeConfig = new( |
| 22 | + Schema: "schema", |
| 23 | + DataSource: new DataSource(DatabaseType.MSSQL, "", new()), |
| 24 | + Runtime: new(Rest: new(), GraphQL: new(), Host: new(null, null)), |
| 25 | + Entities: new(new Dictionary<string, Entity>()) |
| 26 | + ); |
| 27 | + |
| 28 | + // Setup the mock to return a schema when the HTTPS endpoint is used |
| 29 | + mockExporter.Setup(e => e.GetGraphQLSchema(runtimeConfig, false)) |
| 30 | + .Returns("schema from HTTPS endpoint"); |
| 31 | + |
| 32 | + // Act |
| 33 | + string result = mockExporter.Object.ExportGraphQLFromDabService(runtimeConfig, mockLogger.Object); |
| 34 | + |
| 35 | + // Assert |
| 36 | + Assert.AreEqual("schema from HTTPS endpoint", result); |
| 37 | + mockLogger.Verify(logger => logger.Log( |
| 38 | + LogLevel.Information, |
| 39 | + It.IsAny<EventId>(), |
| 40 | + It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("schema from HTTPS endpoint.")), |
| 41 | + It.IsAny<Exception>(), |
| 42 | + It.IsAny<Func<It.IsAnyType, Exception, string>>()!), Times.Never); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Tests the ExportGraphQLFromDabService method to ensure it logs correctly when the HTTPS endpoint fails and falls back to the HTTP endpoint. |
| 47 | + /// This test verifies that: |
| 48 | + /// 1. The method attempts to fetch the schema using the HTTPS endpoint first. |
| 49 | + /// 2. If the HTTPS endpoint fails, it logs the failure and attempts to fetch the schema using the HTTP endpoint. |
| 50 | + /// 3. The method logs the appropriate messages during the process. |
| 51 | + /// 4. The method returns the schema fetched from the HTTP endpoint when the HTTPS endpoint fails. |
| 52 | + /// </summary> |
| 53 | + [TestMethod] |
| 54 | + public void ExportGraphQLFromDabService_LogsFallbackToHttp_WhenHttpsFails() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + Mock<ILogger> mockLogger = new(); |
| 58 | + Mock<Exporter> mockExporter = new(); |
| 59 | + RuntimeConfig runtimeConfig = new( |
| 60 | + Schema: "schema", |
| 61 | + DataSource: new DataSource(DatabaseType.MSSQL, "", new()), |
| 62 | + Runtime: new(Rest: new(), GraphQL: new(), Host: new(null, null)), |
| 63 | + Entities: new(new Dictionary<string, Entity>()) |
| 64 | + ); |
| 65 | + |
| 66 | + mockExporter.Setup(e => e.GetGraphQLSchema(runtimeConfig, false)) |
| 67 | + .Throws(new Exception("HTTPS endpoint failed")); |
| 68 | + mockExporter.Setup(e => e.GetGraphQLSchema(runtimeConfig, true)) |
| 69 | + .Returns("Fallback schema"); |
| 70 | + |
| 71 | + // Act |
| 72 | + string result = mockExporter.Object.ExportGraphQLFromDabService(runtimeConfig, mockLogger.Object); |
| 73 | + |
| 74 | + // Assert |
| 75 | + Assert.AreEqual("Fallback schema", result); |
| 76 | + mockLogger.Verify(logger => logger.Log( |
| 77 | + LogLevel.Information, |
| 78 | + It.IsAny<EventId>(), |
| 79 | + It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Trying to fetch schema from DAB Service using HTTPS endpoint.")), |
| 80 | + It.IsAny<Exception>(), |
| 81 | + It.IsAny<Func<It.IsAnyType, Exception, string>>()!), Times.Once); |
| 82 | + |
| 83 | + mockLogger.Verify(logger => logger.Log( |
| 84 | + LogLevel.Information, |
| 85 | + It.IsAny<EventId>(), |
| 86 | + It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Failed to fetch schema from DAB Service using HTTPS endpoint. Trying with HTTP endpoint.")), |
| 87 | + It.IsAny<Exception>(), |
| 88 | + It.IsAny<Func<It.IsAnyType, Exception, string>>()!), Times.Once); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Tests the ExportGraphQLFromDabService method to ensure it throws an exception when both the HTTPS and HTTP endpoints fail. |
| 93 | + /// This test verifies that: |
| 94 | + /// 1. The method attempts to fetch the schema using the HTTPS endpoint first. |
| 95 | + /// 2. If the HTTPS endpoint fails, it logs the failure and attempts to fetch the schema using the HTTP endpoint. |
| 96 | + /// 3. If both endpoints fail, the method throws an exception. |
| 97 | + /// 4. The method logs the appropriate messages during the process. |
| 98 | + /// </summary> |
| 99 | + [TestMethod] |
| 100 | + public void ExportGraphQLFromDabService_ThrowsException_WhenBothHttpsAndHttpFail() |
| 101 | + { |
| 102 | + // Arrange |
| 103 | + Mock<ILogger> mockLogger = new(); |
| 104 | + Mock<Exporter> mockExporter = new() { CallBase = true }; |
| 105 | + RuntimeConfig runtimeConfig = new( |
| 106 | + Schema: "schema", |
| 107 | + DataSource: new DataSource(DatabaseType.MSSQL, "", new()), |
| 108 | + Runtime: new(Rest: new(), GraphQL: new(), Host: new(null, null)), |
| 109 | + Entities: new(new Dictionary<string, Entity>()) |
| 110 | + ); |
| 111 | + |
| 112 | + // Setup the mock to throw an exception when the HTTPS endpoint is used |
| 113 | + mockExporter.Setup(e => e.GetGraphQLSchema(runtimeConfig, false)) |
| 114 | + .Throws(new Exception("HTTPS endpoint failed")); |
| 115 | + |
| 116 | + // Setup the mock to throw an exception when the HTTP endpoint is used |
| 117 | + mockExporter.Setup(e => e.GetGraphQLSchema(runtimeConfig, true)) |
| 118 | + .Throws(new Exception("Both HTTP and HTTPS endpoint failed")); |
| 119 | + |
| 120 | + // Act & Assert |
| 121 | + // Verify that the method throws an exception when both endpoints fail |
| 122 | + Exception exception = Assert.ThrowsException<Exception>(() => |
| 123 | + mockExporter.Object.ExportGraphQLFromDabService(runtimeConfig, mockLogger.Object)); |
| 124 | + |
| 125 | + Assert.AreEqual("Both HTTP and HTTPS endpoint failed", exception.Message); |
| 126 | + |
| 127 | + // Verify that the correct log message is generated when attempting to use the HTTPS endpoint |
| 128 | + mockLogger.Verify(logger => logger.Log( |
| 129 | + LogLevel.Information, |
| 130 | + It.IsAny<EventId>(), |
| 131 | + It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Trying to fetch schema from DAB Service using HTTPS endpoint.")), |
| 132 | + It.IsAny<Exception>(), |
| 133 | + It.IsAny<Func<It.IsAnyType, Exception, string>>()!), Times.Once); |
| 134 | + |
| 135 | + // Verify that the correct log message is generated when falling back to the HTTP endpoint |
| 136 | + mockLogger.Verify(logger => logger.Log( |
| 137 | + LogLevel.Information, |
| 138 | + It.IsAny<EventId>(), |
| 139 | + It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Failed to fetch schema from DAB Service using HTTPS endpoint. Trying with HTTP endpoint.")), |
| 140 | + It.IsAny<Exception>(), |
| 141 | + It.IsAny<Func<It.IsAnyType, Exception, string>>()!), Times.Once); |
| 142 | + } |
| 143 | +} |
0 commit comments