Skip to content

slymanmrcan/sonarqube-server-netcore

Repository files navigation

SonarQube Action Server Test Project

Bu proje, .NET 11.0 Web API yapısı kullanılarak geliştirilmiş ve SonarQube kod kalitesi analizi entegrasyonu için örnek teşkil etmesi amacıyla yapılandırılmış bir test projesidir. Proje içerisinde hem Unit Test'ler hem de ASP.NET Core WebApplicationFactory kullanan entegrasyon testleri yer almaktadır.


🛠 Proje Yapısı ve Mimarisi

Proje, standart Controller-Service mimarisini takip eder ve aşağıdaki dizin düzenine sahiptir:

├── Controllers/                      # API Endpoint Tanımları
│   ├── CalculatorController.cs       # Hesap makinesi işlemleri, asal sayı, faktöriyel, fibonacci
│   ├── ProductsController.cs         # Ürün ekleme, silme, güncelleme, listeleme
│   └── UsersController.cs            # Kullanıcı yönetimi
├── Services/                         # İş Mantığı (Business Logic) ve Arayüzler
│   ├── CalculatorService.cs
│   ├── ProductService.cs
│   └── UserService.cs
├── tests/
│   ├── UnitTests/                    # Birim testleri (xUnit + Moq + FluentAssertions)
│   └── IntegrationTests/             # Entegrasyon testleri (xUnit + Microsoft.AspNetCore.Mvc.Testing)
└── .github/workflows/
    └── sonarqube.yml                 # GitHub Actions SonarQube CI Workflow'u

🚀 API Endpoint Listesi

1. Hesap Makinesi (/api/calculator)

  • GET /api/calculator/add?a={double}&b={double}: İki sayıyı toplar.
  • GET /api/calculator/subtract?a={double}&b={double}: İki sayıyı çıkarır.
  • GET /api/calculator/multiply?a={double}&b={double}: İki sayıyı çarpar.
  • GET /api/calculator/divide?a={double}&b={double}: İki sayıyı böler (0'a bölme hatası kontrolü içerir).
  • GET /api/calculator/power?base={double}&exponent={double}: Üs alır.
  • GET /api/calculator/sqrt?value={double}: Karekök alır.
  • GET /api/calculator/percentage?value={double}&percent={double}: Yüzde hesaplar.
  • GET /api/calculator/is-prime/{number:int}: Sayının asal olup olmadığını sorgular (Route parameter).
  • GET /api/calculator/factorial/{n:int}: Faktöriyel hesaplar (Route parameter).
  • GET /api/calculator/fibonacci?count={int}: Fibonacci serisi üretir (Query parameter).

2. Ürün Yönetimi (/api/products)

  • GET /api/products: Tüm ürünleri listeler.
  • GET /api/products/{id}: Belirli bir ürünü getirir.
  • POST /api/products: Yeni ürün ekler.
  • PUT /api/products/{id}: Ürün günceller.
  • DELETE /api/products/{id}: Ürün siler.

3. Kullanıcı Yönetimi (/api/users)

  • GET /api/users: Tüm kullanıcıları listeler.
  • GET /api/users/{id}: Belirli bir kullanıcıyı getirir.
  • POST /api/users: Yeni kullanıcı kaydeder.
  • DELETE /api/users/{id}: Kullanıcı siler.

🧪 Testleri Çalıştırma

Projede toplam 93 adet test bulunmakta olup tamamı başarıyla geçmektedir.

1. Tüm Testleri Çalıştırmak İçin:

dotnet test

2. Sadece Birim Testleri Çalıştırmak İçin:

dotnet test tests/UnitTests/UnitTests.csproj

3. Sadece Entegrasyon Testlerini Çalıştırmak İçin:

dotnet test tests/IntegrationTests/IntegrationTests.csproj

4. Kod Kapsama (Code Coverage) Raporu Üretmek İçin:

SonarQube'ün test kapsama oranını (test coverage) algılayabilmesi için testler opencover formatında çalıştırılmalıdır:

dotnet test --collect:"XPlat Code Coverage" \
  -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

🛡 SonarQube & CI/CD Entegrasyonu

GitHub Actions Workflow Ayarı (.github/workflows/sonarqube.yml)

Workflow dosyası her push işleminde projeyi derler, testleri çalıştırarak OpenCover formatında kapsama raporu üretir ve bu sonuçları SonarQube sunucusuna gönderir.

⚠️ .NET Sürümü Bilgilendirmesi:

Projemizin TargetFramework sürümü .NET 11.0 (net11.0) olarak yapılandırılmıştır. Bu sebeple GitHub Actions workflow'undaki Setup .NET adımında kullanılan sürüm 11.0.x olarak güncellenmiştir.

  • Eski hatalı ayar: dotnet-version: '9.0.x' (Projeyi derleyememesine sebep olur).
  • Güncel doğru ayar:
          - name: Setup .NET
            uses: actions/setup-dotnet@v4
            with:
              dotnet-version: '11.0.x'

Manuel/Lokal SonarQube Analizi Çalıştırmak İçin:

# SonarScanner aracını kurun/güncelleyin:
dotnet tool update dotnet-sonarscanner --global

# Analizi başlatın:
dotnet-sonarscanner begin \
  /k:"my-first-local-project" \
  /d:sonar.token="YOUR_SONAR_TOKEN" \
  /d:sonar.host.url="YOUR_SONAR_HOST_URL" \
  /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" \
  /d:sonar.exclusions="**/bin/**,**/obj/**,**/tests/**"

# Projeyi derleyin ve test edin:
dotnet build sonarqube-test.slnx --no-restore
dotnet test --no-build --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

# Analizi bitirip sunucuya gönderin:
dotnet-sonarscanner end /d:sonar.token="YOUR_SONAR_TOKEN"

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages