From d823c3ea360c6d741084a763d0b98d04a65172cf Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 26 Jul 2024 23:57:07 +0200 Subject: [PATCH] zwischenstand PC reset --- .../java/de/w665/fluidcms/model/CObject.java | 2 +- .../w665/fluidcms/model/ObjectAttribute.java | 2 +- .../fluidcms/rest/mapping/ObjectResource.java | 76 +++++++++++++++++++ .../de/w665/fluidcms/ObjectResourceTest.java | 75 ++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/w665/fluidcms/rest/mapping/ObjectResource.java create mode 100644 src/test/java/de/w665/fluidcms/ObjectResourceTest.java diff --git a/src/main/java/de/w665/fluidcms/model/CObject.java b/src/main/java/de/w665/fluidcms/model/CObject.java index 1bfd515..bf6265a 100644 --- a/src/main/java/de/w665/fluidcms/model/CObject.java +++ b/src/main/java/de/w665/fluidcms/model/CObject.java @@ -14,6 +14,6 @@ import java.util.Map; public class CObject { private String id; private String name; - private ObjectType type; // TODO: When developing the object repository, replace this with a reference to the object type (e.g. ObjectType ID) + private ObjectType type; private Map attributes; } diff --git a/src/main/java/de/w665/fluidcms/model/ObjectAttribute.java b/src/main/java/de/w665/fluidcms/model/ObjectAttribute.java index e09a7ab..1ad5f5b 100644 --- a/src/main/java/de/w665/fluidcms/model/ObjectAttribute.java +++ b/src/main/java/de/w665/fluidcms/model/ObjectAttribute.java @@ -23,7 +23,7 @@ public abstract class ObjectAttribute { private T defaultValue; public ObjectAttribute() { - this.type = getClass().getSimpleName(); + this.type = this.getClass().getSimpleName(); } @JsonIgnore diff --git a/src/main/java/de/w665/fluidcms/rest/mapping/ObjectResource.java b/src/main/java/de/w665/fluidcms/rest/mapping/ObjectResource.java new file mode 100644 index 0000000..87104db --- /dev/null +++ b/src/main/java/de/w665/fluidcms/rest/mapping/ObjectResource.java @@ -0,0 +1,76 @@ +package de.w665.fluidcms.rest.mapping; + +import de.w665.fluidcms.db.entity.CObjectEntity; +import de.w665.fluidcms.db.repo.CObjectRepository; +import de.w665.fluidcms.db.repo.ObjectTypeRepository; +import de.w665.fluidcms.model.ObjectAttribute; +import de.w665.fluidcms.model.ObjectType; +import jakarta.inject.Inject; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import lombok.extern.slf4j.Slf4j; +import org.jboss.resteasy.reactive.RestResponse; + +import java.util.Map; + +@Slf4j +@Path("/object") +public class ObjectResource { + + @Inject + ObjectTypeRepository objectTypeRepository; + + @Inject + CObjectRepository cObjectRepository; + + @POST + @Consumes("application/json") + @Produces("application/json") + public RestResponse createObject(CObjectEntity objectEntity) { + // Verify if objectType exists + // Verify if attributes are valid (matching the object type) + // Insert object into database + log.debug(objectEntity.toString()); + ObjectType objectType = objectTypeRepository.get(objectEntity.getObjectTypeId()); + if(objectType == null) { + log.error("Object type with ID {} not found", objectEntity.getObjectTypeId()); + return RestResponse.status(400, "Invalid object type."); + } + // Loop through attributes and verify if they match the object type attributes + for(String key : objectEntity.getAttributes().keySet()) { + if(objectType.getAttributes().stream().noneMatch(attr -> attr.getName().equals(key))) { + log.error("Attribute {} not found in object type {}", key, objectType.getName()); + return RestResponse.status(400, "Invalid attribute."); + } + } + + // Verify (type) if object attributes are match object type attributes + for (Map.Entry entry : objectEntity.getAttributes().entrySet()) { + String key = entry.getKey(); + ObjectAttribute entityAttribute = entry.getValue(); + + // Find the corresponding attribute in the objectType + ObjectAttribute objectTypeAttribute = objectType.getAttributes().stream() + .filter(attr -> attr.getName().equals(key)) + .findFirst() + .orElse(null); + + if (objectTypeAttribute == null) { + log.error("Attribute {} not found in object type {}", key, objectType.getName()); + return RestResponse.status(400, "Invalid attribute."); + } + + // Check if the value type is valid + if (!objectTypeAttribute.verifyValueType(entityAttribute.getValue())) { + log.error("Attribute {} type does not match object type {}", key, objectType.getName()); + return RestResponse.status(400, "Invalid attribute type."); + } + } + + cObjectRepository.insert(objectEntity); + + return RestResponse.ok(objectEntity); + } +} diff --git a/src/test/java/de/w665/fluidcms/ObjectResourceTest.java b/src/test/java/de/w665/fluidcms/ObjectResourceTest.java new file mode 100644 index 0000000..6ba1743 --- /dev/null +++ b/src/test/java/de/w665/fluidcms/ObjectResourceTest.java @@ -0,0 +1,75 @@ +package de.w665.fluidcms; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import de.w665.fluidcms.model.CObject; +import de.w665.fluidcms.model.ObjectAttribute; +import de.w665.fluidcms.model.ObjectType; +import de.w665.fluidcms.model.objectattributes.StringObjectAttribute; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static io.restassured.RestAssured.given; + +public class ObjectResourceTest { + @Test + void testObjectEndpoint() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + + // Create sample data using Java objects + StringObjectAttribute attribute1 = new StringObjectAttribute(); + attribute1.setName("Attribute 13"); + attribute1.setDescription("This is attribute 13"); + attribute1.setRequired(true); + attribute1.setDefaultValue("Default Value 13"); + attribute1.setValue("Attribute 13 Value"); + + ObjectType objectType = new ObjectType(); + objectType.setName("Object Type 71"); + objectType.setDescription("This is object type 71"); + objectType.setIcon("icon.png"); + objectType.setColor("blue"); + objectType.setAttributes(Arrays.asList(attribute1)); + + Map attributes = new HashMap<>(); + attributes.put(attribute1.getName(), attribute1); + + CObject sampleData = new CObject(); + sampleData.setName("Object 71"); + sampleData.setType(objectType); + sampleData.setAttributes(attributes); + + // Serialize Java object to JSON + String jsonSampleData = objectMapper.writeValueAsString(sampleData); + + // Perform the request and validate the response + String responseJson = given() + .contentType(ContentType.JSON) + .body(jsonSampleData) + .when().post("/api/v1/object") + .then() + .statusCode(200) + .extract().asString(); + + // Deserialize response JSON to Java object + CObject responseData = objectMapper.readValue(responseJson, CObject.class); + + // Perform assertions on the deserialized Java object + assert responseData.getId() != null; + assert responseData.getName().equals("Object 71"); + assert responseData.getType().getName().equals("Object Type 71"); + assert responseData.getAttributes().size() == 1; + + StringObjectAttribute responseAttribute1 = (StringObjectAttribute) responseData.getAttributes().get("Attribute 13"); + assert responseAttribute1.getType().equals("StringObjectAttribute"); + assert responseAttribute1.getName().equals("Attribute 13"); + assert responseAttribute1.getDescription().equals("This is attribute 13"); + assert responseAttribute1.isRequired(); + assert responseAttribute1.getDefaultValue().equals("Default Value 13"); + assert responseAttribute1.getValue().equals("Attribute 13 Value"); + } +}