View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.jboss.extension;
17  
18  import static de.smartics.properties.jboss.extension.AbstractSubsystemFileBasedParsingTestCase.fetchXml;
19  import static de.smartics.properties.jboss.extension.resources.SubsystemDefinition.SUBSYSTEM_NAME;
20  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
21  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DESCRIBE;
22  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
23  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
24  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
25  
26  import java.io.IOException;
27  import java.util.List;
28  
29  import junit.framework.Assert;
30  
31  import org.jboss.as.controller.PathAddress;
32  import org.jboss.as.controller.PathElement;
33  import org.jboss.as.subsystem.test.AbstractSubsystemTest;
34  import org.jboss.as.subsystem.test.KernelServices;
35  import org.jboss.dmr.ModelNode;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  
39  /**
40   * Tests all management expects for subsystem, parsing, marshaling, model
41   * definition and other Here is an example that allows you a fine grained
42   * controler over what is tested and how. So it can give you ideas what can be
43   * done and tested. If you have no need for advanced testing of subsystem you
44   * look at {@link SubsystemBaseParsingTestCase} that testes same stuff but most
45   * of the code is hidden inside of test harness
46   *
47   * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
48   */
49  @Ignore
50  public class SubsystemParsingTestCase extends AbstractSubsystemTest
51  {
52  
53    public SubsystemParsingTestCase()
54    {
55      super(SUBSYSTEM_NAME, new SubsystemExtension());
56    }
57  
58    /**
59     * Tests that the xml is parsed into the correct operations
60     */
61    @Test
62    public void testParseSubsystem() throws Exception
63    {
64      // Parse the subsystem xml into operations
65      final String subsystemXml = createXml();
66      final List<ModelNode> operations = super.parse(subsystemXml);
67  
68      // /Check that we have the expected number of operations
69      Assert.assertEquals(2, operations.size());
70  
71      // Check that each operation has the correct content
72      final ModelNode addSubsystem = operations.get(0);
73      Assert.assertEquals(ADD, addSubsystem.get(OP).asString());
74      final PathAddress addr = PathAddress.pathAddress(addSubsystem.get(OP_ADDR));
75      Assert.assertEquals(1, addr.size());
76      final PathElement element = addr.getElement(0);
77      Assert.assertEquals(SUBSYSTEM, element.getKey());
78      Assert.assertEquals(SUBSYSTEM_NAME, element.getValue());
79    }
80  
81    private String createXml() throws IOException
82    {
83      final String xml = fetchXml("cache-only.xml");
84      return xml;
85    }
86  
87    /**
88     * Test that the model created from the xml looks as expected
89     */
90    @Test
91    public void testInstallIntoController() throws Exception
92    {
93      // Parse the subsystem xml and install into the controller
94      final String subsystemXml = createXml();
95      final KernelServices services = super.installInController(subsystemXml);
96  
97      // Read the whole model and make sure it looks as expected
98      final ModelNode model = services.readWholeModel();
99      Assert.assertTrue(model.get(SUBSYSTEM).hasDefined(SUBSYSTEM_NAME));
100   }
101 
102   /**
103    * Starts a controller with a given subsystem xml and then checks that a
104    * second controller started with the xml marshalled from the first one
105    * results in the same model
106    */
107   @Test
108   public void testParseAndMarshalModel() throws Exception
109   {
110     // Parse the subsystem xml and install into the first controller
111     final String subsystemXml = createXml();
112     final KernelServices servicesA = super.installInController(subsystemXml);
113     // Get the model and the persisted xml from the first controller
114     final ModelNode modelA = servicesA.readWholeModel();
115     final String marshalled = servicesA.getPersistedSubsystemXml();
116 
117     // Install the persisted xml from the first controller into a second
118     // controller
119     final KernelServices servicesB = super.installInController(marshalled);
120     final ModelNode modelB = servicesB.readWholeModel();
121 
122     // Make sure the models from the two controllers are identical
123     super.compare(modelA, modelB);
124   }
125 
126   /**
127    * Starts a controller with the given subsystem xml and then checks that a
128    * second controller started with the operations from its describe action
129    * results in the same model
130    */
131   @Test
132   public void testDescribeHandler() throws Exception
133   {
134     // Parse the subsystem xml and install into the first controller
135     final String subsystemXml = createXml();
136     final KernelServices servicesA = super.installInController(subsystemXml);
137     // Get the model and the describe operations from the first controller
138     final ModelNode modelA = servicesA.readWholeModel();
139     final ModelNode describeOp = new ModelNode();
140     describeOp.get(OP).set(DESCRIBE);
141     describeOp.get(OP_ADDR).set(
142         PathAddress.pathAddress(
143             PathElement.pathElement(SUBSYSTEM, SUBSYSTEM_NAME)).toModelNode());
144     final List<ModelNode> operations =
145         super.checkResultAndGetContents(servicesA.executeOperation(describeOp))
146             .asList();
147 
148     // Install the describe options from the first controller into a second
149     // controller
150     final KernelServices servicesB = super.installInController(operations);
151     final ModelNode modelB = servicesB.readWholeModel();
152 
153     // Make sure the models from the two controllers are identical
154     super.compare(modelA, modelB);
155   }
156 
157   /**
158    * Tests that the subsystem can be removed
159    */
160   @Test
161   public void testSubsystemRemoval() throws Exception
162   {
163     // Parse the subsystem xml and install into the first controller
164     final String subsystemXml = createXml();
165     final KernelServices services = super.installInController(subsystemXml);
166     // Checks that the subsystem was removed from the model
167     super.assertRemoveSubsystemResources(services);
168 
169     // TODO Chek that any services that were installed were removed here
170   }
171 }