Spaces:
Paused
Paused
File size: 20,505 Bytes
5a81b95 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | # π§ͺ Citizen Intelligence Agency - Unit Test Plan
[](https://hack23.github.io/cia/jacoco/)
[](https://hack23.github.io/cia/surefire.html)
[](https://sonarcloud.io/summary/new_code?id=Hack23_cia)
[](https://sonarcloud.io/summary/new_code?id=Hack23_cia)
> **Compliance Statement**: This document fulfills the testing requirements of the [Hack23 Secure Development Policy](https://github.com/Hack23/ISMS-PUBLIC/blob/main/Secure_Development_Policy.md) as mandated by ISO 27001 (A.12.1.4), NIST CSF (PR.IP-2), and CIS Controls (16.8).
---
## π Table of Contents
- [Testing Objectives](#-testing-objectives)
- [Testing Strategy](#-testing-strategy)
- [Test Infrastructure](#-test-infrastructure)
- [Coverage Metrics](#-coverage-metrics)
- [Module Testing Approach](#-module-testing-approach)
- [Test Execution](#-test-execution)
- [Quality Gates](#-quality-gates)
- [Continuous Improvement](#-continuous-improvement)
- [Compliance & Standards](#-compliance--standards)
---
## π― Testing Objectives
The Citizen Intelligence Agency maintains comprehensive unit testing to ensure:
- β
**80% minimum line coverage** across all production modules
- β
**70% minimum branch coverage** for critical business logic
- β
**90%+ coverage** for security and authentication components
- β
**100% coverage** for utility and validation classes
- β
**Zero critical security vulnerabilities** in tested code paths
- β
**Public transparency** through live coverage reporting
### Business Goals
- **Quality Assurance**: Prevent regressions through automated testing
- **Security Validation**: Test security controls and authentication flows
- **Maintainability**: Enable safe refactoring with comprehensive test suite
- **Documentation**: Tests serve as executable specifications
- **Compliance**: Meet ISO 27001, NIST CSF, and CIS Controls requirements
---
## ποΈ Testing Strategy
### Test Pyramid Implementation
Our testing strategy follows the industry-standard test pyramid approach:
```
/\
/ \
/ E2E \ 10% - End-to-End Tests
/ Tests \ (Critical user journeys)
/----------\
/ \
/ Integration \ 20% - Integration Tests
/ Tests \ (Component interactions)
/------------------\
/ \
/ Unit Tests \ 70% - Unit Tests
/________________________\ (Isolated component testing)
```
### Coverage Targets by Module Type
| Module Category | Line Coverage | Branch Coverage | Priority | Rationale |
|----------------|---------------|-----------------|----------|-----------|
| **Security & Authentication** | 90%+ | 80%+ | Critical | Security-critical code requires maximum validation |
| **Service Layer** | 80%+ | 70%+ | High | Business logic implementation |
| **Data Access (DAO)** | 80%+ | 70%+ | High | Database operations and query validation |
| **Business Logic** | 80%+ | 70%+ | High | Core application functionality |
| **External Integration** | 75%+ | 65%+ | Medium | Third-party API integration |
| **Data Models** | 80%+ | 70%+ | Medium | Entity validation and integrity |
| **UI Components** | 60%+ | 50%+ | Low | Vaadin UI components (E2E tested separately) |
| **Configuration** | 70%+ | 60%+ | Medium | Spring configuration and wiring |
### Test Categories
#### 1. **Unit Tests** (70% of test suite)
- **Purpose**: Test individual classes and methods in isolation
- **Scope**: Single class or component
- **Speed**: < 1 second per test
- **Dependencies**: Mocked using Mockito
- **Execution**: Every commit, every PR
#### 2. **Integration Tests** (20% of test suite)
- **Purpose**: Test component interactions and data flow
- **Scope**: Multiple components, database operations
- **Speed**: < 10 seconds per test
- **Dependencies**: Spring Test Context, H2 in-memory database
- **Execution**: Every commit, every PR
#### 3. **Functional Tests** (10% of test suite)
- **Purpose**: Test complete features and user scenarios
- **Scope**: Full application stack
- **Speed**: < 30 seconds per test
- **Dependencies**: Embedded containers, test database
- **Execution**: Pre-merge, nightly builds
---
## π οΈ Test Infrastructure
### Testing Frameworks & Tools
| Component | Technology | Version | Purpose |
|-----------|-----------|---------|---------|
| **Test Framework** | JUnit | 4.13.2 | Core testing framework |
| **Mocking** | Mockito | 5.20.0 | Mock object creation |
| **Spring Testing** | Spring Test | 5.3.39 | Spring context testing |
| **Coverage Tool** | JaCoCo | 0.8.14 | Code coverage analysis |
| **Database Testing** | H2 Database | 2.4.240 | In-memory test database |
| **DBUnit** | DBUnit | 3.0.0 | Database test data management |
| **CI/CD Platform** | GitHub Actions | - | Automated test execution |
| **Quality Analysis** | SonarCloud | - | Static analysis & coverage reporting |
### JaCoCo Configuration
The project uses JaCoCo Maven Plugin for comprehensive coverage reporting:
```xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
```
**Report Locations:**
- π **JaCoCo HTML Reports**: `target/site/jacoco/index.html` (per module)
- π **SonarCloud Dashboard**: https://sonarcloud.io/summary/new_code?id=Hack23_cia
- π **GitHub Pages**: https://hack23.github.io/cia/jacoco/
---
## π Coverage Metrics
### Current Test Statistics
| Metric | Count | Details |
|--------|-------|---------|
| **Total Maven Modules** | 48 | Multi-module Maven project |
| **Java Source Files** | 1,153 | Production code files |
| **Test Files** | 207 | Test classes (*Test.java) |
| **Test-to-Code Ratio** | 17.9% | 207 tests / 1,153 source files |
| **Lines of Code** | 100,000+ | As reported by SonarCloud |
### Coverage Goals & Thresholds
#### Minimum Thresholds (Enforced in CI/CD)
```
β Line Coverage: 80% minimum (global)
β Branch Coverage: 70% minimum (global)
β Security Modules: 90% minimum (line coverage)
β Utility Classes: 100% recommended
```
#### Coverage Enforcement
Coverage thresholds are **enforced** through:
1. **Maven Build**: JaCoCo plugin fails build if thresholds not met
2. **SonarCloud Quality Gate**: Blocks PRs below coverage targets
3. **GitHub Actions**: CI workflow validates coverage on every commit
---
## ποΈ Module Testing Approach
The CIA project consists of **48 Maven modules** organized into logical categories:
### 1. **Model Modules** (23 modules)
#### External Data Models
API integration models for external Swedish government services:
- **Riksdagen (Parliament) API Models** (9 modules)
- `model.external.riksdagen` - Core parliament data
- `model.external.riksdagen.documentcontent.impl` - Document content
- `model.external.riksdagen.dokumentlista.impl` - Document lists
- `model.external.riksdagen.dokumentstatus.impl` - Document status
- `model.external.riksdagen.person.impl` - Person data
- `model.external.riksdagen.personlista.impl` - Person lists
- `model.external.riksdagen.utskottsforslag.impl` - Committee proposals
- `model.external.riksdagen.votering.impl` - Voting records
- `model.external.riksdagen.voteringlista.impl` - Voting lists
- **Val (Election Authority) Models** (5 modules)
- `model.external.val` - Core election data
- `model.external.val.kommunvalkrets.impl` - Municipal districts
- `model.external.val.landstingvalkrets.impl` - County districts
- `model.external.val.partier.impl` - Political parties
- `model.external.val.riksdagsvalkrets.impl` - Parliamentary districts
- **World Bank Models** (5 modules)
- `model.external.worldbank` - Core World Bank data
- `model.external.worldbank.countries.impl` - Country data
- `model.external.worldbank.data.impl` - Economic indicators
- `model.external.worldbank.indicators.impl` - Indicator metadata
- `model.external.worldbank.topic.impl` - Topic classifications
#### Internal Data Models
- `model.common.api` - Common model interfaces
- `model.common.impl` - Common model implementations
- `model.internal.application` - Application domain models
- `model.internal.application.user.impl` - User and security models
**Testing Strategy for Models:**
- β
Entity validation tests
- β
JAXB marshaling/unmarshaling roundtrip tests
- β
JPA entity mapping verification
- β
Equals/hashCode contract validation
- β
Serialization compatibility tests
**Example Test Pattern:**
```java
@Test
public void testRoundtripSerialization() {
DocumentData original = createTestData();
String xml = marshal(original);
DocumentData deserialized = unmarshal(xml);
assertEquals(original, deserialized);
}
```
### 2. **Service Modules** (11 modules)
#### Core Service Layer
- `service.api` - Service interfaces and contracts
- `service.impl` - Core service implementations
#### Component Services
- `service.component.agent.api` - Agent component interfaces
- `service.component.agent.impl` - Agent component implementations
#### Data Access Services
- `service.data.api` - DAO interfaces
- `service.data.impl` - DAO implementations (Hibernate/JPA)
#### External Integration Services
- `service.external.common` - Common integration utilities
- `service.external.esv` - Swedish Financial Management Authority integration
- `service.external.riksdagen` - Parliament API integration
- `service.external.val` - Election Authority integration
- `service.external.worldbank` - World Bank API integration
**Testing Strategy for Services:**
- β
Business logic validation with unit tests
- β
Transaction management verification
- β
Exception handling scenarios
- β
External API mocking (Mockito)
- β
Database operations with H2 in-memory
- β
Spring context integration tests
**Example Test Pattern:**
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ServiceIntegrationTest {
@Autowired
private UserService userService;
@Mock
private ExternalApiClient apiClient;
@Test
public void testServiceOperation() {
when(apiClient.fetchData()).thenReturn(mockData);
Result result = userService.processData();
verify(apiClient, times(1)).fetchData();
assertNotNull(result);
}
}
```
### 3. **Web Application Modules (2 modules)**
- `citizen-intelligence-agency` - Main Vaadin web application
- `web-widgets` - Reusable UI components
**Testing Strategy for Web:**
- β
Spring MVC controller tests
- β
Vaadin UI component tests
- β
Security filter chain validation
- β
Session management tests
- β
Integration with service layer
- π E2E tests (separate test plan)
### 4. **Infrastructure Modules** (8 modules)
- `parent-pom` - Parent POM configuration
- `parent-model-pom` - Model modules parent
- `parent-model-external-pom` - External models parent
- `parent-model-internal-pom` - Internal models parent
- `parent-model-jpa2-pom` - JPA models parent
- `parent-service-pom` - Service modules parent
- `parent-web-pom` - Web modules parent
- `testfoundation` - Common test utilities
**Testing Strategy for Infrastructure:**
- β
Test utility validation
- β
Configuration verification
- β
Build configuration tests
- βοΈ Minimal direct testing (build-time validation)
### 5. **Distribution & Deployment** (4 modules)
- `cia-dist-deb` - Debian package distribution
- `cia-dist-cloudformation` - AWS CloudFormation templates
- `jms-broker` - JMS message broker configuration
- `encrypt.properties` - Property encryption utilities
**Testing Strategy for Distribution:**
- β
Package assembly verification
- β
Configuration validation
- β
Integration smoke tests
- π Deployment validation (separate E2E tests)
---
## π Test Execution
### Local Development
#### Run All Tests
```bash
mvn clean test
```
#### Run Tests with Coverage Report
```bash
mvn clean test jacoco:report
```
#### Run Tests for Specific Module
```bash
cd model.external.riksdagen.person.impl
mvn test
```
#### View Coverage Report
```bash
# After running tests with jacoco:report
open target/site/jacoco/index.html
```
### Continuous Integration (GitHub Actions)
Tests are automatically executed on:
- β
**Every Commit**: Fast unit tests on all branches
- β
**Pull Requests**: Full test suite + coverage analysis
- β
**Merge to Main**: Integration tests + deployment validation
- β
**Nightly Builds**: Extended test suite + performance tests
**GitHub Actions Workflow:**
```yaml
name: CI Build
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 25
uses: actions/setup-java@v4
with:
java-version: '25'
- name: Run Tests
run: mvn clean test jacoco:report
- name: SonarCloud Analysis
run: mvn sonar:sonar
```
### Test Reports
| Report Type | Location | Access |
|-------------|----------|--------|
| **JaCoCo HTML** | `target/site/jacoco/` | Local, GitHub Pages |
| **Surefire Report** | `target/surefire-reports/` | Local, GitHub Pages |
| **SonarCloud** | https://sonarcloud.io | Public dashboard |
| **GitHub Actions** | Actions tab | PR status checks |
---
## π Quality Gates
### Pre-Merge Validation
All pull requests must pass the following quality gates:
#### 1. **Test Execution**
- β
All tests must pass (0 failures)
- β
No compilation errors
- β
No test timeouts
#### 2. **Coverage Thresholds**
- β
Minimum 80% line coverage (global)
- β
Minimum 70% branch coverage (global)
- β
No decrease in overall coverage (delta β₯ 0%)
#### 3. **Code Quality (SonarCloud)**
- β
Quality Gate: "Passed"
- β
Zero critical vulnerabilities
- β
Zero blocker issues
- β
Maintainability rating: A or B
- β
Security rating: A
#### 4. **Security Scanning**
- β
CodeQL analysis: No new alerts
- β
Dependency Check: No critical CVEs
- β
OWASP ZAP: No high-severity findings
### Build Failure Policy
Builds fail if:
- β Any test fails
- β Coverage drops below 80%/70%
- β New critical security vulnerabilities introduced
- β SonarCloud quality gate fails
---
## π Continuous Improvement
### Coverage Monitoring
- **Weekly Reviews**: Coverage trend analysis in team meetings
- **Monthly Reports**: Test effectiveness metrics and flaky test analysis
- **Quarterly Goals**: Coverage improvement targets per module
### Test Maintenance
- **Flaky Test Management**: Tests with >1% failure rate are investigated immediately
- **Performance Monitoring**: Tests taking >1s (unit) or >10s (integration) are optimized
- **Obsolete Test Cleanup**: Redundant tests removed during refactoring
- **Test Naming Standards**: Descriptive names following `should_expectedBehavior_when_stateUnderTest` pattern
### Testing Best Practices
#### β
Do's
- Write tests before fixing bugs (TDD approach)
- Keep tests independent and isolated
- Use meaningful test names describing behavior
- Mock external dependencies
- Test both success and failure scenarios
- Include edge cases and boundary conditions
- Maintain test data builders/factories
- Review test coverage in code reviews
#### β Don'ts
- Don't test framework code or third-party libraries
- Don't write tests dependent on execution order
- Don't use production data in tests
- Don't skip tests instead of fixing them
- Don't write overly complex tests
- Don't test implementation details
- Don't commit commented-out tests
---
## π Compliance & Standards
### Regulatory Requirements
This testing approach satisfies:
#### ISO 27001:2022
- **A.12.1.4 - Separation of development, testing and operational environments**
- β
Separate test environments with H2 database
- β
Test data isolation from production
- β
Automated test execution in CI/CD
#### NIST Cybersecurity Framework 2.0
- **PR.IP-2 - Secure Development Lifecycle**
- β
Unit testing integrated into development process
- β
Automated security testing (CodeQL, OWASP)
- β
Coverage reporting and quality gates
#### CIS Controls v8.1
- **16.8 - Maintain up-to-date test scripts and processes**
- β
Comprehensive test documentation
- β
Version-controlled test code
- β
Regular test review and updates
### Industry Standards
- **OWASP ASVS (Application Security Verification Standard)**: Security testing integrated
- **IEEE 829 (Software Test Documentation)**: Structured test planning approach
- **ISO/IEC 25010 (Software Quality Requirements)**: Coverage of quality characteristics
---
## π Additional Resources
### Internal Documentation
- [Architecture Overview](ARCHITECTURE.md) - System architecture and design
- [Security Architecture](SECURITY_ARCHITECTURE.md) - Security controls and design
- [Contributing Guidelines](CONTRIBUTING.md) - Development workflow and standards
- [Data Model](DATA_MODEL.md) - Database schema and entities
### External Standards
- [Hack23 ISMS Public Repository](https://github.com/Hack23/ISMS-PUBLIC)
- [Secure Development Policy](https://github.com/Hack23/ISMS-PUBLIC/blob/main/Secure_Development_Policy.md)
- [Information Security Policy](https://github.com/Hack23/ISMS-PUBLIC/blob/main/Information_Security_Policy.md)
### Testing Resources
- [JUnit 4 Documentation](https://junit.org/junit4/)
- [Mockito Documentation](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html)
- [Spring Testing Guide](https://docs.spring.io/spring-framework/reference/testing.html)
- [JaCoCo Documentation](https://www.jacoco.org/jacoco/trunk/doc/)
---
## π Contact & Support
**Test Infrastructure Questions:**
- **GitHub Issues**: https://github.com/Hack23/cia/issues
- **Security Issues**: security@hack23.com (see [SECURITY.md](SECURITY.md))
**Project Leadership:**
- **Maintainer**: James SΓΆrling
- **Organization**: Hack23 AB
- **Website**: https://www.hack23.com
---
## π Appendix: Test Metrics Dashboard
### Key Performance Indicators (KPIs)
| Metric | Target | Current | Status |
|--------|--------|---------|--------|
| **Line Coverage** | β₯80% | See [SonarCloud](https://sonarcloud.io/summary/new_code?id=Hack23_cia) | π Monitored |
| **Branch Coverage** | β₯70% | See [SonarCloud](https://sonarcloud.io/summary/new_code?id=Hack23_cia) | π Monitored |
| **Test Execution Time** | <5 min | π Optimizing | β οΈ In Progress |
| **Flaky Test Rate** | <1% | π Tracking | β
Target |
| **Test-to-Code Ratio** | β₯15% | 17.9% | β
Target |
### Historical Trends
Coverage trends and historical data available at:
- **SonarCloud Dashboard**: https://sonarcloud.io/summary/new_code?id=Hack23_cia
- **GitHub Actions**: https://github.com/Hack23/cia/actions
- **GitHub Pages Reports**: https://hack23.github.io/cia/
---
**Document Version**: 1.0
**Last Updated**: 2025-11-14
**Next Review**: 2026-02-14 (Quarterly)
**Owner**: Hack23 AB Development Team
**Compliance Status**: β
**ISMS-Compliant**
---
<div align="center">
[](https://hack23.github.io/cia/jacoco/)
[](https://hack23.github.io/cia/surefire.html)
[](https://sonarcloud.io/summary/new_code?id=Hack23_cia)
**Transparency Through Testing** | **Quality Through Validation** | **Security Through Verification**
</div>
|