Skip to content
Open

Main #18

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
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: CI Pipeline with SonarCloud

on:
push:
branches: [ "**" ]
pull_request:
branches: [ "**" ]

jobs:
build-test-analyze:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for SonarCloud

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Install SonarScanner
uses: SonarSource/sonarcloud-github-action@v2
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

- name: Restore dependencies
run: dotnet restore

- name: Build solution
run: dotnet build --no-restore --configuration Release

- name: Run Tests with Coverage
run: dotnet test --no-build --configuration Release --collect "XPlat Code Coverage"

- name: Locate coverage file
id: coverage
run: echo "##[set-output name=path;]$(find . -type f -name 'coverage.cobertura.xml')"

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@v2
with:
args: >
-Dsonar.projectKey=chan27052705_bp
-Dsonar.organization=chan27052705-1
-Dsonar.cs.opencover.reportsPaths=${{ steps.coverage.outputs.path }}
-Dsonar.coverage.exclusions=**/wwwroot/**

env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

- name: Upload Coverage Report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: ${{ steps.coverage.outputs.path }}

- name: Vulnerability Scan
run: dotnet list package --vulnerable || true
7 changes: 7 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
sonar.projectKey=chan27052705_bp
sonar.organization=chan27052705-1
sonar.host.url=https://sonarcloud.io

sonar.cs.opencover.reportsPaths=**/coverage.cobertura.xml
sonar.language=cs
sonar.sourceEncoding=UTF-8

45 changes: 45 additions & 0 deletions BPCalculator.Tests/BDD/BloodPressureBDDTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Xunit;
using BPCalculator;

namespace BPCalculator.Tests.BDD
{
public class BloodPressureBDDTests
{
[Fact]
public void GivenLowValues_WhenICheckCategory_ThenItIsLow()
{
// Given
var bp = new BloodPressure { Systolic = 85, Diastolic = 55 };

// When
var category = bp.Category;

// Then
Assert.Equal(BPCategory.Low, category);
}

[Fact]
public void GivenIdealValues_WhenICheckCategory_ThenItIsIdeal()
{
var bp = new BloodPressure { Systolic = 110, Diastolic = 75 };
var category = bp.Category;
Assert.Equal(BPCategory.Ideal, category);
}

[Fact]
public void GivenPreHighValues_WhenICheckCategory_ThenItIsPreHigh()
{
var bp = new BloodPressure { Systolic = 130, Diastolic = 85 };
var category = bp.Category;
Assert.Equal(BPCategory.PreHigh, category);
}

[Fact]
public void GivenHighValues_WhenICheckCategory_ThenItIsHigh()
{
var bp = new BloodPressure { Systolic = 150, Diastolic = 95 };
var category = bp.Category;
Assert.Equal(BPCategory.High, category);
}
}
}
31 changes: 31 additions & 0 deletions BPCalculator.Tests/BPCalculator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

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

</Project>
44 changes: 44 additions & 0 deletions BPCalculator.Tests/BloodPressureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Xunit;
using BPCalculator;

namespace BPCalculator.Tests
{
public class BloodPressureTests
{
[Theory]
[InlineData(85, 55, BPCategory.Low)]
[InlineData(80, 70, BPCategory.Low)]
public void Category_ReturnsLow(int systolic, int diastolic, BPCategory expected)
{
var bp = new BloodPressure { Systolic = systolic, Diastolic = diastolic };
Assert.Equal(expected, bp.Category);
}

[Theory]
[InlineData(100, 70, BPCategory.Ideal)]
[InlineData(110, 75, BPCategory.Ideal)]
public void Category_ReturnsIdeal(int systolic, int diastolic, BPCategory expected)
{
var bp = new BloodPressure { Systolic = systolic, Diastolic = diastolic };
Assert.Equal(expected, bp.Category);
}

[Theory]
[InlineData(130, 85, BPCategory.PreHigh)]
[InlineData(125, 88, BPCategory.PreHigh)]
public void Category_ReturnsPreHigh(int systolic, int diastolic, BPCategory expected)
{
var bp = new BloodPressure { Systolic = systolic, Diastolic = diastolic };
Assert.Equal(expected, bp.Category);
}

[Theory]
[InlineData(150, 95, BPCategory.High)]
[InlineData(140, 70, BPCategory.High)]
public void Category_ReturnsHigh(int systolic, int diastolic, BPCategory expected)
{
var bp = new BloodPressure { Systolic = systolic, Diastolic = diastolic };
Assert.Equal(expected, bp.Category);
}
}
}
26 changes: 26 additions & 0 deletions BPCalculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,42 @@ VisualStudioVersion = 16.0.30002.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BPCalculator", "BPCalculator\BPCalculator.csproj", "{5966DA6C-819D-4303-ADC7-425B8541EC84}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BPCalculator.Tests", "BPCalculator.Tests\BPCalculator.Tests.csproj", "{12E1E52C-274E-4D93-AF85-68A7EF9452AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|x64.ActiveCfg = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|x64.Build.0 = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|x86.ActiveCfg = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Debug|x86.Build.0 = Debug|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|Any CPU.Build.0 = Release|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|x64.ActiveCfg = Release|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|x64.Build.0 = Release|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|x86.ActiveCfg = Release|Any CPU
{5966DA6C-819D-4303-ADC7-425B8541EC84}.Release|x86.Build.0 = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|x64.ActiveCfg = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|x64.Build.0 = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|x86.ActiveCfg = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Debug|x86.Build.0 = Debug|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|Any CPU.Build.0 = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|x64.ActiveCfg = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|x64.Build.0 = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|x86.ActiveCfg = Release|Any CPU
{12E1E52C-274E-4D93-AF85-68A7EF9452AA}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
25 changes: 21 additions & 4 deletions BPCalculator/BloodPressure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ public class BloodPressure
// calculate BP category
public BPCategory Category
{
get
get
{
// implement as part of project
//throw new NotImplementedException("not implemented yet");
return new BPCategory(); // replace this
// LOW BP
if (Systolic < 90 || Diastolic < 60)
return BPCategory.Low;

// IDEAL BP
if (Systolic >= 90 && Systolic < 120 &&
Diastolic >= 60 && Diastolic < 80)
return BPCategory.Ideal;

// PRE-HIGH BP
if ((Systolic >= 120 && Systolic < 140) ||
(Diastolic >= 80 && Diastolic < 90))
return BPCategory.PreHigh;

// HIGH BP
if (Systolic >= 140 || Diastolic >= 90)
return BPCategory.High;

// fallback — should never reach here
return BPCategory.High;
}
}
}
Expand Down