diff options
| author | Keith Wall <kwall@apache.org> | 2014-05-15 12:57:46 +0000 |
|---|---|---|
| committer | Keith Wall <kwall@apache.org> | 2014-05-15 12:57:46 +0000 |
| commit | 2c802479f99ff5eeeb0958b0869c700f55c0e889 (patch) | |
| tree | c3042fce3234bd15e4eaf2d5e5225e020695e44c | |
| parent | ef5392fe1f9df62f348c06cec47f71d8983d5e86 (diff) | |
| download | qpid-python-2c802479f99ff5eeeb0958b0869c700f55c0e889.tar.gz | |
QPID-5715: [Java Broker] Make virtualhostnode respect states ACTIVE and STOPPED
* Added test to ensure that the VHN can transit between ACTIVE <=> STOPPED
* Fixed defect whereby the VHN could not be recovered in the STOPPED state. Added test case.
* Fixed defct whereby on stopping a VHN, the DurableConfigurationStore would be left open.
Work by me and Andrew MacBean <andymacbean@gmail.com>.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1594903 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 213 insertions, 7 deletions
diff --git a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java index ca521f82df..2f8860a3a0 100644 --- a/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java +++ b/qpid/java/broker-core/src/main/java/org/apache/qpid/server/virtualhostnode/AbstractVirtualHostNode.java @@ -211,9 +211,10 @@ public abstract class AbstractVirtualHostNode<X extends AbstractVirtualHostNode< } } - @StateTransition( currentState = { State.ACTIVE, State.ERRORED }, desiredState = State.STOPPED ) + @StateTransition( currentState = { State.ACTIVE, State.ERRORED, State.UNINITIALIZED }, desiredState = State.STOPPED ) protected void doStop() { + closeConfigurationStore(); closeChildren(); _state.set(State.STOPPED); } @@ -221,12 +222,7 @@ public abstract class AbstractVirtualHostNode<X extends AbstractVirtualHostNode< @Override protected void onClose() { - DurableConfigurationStore configurationStore = getConfigurationStore(); - if (configurationStore != null) - { - configurationStore.closeConfigurationStore(); - getEventLogger().message(getConfigurationStoreLogSubject(), ConfigStoreMessages.CLOSE()); - } + closeConfigurationStore(); } @Override @@ -250,6 +246,16 @@ public abstract class AbstractVirtualHostNode<X extends AbstractVirtualHostNode< } } + private void closeConfigurationStore() + { + DurableConfigurationStore configurationStore = getConfigurationStore(); + if (configurationStore != null) + { + configurationStore.closeConfigurationStore(); + getEventLogger().message(getConfigurationStoreLogSubject(), ConfigStoreMessages.CLOSE()); + } + } + protected abstract DurableConfigurationStore createConfigurationStore(); protected abstract void activate(); diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/Asserts.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/Asserts.java index aa4d94ba98..691c533565 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/Asserts.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/Asserts.java @@ -43,6 +43,7 @@ import org.apache.qpid.server.model.Port; import org.apache.qpid.server.model.Queue; import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.model.VirtualHostNode; import org.apache.qpid.server.model.port.AmqpPort; import org.apache.qpid.server.queue.LastValueQueue; import org.apache.qpid.server.queue.PriorityQueue; @@ -53,6 +54,14 @@ public class Asserts { public static final String STATISTICS_ATTRIBUTE = "statistics"; + public static void assertVirtualHostNode(final String nodeName, final Map<String, Object> node) + { + assertNotNull("Virtualhostnode " + nodeName + " data is not found", node); + assertEquals("Unexpected value of attribute " + VirtualHostNode.NAME, + nodeName, + node.get(VirtualHostNode.NAME)); + } + public static void assertVirtualHost(String virtualHostName, Map<String, Object> virtualHost) { assertNotNull("Virtualhost " + virtualHostName + " data are not found", virtualHost); diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java index bbb0ab4dc7..86c37cd50a 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java @@ -515,6 +515,17 @@ public class RestTestHelper return submitRequest(url, method, (byte[])null); } + public void submitRequest(String url, String method, Map<String, Object> attributes, int expectedResponseCode) throws IOException + { + int responseCode = submitRequest(url, method, attributes); + Assert.assertEquals("Unexpected response code from " + method + " " + url , expectedResponseCode, responseCode); + } + + public void submitRequest(String url, String method, int expectedResponseCode) throws IOException + { + submitRequest(url, method, null, expectedResponseCode); + } + public int submitRequest(String url, String method, byte[] parameters) throws IOException { HttpURLConnection connection = openManagementConnection(url, method); diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/VirtualHostNodeRestTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/VirtualHostNodeRestTest.java new file mode 100644 index 0000000000..ee638f6776 --- /dev/null +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/VirtualHostNodeRestTest.java @@ -0,0 +1,177 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.systest.rest; + + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.apache.qpid.server.model.ConfiguredObject; +import org.apache.qpid.server.model.VirtualHostNode; +import org.apache.qpid.server.store.DurableConfigurationStore; +import org.apache.qpid.test.utils.TestBrokerConfiguration; + +/** + * + * TODO: Add test to test the mutation of the storePath. If the store path is mutated + * whilst active then the store should be deleted next time we stop or close. + */ +public class VirtualHostNodeRestTest extends QpidRestTestCase +{ + public void testGet() throws Exception + { + List<Map<String, Object>> virtualhostNodes = getRestTestHelper().getJsonAsList("virtualhostnode"); + assertNotNull("Virtualhostnodes data cannot be null", virtualhostNodes); + assertEquals("Unexpected number of hosts", EXPECTED_VIRTUALHOSTS.length, virtualhostNodes.size()); + for (String nodeName : EXPECTED_VIRTUALHOSTS) + { + Map<String, Object> node = getRestTestHelper().find("name", nodeName, virtualhostNodes); + Asserts.assertVirtualHostNode(nodeName, node); + } + } + + public void testCreateAndDeleteNodeWithTestProfileStore() throws Exception + { + String storeType = getTestProfileVirtualHostNodeType(); + String nodeName = "virtualhostnode-" + getTestName(); + File storePathAsFile = new File(getStoreLocation(nodeName)); + + createAndDeleteVirtualHostNode(storeType, nodeName, storePathAsFile); + assertFalse("Store should not exist after deletion", storePathAsFile.exists()); + } + + public void testCreateAndDeleteNodeWithJsonStore() throws Exception + { + String storeType = "JSON"; + String nodeName = "virtualhostnode-" + getTestName(); + File storePathAsFile = new File(getStoreLocation(nodeName)); + + createAndDeleteVirtualHostNode(storeType, nodeName, storePathAsFile); + // TODO Defect: JSON store currently can't delete itself, uncomment once this is resolved + //assertFalse("Store should not exist after deletion", storePathAsFile.exists()); + } + + public void testRecoverVirtualHostNodeWithDesiredStateStopped() throws Exception + { + stopBroker(); + + TestBrokerConfiguration config = getBrokerConfiguration(); + config.setObjectAttribute(VirtualHostNode.class, TEST3_VIRTUALHOST, ConfiguredObject.DESIRED_STATE, "STOPPED"); + config.setSaved(false); + + startBroker(); + + String restUrl = "virtualhostnode/" + TEST3_VIRTUALHOST; + assertVirtualHostStates(restUrl, "STOPPED", "STOPPED"); + } + + public void testMutateState() throws Exception + { + String restUrl = "virtualhostnode/" + TEST3_VIRTUALHOST; + + assertVirtualHostStates(restUrl, "ACTIVE", "ACTIVE"); + + mutateVirtualHostDesiredState(restUrl, "STOPPED"); + assertVirtualHostStates(restUrl, "STOPPED", "STOPPED"); + + mutateVirtualHostDesiredState(restUrl, "ACTIVE"); + assertVirtualHostStates(restUrl, "ACTIVE", "ACTIVE"); + } + + public void testMutateAttributes() throws Exception + { + String restUrl = "virtualhostnode/" + TEST3_VIRTUALHOST; + + Map<String, Object> virtualhostNode = getRestTestHelper().getJsonAsSingletonList(restUrl); + assertNull(virtualhostNode.get(VirtualHostNode.DESCRIPTION)); + + String newDescription = "My virtualhost node"; + Map<String, Object> newAttributes = new HashMap<String, Object>(); + newAttributes.put(VirtualHostNode.DESCRIPTION, newDescription); + + getRestTestHelper().submitRequest(restUrl, "PUT", newAttributes, HttpServletResponse.SC_OK); + + virtualhostNode = getRestTestHelper().getJsonAsSingletonList(restUrl); + assertEquals(newDescription, virtualhostNode.get(VirtualHostNode.DESCRIPTION)); + } + + private void createAndDeleteVirtualHostNode(final String storeType, + final String nodeName, + final File storePathAsFile) throws Exception + { + assertFalse("Store should not exist", storePathAsFile.exists()); + + createVirtualHostNode(nodeName, storePathAsFile.getAbsolutePath(), storeType); + assertTrue("Store should exist after creation of node", storePathAsFile.exists()); + + String restUrl = "virtualhostnode/" + nodeName; + Map<String, Object> virtualhostNode = getRestTestHelper().getJsonAsSingletonList(restUrl); + Asserts.assertVirtualHostNode(nodeName, virtualhostNode); + + getRestTestHelper().submitRequest(restUrl, "DELETE", HttpServletResponse.SC_OK); + + List<Map<String, Object>> virtualHostNodes = getRestTestHelper().getJsonAsList(restUrl); + assertEquals("Host should be deleted", 0, virtualHostNodes.size()); + } + + private void assertVirtualHostStates(final String restUrl, + final String expectedDesiredState, + final String expectedActualState) throws IOException + { + Map<String, Object> virtualhostNode = getRestTestHelper().getJsonAsSingletonList(restUrl); + assertEquals("Virtualhostnode has unexpected desired state", + expectedDesiredState, + virtualhostNode.get(VirtualHostNode.DESIRED_STATE)); + assertEquals("Virtualhostnode has unexpected actual state", + expectedActualState, virtualhostNode.get(VirtualHostNode.STATE)); + } + + private void mutateVirtualHostDesiredState(final String restUrl, final String newState) throws IOException + { + Map<String, Object> newAttributes = new HashMap<String, Object>(); + newAttributes.put(VirtualHostNode.DESIRED_STATE, newState); + + getRestTestHelper().submitRequest(restUrl, "PUT", newAttributes, HttpServletResponse.SC_OK); + } + + private void createVirtualHostNode(String nodeName, String configStorePath, final String storeType) throws Exception + { + Map<String, Object> nodeData = new HashMap<String, Object>(); + nodeData.put(VirtualHostNode.NAME, nodeName); + nodeData.put(VirtualHostNode.TYPE, storeType); + nodeData.put(DurableConfigurationStore.STORE_PATH, configStorePath); + + getRestTestHelper().submitRequest("virtualhostnode/" + nodeName, + "PUT", + nodeData, + HttpServletResponse.SC_CREATED); + } + + private String getStoreLocation(String hostName) + { + return new File(TMP_FOLDER, "store-" + hostName + "-" + System.currentTimeMillis()).getAbsolutePath(); + } +} diff --git a/qpid/java/test-profiles/JavaTransientExcludes b/qpid/java/test-profiles/JavaTransientExcludes index 190708ee8e..a1962ff6e3 100644 --- a/qpid/java/test-profiles/JavaTransientExcludes +++ b/qpid/java/test-profiles/JavaTransientExcludes @@ -52,3 +52,6 @@ org.apache.qpid.systest.management.jmx.QueueManagementTest#testCopyMessageBetwee org.apache.qpid.test.unit.client.MaxDeliveryCountTest#testWhenBrokerIsRestartedAfterEnqeuingMessages org.apache.qpid.systest.rest.VirtualHostRestTest#testUpdateActiveHost +org.apache.qpid.systest.rest.VirtualHostNodeRestTest#testCreateAndDeleteNodeWithTestProfileStore +org.apache.qpid.systest.rest.VirtualHostNodeRestTest#testCreateAndDeleteNodeWithJsonStore + |
