@Test(expected=IllegalMetaClassException.class) public void testCreateValidation_enum1() { IMetadataService ms = repositoryService.getRepository(repoName).getMetadataService(); MetaClass metaClass = createMetaClass(); MetaAttribute b = new MetaAttribute(); b.setName("b"); b.setDataType(DataTypeEnum.ENUM); List<String> enumValues = new ArrayList<String>(); enumValues.add("TT"); enumValues.add("TA"); b.setEnumValues(enumValues); b.setDefaultValue("TC"); metaClass.addField(b); ms.createMetaClass(metaClass, new MetadataContext()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void applyDefaultValue(BsonEntity currentEntity, MetaAttribute metaField) { String fieldName = metaField.getName(); Object defaultValue = metaField.getValidatedDefaultValue(); List fieldValue = currentEntity.getFieldValues(fieldName); if (fieldValue.isEmpty() && defaultValue != null) { fieldValue = new ArrayList(); fieldValue.add(defaultValue); currentEntity.setFieldValues(fieldName, fieldValue); // use current entity's last modified time if any when apply defualt value to field if (currentEntity.getLastModified() != null) { currentEntity.setFieldTimestamp(fieldName, currentEntity.getLastModified()); } } }
@JsonIgnore private void initFromMetaField(MetaAttribute metaField) { setName(getFieldName()); if (metaField != null) { setDataType(metaField.getDataType()); setCardinality(metaField.getCardinality()); setDescription(metaField.getDescription()); setInternal(metaField.isInternal()); setDbName(metaField.getDbName()); setDefaultValue(metaField.getDefaultValue()); setEnumValues(metaField.getEnumValues()); setExpression(metaField.getExpression()); setMandatory(metaField.isMandatory()); setValidation(metaField.getValidation()); } else { setDataType(DataTypeEnum.INTEGER); setDbName("count"); setCardinality(CardinalityEnum.One); } }
private MetaAttribute getFieldPropertyField(FieldProperty property, String propertyFullName) { MetaAttribute attribute = new MetaAttribute(); attribute.setName(propertyFullName); attribute.setDataType(property.getType()); return attribute; }
@Override public boolean isOverridable(MetaField parentAttr) { if (!(parentAttr instanceof MetaAttribute)) { return false; } if (!super.isOverridable(parentAttr)) { return false; } MetaAttribute o = (MetaAttribute)parentAttr; return EqualsUtil.equal(isConstant(), o.isConstant()) && !this.equals(o); } }
public MetaAttribute newAttribute(String fieldName, DataTypeEnum dataType, CardinalityEnum cardinality){ MetaAttribute metaField = new MetaAttribute(); metaField.setName(fieldName); metaField.setDataType(dataType); metaField.setCardinality(cardinality); return metaField; }
metaClass.setName(name); metaClass.setSharded(true); MetaAttribute ma = new MetaAttribute(); ma.setName("attr1"); ma.setDataType(DataTypeEnum.STRING); metaClass.addField(ma); MetaAttribute ma2 = new MetaAttribute(); ma2.setName("attr2"); ma2.setDataType(DataTypeEnum.STRING); metaClass.addField(ma2); MetaAttribute ma3 = new MetaAttribute(); ma3.setName("attr3"); ma3.setDataType(DataTypeEnum.STRING); metaClass.addField(ma3); ii.addKeyField(ma.getName()); metaClass.addIndex(ii); ii2.addKeyField(ma.getName()); metaClass.addIndex(ii2); metaClass.getOptions().addPrimaryKey(ma3.getName());
m.setId(new ObjectId().toString()); MetaAttribute a = new MetaAttribute(); a.setName("a"); a.setDbName("f_a"); a.setDataType(MetaField.DataTypeEnum.INTEGER); a.setCardinality(MetaField.CardinalityEnum.One); Assert.assertEquals(a.getDataType(), a1.getDataType()); Assert.assertEquals(a.getCardinality(), a1.getCardinality());
enumList.add("12"); enumList.add("13"); MetaAttribute metaAttr1 = new MetaAttribute(); metaAttr1.setName("level"); metaAttr1.setCardinality(CardinalityEnum.Many); metaAttr1.setConstant(true); metaAttr1.setDataType(DataTypeEnum.ENUM); metaAttr1.setDefaultValue("12"); metaAttr1.setDescription("le01"); metaAttr1.setEnumValues(enumList); metaAttr1.setMandatory(false); metaAttr1.setValidation("jojo"); metaAttr1.setVirtual(false); g1Meta01.addField(metaAttr1); metaService.createMetaClass(g1Meta01, new MetadataContext()); g1Meta04.setName("g1"); g1Meta04.setRepository("raptor-paas"); MetaAttribute metaAttr2 = new MetaAttribute(); metaAttr2.setName("level"); metaAttr2.setDataType(MetaField.DataTypeEnum.ENUM); enumList.add("14"); metaAttr2.setEnumValues(enumList); metaAttr2.setDefaultValue("14"); g1Meta04.addField(metaAttr2); Assert.assertEquals(dbName, ma02.getDbName()); Assert.assertEquals(valueDbName, ma02.getValueDbName());
@Test(expected = ExpressionParseException.class) public void invalidExpressionCreate() { MetaClass metaClass = metaService.getMetaClass(SERVICE_INSTANCE); MetaAttribute attribute = new MetaAttribute(false); attribute.setName("CalName"); attribute.setDataType(DataTypeEnum.STRING); attribute.setDbName(sequence.getNext()); attribute.setExpression("$name/"); metaClass.addField(attribute); }
private static MetaClass initPersonMeta() { MetaClass meta = metaService.getMetaClass(RaptorEntityGenerator.TypeEnum.Person.name()); MetaAttribute attr = new MetaAttribute(false); attr.setName("oldPerson"); attr.setDataType(DataTypeEnum.STRING); attr.setValidation("$age > 50"); attr.setDbName(sequence.getNext()); meta.addField(attr); return meta; }
@Test public void testUpdateMetaAddIndex() { MetaClass metaClass = createMetaClass(); metaClass.setName("metaUpdateAddIndex"); MetaAttribute attribute = new MetaAttribute(); attribute.setName("attr_Name1"); attribute.setDataType(DataTypeEnum.STRING); metaClass.addField(attribute); IndexInfo index = new IndexInfo("indexName1"); index.getKeyList().add(attribute.getName()); metaClass.addIndex(index); IMetadataService metaService = repositoryService.getRepository(repoName).getMetadataService(); metaService.createMetaClass(metaClass, new MetadataContext()); MetaClass getMeta = metaService.getMetaClass(metaClass.getName()); Assert.assertEquals(3, getMeta.getIndexes().size()); MetaClass metaClass2 = createMetaClass(); metaClass2.setName(metaClass.getName()); attribute.setDbName(null); attribute.setValueDbName(null); attribute.setFlattenValueDbName(null); metaClass2.addField(attribute); IndexInfo addIndex = new IndexInfo("indexName2"); addIndex.addKeyField(attribute.getName()); metaClass2.addIndex(addIndex); metaService.updateMetaClass(metaClass2, metaContext); getMeta = metaService.getMetaClass(metaClass.getName()); Assert.assertEquals(4, getMeta.getIndexes().size()); }
@Test (expected=IllegalArgumentException.class) public void testJoinEmpty() { MetaField metaField = new MetaAttribute(); metaField.setName(InternalFieldEnum.ID.getName()); new JoinSearchCriteria(metaField, new ArrayList<String>(), strategy); }
@Test(expected = IllegalIndexException.class) public void testValidateForUpdateField_indexSize1() { int maxIndexSize = CMSConsts.MAX_INDEXES_PER_META_CLASS; MetaClass mClass = new MetaClass(); mClass.setName("TestIndexSize"); mClass.setRepository(RAPTOR_PAAS); MetaOption metaOption = mClass.getOptions(); for (int i = 0; i <= maxIndexSize; i++) { MetaAttribute attr = new MetaAttribute(); attr.setName("field" + i); IndexInfo index = new IndexInfo("index" + i); metaOption.addIndex(index); } IMetadataService metaService = repo.getMetadataService(); metaService.createMetaClass(mClass, new MetadataContext()); }
@Test public void testEnum(){ IDataTypeHandler handler = new JsonEnumHandler(); MetaAttribute metaField = new MetaAttribute(); metaField.setName("EnumField"); metaField.addEnumValue("enumtype"); String value = new String("enumtype"); JsonNode writeNode = (JsonNode) handler.write(null, value, metaField); String readValue = (String) handler.read(null, writeNode, metaField); assertEquals(readValue, value); JsonNode nullWriteNode = (JsonNode) handler.write(null, null, metaField); Object nullReadValue = handler.read(null, nullWriteNode, metaField); Assert.assertNull(nullReadValue); }
public void validate(boolean readOnlyCheck) { if(getDataType() == DataTypeEnum.ENUM) { if (getEnumValues().isEmpty()) { throw new IllegalMetaClassException("enum values not defined"); set.addAll(getEnumValues()); if (set.size() != getEnumValues().size()) { throw new IllegalMetaClassException("duplicate enum values found"); if (!getEnumValues().isEmpty()) { throw new IllegalMetaClassException("enum values only occurs when data type is ENUM"); validateDbName(); getValidatedDefaultValue(); if (expression != null && !expression.isEmpty() && isMandatory()) { throw new IllegalMetaClassException("expression field can not be mandatory");
@Override public String toString() { return "MetaAttribute [name=" + getName() + ", defaultValue=" + defaultValue + ", enumValues=" + enumValues + ", expression=" + expression + ", validation=" + validation + "]"; }
@Override public boolean equals(Object other) { if (this == other) return true; if (!super.equals(other)) { return false; } if (!(other instanceof MetaAttribute)) return false; MetaAttribute o = (MetaAttribute)other; return EqualsUtil.equal(defaultValue, o.defaultValue) && EqualsUtil.equal(expression, o.expression) && EqualsUtil.equal(isConstant(), o.isConstant()) && EqualsUtil.equal(validation, o.validation) && EqualsUtil.isEquals(enumValues, o.enumValues); }
@Override protected void validate(IEntity curEntity, Object value, MetaField metaField) throws IllegalArgumentException { MetaAttribute metaAttribute = (MetaAttribute) metaField; // enum actually are strings CheckConditions.checkArgument(value instanceof String, "Invalid enum value %s in MetaAttribute %s", value, metaField.getName()); CheckConditions.checkArgument(metaAttribute.getEnumValues().contains(value), "Invalid enum value %s in MetaAttribute %s", value, metaField.getName()); }
Object convertedValue = defaultValue; if (defaultValue != null) { switch (getDataType()) { case INTEGER: try { case ENUM: HashSet<String> set = new HashSet<String>(); set.addAll(getEnumValues()); if (!set.contains(defaultValue)) { throw new IllegalMetaClassException("default value for enum must be one of enum values"); throw new IllegalMetaClassException("unknow datatype: " + getDataType().toString());