Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 28 additions & 18 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
name: ASP.NET Core 8 API CI/CD

on:
push:
branches: [main]
pull_request:
branches: [main]
branches: [main]

jobs:
build: # Fisrt we we will setup only build job
runs-on: ubuntu-latest # <-- OS to run the job
env: # Environment variables
SOLUTION_PATH: SimpleAPI/SimpleAPI.sln #// Path to the solution file
build:
runs-on: ubuntu-latest
env:
SOLUTION_PATH: SimpleAPI/SimpleAPI.sln
TEST_PROJECT_PATH: SimpleAPI.Test/SimpleAPI.Test.csproj

steps:
- name: Checkout-code # Step 1: Checkout the code from repository
uses: actions/checkout@v3 # This is a predefined action in GutHib to checkout code

- name: Setup .NET 8 SDK # Step 2: we will setup .net 8 sdk for build
uses: actions/setup-dotnet@v3 # Predefined action to setup dotnet
with: #<-- provide parameters to the action
dotnet-version: '8.0.x' # Specify the .NET version to install
- name: Cache NuGet Packages # Step 2.1: Cache NuGet packages to speed up builds
# Step 1: Checkout the code from repository
- name: Checkout code
uses: actions/checkout@v3

# Step 2: Setup .NET 8 SDK
- name: Setup .NET 8 SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'

# Step 2.1: Cache NuGet packages
- name: Cache NuGet Packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-

- name: Restore dependencies # Step 3: Restore the dependencies
run: dotnet restore $SOLUTION_PATH

- name: Build solution # Step 4: Build the solution
run: dotnet build $SOLUTION_PATH --no-restore --configuration Release
# Step 3: Restore dependencies
- name: Restore dependencies
run: dotnet restore $SOLUTION_PATH

# Step 4: Build the solution
- name: Build solution
run: dotnet build $SOLUTION_PATH --no-restore --configuration Release

# Step 5: Run Unit Tests (xUnit)
- name: Run unit tests
run: dotnet test $TEST_PROJECT_PATH --no-build --configuration Release --verbosity normal
25 changes: 25 additions & 0 deletions SimpleAPI.Test/SimpleAPI.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.21" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleAPI\SimpleAPI.csproj" />
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions SimpleAPI.Test/UsersControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using System.Net.Http.Json;
using Xunit;

namespace SimpleAPI.Test
{
public class UsersControllerTests:IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _httpClient;

public UsersControllerTests(WebApplicationFactory<Program> factory)
{
_httpClient = factory.CreateClient();// ← this talks to your in-memory API
}
[Fact]
public async Task GetAllUsers_ReturnsList()
{
var response = await _httpClient.GetAsync("/api/users");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
Assert.Contains("Ravinder", json);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
public async Task GetUser_ById_ReturnsCorrectUser(int id)
{
// Act
var response = await _httpClient.GetAsync($"/api/users/{id}");
response.EnsureSuccessStatusCode();

// Deserialize JSON to a C# object
var user = await response.Content.ReadFromJsonAsync<UserDto>();

// Assert
Assert.NotNull(user);
Assert.Equal(id, user!.Id);
Assert.Equal($"User {id}", user.Name);
}
}
public class UserDto
{
public int Id { get; set; }
public string? Name { get; set; }
}
}
26 changes: 26 additions & 0 deletions SimpleAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;

namespace SimpleAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
[HttpGet]
public IActionResult GetAllUsers()
{
var users = new[]
{
new { Id = 1, Name = "Ravinder" },
new { Id = 2, Name = "John" }
};
return Ok(users);
}

[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
return Ok(new { Id = id, Name = $"User {id}" });
}
}
}
10 changes: 8 additions & 2 deletions SimpleAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddControllers(); // ✅ Add this line
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand All @@ -15,7 +16,10 @@
}

//app.UseHttpsRedirection();

// ✅ Add this before MapControllers()
app.UseRouting();
// Map your controllers 👇
app.MapControllers();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
Expand All @@ -42,3 +46,5 @@ record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
// ✅ Add this at the end
public partial class Program { }
1 change: 1 addition & 0 deletions SimpleAPI/SimpleAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions SimpleAPI/SimpleAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleAPI", "SimpleAPI.csproj", "{6299BDDD-37C4-D32F-2AC5-6E9916FA39B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleAPI.Test", "..\SimpleAPI.Test\SimpleAPI.Test.csproj", "{0BDD7F6D-4893-49BB-B452-9118D720E629}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -14,6 +16,10 @@ Global
{6299BDDD-37C4-D32F-2AC5-6E9916FA39B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6299BDDD-37C4-D32F-2AC5-6E9916FA39B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6299BDDD-37C4-D32F-2AC5-6E9916FA39B9}.Release|Any CPU.Build.0 = Release|Any CPU
{0BDD7F6D-4893-49BB-B452-9118D720E629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0BDD7F6D-4893-49BB-B452-9118D720E629}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BDD7F6D-4893-49BB-B452-9118D720E629}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BDD7F6D-4893-49BB-B452-9118D720E629}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down