Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build summary #2

Merged
merged 45 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
948bc38
Build Summary
Nov 11, 2024
4196995
Build Summary
Nov 11, 2024
4609662
Check 1
Nov 11, 2024
09fcd6b
Check 1
Nov 11, 2024
254fd1e
Check 1
Nov 11, 2024
066306d
Check 1
Nov 11, 2024
6ddf309
Check 1
Nov 11, 2024
e6039b6
Fixed dashboard
Nov 11, 2024
97e40db
Fixed dashboard
Nov 11, 2024
e7e8048
Fixed tests
Nov 11, 2024
7e5a413
Fixed tests
Nov 11, 2024
f0eb314
Coverage settings updated
Nov 12, 2024
0df8a23
Coverage settings test
Nov 12, 2024
d17e16e
Coverage settings test
Nov 12, 2024
8793a14
Coverage settings test
Nov 12, 2024
0a43704
Coverage settings test
Nov 12, 2024
4892140
Coverage settings test
Nov 12, 2024
e96900f
Coverage settings test
Nov 12, 2024
af527d3
Coverage settings test
Nov 12, 2024
d83c944
Coverage settings test
Nov 12, 2024
268f64d
Coverage settings test
Nov 12, 2024
929db0d
Coverage settings test
Nov 12, 2024
d6d94ab
Coverage settings test
Nov 12, 2024
b281a42
Coverage settings test
Nov 12, 2024
c8d7c19
Coverage settings test
Nov 12, 2024
6f52b30
Coverage settings test
Nov 12, 2024
74fe4c5
Coverage settings test
Nov 12, 2024
6caf57a
Coverage settings test
Nov 12, 2024
382de31
Coverage settings test
Nov 12, 2024
e83fd22
Coverage settings test
Nov 12, 2024
2491475
Coverage settings test
Nov 12, 2024
6f599a5
Coverage settings test
Nov 12, 2024
8fa81fa
Coverage settings test
Nov 12, 2024
b99fedf
Coverage settings test
Nov 12, 2024
7d14119
Coverage settings test
Nov 12, 2024
2f11d6a
Coverage settings test
Nov 12, 2024
5176104
Coverage settings test
Nov 12, 2024
cc756d7
Coverage settings test
Nov 12, 2024
f9e3a2f
Coverage settings test
Nov 12, 2024
f3ab026
Coverage settings test
Nov 12, 2024
8ba6a62
Coverage settings test
Nov 12, 2024
d27bc60
Coverage settings test
Nov 12, 2024
222fdc5
Coverage settings test
Nov 12, 2024
87d9fa9
Added xunit runner
Nov 13, 2024
b3525f0
Merge pull request #1 from samyakOO7/main
samyakOO7 Nov 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ on:

jobs:
contract-test:
runs-on: macos-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

Expand All @@ -48,11 +48,41 @@ jobs:
token: ${{ secrets.TC_CLOUD_TOKEN }}
wait: true

- name: Test
run: dotnet test --no-build --verbosity normal
- name: Test with Coverage
run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" /p:CollectCoverage=true --results-directory ./TestResults
env:
COVERLET_OUTPUT_FORMAT: cobertura

- name: Publish Code Coverage Report
uses: irongut/[email protected]
with:
filename: "TestResults/*/coverage.cobertura.xml"
badge: true
fail_below_min: false
format: markdown
hide_branch_rate: false
hide_complexity: false
indicators: true
output: file
thresholds: "60 80"

- name: Upload Code Coverage Results
uses: actions/upload-artifact@v3
with:
name: code-coverage-results
path: code-coverage-results.md


- name: Display Coverage Summary in Workflow UI
run: |
echo "Code Coverage Summary" >> $GITHUB_STEP_SUMMARY
cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY

- name: Upload HTML Test Report
uses: actions/upload-artifact@v4
with:
name: html-report
path: specmatic-order-bff-csharp.test/build/reports/specmatic/html



Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Testcontainers" Version="3.10.0" />
<PackageReference Include="TestContainers.Container.Abstractions" Version="1.5.4" />
<PackageReference Include="WireMock.Net" Version="1.6.4" />
Expand Down
75 changes: 75 additions & 0 deletions specmatic-order-bff-csharp.test/tests/OrderBffServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Moq;
using Xunit;
using specmatic_order_bff_csharp.models;
using System.Collections.Generic;
using specmatic_order_bff_csharp.backend;
using specmatic_order_bff_csharp.services;
namespace specmatic_order_bff_csharp.test.tests;
public class OrderBffServiceTests
{
private readonly OrderBffService _orderBffService;
private readonly Mock<OrderService> _mockOrderService;
private readonly Mock<HttpMessageHandler> _mockHttpMessageHandler;
public OrderBffServiceTests()
{
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
var mockHttpClient = new HttpClient(_mockHttpMessageHandler.Object);
_mockOrderService = new Mock<OrderService>(mockHttpClient) { CallBase = true };
_orderBffService = new OrderBffService(_mockOrderService.Object);

}

[Fact]
public void CreateOrder_ShouldReturnIdResponse_WhenOrderIsCreated()
{
// Arrange

var orderRequest = new OrderRequest(count:2,productid:2);
var expectedId = 123;
_mockOrderService.Setup(service => service.CreateOrder(It.IsAny<OrderRequest>())).Returns(expectedId);

// Act
var result = _orderBffService.CreateOrder(orderRequest);

// Assert
Assert.NotNull(result);
Assert.Equal(expectedId, result.Id);
}

[Fact]
public void CreateProduct_ShouldReturnIdResponse_WhenProductIsCreated()
{
// Arrange
var productRequest = new ProductRequest("iPhone", "goods", 100);
var expectedId = 456;
_mockOrderService.Setup(service => service.CreateProduct(It.IsAny<ProductRequest>())).Returns(expectedId);

// Act
var result = _orderBffService.CreateProduct(productRequest);

// Assert
Assert.NotNull(result);
Assert.Equal(expectedId, result.Id);
}

[Fact]
public void FindProducts_ShouldReturnListOfProducts_WhenProductsAreFound()
{
// Arrange
var productType = "Electronics";
var expectedProducts = new List<Product>
{
new Product(id: 1,name: "Phone", type: "Electronics", inventory: 100),
new Product(id: 2, name: "Laptop", type: "Electronics", inventory: 100)
};
_mockOrderService.Setup(service => service.FindProducts(productType)).Returns(expectedProducts);

// Act
var result = _orderBffService.FindProducts(productType);

// Assert
Assert.NotNull(result);
Assert.Equal(expectedProducts.Count, result.Count());
Assert.Equal(expectedProducts, result);
}
}
124 changes: 124 additions & 0 deletions specmatic-order-bff-csharp.test/tests/OrderServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Moq;
using Xunit;
using specmatic_order_bff_csharp.models;
using specmatic_order_bff_csharp.backend;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using Moq.Protected;
namespace specmatic_order_bff_csharp.test.tests;
public class OrderServiceTests
{
private readonly OrderService _orderService;
private readonly Mock<HttpMessageHandler> _mockHandler;
public OrderServiceTests()
{
// Mock HttpMessageHandler to simulate HttpClient behavior
_mockHandler = new Mock<HttpMessageHandler>();
// Create HttpClient using the mocked handler
var httpClient = new HttpClient(_mockHandler.Object)
{
BaseAddress = new Uri("https://dummyapi.com/") // Set the base address as needed
};

_orderService = new OrderService(httpClient); // Pass HttpClient to OrderService
}

[Fact]
public void CreateOrder_ShouldReturnOrderId_WhenOrderIsCreated()
{
// Arrange
var orderRequest = new OrderRequest(count:2,productid:2);
var expectedOrderId = 123;

// Set up the mock response for the HTTP request
var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(new IdResponse(id: expectedOrderId)))
};

// Mock SendAsync to return the mockResponse for CreateOrder
_mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(mockResponse);

// Act
var result = _orderService.CreateOrder(orderRequest);

// Assert
Assert.Equal(expectedOrderId, result);
}

[Fact]
public void CreateProduct_ShouldReturnProductId_WhenProductIsCreated()
{
// Arrange
var productRequest = new ProductRequest("iPhone", "goods", 100);
var expectedProductId = 456;

var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(new IdResponse ( id: expectedProductId )))
};

// Mock SendAsync to return the mockResponse for CreateProduct
_mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(mockResponse);

// Act
var result = _orderService.CreateProduct(productRequest);

// Assert
Assert.Equal(expectedProductId, result);
}

[Fact]
public void FindProducts_ShouldReturnListOfProducts_WhenProductsAreFound()
{
// Arrange
var productType = "Electronics";
var expectedProducts = new List<Product>
{
new Product(id: 1, name: "Phone", type: "Electronics", inventory: 100),
new Product(id: 2, name: "Laptop", type: "Electronics", inventory: 100)
};

var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(expectedProducts))
};

// Mock SendAsync to return the mockResponse for FindProducts
_mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(mockResponse);

// Act
var result = _orderService.FindProducts(productType);

// Assert
Assert.NotNull(result);
Assert.Equal(expectedProducts.Count, result.Count());
Assert.Collection(result,
item => Assert.Equal(expectedProducts[0].Id, item.Id),
item => Assert.Equal(expectedProducts[1].Id, item.Id)
// Add more checks for each item based on properties
);

}
}
2 changes: 2 additions & 0 deletions specmatic-order-bff-csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace specmatic_order_bff_csharp;
using System.Diagnostics.CodeAnalysis;

[ExcludeFromCodeCoverage]
public static class Program
{
public static void Main(string[] args)
Expand Down
8 changes: 4 additions & 4 deletions specmatic-order-bff-csharp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using specmatic_order_bff_csharp.backend;
using specmatic_order_bff_csharp.services;
using ValidationException = specmatic_order_bff_csharp.exceptions.ValidationException;

using System.Diagnostics.CodeAnalysis;
namespace specmatic_order_bff_csharp;

[ExcludeFromCodeCoverage]
public class Startup
{
public void ConfigureServices(IServiceCollection services)
Expand All @@ -19,8 +19,8 @@ public void ConfigureServices(IServiceCollection services)
})
.AddXmlSerializerFormatters();

services.AddSingleton<OrderBffService>();
services.AddSingleton<OrderService>();
services.AddScoped<OrderBffService>();
services.AddScoped<OrderService>();

services.AddHttpClient();
}
Expand Down
16 changes: 10 additions & 6 deletions specmatic-order-bff-csharp/backend/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
{
private readonly string _orderApiUrl = Environment.GetEnvironmentVariable("ORDER_API_URL") ?? string.Empty;
private const string AuthToken = "API-TOKEN-SPEC";

public int CreateOrder(OrderRequest orderRequest)
private readonly HttpClient _httpClient;
public OrderService(HttpClient httpClient = null)

Check warning on line 14 in specmatic-order-bff-csharp/backend/OrderService.cs

View workflow job for this annotation

GitHub Actions / contract-test

Cannot convert null literal to non-nullable reference type.

Check warning on line 14 in specmatic-order-bff-csharp/backend/OrderService.cs

View workflow job for this annotation

GitHub Actions / contract-test

Cannot convert null literal to non-nullable reference type.
{
_httpClient = httpClient ?? new HttpClient();
}
public virtual int CreateOrder(OrderRequest orderRequest)
{
return CreateOrderAsync(new Order(orderRequest.Productid, orderRequest.Count, "pending")).Result;
}

public int CreateProduct(ProductRequest productRequest)
public virtual int CreateProduct(ProductRequest productRequest)
{
return CreateProductAsync(productRequest).Result;
}

public IEnumerable<Product> FindProducts(string type)
public virtual IEnumerable<Product> FindProducts(string type)
{
return FindProductsAsync(type).Result;
}
Expand Down Expand Up @@ -71,8 +75,8 @@

private HttpClient GetHttpClient()
{
var client = new HttpClient();
client.BaseAddress = new Uri(_orderApiUrl);
var client = _httpClient;
client.BaseAddress = client.BaseAddress ?? new Uri(_orderApiUrl);
client.Timeout = TimeSpan.FromSeconds(4);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
Expand Down
3 changes: 2 additions & 1 deletion specmatic-order-bff-csharp/controllers/OrdersController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using specmatic_order_bff_csharp.models;
using specmatic_order_bff_csharp.services;

using System.Diagnostics.CodeAnalysis;
namespace specmatic_order_bff_csharp.controllers;

[ApiController]
[Route("[controller]")]
[ExcludeFromCodeCoverage]
public class OrdersController(OrderBffService orderBffService) : ControllerBase
{
[HttpPost]
Expand Down
3 changes: 2 additions & 1 deletion specmatic-order-bff-csharp/controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using specmatic_order_bff_csharp.models;
using specmatic_order_bff_csharp.services;
using ValidationException = specmatic_order_bff_csharp.exceptions.ValidationException;

using System.Diagnostics.CodeAnalysis;
namespace specmatic_order_bff_csharp.controllers;

[ApiController]
[Route("[controller]")]
[ExcludeFromCodeCoverage]
public class ProductsController(OrderBffService orderBffService) : ControllerBase
{
[HttpPost]
Expand Down
3 changes: 2 additions & 1 deletion specmatic-order-bff-csharp/exceptions/ValidationException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace specmatic_order_bff_csharp.exceptions;

using System.Diagnostics.CodeAnalysis;
[ExcludeFromCodeCoverage]
public class ValidationException(string error)
{
public string Error { get; init; } = error;
Expand Down
2 changes: 2 additions & 0 deletions specmatic-order-bff-csharp/models/Api.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace specmatic_order_bff_csharp.models;
using System.Diagnostics.CodeAnalysis;

[ExcludeFromCodeCoverage]
public class Api
{
public HttpMethod Method { get; }
Expand Down
3 changes: 2 additions & 1 deletion specmatic-order-bff-csharp/models/IdResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Text.Json.Serialization;

using System.Diagnostics.CodeAnalysis;
namespace specmatic_order_bff_csharp.models;

[ExcludeFromCodeCoverage]
public class IdResponse(int id)
{
[JsonPropertyName("id")]public int Id { get; init; } = id;
Expand Down
2 changes: 2 additions & 0 deletions specmatic-order-bff-csharp/models/Order.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace specmatic_order_bff_csharp.models;
using System.Diagnostics.CodeAnalysis;

[ExcludeFromCodeCoverage]
public class Order( int productid, int count, string status)
{
public int Productid { get; init; } = productid;
Expand Down
Loading
Loading