use of org.jboss.dmr.ModelType in project wildfly by wildfly.
the class AbstractExpressionSupportTestCase method organizeAttributes.
private void organizeAttributes(PathAddress address, ModelNode description, ModelNode resource, ModelNode resourceNoDefaults, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
ModelNode attributeDescriptions = description.get(ATTRIBUTES);
for (Property descProp : attributeDescriptions.asPropertyList()) {
String attrName = descProp.getName();
ModelNode attrDesc = descProp.getValue();
if (isAttributeExcluded(address, attrName, attrDesc, resourceNoDefaults)) {
continue;
}
ModelNode noDefaultValue = resourceNoDefaults.get(attrName);
if (!noDefaultValue.isDefined()) {
// We need to see if it's legal to set this attribute, or whether it's undefined
// because an alternative attribute is defined or a required attribute is not defined.
Set<String> base = new HashSet<String>();
base.add(attrName);
if (attrDesc.hasDefined(REQUIRES)) {
for (ModelNode node : attrDesc.get(REQUIRES).asList()) {
base.add(node.asString());
}
}
boolean conflict = false;
for (String baseAttr : base) {
if (!resource.hasDefined(baseAttr)) {
conflict = true;
break;
}
ModelNode baseAttrAlts = attributeDescriptions.get(baseAttr, ALTERNATIVES);
if (baseAttrAlts.isDefined()) {
for (ModelNode alt : baseAttrAlts.asList()) {
String altName = alt.asString();
if (resourceNoDefaults.hasDefined(alt.asString()) || expressionAttrs.containsKey(altName) || otherAttrs.containsKey(altName)) {
conflict = true;
break;
}
}
}
}
if (conflict) {
conflicts++;
logHandling("Skipping conflicted attribute " + attrName + " at " + address.toModelNode().asString());
continue;
}
}
ModelNode attrValue = resource.get(attrName);
ModelType attrType = attrValue.getType();
if (attrDesc.get(EXPRESSIONS_ALLOWED).asBoolean(false)) {
// If it's defined and not an expression, use the current value to create an expression
if (attrType != ModelType.UNDEFINED && attrType != ModelType.EXPRESSION) {
// Deal with complex types specially
if (COMPLEX_TYPES.contains(attrType)) {
ModelNode valueType = attrDesc.get(VALUE_TYPE);
if (valueType.getType() == ModelType.TYPE) {
// Simple collection whose elements support expressions
handleSimpleCollection(address, attrName, attrValue, valueType.asType(), expressionAttrs, otherAttrs, expectedAttrs);
} else if (valueType.isDefined()) {
handleComplexCollection(address, attrName, attrValue, attrType, valueType, expressionAttrs, otherAttrs, expectedAttrs);
} else {
noSimple++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
} else {
if (attrType == ModelType.STRING) {
checkForUnconvertedExpression(address, attrName, attrValue);
}
String expression = "${exp.test:" + attrValue.asString() + "}";
expressionAttrs.put(attrName, new ModelNode(expression));
expectedAttrs.put(attrName, new ModelNode().set(new ValueExpression(expression)));
simple++;
logHandling("Added expression to simple attribute " + attrName + " at " + address.toModelNode().asString());
}
} else {
if (attrType != ModelType.EXPRESSION) {
supportedUndefined++;
logHandling("Expression supported but value undefined on simple attribute " + attrName + " at " + address.toModelNode().asString());
} else {
simple++;
logHandling("Already found an expression on simple attribute " + attrName + " at " + address.toModelNode().asString());
}
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
} else if (COMPLEX_TYPES.contains(attrType) && attrDesc.get(VALUE_TYPE).getType() != ModelType.TYPE && attrDesc.get(VALUE_TYPE).isDefined()) {
handleComplexCollection(address, attrName, attrValue, attrType, attrDesc.get(VALUE_TYPE), expressionAttrs, otherAttrs, expectedAttrs);
} else /*if (!attrDesc.hasDefined(DEPRECATED))*/
{
noSimple++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
}
}
use of org.jboss.dmr.ModelType in project eap-additional-testsuite by jboss-set.
the class ExpressionSupportSmokeTestCase method handleSimpleCollection.
private void handleSimpleCollection(PathAddress address, String attrName, ModelNode attrValue, ModelType valueType, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
if (COMPLEX_TYPES.contains(valueType)) {
// Too complicated
noSimpleCollection++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
} else {
boolean hasExpression = false;
ModelNode updated = new ModelNode();
ModelNode expected = new ModelNode();
for (ModelNode item : attrValue.asList()) {
ModelType itemType = item.getType();
if (itemType == ModelType.PROPERTY) {
Property prop = item.asProperty();
ModelNode propVal = prop.getValue();
ModelType propValType = propVal.getType();
if (propVal.isDefined() && propValType != ModelType.EXPRESSION) {
// Convert property value to expression
if (propValType == ModelType.STRING) {
checkForUnconvertedExpression(address, attrName, propVal);
}
String expression = "${exp.test:" + propVal.asString() + "}";
updated.get(prop.getName()).set(expression);
expected.get(prop.getName()).set(new ModelNode().set(new ValueExpression(expression)));
hasExpression = true;
} else {
updated.get(prop.getName()).set(propVal);
expected.get(prop.getName()).set(propVal);
}
} else if (item.isDefined() && itemType != ModelType.EXPRESSION) {
// Convert item to expression
if (itemType == ModelType.STRING) {
checkForUnconvertedExpression(address, attrName, item);
}
String expression = "${exp.test:" + item.asString() + "}";
updated.add(expression);
expected.add(new ModelNode().set(new ValueExpression(expression)));
hasExpression = true;
} else {
updated.add(item);
expected.add(item);
}
}
if (hasExpression) {
simpleCollection++;
logHandling("Added expression to SIMPLE " + attrValue.getType() + " attribute " + attrName + " at " + address.toModelNode().asString());
expressionAttrs.put(attrName, updated);
expectedAttrs.put(attrName, expected);
} else {
// We didn't change anything
noSimpleCollection++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
}
}
use of org.jboss.dmr.ModelType in project eap-additional-testsuite by jboss-set.
the class ExpressionSupportSmokeTestCase method organizeAttributes.
private void organizeAttributes(PathAddress address, ModelNode description, ModelNode resource, ModelNode resourceNoDefaults, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
ModelNode attributeDescriptions = description.get(ATTRIBUTES);
for (Property descProp : attributeDescriptions.asPropertyList()) {
String attrName = descProp.getName();
ModelNode attrDesc = descProp.getValue();
if (isAttributeExcluded(address, attrName, attrDesc, resourceNoDefaults)) {
continue;
}
ModelNode noDefaultValue = resourceNoDefaults.get(attrName);
if (!noDefaultValue.isDefined()) {
// We need to see if it's legal to set this attribute, or whether it's undefined
// because an alternative attribute is defined
Set<String> base = new HashSet<String>();
base.add(attrName);
if (attrDesc.hasDefined(REQUIRES)) {
for (ModelNode node : attrDesc.get(REQUIRES).asList()) {
base.add(node.asString());
}
}
boolean conflict = false;
for (String baseAttr : base) {
if (!resource.hasDefined(baseAttr)) {
conflict = true;
break;
}
ModelNode baseAttrAlts = attributeDescriptions.get(baseAttr, ALTERNATIVES);
if (baseAttrAlts.isDefined()) {
for (ModelNode alt : baseAttrAlts.asList()) {
String altName = alt.asString();
if (resourceNoDefaults.hasDefined(alt.asString()) || expressionAttrs.containsKey(altName) || otherAttrs.containsKey(altName)) {
conflict = true;
break;
}
}
}
}
if (conflict) {
conflicts++;
logHandling("Skipping conflicted attribute " + attrName + " at " + address.toModelNode().asString());
continue;
}
}
ModelNode attrValue = resource.get(attrName);
ModelType attrType = attrValue.getType();
if (attrDesc.get(EXPRESSIONS_ALLOWED).asBoolean(false)) {
// If it's defined and not an expression, use the current value to create an expression
if (attrType != ModelType.UNDEFINED && attrType != ModelType.EXPRESSION) {
// Deal with complex types specially
if (COMPLEX_TYPES.contains(attrType)) {
ModelNode valueType = attrDesc.get(VALUE_TYPE);
if (valueType.getType() == ModelType.TYPE) {
// Simple collection whose elements support expressions
handleSimpleCollection(address, attrName, attrValue, valueType.asType(), expressionAttrs, otherAttrs, expectedAttrs);
} else if (valueType.isDefined()) {
handleComplexCollection(address, attrName, attrValue, attrType, valueType, expressionAttrs, otherAttrs, expectedAttrs);
} else {
noSimple++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
} else {
if (attrType == ModelType.STRING) {
checkForUnconvertedExpression(address, attrName, attrValue);
}
String expression = "${exp.test:" + attrValue.asString() + "}";
expressionAttrs.put(attrName, new ModelNode(expression));
expectedAttrs.put(attrName, new ModelNode().set(new ValueExpression(expression)));
simple++;
logHandling("Added expression to simple attribute " + attrName + " at " + address.toModelNode().asString());
}
} else {
if (attrType != ModelType.EXPRESSION) {
supportedUndefined++;
logHandling("Expression supported but value undefined on simple attribute " + attrName + " at " + address.toModelNode().asString());
} else {
simple++;
logHandling("Already found an expression on simple attribute " + attrName + " at " + address.toModelNode().asString());
}
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
} else if (COMPLEX_TYPES.contains(attrType) && attrDesc.get(VALUE_TYPE).getType() != ModelType.TYPE && attrDesc.get(VALUE_TYPE).isDefined()) {
handleComplexCollection(address, attrName, attrValue, attrType, attrDesc.get(VALUE_TYPE), expressionAttrs, otherAttrs, expectedAttrs);
} else /*if (!attrDesc.hasDefined(DEPRECATED))*/
{
noSimple++;
logNoExpressions(address, attrName);
otherAttrs.put(attrName, attrValue);
expectedAttrs.put(attrName, attrValue);
}
}
}
use of org.jboss.dmr.ModelType in project revapi by revapi.
the class XmlToJson method convertByEnum.
private ModelNode convertByEnum(Xml configuration, ModelNode enumValues) {
String xmlValue = getValue.apply(configuration);
Map<String, ModelType> jsonValues = enumValues.asList().stream().collect(Collectors.toMap(ModelNode::asString, ModelNode::getType));
for (Map.Entry<String, ModelType> e : jsonValues.entrySet()) {
String jsonValue = e.getKey();
ModelType jsonType = e.getValue();
if (Objects.equals(xmlValue, jsonValue)) {
switch(jsonType) {
case BIG_INTEGER:
case INT:
case LONG:
return new ModelNode(Long.parseLong(xmlValue));
case BIG_DECIMAL:
case DOUBLE:
return new ModelNode(Double.parseDouble(xmlValue));
case BOOLEAN:
return new ModelNode(Boolean.parseBoolean(xmlValue));
case STRING:
return new ModelNode(xmlValue);
default:
throw new IllegalArgumentException("Unsupported type of enum value defined in schema: " + jsonValue);
}
}
}
throw new IllegalArgumentException("XML value '" + xmlValue + " doesn't match any of the allowed: " + jsonValues.keySet());
}
use of org.jboss.dmr.ModelType in project wildfly by wildfly.
the class AbstractExpressionSupportTestCase method handleComplexItem.
private void handleComplexItem(PathAddress address, String attrName, ModelNode item, ModelNode valueTypeDesc, ModelNode updatedItem, ModelNode itemToExpect) {
// Hack to deal with time unit processing
Set<String> keys = valueTypeDesc.keys();
boolean timeAttr = keys.size() == 2 && keys.contains("time") && keys.contains("unit");
boolean changed = false;
for (Property fieldProp : valueTypeDesc.asPropertyList()) {
String fieldName = fieldProp.getName();
if (!item.has(fieldName)) {
continue;
}
boolean timeunit = timeAttr && "unit".equals(fieldName);
ModelNode fieldDesc = fieldProp.getValue();
ModelNode fieldValue = item.get(fieldName);
ModelType valueType = fieldValue.getType();
if (valueType == ModelType.UNDEFINED || valueType == ModelType.EXPRESSION || // too complex
COMPLEX_TYPES.contains(valueType) || !fieldDesc.get(EXPRESSIONS_ALLOWED).asBoolean(false)) {
updatedItem.get(fieldName).set(fieldValue);
itemToExpect.get(fieldName).set(fieldValue);
} else {
if (valueType == ModelType.STRING) {
checkForUnconvertedExpression(address, attrName, item);
}
String valueString = timeunit ? fieldValue.asString().toLowerCase() : fieldValue.asString();
String expression = "${exp.test:" + valueString + "}";
updatedItem.get(fieldName).set(expression);
itemToExpect.get(fieldName).set(new ModelNode().set(new ValueExpression(expression)));
changed = true;
}
}
if (!changed) {
// Use unchanged 'item'
updatedItem.set(item);
itemToExpect.set(item);
}
}
Aggregations