Search in sources :

Example 71 with Property

use of org.jboss.dmr.Property in project wildfly by wildfly.

the class AbstractExpressionSupportTestCase method handleComplexProperty.

private void handleComplexProperty(PathAddress address, String attrName, ModelNode attrValue, ModelNode valueTypeDesc, Map<String, ModelNode> expressionAttrs, Map<String, ModelNode> otherAttrs, Map<String, ModelNode> expectedAttrs) {
    Property prop = attrValue.asProperty();
    ModelNode propVal = prop.getValue();
    ModelNode updatedPropVal = new ModelNode();
    ModelNode propValToExpect = new ModelNode();
    handleComplexItem(address, attrName, propVal, valueTypeDesc, updatedPropVal, propValToExpect);
    if (!updatedPropVal.equals(propVal)) {
        complexProperty++;
        ModelNode updatedProp = new ModelNode().set(prop.getName(), updatedPropVal);
        logHandling("Added expression to COMPLEX PROPERTY attribute " + attrName + " at " + address.toModelNode().asString());
        expressionAttrs.put(attrName, updatedProp);
        expectedAttrs.put(attrName, new ModelNode().set(prop.getName(), propValToExpect));
    } else {
        noComplexProperty++;
        logNoExpressions(address, attrName);
        otherAttrs.put(attrName, attrValue);
    }
}
Also used : ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 72 with Property

use of org.jboss.dmr.Property in project wildfly by wildfly.

the class AbstractExpressionSupportTestCase 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);
        }
    }
}
Also used : ValueExpression(org.jboss.dmr.ValueExpression) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 73 with Property

use of org.jboss.dmr.Property 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);
        }
    }
}
Also used : ValueExpression(org.jboss.dmr.ValueExpression) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) HashSet(java.util.HashSet)

Example 74 with Property

use of org.jboss.dmr.Property in project wildfly by wildfly.

the class AccessConstraintUtilizationTestCase method testConstraintUtilization.

@Test
public void testConstraintUtilization() throws Exception {
    ModelControllerClient client = managementClient.getControllerClient();
    for (ExpectedDef expectedDef : EXPECTED_DEFS) {
        AccessConstraintKey acdKey = expectedDef.key;
        String constraint = ModelDescriptionConstants.SENSITIVE.equals(acdKey.getType()) ? ModelDescriptionConstants.SENSITIVITY_CLASSIFICATION : ModelDescriptionConstants.APPLICATION_CLASSIFICATION;
        String acdType = acdKey.isCore() ? "core" : acdKey.getSubsystemName();
        String path = String.format(ADDR_FORMAT, acdKey.getType(), acdType, acdKey.getName());
        ModelNode op = createOpNode(path, READ_CHILDREN_RESOURCES_OPERATION);
        op.get(ModelDescriptionConstants.CHILD_TYPE).set(ModelDescriptionConstants.APPLIES_TO);
        // System.out.println("Testing " + acdKey);
        ModelNode result = RbacUtil.executeOperation(client, op, Outcome.SUCCESS).get(ModelDescriptionConstants.RESULT);
        Assert.assertTrue(acdKey + "result is defined", result.isDefined());
        Assert.assertTrue(acdKey + "result has content", result.asInt() > 0);
        boolean foundResource = false;
        boolean foundAttr = false;
        boolean foundOps = false;
        for (Property prop : result.asPropertyList()) {
            ModelNode pathResult = prop.getValue();
            if (pathResult.get(ModelDescriptionConstants.ENTIRE_RESOURCE).asBoolean()) {
                Assert.assertTrue(acdKey + " -- " + prop.getName() + " resource", expectedDef.expectResource);
                foundResource = true;
            }
            ModelNode attrs = pathResult.get(ATTRIBUTES);
            if (attrs.isDefined() && attrs.asInt() > 0) {
                Assert.assertTrue(acdKey + " -- " + prop.getName() + " attributes = " + attrs.asString(), expectedDef.expectAttributes);
                foundAttr = true;
            }
            ModelNode ops = pathResult.get(OPERATIONS);
            if (ops.isDefined() && ops.asInt() > 0) {
                Assert.assertTrue(acdKey + " -- " + prop.getName() + " operations = " + ops.asString(), expectedDef.expectOps);
                foundOps = true;
            }
        }
        Assert.assertEquals(acdKey + " -- resource", expectedDef.expectResource, foundResource);
        Assert.assertEquals(acdKey + " -- attributes", expectedDef.expectAttributes, foundAttr);
        Assert.assertEquals(acdKey + " -- operations", expectedDef.expectOps, foundOps);
    }
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) AccessConstraintKey(org.jboss.as.controller.access.management.AccessConstraintKey) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) Test(org.junit.Test)

Example 75 with Property

use of org.jboss.dmr.Property in project wildfly by wildfly.

the class SessionManagementTestCase method testSessionManagementOperations.

@Test
public void testSessionManagementOperations() throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ModelNode operation = new ModelNode();
        operation.get(ModelDescriptionConstants.OP).set(LIST_SESSIONS);
        operation.get(ModelDescriptionConstants.OP_ADDR).set(PathAddress.parseCLIStyleAddress("/deployment=management.war/subsystem=undertow").toModelNode());
        ModelNode opRes = managementClient.getControllerClient().execute(operation);
        Assert.assertEquals(opRes.toString(), "success", opRes.get(ModelDescriptionConstants.OUTCOME).asString());
        Assert.assertEquals(Collections.emptyList(), opRes.get(ModelDescriptionConstants.RESULT).asList());
        long c1 = System.currentTimeMillis();
        HttpGet get = new HttpGet("http://" + TestSuiteEnvironment.getServerAddress() + ":8080/management/SessionPersistenceServlet");
        HttpResponse res = client.execute(get);
        long c2 = System.currentTimeMillis();
        String sessionId = null;
        for (Header cookie : res.getHeaders("Set-Cookie")) {
            if (cookie.getValue().startsWith("JSESSIONID=")) {
                sessionId = cookie.getValue().split("=")[1].split("\\.")[0];
                break;
            }
        }
        Assert.assertNotNull(sessionId);
        opRes = managementClient.getControllerClient().execute(operation);
        Assert.assertEquals(opRes.toString(), "success", opRes.get(ModelDescriptionConstants.OUTCOME).asString());
        Assert.assertEquals(opRes.toString(), Collections.singletonList(new ModelNode(sessionId)), opRes.get(ModelDescriptionConstants.RESULT).asList());
        operation.get(SESSION_ID).set(sessionId);
        opRes = executeOperation(operation, GET_SESSION_CREATION_TIME_MILLIS);
        long time1 = opRes.get(ModelDescriptionConstants.RESULT).asLong();
        Assert.assertTrue(c1 <= time1);
        Assert.assertTrue(time1 <= c2);
        opRes = executeOperation(operation, GET_SESSION_CREATION_TIME);
        long sessionCreationTime = LocalDateTime.parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME).toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, sessionCreationTime);
        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME_MILLIS);
        Assert.assertEquals(time1, opRes.get(ModelDescriptionConstants.RESULT).asLong());
        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME);
        long aTime2 = LocalDateTime.parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME).toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, aTime2);
        Assert.assertEquals(sessionCreationTime, aTime2);
        opRes = executeOperation(operation, LIST_SESSION_ATTRIBUTE_NAMES);
        List<ModelNode> resultList = opRes.get(ModelDescriptionConstants.RESULT).asList();
        Assert.assertEquals(1, resultList.size());
        Assert.assertEquals(opRes.toString(), "val", resultList.get(0).asString());
        opRes = executeOperation(operation, LIST_SESSION_ATTRIBUTES);
        List<Property> properties = opRes.get(ModelDescriptionConstants.RESULT).asPropertyList();
        Assert.assertEquals(opRes.toString(), 1, properties.size());
        Property property = properties.get(0);
        Assert.assertEquals(opRes.toString(), "val", property.getName());
        Assert.assertEquals(opRes.toString(), "0", property.getValue().asString());
        // we want to make sure that the values will be different
        // so we wait 10ms
        Thread.sleep(10);
        long a1 = System.currentTimeMillis();
        client.execute(get);
        long a2 = System.currentTimeMillis();
        do {
            // because the last access time is updated after the request returns there is a possible race here
            // to get around this we execute this op in a loop and wait for the value to change
            // in 99% of cases this will only iterate once
            // because of the 10ms sleep above they should ways be different
            // we have a max wait time of 1s if something goes wrong
            opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME_MILLIS);
            time1 = opRes.get(ModelDescriptionConstants.RESULT).asLong();
            if (time1 != sessionCreationTime) {
                break;
            }
        } while (System.currentTimeMillis() < a1 + 1000);
        Assert.assertTrue(a1 <= time1);
        Assert.assertTrue(time1 <= a2);
        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME);
        long time2 = LocalDateTime.parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME).toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, time2);
        operation.get(ATTRIBUTE).set("val");
        opRes = executeOperation(operation, GET_SESSION_ATTRIBUTE);
        Assert.assertEquals("1", opRes.get(ModelDescriptionConstants.RESULT).asString());
        executeOperation(operation, INVALIDATE_SESSION);
        opRes = executeOperation(operation, LIST_SESSIONS);
        Assert.assertEquals(Collections.emptyList(), opRes.get(ModelDescriptionConstants.RESULT).asList());
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) Test(org.junit.Test)

Aggregations

Property (org.jboss.dmr.Property)179 ModelNode (org.jboss.dmr.ModelNode)144 HashMap (java.util.HashMap)19 Test (org.junit.Test)19 AttributeDefinition (org.jboss.as.controller.AttributeDefinition)12 PathAddress (org.jboss.as.controller.PathAddress)11 ArrayList (java.util.ArrayList)10 ValueExpression (org.jboss.dmr.ValueExpression)10 ModelType (org.jboss.dmr.ModelType)9 Map (java.util.Map)8 HashSet (java.util.HashSet)7 ArrayDeque (java.util.ArrayDeque)6 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)6 OperationFailedException (org.jboss.as.controller.OperationFailedException)6 SimpleAttributeDefinition (org.jboss.as.controller.SimpleAttributeDefinition)6 Properties (java.util.Properties)5 LoginModuleControlFlag (javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag)4 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 HttpResponse (org.apache.http.HttpResponse)3 HttpGet (org.apache.http.client.methods.HttpGet)3