Skip to content

Commit

Permalink
Added new endpoint for creating synthetic test data (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
ieuna authored Jul 8, 2024
1 parent 11286d9 commit 0cced3e
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 38 deletions.
18 changes: 17 additions & 1 deletion mxd/backend-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ The Backend Service is used to simulate a data source or sink enabling the trans
### Generate a random content

- Method: GET
- URL: http://localhost/backend-service/api/v1/contents/random
- URL: http://localhost/backend-service/api/v1/contents/random?size={size}
- 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.
- Sample response:

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

### Create and save a random content

- Method: GET
- URL: http://localhost/backend-service/api/v1/contents/create/random?size={size}
- 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.
- Sample response:

```json
{
"id": "3b777103-5e06-461b-90c6-1f99e597f60d",
"url": "http://localhost:8080/api/v1/contents/3b777103-5e06-461b-90c6-1f99e597f60d"
}
```
This URL will be used as an endpoint in the transfer API.

### Fetch a content

- Method: GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"listen": "test",
"script": {
"exec": [
""
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});\r",
"pm.test(\"Post Content\", function () {\r",
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
"});"
],
"type": "text/javascript",
"packages": {}
Expand All @@ -35,7 +40,84 @@
}
}
},
"url": "http://localhost/backend-service/api/v1/contents/random"
"url": {
"raw": "http://localhost/backend-service/api/v1/contents/random?size=1KB",
"protocol": "http",
"host": [
"localhost"
],
"path": [
"backend-service",
"api",
"v1",
"contents",
"random"
],
"query": [
{
"key": "size",
"value": "1KB"
}
]
}
},
"response": []
},
{
"name": "CreateRandomContent",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});\r",
"pm.test(\"Post Content\", function () {\r",
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost/backend-service/api/v1/contents/create/random?size=10MB",
"protocol": "http",
"host": [
"localhost"
],
"path": [
"backend-service",
"api",
"v1",
"contents",
"create",
"random"
],
"query": [
{
"key": "size",
"value": "10MB"
}
]
}
},
"response": []
},
Expand All @@ -46,10 +128,12 @@
"listen": "test",
"script": {
"exec": [
"const responseJson = pm.response.json();\r",
"const id = responseJson.id\r",
"\r",
"pm.collectionVariables.set('content-id', id);"
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);\r",
"});\r",
"pm.test(\"Post Content\", function () {\r",
" pm.collectionVariables.set('content-id', pm.response.json().id);\r",
"});"
],
"type": "text/javascript",
"packages": {}
Expand All @@ -61,7 +145,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"userId\": 918704604,\r\n \"title\": \"agwng\",\r\n \"text\": \"oz\"\r\n}",
"raw": "{\r\n \"userId\": 816339151,\r\n \"title\": \"aaaaaaaa\",\r\n \"text\": \"aaaaaaaa\"\r\n}",
"options": {
"raw": {
"language": "json"
Expand Down Expand Up @@ -136,7 +220,7 @@
"header": [],
"body": {
"mode": "raw",
"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}",
"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}",
"options": {
"raw": {
"language": "json"
Expand Down Expand Up @@ -190,22 +274,40 @@
"response": []
}
],
"variable": [
"event": [
{
"key": "content-id",
"value": ""
"listen": "prerequest",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
},
{
"key": "create-req",
"value": ""
},
"listen": "test",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
}
],
"variable": [
{
"key": "content-url",
"key": "content-id",
"value": ""
},
{
"key": "transfer-id",
"value": ""
},
{
"value": "",
"disabled": true
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,44 @@ public String getContentByID(@PathParam("contentId") String contentId) {
return Optional.of(contentId)
.map(id -> service.getContent(contentId))
.map(content -> content.getContent() != null ? content.getContent().getData() : Converter.toJson(content.getFailure(), objectMapper))
.orElse(Constants.DEFAULT_ERROR_MESSAGE);
.orElse(Constants.CONTENT_ID_ERROR_MESSAGE);
}

@GET
@Path("/random")
public String getRandomContent() {
return this.service.getRandomContent();
public String getRandomContent(@QueryParam("size") @DefaultValue("1KB") String size) {
return parseSize(size)
.map(sizeInBytes -> this.service.getRandomContent(sizeInBytes))
.orElse(Constants.CONTENT_SIZE_ERROR_MESSAGE);
}

@GET
@Path("/create/random")
public String createRandomContent(@QueryParam("size") @DefaultValue("1KB") String size) {
return parseSize(size)
.map(sizeInBytes -> this.service.createRandomContent(sizeInBytes))
.map(this::createJsonResponse)
.orElse(Constants.CONTENT_SIZE_ERROR_MESSAGE);
}

private Optional<Integer> parseSize(String size) {
try {
int sizeInBytes;
if (size.endsWith("KB")) {
sizeInBytes = Integer.parseInt(size.replace("KB", "").trim()) * 1024;
} else if (size.endsWith("MB")) {
sizeInBytes = Integer.parseInt(size.replace("MB", "").trim()) * 1024 * 1024;
} else {
return Optional.empty();
}
if (sizeInBytes > 10 * 1024 * 1024 || sizeInBytes < 1024) {
return Optional.empty();
}

return Optional.of(sizeInBytes);
} catch (NumberFormatException e) {
return Optional.empty();
}
}

private String createJsonResponse(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface ContentService {

ServiceResult<ContentResponse> getContent(String contentId);

String getRandomContent();
String getRandomContent(int size);

String createRandomContent(int size);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ public ServiceResult<ContentResponse> getContent(String contentId) {
}

@Override
public String getRandomContent() {
return RandomWordUtil.generateRandom();
public String getRandomContent(int size) {
return RandomWordUtil.generateRandom(size);
}

@Override
public String createRandomContent(int size) {
Object content = RandomWordUtil.generateRandom(size);
return contentStoreService.save(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
public class Constants {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
package org.eclipse.tractusx.mxd.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.edc.spi.EdcException;

import java.security.SecureRandom;

public class RandomWordUtil {

public static String generateRandom() {
public static String generateRandom(int size) {
try {
ObjectMapper objectMapper = new ObjectMapper();
RandomData randomData = new RandomData();
randomData.setUserId(generateRandomUserId());
randomData.setTitle(generateRandomString());
randomData.setText(generateRandomString());
randomData.setTitle(generateRandomString(8));
randomData.setText(generateRandomString(size));
return objectMapper.writeValueAsString(randomData);
} catch (Exception e) {
throw new EdcException(e.getMessage());
Expand All @@ -38,17 +39,8 @@ private static int generateRandomUserId() {
return Math.abs(new SecureRandom().nextInt());
}

private static String generateRandomString() {
String characters = "abcdefghijklmnopqrstuvwxyz";
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder();

int length = random.nextInt(8) + 1;
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
private static String generateRandomString(int length) {
return StringUtils.repeat("a", length);
}

private static class RandomData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,36 @@ void getRandomContent() {
JsonObject content = getContentJson();

assertThat(content.getString("title")).isNotNull();
assertThat(content.getString("text")).isNotNull();
assertThat(content.getString("text")).hasSize(1024);
assertThat(content.getInt("userId")).isGreaterThan(-1);
}

@Test
void createRandomContent() {
var responseBody = baseRequest()
.when()
.get(ENDPOINT + "create/random?size=1KB")
.then()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON);

String contentId = responseBody.extract().jsonPath()
.getString("id");

responseBody = baseRequest()
.when()
.get(ENDPOINT + contentId)
.then()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON);

assertThat(responseBody.extract().jsonPath().getString("title")).isNotNull();
assertThat(responseBody.extract().jsonPath().getString("text")).hasSize(1024);
assertThat(responseBody.extract().jsonPath().getInt("userId")).isGreaterThan(-1);
}

String createContent(JsonObject contentJson) {
var responseBody = baseRequest()
.contentType(ContentType.JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static void createDatabase(String dbName) {
throw new EdcPersistenceException(exception.getMessage(), exception);
}
} catch (Exception e) {
System.out.println("Ex : " + e.getMessage());
throw new EdcPersistenceException(e);
}
}
Expand Down

0 comments on commit 0cced3e

Please sign in to comment.