Skip to content

Repository files navigation

Employee Management System

A comprehensive Spring Boot application for managing employees, departments, and leave requests with RabbitMQ notifications and Docker deployment.

Table of Contents

Features

Core Functionality

  • Employee Management: Create, read, update, and delete employee records
  • Department Management: Organize employees by departments
  • Leave Request System: Submit and manage leave requests with approval workflow
  • Notification System: Real-time notifications via RabbitMQ for:
    • New employee welcome messages
    • Leave request status updates
  • Security: Role-based access control (ADMIN and USER roles)
  • Pagination & Sorting: Efficient data retrieval with pagination support
  • Containerization: Full Docker support for easy deployment

Technology Stack

  • Java: 17
  • Spring Boot: 3.2.1
  • Database: PostgreSQL (production), H2 (testing)
  • Message Queue: RabbitMQ
  • Security: Spring Security with Basic Authentication
  • Build Tool: Maven
  • Testing: JUnit 5, Mockito, Spring Boot Test
  • Containerization: Docker & Docker Compose

Architecture Overview

The application follows a layered architecture pattern:

┌─────────────────────────────────────────┐
│           REST Controllers              │
│   (Employee, Department, LeaveRequest)  │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│           Service Layer                 │
│   (Business Logic & Validation)         │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│       Repository Layer (JPA)            │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│          Database (PostgreSQL)          │
└─────────────────────────────────────────┘

         Message Flow
┌─────────────────────────────────────────┐
│      RabbitMQ Message Publisher         │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│           RabbitMQ Broker               │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│      RabbitMQ Message Consumer          │
│      (Simulates Email Sending)          │
└─────────────────────────────────────────┘

Key Components

  1. Entities: Employee, Department, LeaveRequest
  2. DTOs: Data Transfer Objects for API communication
  3. Repositories: Spring Data JPA repositories
  4. Services: Business logic implementation
  5. Controllers: REST API endpoints
  6. Message Publisher/Consumer: RabbitMQ integration
  7. Security Config: Spring Security configuration

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Docker and Docker Compose (for containerized deployment)
  • PostgreSQL (if running locally without Docker)
  • RabbitMQ (if running locally without Docker)

Setup Instructions

Option 1: Docker Deployment (Recommended)

  1. Clone the repository

    git clone <repository-url>
    cd employee-management-system
  2. Build and run with Docker Compose

    docker-compose up --build
  3. Access the application

  4. Stop the application

    docker-compose down

Option 2: Local Development

  1. Clone the repository

    git clone <repository-url>
    cd employee-management-system
  2. Configure database

    • Install PostgreSQL
    • Create database: employeedb
    • Update src/main/resources/application.yml with your database credentials
  3. Configure RabbitMQ

    • Install RabbitMQ
    • Start RabbitMQ service
    • Default credentials: guest/guest
  4. Build the application

    mvn clean install
  5. Run the application

    mvn spring-boot:run
  6. Access the application

API Documentation

Authentication

All endpoints require Basic Authentication. Use the following credentials:

Admin User:

  • Username: admin
  • Password: admin123
  • Roles: ADMIN (full access)

Regular User:

  • Username: user
  • Password: user123
  • Roles: USER (limited access)

Base URL

http://localhost:8080/api

Employee Endpoints

1. Get All Employees

GET /api/employees?page=0&size=10&sortBy=fullName&sortDirection=ASC&departmentId=1

Query Parameters:

  • page (optional, default: 0): Page number
  • size (optional, default: 10): Page size
  • sortBy (optional, default: fullName): Sort field (fullName, department, joiningDate)
  • sortDirection (optional, default: ASC): Sort direction (ASC, DESC)
  • departmentId (optional): Filter by department ID

Authorization: ADMIN, USER

Response:

{
  "success": true,
  "message": "Employees retrieved successfully",
  "data": {
    "content": [
      {
        "employeeId": 1,
        "fullName": "John Doe",
        "email": "john@example.com",
        "departmentId": 1,
        "departmentName": "Engineering",
        "salary": 50000,
        "joiningDate": "2024-01-15",
        "createdAt": "2024-01-15T10:30:00",
        "updatedAt": "2024-01-15T10:30:00"
      }
    ],
    "totalElements": 1,
    "totalPages": 1,
    "size": 10,
    "number": 0
  }
}

2. Get Employee by ID

GET /api/employees/{id}

Authorization: ADMIN, USER

3. Create Employee

POST /api/employees
Content-Type: application/json

{
  "fullName": "John Doe",
  "email": "john@example.com",
  "departmentId": 1,
  "salary": 50000,
  "joiningDate": "2024-01-15"
}

Authorization: ADMIN only

Response: 201 Created

4. Update Employee

PUT /api/employees/{id}
Content-Type: application/json

{
  "fullName": "John Doe Updated",
  "email": "john@example.com",
  "departmentId": 1,
  "salary": 55000,
  "joiningDate": "2024-01-15"
}

Authorization: ADMIN only

5. Delete Employee

DELETE /api/employees/{id}

Authorization: ADMIN only

Department Endpoints

1. Get All Departments

GET /api/departments

Authorization: ADMIN, USER

2. Get Department by ID

GET /api/departments/{id}

Authorization: ADMIN, USER

3. Create Department

POST /api/departments
Content-Type: application/json

{
  "departmentName": "Engineering",
  "location": "Building A"
}

Authorization: ADMIN only

4. Update Department

PUT /api/departments/{id}
Content-Type: application/json

{
  "departmentName": "Engineering",
  "location": "Building B"
}

Authorization: ADMIN only

5. Delete Department

DELETE /api/departments/{id}

Authorization: ADMIN only

Note: Cannot delete department with existing employees

6. Get Department Employees

GET /api/departments/{id}/employees

Authorization: ADMIN, USER

Leave Request Endpoints

1. Create Leave Request

POST /api/leaves
Content-Type: application/json

{
  "employeeId": 1,
  "startDate": "2024-02-01",
  "endDate": "2024-02-05",
  "reason": "Vacation"
}

Authorization: ADMIN, USER

Response: 201 Created

2. Update Leave Status

PUT /api/leaves/{id}/status
Content-Type: application/json

{
  "status": "APPROVED"
}

Status Values: PENDING, APPROVED, REJECTED

Authorization: ADMIN only

3. Get Employee Leave Requests

GET /api/leaves/employee/{empId}

Authorization: ADMIN, USER

4. Get Leave Request by ID

GET /api/leaves/{id}

Authorization: ADMIN, USER

5. Get All Leave Requests

GET /api/leaves

Authorization: ADMIN only

Security

Authentication

  • Uses Spring Security with Basic Authentication
  • Credentials are sent with each request

Authorization

Two roles are defined:

ADMIN Role:

  • Full CRUD operations on employees
  • Full CRUD operations on departments
  • Can approve/reject leave requests
  • Can view all leave requests

USER Role:

  • Can view employees and departments
  • Can apply for leave
  • Can view own leave requests

Example cURL Request

curl -u admin:admin123 http://localhost:8080/api/employees

Testing

Run All Tests

mvn test

Run Tests with Coverage

mvn clean test jacoco:report

Coverage report will be available at: target/site/jacoco/index.html

Coverage Target: Minimum 70% line coverage

Test Structure

The project includes:

  • Unit Tests: Service layer, mappers
  • Integration Tests: Repository layer, controllers
  • Message Queue Tests: RabbitMQ publisher/consumer

Test files are located in src/test/java/com/employee/management/

Docker Deployment

Docker Compose Services

The docker-compose.yml defines three services:

  1. PostgreSQL Database

    • Image: postgres:16-alpine
    • Port: 5432
    • Database: employeedb
    • Health check enabled
  2. RabbitMQ

    • Image: rabbitmq:3.12-management-alpine
    • Ports: 5672 (AMQP), 15672 (Management UI)
    • Health check enabled
  3. Application

    • Built from Dockerfile
    • Port: 8080
    • Depends on postgres and rabbitmq

Docker Commands

Start all services:

docker-compose up -d

View logs:

docker-compose logs -f app

Stop all services:

docker-compose down

Stop and remove volumes:

docker-compose down -v

Rebuild application:

docker-compose up --build app

Environment Variables

Application configuration via environment variables:

  • DB_URL: Database connection URL
  • DB_USERNAME: Database username
  • DB_PASSWORD: Database password
  • RABBITMQ_HOST: RabbitMQ host
  • RABBITMQ_PORT: RabbitMQ port
  • RABBITMQ_USERNAME: RabbitMQ username
  • RABBITMQ_PASSWORD: RabbitMQ password

Assumptions

  1. Database: PostgreSQL is used as the primary database. The application uses JPA with Hibernate for ORM.

  2. Email Simulation: The notification system simulates email sending by logging messages to the console. In production, integrate with actual email service (SMTP, SendGrid, etc.).

  3. User Management: Uses in-memory user authentication with two predefined users (admin and user). In production, implement database-backed user management with registration and password management.

  4. Leave Request Validation:

    • End date must be after start date
    • No overlap checking between leave requests
    • No leave balance tracking
  5. Department Deletion: Cannot delete a department that has employees. Employees must be reassigned or deleted first.

  6. Unique Constraints:

    • Employee email must be unique
    • Department name should be unique (enforced at service layer)
  7. Pagination: Default page size is 10 records. Maximum page size is 100.

  8. Audit Fields: All entities include createdAt and updatedAt timestamps managed by JPA.

  9. Error Handling: Global exception handler provides consistent error responses across all endpoints.

  10. Message Queue: RabbitMQ uses topic exchange with separate queues for employee and leave notifications.

  11. Transaction Management: Service layer methods are transactional to ensure data consistency.

  12. Soft Delete: Not implemented. Delete operations are hard deletes.

  13. API Versioning: Not implemented. Consider adding /api/v1/ prefix for future versions.

  14. Rate Limiting: Not implemented. Consider adding for production deployment.

  15. CORS: Disabled by default. Configure based on frontend requirements.

Project Structure

employee-management-system/
├── src/
│   ├── main/
│   │   ├── java/com/employee/management/
│   │   │   ├── config/              # Configuration classes
│   │   │   ├── controller/          # REST controllers
│   │   │   ├── dto/                 # Data Transfer Objects
│   │   │   ├── entity/              # JPA entities
│   │   │   ├── exception/           # Custom exceptions
│   │   │   ├── mapper/              # Entity-DTO mappers
│   │   │   ├── messaging/           # RabbitMQ components
│   │   │   ├── repository/          # JPA repositories
│   │   │   ├── service/             # Business logic
│   │   │   └── EmployeeManagementSystemApplication.java
│   │   └── resources/
│   │       └── application.yml      # Application configuration
│   └── test/
│       └── java/com/employee/management/  # Test classes
├── Dockerfile                       # Docker build configuration
├── docker-compose.yml              # Docker Compose configuration
├── pom.xml                         # Maven dependencies
└── README.md                       # This file

Future Enhancements

  1. JWT Authentication: Replace Basic Auth with JWT tokens
  2. Email Integration: Integrate with actual email service
  3. Leave Balance: Track and enforce leave balance
  4. Audit Logging: Comprehensive audit trail for all operations
  5. File Upload: Support for employee documents
  6. Reporting: Generate reports for HR analytics
  7. API Documentation: Integrate Swagger/OpenAPI
  8. Caching: Implement Redis for performance
  9. Search: Full-text search capabilities
  10. Notifications: Add webhook support

Support

For issues, questions, or contributions, please contact the development team or create an issue in the repository.

License

[Specify your license here]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages