示例#1
0
        public async Task <FullStubModel> AddStubAsync(StubModel stub)
        {
            if (string.IsNullOrWhiteSpace(stub.Id))
            {
                // If no ID is sent, create one here.
                var id = HashingUtilities.GetMd5String(JsonConvert.SerializeObject(stub));
                stub.Id = $"stub-{id}";
            }

            // Check that a stub with the new ID isn't already added to a readonly stub source.
            var stubs = await GetStubsAsync(true);

            if (stubs.Any(s => string.Equals(stub.Id, s.Stub.Id, StringComparison.OrdinalIgnoreCase)))
            {
                throw new ConflictException($"Stub with ID '{stub.Id}'.");
            }

            var source = GetWritableStubSource();
            await source.AddStubAsync(stub);

            return(new FullStubModel {
                Stub = stub, Metadata = new StubMetadataModel {
                    ReadOnly = false
                }
            });
        }
    /// <summary>
    /// Sets an autogenerated ID on a stub if no ID has been set yet.
    /// </summary>
    /// <param name="stub">The stub.</param>
    /// <returns>The stub ID.</returns>
    public static string EnsureStubId(this StubModel stub)
    {
        if (!string.IsNullOrWhiteSpace(stub.Id))
        {
            return(stub.Id);
        }

        var id = $"generated-{HashingUtilities.GetMd5String(JsonConvert.SerializeObject(stub))}";

        stub.Id = id;
        return(id);
    }
    public void GetMd5String_HappyFlow()
    {
        // Arrange
        var input          = "test 123";
        var expectedOutput = "39d0d586a701e199389d954f2d592720";

        // Act
        var result = HashingUtilities.GetMd5String(input);

        // Assert
        Assert.AreEqual(expectedOutput, result);
    }
        private static void EnsureStubsHaveId(IEnumerable <StubModel> stubs)
        {
            foreach (var stub in stubs)
            {
                if (!string.IsNullOrWhiteSpace(stub.Id))
                {
                    continue;
                }

                // If no ID is set, calculate a unique ID based on the stub contents.
                var contents = JsonConvert.SerializeObject(stub);
                stub.Id = HashingUtilities.GetMd5String(contents);
            }
        }
        public async Task <FullStubModel> GenerateStubBasedOnRequestAsync(string requestCorrelationId)
        {
            _logger.LogInformation($"Creating stub based on request with corr.ID '{requestCorrelationId}'.");

            // TODO lateron, when the querying is fixed, only query for one request result.
            var requestResults = await _stubContext.GetRequestResultsAsync();

            var requestResult = requestResults.FirstOrDefault(r => r.CorrelationId == requestCorrelationId);

            if (requestResult == null)
            {
                throw new NotFoundException(nameof(RequestResultModel), requestCorrelationId);
            }

            var stub = new StubModel();

            foreach (var handler in _handlers.OrderByDescending(w => w.Priority))
            {
                var executed = await handler.HandleStubGenerationAsync(requestResult, stub);

                _logger.LogInformation($"Handler '{handler.GetType().Name}'" + (executed ? " executed" : "") + ".");
            }

            // Set a default response
            stub.Response.Text = "OK!";

            // Generate an ID based on the created stub.
            var contents = JsonConvert.SerializeObject(stub);

            stub.Id = "generated-" + HashingUtilities.GetMd5String(contents);
            await _stubContext.DeleteStubAsync(stub.Id);

            var result = await _stubContext.AddStubAsync(stub);

            _logger.LogInformation($"Stub with ID '{stub.Id}' generated!");

            return(result);
        }