public void EnsureStubId_StubIdNotSet_ShouldSetStubId()
    {
        // Arrange
        var stub = new StubModel
        {
            Id         = null,
            Conditions = new StubConditionsModel
            {
                Url = new StubUrlConditionModel
                {
                    Path = "/path"
                }
            },
            Response = new StubResponseModel
            {
                Text = "OK!!"
            }
        };

        // Act
        var result = stub.EnsureStubId();

        // Assert
        Assert.AreEqual(stub.Id, result);
        Assert.AreEqual("generated-0636a401deba2996db19d55558faf594", result);
    }
예제 #2
0
    /// <inheritdoc />
    public async Task <IEnumerable <FullStubModel> > GenerateCurlStubsAsync(string input, bool doNotCreateStub,
                                                                            string tenant)
    {
        _logger.LogDebug($"Creating stubs based on cURL command {input}.");
        var requests = _curlToHttpRequestMapper.MapCurlCommandsToHttpRequest(input);
        var results  = new List <FullStubModel>();

        foreach (var request in requests)
        {
            var conditions = await _httpRequestToConditionsService.ConvertToConditionsAsync(request);

            var stub = new StubModel
            {
                Tenant      = tenant,
                Description = $"{conditions.Method} request to path {conditions.Url?.Path}",
                Conditions  = conditions,
                Response    = { Text = "OK!" }
            };

            // Generate an ID based on the created stub.
            stub.EnsureStubId();
            results.Add(await CreateStub(doNotCreateStub, stub));
        }

        return(results);
    }
    /// <inheritdoc />
    public async Task <StubModel> ConvertToStubAsync(OpenApiServer server, OpenApiLine line, string tenant)
    {
        var request = new HttpRequestModel
        {
            Body    = _openApiDataFiller.BuildRequestBody(line.Operation),
            Headers = _openApiDataFiller.BuildRequestHeaders(line.Operation),
            Method  = line.OperationType.ToString().ToUpper(),
            Url     = $"{_openApiDataFiller.BuildServerUrl(server)}{_openApiDataFiller.BuildRelativeRequestPath(line.Operation, line.PathKey)}"
        };
        var response = new HttpResponseModel
        {
            Content    = _openApiDataFiller.BuildResponseBody(line.Response),
            Headers    = _openApiDataFiller.BuildResponseHeaders(line.Response),
            StatusCode = _openApiDataFiller.BuildHttpStatusCode(line.ResponseKey)
        };
        var stub = new StubModel
        {
            Tenant      = tenant,
            Description = line.Operation.Summary,
            Conditions  = await _httpRequestToConditionsService.ConvertToConditionsAsync(request),
            Response    = await _httpResponseToStubResponseService.ConvertToResponseAsync(response),
        };

        stub.EnsureStubId();
        return(stub);
    }
    public void EnsureStubId_StubIdAlreadySet_ShouldNotSetItAgain()
    {
        // Arrange
        var stub = new StubModel {
            Id = "stub1"
        };

        // Act
        var result = stub.EnsureStubId();

        // Assert
        Assert.AreEqual(stub.Id, result);
        Assert.AreEqual("stub1", result);
    }
예제 #5
0
    /// <inheritdoc />
    public async Task <FullStubModel> GenerateStubBasedOnRequestAsync(
        string requestCorrelationId,
        bool doNotCreateStub)
    {
        _logger.LogDebug($"Creating stub based on request with corr.ID '{requestCorrelationId}'.");
        var requestResult = await _stubContext.GetRequestResultAsync(requestCorrelationId);

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

        var request = _mapper.Map <HttpRequestModel>(requestResult.RequestParameters);
        var stub    = new StubModel
        {
            Conditions = await _httpRequestToConditionsService.ConvertToConditionsAsync(request),
            Response   =
            {
                Text = "OK!"
            }
        };

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

        stub.EnsureStubId();

        FullStubModel result;

        if (doNotCreateStub)
        {
            result = new FullStubModel {
                Stub = stub, Metadata = new StubMetadataModel()
            };
        }
        else
        {
            await _stubContext.DeleteStubAsync(stub.Id);

            result = await _stubContext.AddStubAsync(stub);
        }

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

        return(result);
    }
    private async Task <StubModel> MapStub(HttpRequestModel req, HttpResponseModel res, string tenant)
    {
        var conditions = await _httpRequestToConditionsService.ConvertToConditionsAsync(req);

        var response = await _httpResponseToStubResponseService.ConvertToResponseAsync(res);

        var stub = new StubModel
        {
            Tenant      = tenant,
            Description = $"{conditions.Method} request to path {conditions.Url?.Path}",
            Conditions  = conditions,
            Response    = response
        };

        stub.EnsureStubId();
        return(stub);
    }