Skip to content

Commit 0cced3e

Browse files
authored
Added new endpoint for creating synthetic test data (eclipse-tractusx#377)
1 parent 11286d9 commit 0cced3e

File tree

9 files changed

+214
-38
lines changed

9 files changed

+214
-38
lines changed

mxd/backend-service/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ The Backend Service is used to simulate a data source or sink enabling the trans
1111
### Generate a random content
1212

1313
- Method: GET
14-
- URL: http://localhost/backend-service/api/v1/contents/random
14+
- URL: http://localhost/backend-service/api/v1/contents/random?size={size}
15+
- URL Parameter `size` (optional): Specifies the size of the random content to be generated. The size of the content can be expressed in either kilobytes (KB) or megabytes (MB), ranging from 1 KB to 10 MB.
1516
- Sample response:
1617

1718
```json
@@ -46,6 +47,21 @@ The Backend Service is used to simulate a data source or sink enabling the trans
4647
```
4748
This URL will be used as an endpoint in the transfer API.
4849

50+
### Create and save a random content
51+
52+
- Method: GET
53+
- URL: http://localhost/backend-service/api/v1/contents/create/random?size={size}
54+
- URL Parameter `size` (optional): Specifies the size of the random content to be generated. The size of the content can be expressed in either kilobytes (KB) or megabytes (MB), ranging from 1 KB to 10 MB.
55+
- Sample response:
56+
57+
```json
58+
{
59+
"id": "3b777103-5e06-461b-90c6-1f99e597f60d",
60+
"url": "http://localhost:8080/api/v1/contents/3b777103-5e06-461b-90c6-1f99e597f60d"
61+
}
62+
```
63+
This URL will be used as an endpoint in the transfer API.
64+
4965
### Fetch a content
5066

5167
- Method: GET

mxd/backend-service/assets/postman/BackendService.postman_collection.json

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
"listen": "test",
1414
"script": {
1515
"exec": [
16-
""
16+
"pm.test(\"Status code is 200\", function () {\r",
17+
" pm.response.to.have.status(200);\r",
18+
"});\r",
19+
"pm.test(\"Post Content\", function () {\r",
20+
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
21+
"});"
1722
],
1823
"type": "text/javascript",
1924
"packages": {}
@@ -35,7 +40,84 @@
3540
}
3641
}
3742
},
38-
"url": "http://localhost/backend-service/api/v1/contents/random"
43+
"url": {
44+
"raw": "http://localhost/backend-service/api/v1/contents/random?size=1KB",
45+
"protocol": "http",
46+
"host": [
47+
"localhost"
48+
],
49+
"path": [
50+
"backend-service",
51+
"api",
52+
"v1",
53+
"contents",
54+
"random"
55+
],
56+
"query": [
57+
{
58+
"key": "size",
59+
"value": "1KB"
60+
}
61+
]
62+
}
63+
},
64+
"response": []
65+
},
66+
{
67+
"name": "CreateRandomContent",
68+
"event": [
69+
{
70+
"listen": "test",
71+
"script": {
72+
"exec": [
73+
"pm.test(\"Status code is 200\", function () {\r",
74+
" pm.response.to.have.status(200);\r",
75+
"});\r",
76+
"pm.test(\"Post Content\", function () {\r",
77+
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
78+
"});"
79+
],
80+
"type": "text/javascript",
81+
"packages": {}
82+
}
83+
}
84+
],
85+
"protocolProfileBehavior": {
86+
"disableBodyPruning": true
87+
},
88+
"request": {
89+
"method": "GET",
90+
"header": [],
91+
"body": {
92+
"mode": "raw",
93+
"raw": "",
94+
"options": {
95+
"raw": {
96+
"language": "json"
97+
}
98+
}
99+
},
100+
"url": {
101+
"raw": "http://localhost/backend-service/api/v1/contents/create/random?size=10MB",
102+
"protocol": "http",
103+
"host": [
104+
"localhost"
105+
],
106+
"path": [
107+
"backend-service",
108+
"api",
109+
"v1",
110+
"contents",
111+
"create",
112+
"random"
113+
],
114+
"query": [
115+
{
116+
"key": "size",
117+
"value": "10MB"
118+
}
119+
]
120+
}
39121
},
40122
"response": []
41123
},
@@ -46,10 +128,12 @@
46128
"listen": "test",
47129
"script": {
48130
"exec": [
49-
"const responseJson = pm.response.json();\r",
50-
"const id = responseJson.id\r",
51-
"\r",
52-
"pm.collectionVariables.set('content-id', id);"
131+
"pm.test(\"Status code is 200\", function () {\r",
132+
" pm.response.to.have.status(200);\r",
133+
"});\r",
134+
"pm.test(\"Post Content\", function () {\r",
135+
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
136+
"});"
53137
],
54138
"type": "text/javascript",
55139
"packages": {}
@@ -61,7 +145,7 @@
61145
"header": [],
62146
"body": {
63147
"mode": "raw",
64-
"raw": "{\r\n \"userId\": 918704604,\r\n \"title\": \"agwng\",\r\n \"text\": \"oz\"\r\n}",
148+
"raw": "{\r\n \"userId\": 816339151,\r\n \"title\": \"aaaaaaaa\",\r\n \"text\": \"aaaaaaaa\"\r\n}",
65149
"options": {
66150
"raw": {
67151
"language": "json"
@@ -136,7 +220,7 @@
136220
"header": [],
137221
"body": {
138222
"mode": "raw",
139-
"raw": "{\r\n \"id\": \"31\",\r\n \"endpoint\": \"http://localhost:8080/api/v1/contents/{{content-id}}\",\r\n \"authKey\": \"Authorization\",\r\n \"authCode\": \"100000\",\r\n \"properties\": {}\r\n}",
223+
"raw": "{\r\n \"id\": \"35\",\r\n \"endpoint\": \"http://localhost:8080/api/v1/contents/{{content-id}}\",\r\n \"authKey\": \"Authorization\",\r\n \"authCode\": \"100000\",\r\n \"properties\": {}\r\n}",
140224
"options": {
141225
"raw": {
142226
"language": "json"
@@ -190,22 +274,40 @@
190274
"response": []
191275
}
192276
],
193-
"variable": [
277+
"event": [
194278
{
195-
"key": "content-id",
196-
"value": ""
279+
"listen": "prerequest",
280+
"script": {
281+
"type": "text/javascript",
282+
"packages": {},
283+
"exec": [
284+
""
285+
]
286+
}
197287
},
198288
{
199-
"key": "create-req",
200-
"value": ""
201-
},
289+
"listen": "test",
290+
"script": {
291+
"type": "text/javascript",
292+
"packages": {},
293+
"exec": [
294+
""
295+
]
296+
}
297+
}
298+
],
299+
"variable": [
202300
{
203-
"key": "content-url",
301+
"key": "content-id",
204302
"value": ""
205303
},
206304
{
207305
"key": "transfer-id",
208306
"value": ""
307+
},
308+
{
309+
"value": "",
310+
"disabled": true
209311
}
210312
]
211313
}

mxd/backend-service/src/main/java/org/eclipse/tractusx/mxd/backendservice/controller/ContentApiController.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,44 @@ public String getContentByID(@PathParam("contentId") String contentId) {
6868
return Optional.of(contentId)
6969
.map(id -> service.getContent(contentId))
7070
.map(content -> content.getContent() != null ? content.getContent().getData() : Converter.toJson(content.getFailure(), objectMapper))
71-
.orElse(Constants.DEFAULT_ERROR_MESSAGE);
71+
.orElse(Constants.CONTENT_ID_ERROR_MESSAGE);
7272
}
7373

7474
@GET
7575
@Path("/random")
76-
public String getRandomContent() {
77-
return this.service.getRandomContent();
76+
public String getRandomContent(@QueryParam("size") @DefaultValue("1KB") String size) {
77+
return parseSize(size)
78+
.map(sizeInBytes -> this.service.getRandomContent(sizeInBytes))
79+
.orElse(Constants.CONTENT_SIZE_ERROR_MESSAGE);
80+
}
81+
82+
@GET
83+
@Path("/create/random")
84+
public String createRandomContent(@QueryParam("size") @DefaultValue("1KB") String size) {
85+
return parseSize(size)
86+
.map(sizeInBytes -> this.service.createRandomContent(sizeInBytes))
87+
.map(this::createJsonResponse)
88+
.orElse(Constants.CONTENT_SIZE_ERROR_MESSAGE);
89+
}
90+
91+
private Optional<Integer> parseSize(String size) {
92+
try {
93+
int sizeInBytes;
94+
if (size.endsWith("KB")) {
95+
sizeInBytes = Integer.parseInt(size.replace("KB", "").trim()) * 1024;
96+
} else if (size.endsWith("MB")) {
97+
sizeInBytes = Integer.parseInt(size.replace("MB", "").trim()) * 1024 * 1024;
98+
} else {
99+
return Optional.empty();
100+
}
101+
if (sizeInBytes > 10 * 1024 * 1024 || sizeInBytes < 1024) {
102+
return Optional.empty();
103+
}
104+
105+
return Optional.of(sizeInBytes);
106+
} catch (NumberFormatException e) {
107+
return Optional.empty();
108+
}
78109
}
79110

80111
private String createJsonResponse(String id) {

mxd/backend-service/src/main/java/org/eclipse/tractusx/mxd/backendservice/service/ContentService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public interface ContentService {
2727

2828
ServiceResult<ContentResponse> getContent(String contentId);
2929

30-
String getRandomContent();
30+
String getRandomContent(int size);
31+
32+
String createRandomContent(int size);
3133

3234
}

mxd/backend-service/src/main/java/org/eclipse/tractusx/mxd/backendservice/service/ContentServiceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public ServiceResult<ContentResponse> getContent(String contentId) {
5656
}
5757

5858
@Override
59-
public String getRandomContent() {
60-
return RandomWordUtil.generateRandom();
59+
public String getRandomContent(int size) {
60+
return RandomWordUtil.generateRandom(size);
61+
}
62+
63+
@Override
64+
public String createRandomContent(int size) {
65+
Object content = RandomWordUtil.generateRandom(size);
66+
return contentStoreService.save(content);
6167
}
6268
}

mxd/backend-service/src/main/java/org/eclipse/tractusx/mxd/util/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
public class Constants {
1818

1919
public static final String DEFAULT_ERROR_MESSAGE = "Unexpected error from backend service";
20+
public static final String CONTENT_ID_ERROR_MESSAGE = "Invalid content id";
21+
public static final String CONTENT_SIZE_ERROR_MESSAGE = "Invalid size. Size must be specified in KB (1KB-10240KB) or MB (1MB-10MB).";
2022
public static final String DEFAULT_DRIVE = "org.postgresql.Driver";
2123
public static final String DATASOURCE_NAME_SETTING = "edc.datasource.backendservice.name";
2224

mxd/backend-service/src/main/java/org/eclipse/tractusx/mxd/util/RandomWordUtil.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
package org.eclipse.tractusx.mxd.util;
1616

1717
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import org.apache.commons.lang3.StringUtils;
1819
import org.eclipse.edc.spi.EdcException;
1920

2021
import java.security.SecureRandom;
2122

2223
public class RandomWordUtil {
2324

24-
public static String generateRandom() {
25+
public static String generateRandom(int size) {
2526
try {
2627
ObjectMapper objectMapper = new ObjectMapper();
2728
RandomData randomData = new RandomData();
2829
randomData.setUserId(generateRandomUserId());
29-
randomData.setTitle(generateRandomString());
30-
randomData.setText(generateRandomString());
30+
randomData.setTitle(generateRandomString(8));
31+
randomData.setText(generateRandomString(size));
3132
return objectMapper.writeValueAsString(randomData);
3233
} catch (Exception e) {
3334
throw new EdcException(e.getMessage());
@@ -38,17 +39,8 @@ private static int generateRandomUserId() {
3839
return Math.abs(new SecureRandom().nextInt());
3940
}
4041

41-
private static String generateRandomString() {
42-
String characters = "abcdefghijklmnopqrstuvwxyz";
43-
SecureRandom random = new SecureRandom();
44-
StringBuilder sb = new StringBuilder();
45-
46-
int length = random.nextInt(8) + 1;
47-
for (int i = 0; i < length; i++) {
48-
int index = random.nextInt(characters.length());
49-
sb.append(characters.charAt(index));
50-
}
51-
return sb.toString();
42+
private static String generateRandomString(int length) {
43+
return StringUtils.repeat("a", length);
5244
}
5345

5446
private static class RandomData {

mxd/backend-service/src/test/java/org/eclipse/tractusx/mxd/e2e/ContentApiEndToEndTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,36 @@ void getRandomContent() {
111111
JsonObject content = getContentJson();
112112

113113
assertThat(content.getString("title")).isNotNull();
114-
assertThat(content.getString("text")).isNotNull();
114+
assertThat(content.getString("text")).hasSize(1024);
115115
assertThat(content.getInt("userId")).isGreaterThan(-1);
116116
}
117117

118+
@Test
119+
void createRandomContent() {
120+
var responseBody = baseRequest()
121+
.when()
122+
.get(ENDPOINT + "create/random?size=1KB")
123+
.then()
124+
.log().ifValidationFails()
125+
.statusCode(200)
126+
.contentType(JSON);
127+
128+
String contentId = responseBody.extract().jsonPath()
129+
.getString("id");
130+
131+
responseBody = baseRequest()
132+
.when()
133+
.get(ENDPOINT + contentId)
134+
.then()
135+
.log().ifValidationFails()
136+
.statusCode(200)
137+
.contentType(JSON);
138+
139+
assertThat(responseBody.extract().jsonPath().getString("title")).isNotNull();
140+
assertThat(responseBody.extract().jsonPath().getString("text")).hasSize(1024);
141+
assertThat(responseBody.extract().jsonPath().getInt("userId")).isGreaterThan(-1);
142+
}
143+
118144
String createContent(JsonObject contentJson) {
119145
var responseBody = baseRequest()
120146
.contentType(ContentType.JSON)

mxd/backend-service/src/test/java/org/eclipse/tractusx/mxd/testfixtures/PostgresqlEndToEndInstance.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ static void createDatabase(String dbName) {
6868
throw new EdcPersistenceException(exception.getMessage(), exception);
6969
}
7070
} catch (Exception e) {
71-
System.out.println("Ex : " + e.getMessage());
7271
throw new EdcPersistenceException(e);
7372
}
7473
}

0 commit comments

Comments
 (0)