Compare commits

...

4 Commits

Author SHA1 Message Date
Max
d823c3ea36 zwischenstand PC reset 2024-07-26 23:57:07 +02:00
Max
ea1a06820f Refactor CObjectRepository
Refactor implementation structure
Add RestEndpoint test
Refactor ObjectAttribute
2024-06-20 20:08:52 +02:00
Max
fdb5f426be Add CObject, CObjectEntity, Repository
Add ObjectTypeRepository
Refactor ObjectType attributes to List
2024-06-18 00:22:22 +02:00
Max
38c7658854 Add ObjectType
Add ObjectAttribute
Add rest ObjectTypeResource
Add sample ObjectAttribute String implementation
Add jackson dependency
2024-06-17 23:10:46 +02:00
12 changed files with 557 additions and 0 deletions

View File

@ -15,6 +15,7 @@ dependencies {
implementation 'io.quarkus:quarkus-arc'
testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
implementation 'io.quarkus:quarkus-rest-jackson:3.11.2'
compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'
implementation ('com.rethinkdb:rethinkdb-driver:2.4.4') {

View File

@ -0,0 +1,20 @@
package de.w665.fluidcms.db.entity;
import de.w665.fluidcms.model.ObjectAttribute;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class CObjectEntity {
private String id;
private String name;
private String objectTypeId;
private Map<String, ObjectAttribute> attributes;
}

View File

@ -0,0 +1,69 @@
package de.w665.fluidcms.db.repo;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import de.w665.fluidcms.db.RethinkDBConfig;
import de.w665.fluidcms.db.RethinkDBConnector;
import de.w665.fluidcms.db.entity.CObjectEntity;
import de.w665.fluidcms.model.CObject;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ApplicationScoped
public class CObjectRepository {
@Inject
RethinkDBConnector connector;
@Inject
RethinkDBConfig config;
@Inject
ObjectTypeRepository objectTypeRepository;
private final String TABLE_NAME = "c_objects";
private RethinkDB r;
private Connection connection;
@PostConstruct
public void init() {
this.r = this.connector.getR();
this.connection = this.connector.getConnection();
// Check if table exists
try {
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " created");
} catch (Exception e) {
log.debug("Table " + TABLE_NAME + " already exists");
if (config.isAutoresetonstartup()) {
r.db(config.getDatabase()).tableDrop(TABLE_NAME).run(connection);
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " reset");
}
}
}
public void insert(CObject cObject) {
// Create entity CObject and replace ObjectType with a reference to the object type (e.g. ObjectType ID)
CObjectEntity entity = new CObjectEntity(cObject.getId(), cObject.getName(), cObject.getType().getId(), cObject.getAttributes());
r.db(config.getDatabase()).table(TABLE_NAME).insert(entity).run(connection);
}
public void insert(CObjectEntity entity) {
r.db(config.getDatabase()).table(TABLE_NAME).insert(entity).run(connection);
}
public CObject get(String id) {
CObjectEntity entity = r.db(config.getDatabase()).table(TABLE_NAME).filter(r.hashMap("id", id)).run(connection, CObjectEntity.class).first();
if(entity == null) {
log.warn("Entity CObject with ID {} not found", id);
return null;
}
// Construct CObject from entity
return new CObject(entity.getId(), entity.getName(), objectTypeRepository.get(entity.getId()), entity.getAttributes());
}
}

View File

@ -0,0 +1,54 @@
package de.w665.fluidcms.db.repo;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import de.w665.fluidcms.db.RethinkDBConfig;
import de.w665.fluidcms.db.RethinkDBConnector;
import de.w665.fluidcms.model.ObjectType;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ApplicationScoped
public class ObjectTypeRepository {
@Inject
RethinkDBConnector connector;
@Inject
RethinkDBConfig config;
private final String TABLE_NAME = "object_types";
private RethinkDB r;
private Connection connection;
@PostConstruct
public void init() {
this.r = this.connector.getR();
this.connection = this.connector.getConnection();
// Check if table exists
try {
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " created");
} catch (Exception e) {
log.debug("Table " + TABLE_NAME + " already exists");
if(config.isAutoresetonstartup()) {
r.db(config.getDatabase()).tableDrop(TABLE_NAME).run(connection);
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
log.debug("Table " + TABLE_NAME + " reset");
}
}
}
public void insert(ObjectType objectType) {
objectType.setId(r.uuid().run(connection, String.class).first());
r.db(config.getDatabase()).table(TABLE_NAME).insert(objectType).run(connection);
}
public ObjectType get(String id) {
return r.db(config.getDatabase()).table(TABLE_NAME).filter(r.hashMap("id", id)).run(connection, ObjectType.class).first();
}
}

View File

@ -0,0 +1,19 @@
package de.w665.fluidcms.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class CObject {
private String id;
private String name;
private ObjectType type;
private Map<String, ObjectAttribute> attributes;
}

View File

@ -0,0 +1,34 @@
package de.w665.fluidcms.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import de.w665.fluidcms.model.objectattributes.StringObjectAttribute;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = StringObjectAttribute.class, name = "String"),
})
public abstract class ObjectAttribute <T> {
private String type;
private String name;
private String description;
private boolean required;
private T defaultValue;
public ObjectAttribute() {
this.type = this.getClass().getSimpleName();
}
@JsonIgnore
public abstract String getValueAsString();
public abstract T getValue();
public abstract void setValue(T value);
public abstract boolean verifyValueType(Object value);
}

View File

@ -0,0 +1,19 @@
package de.w665.fluidcms.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
@Getter
@Setter
@ToString
public class ObjectType {
private String id;
private String name;
private String description;
private String icon;
private String color;
private List<ObjectAttribute> attributes;
}

View File

@ -0,0 +1,26 @@
package de.w665.fluidcms.model.objectattributes;
import de.w665.fluidcms.model.ObjectAttribute;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class StringObjectAttribute extends ObjectAttribute<String> {
private String value;
@Override
public String getValueAsString() {
return value;
}
@Override
public void setValue(String value) {
this.value = value;
}
@Override
public boolean verifyValueType(Object value) {
return value instanceof String;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,71 @@
package de.w665.fluidcms.rest.mapping;
import de.w665.fluidcms.db.repo.ObjectTypeRepository;
import de.w665.fluidcms.model.ObjectAttribute;
import de.w665.fluidcms.model.ObjectType;
import de.w665.fluidcms.model.objectattributes.StringObjectAttribute;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.reactive.RestResponse;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Path("/objectType")
public class ObjectTypeResource {
@Inject
ObjectTypeRepository objectTypeRepository;
@POST
@Consumes("application/json")
@Produces("application/json")
public RestResponse<ObjectType> createObjectType(ObjectType objectType) {
log.debug(objectType.toString());
objectTypeRepository.insert(objectType);
return RestResponse.ok(objectType);
}
@GET
@Produces("application/json")
public RestResponse<ObjectType> getObjectType() {
StringObjectAttribute attr1 = new StringObjectAttribute();
String number = String.valueOf((int) (Math.random() * 100));
attr1.setName("Attribute " + number);
attr1.setDescription("This is attribute " + number);
attr1.setRequired(true);
attr1.setDefaultValue("Default Value " + number);
attr1.setValue("Attribute " + number + " Value");
number = String.valueOf((int) (Math.random() * 100));
StringObjectAttribute attr2 = new StringObjectAttribute();
attr2.setName("Attribute " + number);
attr2.setDescription("This is attribute " + number);
attr2.setRequired(false);
attr2.setDefaultValue("Default Value " + number);
attr2.setValue("Attribute " + number + " Value");
// Add the StringObjectAttribute objects to a Map
List<ObjectAttribute> attributes = new ArrayList<>();
attributes.add(attr1);
attributes.add(attr2);
// Create an ObjectType object and set the attributes
ObjectType objectType = new ObjectType();
objectType.setId("123");
number = String.valueOf((int) (Math.random() * 100));
objectType.setName("Object Type " + number);
objectType.setDescription("This is object type " + number);
// random number
objectType.setIcon("icon.png");
objectType.setColor("blue");
objectType.setAttributes(attributes);
return RestResponse.ok(objectType);
}
}

View 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");
}
}

View File

@ -0,0 +1,93 @@
package de.w665.fluidcms;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.w665.fluidcms.model.ObjectType;
import de.w665.fluidcms.model.objectattributes.StringObjectAttribute;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static io.restassured.RestAssured.given;
@QuarkusTest
class ObjectTypeResourceTest {
@Test
void testObjectTypeEndpoint() 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");
StringObjectAttribute attribute2 = new StringObjectAttribute();
attribute2.setName("Attribute 94");
attribute2.setDescription("This is attribute 94");
attribute2.setRequired(false);
attribute2.setDefaultValue("Default Value 94");
attribute2.setValue("Attribute 94 Value");
ObjectType sampleData = new ObjectType();
sampleData.setName("Object Type 71");
sampleData.setDescription("This is object type 71");
sampleData.setIcon("icon.png");
sampleData.setColor("blue");
sampleData.setAttributes(Arrays.asList(attribute1, attribute2));
// Serialize Java object to JSON
String jsonSampleData = objectMapper.writeValueAsString(sampleData);
System.out.println("--------------JSON SAMPLE DATA--------------");
System.out.println(jsonSampleData);
System.out.println("--------------------------------------------");
// Perform the request and validate the response
String responseJson = given()
.contentType(ContentType.JSON)
.body(jsonSampleData)
.when().post("/api/v1/objectType")
.then()
.statusCode(200)
.extract().asString();
System.out.println("--------------RESPONSE JSON-----------------");
System.out.println(jsonSampleData);
System.out.println("--------------------------------------------");
// Deserialize response JSON to Java object
ObjectType responseData = objectMapper.readValue(responseJson, ObjectType.class);
// Perform assertions on the deserialized Java object
assert responseData.getId() != null;
assert responseData.getName().equals("Object Type 71");
assert responseData.getDescription().equals("This is object type 71");
assert responseData.getIcon().equals("icon.png");
assert responseData.getColor().equals("blue");
assert responseData.getAttributes().size() == 2;
StringObjectAttribute responseAttribute1 = (StringObjectAttribute) responseData.getAttributes().getFirst();
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");
assert responseAttribute1.getValueAsString().equals("Attribute 13 Value");
StringObjectAttribute responseAttribute2 = (StringObjectAttribute) responseData.getAttributes().get(1);
assert responseAttribute1.getType().equals("StringObjectAttribute");
assert responseAttribute2.getName().equals("Attribute 94");
assert responseAttribute2.getDescription().equals("This is attribute 94");
assert !responseAttribute2.isRequired();
assert responseAttribute2.getDefaultValue().equals("Default Value 94");
assert responseAttribute2.getValue().equals("Attribute 94 Value");
assert responseAttribute2.getValueAsString().equals("Attribute 94 Value");
}
}