Refactor CObjectRepository

Refactor implementation structure
Add RestEndpoint test
Refactor ObjectAttribute
This commit is contained in:
Max W. 2024-06-20 20:08:52 +02:00
parent fdb5f426be
commit ea1a06820f
4 changed files with 110 additions and 13 deletions

View File

@ -53,6 +53,10 @@ public class CObjectRepository {
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) {

View File

@ -1,17 +1,14 @@
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.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@ -19,12 +16,19 @@ import lombok.Setter;
@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 = 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

@ -1,15 +1,11 @@
package de.w665.fluidcms.model.objectattributes;
import de.w665.fluidcms.model.ObjectAttribute;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class StringObjectAttribute extends ObjectAttribute<String> {
private String value;
@ -18,13 +14,13 @@ public class StringObjectAttribute extends ObjectAttribute<String> {
return value;
}
@Override
public String getValue() {
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,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");
}
}