Skip to content

Commit

Permalink
Extension methods to get/set entity and composite types
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jun 22, 2024
1 parent 266ce3a commit 63cd358
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
9 changes: 9 additions & 0 deletions net/TdRules/Stubs/Sharpen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ namespace Sharpen
public class Stub
{
}

public static class Runtime
{
public static bool EqualsIgnoreCase(string thisString, string anotherString)
{
return thisString.Equals(anotherString, System.StringComparison.CurrentCultureIgnoreCase);
}
}

public class Collections
{
public static bool AddAll<T>(ICollection<T> list, IEnumerable toAdd)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import giis.tdrules.openapi.model.TdAttribute;

/**
* Extends the OpenApi generated TdAttribute model using default implementations in this interface
* (the name of this interface must be defined as the x-implements vendor extension)
Expand All @@ -18,6 +20,8 @@ public interface ITdAttributeExtension {
public String getRid();
public ITdAttributeExtension notnull(String value);
public ITdAttributeExtension readonly(String value);
public String getCompositetype();
public void setCompositetype(String compositeType);

// Default implementations to extend the generated model

Expand Down Expand Up @@ -91,5 +95,29 @@ default String getRidEntityOrAttribute(String rid, boolean getEntityPart) {
throw new ModelException("Referenced id " + rid + " should have at least two components separated by a dot");
return getEntityPart ? rid.substring(0, dotPosition) : rid.substring(dotPosition + 1, rid.length());
}

// Convenience functions to set and check the composite type

@JsonIgnore
default boolean isType() {
return EntityTypes.DT_TYPE.equalsIgnoreCase(getCompositetype());
}

@JsonIgnore
default boolean isArray() {
return EntityTypes.DT_ARRAY.equalsIgnoreCase(getCompositetype());
}

@JsonIgnore
default TdAttribute setType() {
setCompositetype(EntityTypes.DT_TYPE);
return (TdAttribute) this;
}

@JsonIgnore
default TdAttribute setArray() {
setCompositetype(EntityTypes.DT_ARRAY);
return (TdAttribute) this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import giis.tdrules.openapi.model.TdAttribute;
import giis.tdrules.openapi.model.TdEntity;

/**
* Extends the OpenApi generated TdEntity model using default implementations in this interface
Expand All @@ -20,6 +21,8 @@ public interface ITdEntityExtension {
// Methods from the generated model that are used here

public List<TdAttribute> getAttributes();
public String getEntitytype();
public void setEntitytype(String type);

// Default implementations to extend the generated model

Expand Down Expand Up @@ -81,5 +84,40 @@ default TdAttribute getAttribute(String name) {
return attribute;
return null;
}

// Convenience functions to set and check entity types

@JsonIgnore
default boolean isObject() {
return EntityTypes.DT_TABLE.equalsIgnoreCase(getEntitytype());
}

@JsonIgnore
default boolean isType() {
return EntityTypes.DT_TYPE.equalsIgnoreCase(getEntitytype());
}

@JsonIgnore
default boolean isArray() {
return EntityTypes.DT_ARRAY.equalsIgnoreCase(getEntitytype());
}

@JsonIgnore
default TdEntity setObject() {
setEntitytype(EntityTypes.DT_TABLE);
return (TdEntity) this;
}

@JsonIgnore
default TdEntity setType() {
setEntitytype(EntityTypes.DT_TYPE);
return (TdEntity) this;
}

@JsonIgnore
default TdEntity setArray() {
setEntitytype(EntityTypes.DT_ARRAY);
return (TdEntity) this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.junit.Test;

import giis.tdrules.model.EntityTypes;
import giis.tdrules.model.io.TdSchemaXmlSerializer;
import giis.tdrules.openapi.model.TdCheck;
import giis.tdrules.openapi.model.TdAttribute;
Expand All @@ -33,7 +34,7 @@ public static TdSchema getSchema() {
// not needed a real schema, only to fill all properties
TdEntity tab1 = new TdEntity();
tab1.setName("clirdb1");
tab1.setEntitytype("table");
tab1.setEntitytype(EntityTypes.DT_TABLE);
tab1.setSubtype("voidsubtype");
TdAttribute col1 = new TdAttribute();
col1.setName("col11");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public void testTdAttributeSetters() {
assertFalse(col22.notnull(false).isNotnull());
}

@Test
public void testCompositeTypes() {
TdSchema model = TestTdSchemaExtensions.getSchema();
TdAttribute col22 = model.getEntities().get(1).getAttributes().get(1);
assertFalse(col22.isType());
assertFalse(col22.isArray());

col22.setType();
assertTrue(col22.isType());
col22.setArray();
assertTrue(col22.isArray());
}

@Test
public void testMalformedRids() {
TdSchema model = TestTdSchemaExtensions.getSchema();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package test4giis.tdrules.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

Expand Down Expand Up @@ -61,4 +63,20 @@ public void testFindAttribute() {
assertNull(model.getEntity("clirdb1").getAttribute("col99"));
}

@Test
public void testEntityTypes() {
TdSchema model = TestTdSchemaExtensions.getSchema();
TdEntity entity = model.getEntity("clirdb1");
assertTrue(entity.isObject());
assertFalse(entity.isType());
assertFalse(entity.isArray());

entity.setType();
assertTrue(entity.isType());
entity.setArray();
assertTrue(entity.isArray());
entity.setObject();
assertTrue(entity.isObject());
}

}

0 comments on commit 63cd358

Please sign in to comment.