zwischenstand PC reset
This commit is contained in:
parent
ea1a06820f
commit
d823c3ea36
@ -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<String, ObjectAttribute> attributes;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ public abstract class ObjectAttribute <T> {
|
||||
private T defaultValue;
|
||||
|
||||
public ObjectAttribute() {
|
||||
this.type = getClass().getSimpleName();
|
||||
this.type = this.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
|
@ -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<CObjectEntity> 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<String, ObjectAttribute> 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);
|
||||
}
|
||||
}
|
75
src/test/java/de/w665/fluidcms/ObjectResourceTest.java
Normal file
75
src/test/java/de/w665/fluidcms/ObjectResourceTest.java
Normal file
@ -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<String, ObjectAttribute> 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");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user