Skip to content

Commit a2aa935

Browse files
Copilotaxellab
andcommitted
Add data processing microservice with API endpoints and tests
Co-authored-by: axellab <38169843+axellab@users.noreply.github.com>
1 parent 0230b7f commit a2aa935

File tree

6 files changed

+1040
-0
lines changed

6 files changed

+1040
-0
lines changed

DATA_PROCESSING_SERVICE.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# Data Processing Microservice
2+
3+
A microservice for processing and transforming information that can connect with other services.
4+
5+
## Overview
6+
7+
The Data Processing Microservice provides REST API endpoints for transforming and processing data in various ways. It supports operations like uppercase/lowercase transformations, filtering, mapping, and aggregation.
8+
9+
## API Endpoints
10+
11+
### Health Check
12+
- **GET** `/rest/data-processing/health`
13+
- Returns the health status of the microservice
14+
- Response:
15+
```json
16+
{
17+
"status": "healthy",
18+
"service": "data-processing",
19+
"timestamp": "2025-11-12T05:19:19.721Z"
20+
}
21+
```
22+
23+
### Get Available Operations
24+
- **GET** `/rest/data-processing/operations`
25+
- Returns a list of all available operations with descriptions and examples
26+
- Response:
27+
```json
28+
{
29+
"status": "success",
30+
"operations": [...]
31+
}
32+
```
33+
34+
### Process Data
35+
- **POST** `/rest/data-processing/process`
36+
- Processes and transforms data based on the specified operation
37+
- Request Body:
38+
```json
39+
{
40+
"operation": "uppercase|lowercase|filter|map|aggregate",
41+
"data": "any",
42+
"options": {}
43+
}
44+
```
45+
- Response:
46+
```json
47+
{
48+
"status": "success",
49+
"result": "transformed data"
50+
}
51+
```
52+
53+
### Process Multiple Sources
54+
- **POST** `/rest/data-processing/process-multiple`
55+
- Combines and processes data from multiple sources
56+
- Request Body:
57+
```json
58+
{
59+
"sources": [
60+
{ "name": "source1", "data": {...} },
61+
{ "name": "source2", "data": {...} }
62+
]
63+
}
64+
```
65+
66+
## Operations
67+
68+
### Uppercase
69+
Transforms string data to uppercase. Works with strings, arrays, and objects.
70+
71+
**Example:**
72+
```json
73+
{
74+
"operation": "uppercase",
75+
"data": "hello world"
76+
}
77+
```
78+
**Result:** `"HELLO WORLD"`
79+
80+
### Lowercase
81+
Transforms string data to lowercase. Works with strings, arrays, and objects.
82+
83+
**Example:**
84+
```json
85+
{
86+
"operation": "lowercase",
87+
"data": "HELLO WORLD"
88+
}
89+
```
90+
**Result:** `"hello world"`
91+
92+
### Filter
93+
Filters array data based on conditions.
94+
95+
**Example:**
96+
```json
97+
{
98+
"operation": "filter",
99+
"data": [
100+
{ "name": "John", "age": 30 },
101+
{ "name": "Jane", "age": 25 }
102+
],
103+
"options": {
104+
"conditions": { "age": 30 }
105+
}
106+
}
107+
```
108+
**Result:** `[{ "name": "John", "age": 30 }]`
109+
110+
### Map
111+
Maps array data to specific fields.
112+
113+
**Example:**
114+
```json
115+
{
116+
"operation": "map",
117+
"data": [
118+
{ "name": "John", "age": 30, "city": "NYC" }
119+
],
120+
"options": {
121+
"fields": ["name", "age"]
122+
}
123+
}
124+
```
125+
**Result:** `[{ "name": "John", "age": 30 }]`
126+
127+
### Aggregate
128+
Aggregates array data with optional grouping.
129+
130+
**Example:**
131+
```json
132+
{
133+
"operation": "aggregate",
134+
"data": [
135+
{ "category": "A", "value": 10 },
136+
{ "category": "A", "value": 20 },
137+
{ "category": "B", "value": 15 }
138+
],
139+
"options": {
140+
"groupBy": "category",
141+
"sumFields": ["value"]
142+
}
143+
}
144+
```
145+
**Result:**
146+
```json
147+
[
148+
{ "category": "A", "value": 30 },
149+
{ "category": "B", "value": 15 }
150+
]
151+
```
152+
153+
## Architecture
154+
155+
The microservice is composed of two main modules:
156+
157+
1. **lib/dataProcessing.ts** - Core processing logic
158+
- Data transformation functions
159+
- Validation logic
160+
- Processing algorithms
161+
162+
2. **routes/dataProcessing.ts** - REST API handlers
163+
- Express route handlers
164+
- Request validation
165+
- Response formatting
166+
167+
## Integration
168+
169+
The microservice is integrated into the OWASP Juice Shop application through the main server.ts file. The routes are registered under the `/rest/data-processing` path.
170+
171+
## Testing
172+
173+
Tests are located in:
174+
- `test/api/dataProcessingSpec.ts` - API integration tests
175+
- `test/server/dataProcessingSpec.ts` - Unit tests
176+
177+
Run tests with:
178+
```bash
179+
npm test
180+
```
181+
182+
## Usage Examples
183+
184+
### Using curl
185+
186+
```bash
187+
# Health check
188+
curl http://localhost:3000/rest/data-processing/health
189+
190+
# Transform to uppercase
191+
curl -X POST http://localhost:3000/rest/data-processing/process \
192+
-H "Content-Type: application/json" \
193+
-d '{"operation": "uppercase", "data": "hello world"}'
194+
195+
# Filter data
196+
curl -X POST http://localhost:3000/rest/data-processing/process \
197+
-H "Content-Type: application/json" \
198+
-d '{
199+
"operation": "filter",
200+
"data": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}],
201+
"options": {"conditions": {"age": 30}}
202+
}'
203+
```
204+
205+
### Using JavaScript/TypeScript
206+
207+
```typescript
208+
// Example using fetch
209+
const response = await fetch('http://localhost:3000/rest/data-processing/process', {
210+
method: 'POST',
211+
headers: {
212+
'Content-Type': 'application/json'
213+
},
214+
body: JSON.stringify({
215+
operation: 'uppercase',
216+
data: 'hello world'
217+
})
218+
});
219+
220+
const result = await response.json();
221+
console.log(result.result); // "HELLO WORLD"
222+
```
223+
224+
## Error Handling
225+
226+
The microservice returns appropriate HTTP status codes:
227+
- `200` - Success
228+
- `400` - Bad request (invalid input)
229+
- `500` - Internal server error
230+
231+
Error responses follow this format:
232+
```json
233+
{
234+
"status": "error",
235+
"error": "Error message"
236+
}
237+
```
238+
239+
## Future Enhancements
240+
241+
Possible future enhancements include:
242+
- Additional transformation operations
243+
- Batch processing capabilities
244+
- Data validation operations
245+
- Format conversion (JSON to XML, CSV, etc.)
246+
- Scheduled processing jobs
247+
- Webhook support for async processing

0 commit comments

Comments
 (0)