/** * Executes a given operation asserting that an attachment has been created. Given {@link KernelServices} must have enabled attachment grabber. * * @return {@link ModelNode} result of the transformed operation */ public static ModelNode executeOpInBothControllersWithAttachments(KernelServices services, ModelVersion version, ModelNode operation) throws Exception { OperationTransformer.TransformedOperation op = services.executeInMainAndGetTheTransformedOperation(operation, version); Assert.assertFalse(op.rejectOperation(success())); // System.out.println(operation + "\nbecomes\n" + op.getTransformedOperation()); if (op.getTransformedOperation() != null) { return ModelTestUtils.checkOutcome(services.getLegacyServices(version).executeOperation(op.getTransformedOperation())); } return null; }
public static void checkMapModels(KernelServices services, ModelVersion version, PathAddress address, String... properties) throws Exception { final ModelNode readResource = Util.createEmptyOperation(READ_RESOURCE_OPERATION, address); readResource.get(RECURSIVE).set(true); readResource.get(INCLUDE_DEFAULTS).set(false); ModelNode mainModel = services.executeForResult(readResource.clone()); checkMainMapModel(mainModel, properties); final ModelNode legacyModel; if (address.getLastElement().getKey().equals("transport")) { //TODO get rid of this once the PathAddress transformer works properly //Temporary workaround readResource.get(OP_ADDR).set(address.subAddress(0, address.size() - 1).append("transport", "TRANSPORT").toModelNode()); legacyModel = services.getLegacyServices(version).executeForResult(readResource); } else { legacyModel = ModelTestUtils.checkResultAndGetContents(services.executeOperation(version, services.transformOperation(version, readResource.clone()))); } checkLegacyChildResourceModel(legacyModel, properties); }
public static void checkMapResults(KernelServices services, ModelNode expected, ModelVersion version, ModelNode operation) throws Exception { ModelNode main = ModelTestUtils.checkOutcome(services.executeOperation(operation.clone())).get(ModelDescriptionConstants.RESULT); ModelNode legacyResult = services.executeOperation(version, services.transformOperation(version, operation.clone())); ModelNode legacy; if (expected.isDefined()) { legacy = ModelTestUtils.checkOutcome(legacyResult).get(ModelDescriptionConstants.RESULT); } else { ModelTestUtils.checkFailed(legacyResult); legacy = new ModelNode(); } Assert.assertEquals(main, legacy); Assert.assertEquals(expected, legacy); }
legacyReadResource.get(ModelDescriptionConstants.INCLUDE_RUNTIME).set(false); legacyReadResource.get(ModelDescriptionConstants.INCLUDE_DEFAULTS).set(includeDefaults); ModelNode legacyModel = ModelTestUtils.checkResultAndGetContents(kernelServices.executeOperation(modelVersion, kernelServices.transformOperation(modelVersion, legacyReadResource))); ModelNode legacySubsystem = legacyModel.require(SUBSYSTEM); legacySubsystem = legacySubsystem.require(mainSubsystemName); ModelTestUtils.compare(legacySubsystem, transformed, true); Assert.assertNotNull("Could not load legacy dmr for subsystem '" + mainSubsystemName + "' version: '" + modelVersion + "' please add it", rd); ManagementResourceRegistration rr = ManagementResourceRegistration.Factory.forProcessType(getProcessType()).createRegistration(rd); ModelTestUtils.checkModelAgainstDefinition(transformed, rr); return legacyModel;
/** * Compares two models to make sure that they are the same * * @param node1 the first model * @param node2 the second model */ protected void compare(ModelNode node1, ModelNode node2) { ModelTestUtils.compare(node1, node2); }
ModelTestUtils.validateModelDescriptions(PathAddress.EMPTY_ADDRESS, kernelServices.getRootRegistration()); ModelNode model = kernelServices.readWholeModel(); model = removeForIntellij(model); ModelTestUtils.scanForExpressionFormattedStrings(model);
public static void validateModelDescriptions(PathAddress address, ManagementResourceRegistration reg) { ModelTestUtils.validateModelDescriptions(address, reg); }
ModelTestUtils.checkOutcome(mainServices.executeOperation(mainComposite)); for (ModelNode op : operations) { writeOps.addAll(config.createWriteAttributeOperations(op)); checkFailedTransformedAddOperation(mainServices, modelVersion, op, config, legacySteps); if (transformedComposite.rejectOperation(successResult())) { Assert.fail(transformedComposite.getFailureDescription()); ModelTestUtils.checkOutcome(mainServices.executeOperation(modelVersion, transformedComposite)); } else { ModelTestUtils.checkOutcome(legacyServices.executeOperation(legacyComposite)); checkFailedTransformedWriteAttributeOperation(mainServices, modelVersion, writeOp, config);
/** * Scans for entries of type STRING containing expression formatted strings. This is to trap where parsers * call ModelNode.set("${A}") when ModelNode.setExpression("${A}) should have been used * * @param model the model to check */ public static void scanForExpressionFormattedStrings(ModelNode model) { if (model.getType().equals(ModelType.STRING)) { if (EXPRESSION_PATTERN.matcher(model.asString()).matches()) { Assert.fail("ModelNode with type==STRING contains an expression formatted string: " + model.asString()); } } else if (model.getType() == ModelType.OBJECT) { for (String key : model.keys()) { final ModelNode child = model.get(key); scanForExpressionFormattedStrings(child); } } else if (model.getType() == ModelType.LIST) { List<ModelNode> list = model.asList(); for (ModelNode entry : list) { scanForExpressionFormattedStrings(entry); } } else if (model.getType() == ModelType.PROPERTY) { Property prop = model.asProperty(); scanForExpressionFormattedStrings(prop.getValue()); } }
/** * Validate the marshalled xml without adjusting the namespaces for the original and marshalled xml. * * @param configId the id of the xml configuration * @param original the original subsystem xml * @param marshalled the marshalled subsystem xml * @throws Exception */ protected void compareXml(String configId, final String original, final String marshalled) throws Exception { ModelTestUtils.compareXml(original, marshalled); }
/** * Checks that the transformed model is the same as the model built up in the legacy subsystem controller via the transformed operations, * and that the transformed model is valid according to the resource definition in the legacy subsystem controller. * * @param kernelServices the main kernel services * @param modelVersion the model version of the targetted legacy subsystem * @param legacyModelFixer use to touch up the model read from the legacy controller, use sparingly when the legacy model is just wrong. May be {@code null} * @return the whole model of the legacy controller */ ModelNode checkSubsystemModelTransformation(KernelServices kernelServices, ModelVersion modelVersion, ModelFixer legacyModelFixer) throws IOException { KernelServices legacy = kernelServices.getLegacyServices(modelVersion); ModelNode legacyModel = legacy.readWholeModel(); ModelNode legacySubsystem = legacyModel.require(SUBSYSTEM); legacySubsystem = legacySubsystem.require(mainSubsystemName); if (legacyModelFixer != null) { legacySubsystem = legacyModelFixer.fixModel(legacySubsystem); } //1) Check that the transformed model is the same as the whole model read from the legacy controller. //The transformed model is done via the resource transformers //The model in the legacy controller is built up via transformed operations ModelNode transformed = kernelServices.readTransformedModel(modelVersion).get(SUBSYSTEM, mainSubsystemName); ModelTestUtils.compare(legacySubsystem, transformed, true); //2) Check that the transformed model is valid according to the resource definition in the legacy subsystem controller ResourceDefinition rd = TransformerRegistry.loadSubsystemDefinitionFromFile(this.getClass(), mainSubsystemName, modelVersion); Assert.assertNotNull("Could not load legacy dmr for subsystem '" + mainSubsystemName + "' version: '" + modelVersion + "' please add it", rd); ManagementResourceRegistration rr = ManagementResourceRegistration.Factory.create(rd); ModelTestUtils.checkModelAgainstDefinition(transformed, rr); return legacyModel; }
private static void compare(ModelNode node1, ModelNode node2, boolean ignoreUndefined, boolean ignoreType, Stack<String> stack) { if (! ignoreType) { Assert.assertEquals(getCompareStackAsString(stack) + " types", node1.getType(), node2.getType()); ModelNode model1 = ignoreUndefined ? trimUndefinedChildren(node1) : node1; ModelNode model2 = ignoreUndefined ? trimUndefinedChildren(node2) : node2; final Set<String> keys1 = new TreeSet<String>(model1.keys()); final Set<String> keys2 = new TreeSet<String>(model2.keys()); Assert.assertEquals(getCompareStackAsString(stack) + ": " + node1 + "\n" + node2, keys1.toString(), keys2.toString()); Assert.assertTrue(keys1.containsAll(keys2)); Assert.assertTrue(getCompareStackAsString(stack) + " key=" + key + "\n with child1 \n" + child1.toString() + "\n has child2 not defined\n node2 is:\n" + node2.toString(), child2.isDefined()); compare(child1, child2, ignoreUndefined, ignoreType, stack); stack.pop(); } else if (!ignoreUndefined) { Assert.assertFalse(getCompareStackAsString(stack) + " key=" + key + "\n with child1 undefined has child2 \n" + child2.asString(), child2.isDefined()); compare(list1.get(i), list2.get(i), ignoreUndefined, ignoreType, stack); stack.pop(); Assert.assertEquals(prop1 + "\n" + prop2, prop1.getName(), prop2.getName()); stack.push(prop1.getName() + "/"); compare(prop1.getValue(), prop2.getValue(), ignoreUndefined, ignoreType, stack); stack.pop(); Assert.assertEquals(getCompareStackAsString(stack) + "\n\"" + node1.asString() + "\"\n\"" + node2.asString() + "\"\n-----", node1.asString().trim(), node2.asString().trim());
/** * Read the classpath resource with the given name and return its contents as a string. Hook to * for reading in classpath resources for subsequent parsing. The resource is loaded using similar * semantics to {@link Class#getResource(String)} * * @param name the name of the resource * @return the contents of the resource as a string * @throws IOException */ protected String readResource(final String name) throws IOException { return ModelTestUtils.readResource(getClass(), name); }
/** * Normalize and pretty-print XML so that it can be compared using string * compare. The following code does the following: - Removes comments - * Makes sure attributes are ordered consistently - Trims every element - * Pretty print the document * * @param xml The XML to be normalized * @return The equivalent XML, but now normalized */ protected String normalizeXML(String xml) throws Exception { return ModelTestUtils.normalizeXML(xml); }
/** * Resolve two models and compare them to make sure that they have same content after expression resolution * * @param node1 the first model * @param node2 the second model */ public static void resolveAndCompareModels(ModelNode node1, ModelNode node2) { compare(resolve(node1), resolve(node2), false, true, new Stack<String>()); }
validateRequiredNillable(ATTRIBUTE + " " + entry.getKey(), entry.getValue()); String prefix = "operation " + property.getName() + " param "; for (Property param : opDesc.get(REQUEST_PROPERTIES).asPropertyList()) { validateRequiredNillable(prefix + param.getName(), param.getValue()); String prefix = "operation " + property.getName() + " reply field "; for (Property field : opDesc.get(REPLY_PROPERTIES).asPropertyList()) { validateRequiredNillable(prefix + field.getName(), field.getValue()); validateModelDescriptions(address.append(pe), sub);
/** * Validate the marshalled xml without adjusting the namespaces for the original and marshalled xml. * * @param original the original subsystem xml * @param marshalled the marshalled subsystem xml * @param ignoreNamespace if {@code true} the subsystem's namespace is ignored, otherwise it is taken into account when comparing the normalized xml. * @throws Exception */ public static void compareXml(final String original, final String marshalled, final boolean ignoreNamespace) throws Exception { final String xmlOriginal; final String xmlMarshalled; if (ignoreNamespace) { xmlOriginal = removeNamespace(original); xmlMarshalled = removeNamespace(marshalled); } else { xmlOriginal = original; xmlMarshalled = marshalled; } Assert.assertEquals(normalizeXML(xmlOriginal), normalizeXML(xmlMarshalled)); }
if (attributeNames.contains(name)) { AttributeAccess aa = rr.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name); Assert.assertNotNull(getComparePathAsString(stack) + " Attribute " + name + " is not known", aa); AttributeDefinition ad = aa.getAttributeDefinition(); if (!value.isDefined()) { Assert.assertTrue(getComparePathAsString(stack) + " Attribute " + name + " does not allow null", (!ad.isRequired() || ad.getDefaultValue() == null)); } else { Assert.fail(getComparePathAsString(stack) + " validation for attribute '" + name + "' failed, " + e.getFailureDescription().asString()); Assert.fail(getComparePathAsString(stack) + " Element '" + name + "' is not known in target definition"); ModelNode value = v.asProperty().getValue(); ManagementResourceRegistration sub = rr.getSubModel(PathAddress.pathAddress(pe)); Assert.assertNotNull(getComparePathAsString(stack) + " Child with name '" + name + "' not found", sub); if (value.isDefined()) { stack.push(pe); checkModelAgainstDefinition(value, sub, stack); stack.pop(); ModelNode value = model.get(pe.getKeyValuePair()); ManagementResourceRegistration sub = rr.getSubModel(PathAddress.pathAddress(pe)); Assert.assertNotNull(getComparePathAsString(stack) + " Child with name '" + name + "' not found", sub); if (value.isDefined()) { stack.push(pe); checkModelAgainstDefinition(value, sub, stack); stack.pop();
public static void checkModelAgainstDefinition(final ModelNode model, ManagementResourceRegistration rr) { checkModelAgainstDefinition(model, rr, new Stack<PathElement>()); }
legacyReadResource.get(ModelDescriptionConstants.INCLUDE_RUNTIME).set(false); legacyReadResource.get(ModelDescriptionConstants.INCLUDE_DEFAULTS).set(includeDefaults); ModelNode legacyModel = ModelTestUtils.checkResultAndGetContents(kernelServices.executeOperation(modelVersion, kernelServices.transformOperation(modelVersion, legacyReadResource))); ModelNode legacySubsystem = legacyModel.require(SUBSYSTEM); legacySubsystem = legacySubsystem.require(mainSubsystemName); ModelTestUtils.compare(legacySubsystem, transformed, true); Assert.assertNotNull("Could not load legacy dmr for subsystem '" + mainSubsystemName + "' version: '" + modelVersion + "' please add it", rd); ManagementResourceRegistration rr = ManagementResourceRegistration.Factory.forProcessType(getProcessType()).createRegistration(rd); ModelTestUtils.checkModelAgainstDefinition(transformed, rr); return legacyModel;