An open API service indexing awesome lists of open source software.

https://github.com/pramodbharti/cashcard

A RESTful API for managing cash cards built with Spring Boot and Kotlin. This application demonstrates Test-Driven Development (TDD) principles and implements a secure API for creating, reading, updating, and deleting cash cards.
https://github.com/pramodbharti/cashcard

assert junit kotlin monolith rest-api restful-api spring-boot spring-data-jpa spring-mvc spring-security tdd test

Last synced: about 1 month ago
JSON representation

A RESTful API for managing cash cards built with Spring Boot and Kotlin. This application demonstrates Test-Driven Development (TDD) principles and implements a secure API for creating, reading, updating, and deleting cash cards.

Awesome Lists containing this project

README

          

# Cash Card API

A RESTful API for managing cash cards built with Spring Boot and Kotlin. This application demonstrates Test-Driven Development (TDD) principles and implements a secure API for creating, reading, updating, and deleting cash cards.

## Features

- CRUD operations for cash cards (Create, Read, Update, Delete)
- Role-based access control
- User-specific data isolation (users can only access their own cards)
- Pagination and sorting for listing cash cards
- RESTful API design
- Comprehensive test coverage

## Prerequisites

- Java 21 or higher
- Gradle 8.x or higher
- An IDE that supports Kotlin and Spring Boot (IntelliJ IDEA recommended)

## Technologies Used

- **Kotlin 1.9.25**: Modern, concise JVM language
- **Spring Boot 3.4.5**: Framework for building production-ready applications
- **Spring Security**: Authentication and authorization
- **Spring Data JDBC**: Simplified data access layer
- **H2 Database**: In-memory database for development and testing
- **JUnit 5**: Testing framework
- **AssertJ**: Fluent assertions for testing
- **Jackson**: JSON serialization/deserialization

## Architecture

The application follows a layered architecture:

1. **Controller Layer**: Handles HTTP requests and responses
2. **Service Layer**: Contains business logic and coordinates between controllers and repositories
3. **Repository Layer**: Manages data access
4. **Model Layer**: Defines the data structure

The application uses Spring Security for authentication and authorization, ensuring that users can only access their own cash cards.

## Folder Structure

```
src
├── main
│ ├── kotlin
│ │ └── com
│ │ └── db
│ │ └── cashcard
│ │ ├── CashCard.kt # Data model
│ │ ├── CashCardApplication.kt # Application entry point
│ │ ├── CashCardController.kt # REST controller
│ │ ├── CashCardRepository.kt # Data repository
│ │ ├── CashCardService.kt # Service interface
│ │ ├── CashCardServiceImpl.kt # Service implementation
│ │ └── SecurityConfig.kt # Security configuration
│ └── resources
│ ├── application.properties # Application configuration
│ └── schema.sql # Database schema
└── test
├── kotlin
│ └── com
│ └── db
│ └── cashcard
│ ├── CashCardJsonTest.kt # JSON serialization tests
│ └── CashcardApplicationTests.kt # Integration tests
└── resources
├── com
│ └── db
│ └── cashcard
│ ├── list.json # Test data
│ └── single.json # Test data
└── data.sql # Test data initialization
```

## Installation

1. Clone the repository:
```bash
git clone https://github.com/yourusername/cashcard.git
cd cashcard
```

2. Build the project:
```bash
./gradlew build
```

3. Run the application:
```bash
./gradlew bootRun
```

## Usage

The application exposes a RESTful API for managing cash cards. Here are some example commands using curl:

### Get a Cash Card

```bash
curl -X GET http://localhost:8080/cashcards/99 -u sarah1:abc123
```

### Create a Cash Card

```bash
curl -X POST http://localhost:8080/cashcards -H "Content-Type: application/json" -d '{"amount": 250.00}' -u sarah1:abc123
```

### Update a Cash Card

```bash
curl -X PUT http://localhost:8080/cashcards/99 -H "Content-Type: application/json" -d '{"amount": 300.00}' -u sarah1:abc123
```

### Delete a Cash Card

```bash
curl -X DELETE http://localhost:8080/cashcards/99 -u sarah1:abc123
```

### Get All Cash Cards (with pagination and sorting)

```bash
curl -X GET "http://localhost:8080/cashcards?page=0&size=5&sort=amount,desc" -u sarah1:abc123
```

## API Endpoints

| Method | URL | Description | Auth Required |
|--------|------------------------|--------------------------------------------------|---------------|
| GET | /cashcards/{id} | Get a cash card by ID | Yes |
| GET | /cashcards | Get all cash cards (supports pagination/sorting) | Yes |
| POST | /cashcards | Create a new cash card | Yes |
| PUT | /cashcards/{id} | Update an existing cash card | Yes |
| DELETE | /cashcards/{id} | Delete a cash card | Yes |

## Configuration

The application uses Spring Boot's default configuration with minimal customization:

### application.properties
```properties
spring.application.name=cashcard
```

### Database Schema (schema.sql)
```sql
CREATE TABLE cash_card
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
AMOUNT NUMBER NOT NULL DEFAULT 0,
OWNER VARCHAR(256) NOT NULL
);
```

## Testing

The application includes comprehensive tests:

1. **Integration Tests**: Test the entire application flow from HTTP requests to database operations
2. **JSON Tests**: Test JSON serialization and deserialization

To run the tests:

```bash
./gradlew test
```

The tests use an in-memory H2 database that is initialized with test data from `src/test/resources/data.sql`.

### Test Users

The application includes the following test users:

| Username | Password | Role |
|--------------------|----------|-------------|
| sarah1 | abc123 | CARD-OWNER |
| kumar2 | xyz789 | CARD-OWNER |
| hank-owns-no-cards | qrs456 | NON-OWNER |