PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/host-controller/src/test/java/org/jboss/as/domain/controller/operations/ServerGroupAffectedResourceServerGroupOperationsTestCase.java

https://bitbucket.org/cprenzberg/wildfly
Java | 377 lines | 275 code | 73 blank | 29 comment | 26 complexity | 856d240f2354c6b9b03c450e450514ac MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.domain.controller.operations;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
  25. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
  26. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  27. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PROFILE;
  28. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
  29. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_GROUP;
  30. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_GROUP;
  31. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
  32. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.WRITE_ATTRIBUTE_OPERATION;
  33. import org.jboss.as.controller.OperationContext;
  34. import org.jboss.as.controller.OperationFailedException;
  35. import org.jboss.as.controller.OperationStepHandler;
  36. import org.jboss.as.controller.PathAddress;
  37. import org.jboss.as.controller.PathElement;
  38. import org.jboss.as.controller.registry.Resource;
  39. import org.jboss.dmr.ModelNode;
  40. import org.junit.Assert;
  41. import org.junit.Test;
  42. /**
  43. *
  44. * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
  45. */
  46. public class ServerGroupAffectedResourceServerGroupOperationsTestCase extends AbstractOperationTestCase {
  47. @Test
  48. public void testAddServerGroupMaster() throws Exception {
  49. testAddServerGroup(true, false);
  50. }
  51. @Test
  52. public void testAddServerGroupSlave() throws Exception {
  53. testAddServerGroup(false, false);
  54. }
  55. @Test
  56. public void testAddServerGroupMasterRollback() throws Exception {
  57. testAddServerGroup(true, true);
  58. }
  59. @Test
  60. public void testAddServerGroupSlaveRollback() throws Exception {
  61. testAddServerGroup(false, true);
  62. }
  63. private void testAddServerGroup(boolean master, boolean rollback) throws Exception {
  64. testAddServerGroupBadInfo(master, rollback, false, false);
  65. }
  66. @Test(expected=OperationFailedException.class)
  67. public void testAddServerGroupBadProfileMaster() throws Exception {
  68. testAddServerGroupBadInfo(true, false, true, false);
  69. }
  70. @Test
  71. public void testAddServerGroupBadProfileSlave() throws Exception {
  72. testAddServerGroupBadInfo(false, false, true, false);
  73. }
  74. @Test(expected=OperationFailedException.class)
  75. public void testAddServerGroupBadProfileMasterRollback() throws Exception {
  76. //This won't actually get to the rollback part
  77. testAddServerGroupBadInfo(true, true, true, false);
  78. }
  79. @Test
  80. public void testAddServerGroupBadProfileSlaveRollback() throws Exception {
  81. testAddServerGroupBadInfo(false, true, true, false);
  82. }
  83. @Test(expected=OperationFailedException.class)
  84. public void testAddServerGroupBadSocketBindingGroupMaster() throws Exception {
  85. testAddServerGroupBadInfo(true, false, false, true);
  86. }
  87. @Test
  88. public void testAddServerGroupBadSocketBindingGroupSlave() throws Exception {
  89. testAddServerGroupBadInfo(false, false, false, true);
  90. }
  91. @Test(expected=OperationFailedException.class)
  92. public void testAddServerGroupBadSocketBindingGroupMasterRollback() throws Exception {
  93. //This won't actually get to the rollback part
  94. testAddServerGroupBadInfo(true, true, false, true);
  95. }
  96. @Test
  97. public void testAddServerGroupBadSocketBindingGroupSlaveRollback() throws Exception {
  98. testAddServerGroupBadInfo(false, true, false, true);
  99. }
  100. private void testAddServerGroupBadInfo(boolean master, boolean rollback, boolean badProfile, boolean badSocketBindingGroup) throws Exception {
  101. PathAddress pa = PathAddress.pathAddress(PathElement.pathElement(SERVER_GROUP, "group-three"));
  102. final MockOperationContext operationContext = getOperationContext(rollback, pa);
  103. String profileName = badProfile ? "bad-profile" : "profile-two";
  104. String socketBindingGroupName = badSocketBindingGroup ? "bad-group" : "binding-two";
  105. final ModelNode operation = new ModelNode();
  106. operation.get(OP_ADDR).set(pa.toModelNode());
  107. operation.get(OP).set(ADD);
  108. operation.get(PROFILE).set(profileName);
  109. operation.get(SOCKET_BINDING_GROUP).set(socketBindingGroupName);
  110. new ServerGroupAddHandler(master).execute(operationContext, operation);
  111. if (master && (badProfile || badSocketBindingGroup)) {
  112. Assert.fail();
  113. }
  114. if (rollback) {
  115. Assert.assertFalse(operationContext.isReloadRequired());
  116. } else {
  117. if (badProfile || badSocketBindingGroup) {
  118. Assert.assertTrue(operationContext.isReloadRequired());
  119. } else {
  120. Assert.assertFalse(operationContext.isReloadRequired());
  121. }
  122. }
  123. }
  124. @Test
  125. public void testRemoveServerGroupMaster() throws Exception {
  126. testRemoveServerGroup(true, false);
  127. }
  128. @Test
  129. public void testRemoveServerGroupSlave() throws Exception {
  130. testRemoveServerGroup(false, false);
  131. }
  132. @Test
  133. public void testRemoveServerGroupMasterRollback() throws Exception {
  134. testRemoveServerGroup(true, true);
  135. }
  136. @Test
  137. public void testRemoveServerGroupSlaveRollback() throws Exception {
  138. testRemoveServerGroup(false, true);
  139. }
  140. private void testRemoveServerGroup(boolean master, boolean rollback) throws Exception {
  141. PathAddress pa = PathAddress.pathAddress(PathElement.pathElement(SERVER_GROUP, "group-one"));
  142. final MockOperationContext operationContext = getOperationContext(rollback, pa);
  143. final ModelNode operation = new ModelNode();
  144. operation.get(OP_ADDR).set(pa.toModelNode());
  145. operation.get(OP).set(REMOVE);
  146. ServerGroupRemoveHandler.INSTANCE.execute(operationContext, operation);
  147. Assert.assertFalse(operationContext.isReloadRequired());
  148. }
  149. @Test
  150. public void testUpdateServerGroupProfileMaster() throws Exception {
  151. testUpdateServerGroupProfile(true, false, false);
  152. }
  153. @Test
  154. public void testUpdateServerGroupProfileSlave() throws Exception {
  155. testUpdateServerGroupProfile(false, false, false);
  156. }
  157. @Test
  158. public void testUpdateServerGroupProfileMasterRollback() throws Exception {
  159. testUpdateServerGroupProfile(true, true, false);
  160. }
  161. @Test
  162. public void testUpdateServerGroupProfileSlaveRollback() throws Exception {
  163. testUpdateServerGroupProfile(false, true, false);
  164. }
  165. @Test(expected=OperationFailedException.class)
  166. public void testUpdateServerGroupBadProfileMaster() throws Exception {
  167. testUpdateServerGroupProfile(true, false, true);
  168. }
  169. @Test
  170. public void testUpdateServerGroupBadProfileSlave() throws Exception {
  171. testUpdateServerGroupProfile(false, false, true);
  172. }
  173. @Test(expected=OperationFailedException.class)
  174. public void testUpdateServerGroupBadProfileMasterRollback() throws Exception {
  175. testUpdateServerGroupProfile(true, true, true);
  176. }
  177. @Test
  178. public void testUpdateServerGroupBadProfileSlaveRollback() throws Exception {
  179. testUpdateServerGroupProfile(false, true, true);
  180. }
  181. private void testUpdateServerGroupProfile(boolean master, boolean rollback, boolean badProfile) throws Exception {
  182. PathAddress pa = PathAddress.pathAddress(PathElement.pathElement(SERVER_GROUP, "group-one"));
  183. final MockOperationContext operationContext = getOperationContext(rollback, pa);
  184. String profileName = badProfile ? "bad-profile" : "profile-two";
  185. final ModelNode operation = new ModelNode();
  186. operation.get(OP_ADDR).set(pa.toModelNode());
  187. operation.get(OP).set(WRITE_ATTRIBUTE_OPERATION);
  188. operation.get(NAME).set(PROFILE);
  189. operation.get(VALUE).set(profileName);
  190. new ServerGroupProfileWriteAttributeHandler(master, null).execute(operationContext, operation);
  191. if (master && badProfile) {
  192. //master will throw an exception
  193. Assert.fail();
  194. }
  195. if (rollback) {
  196. Assert.assertFalse(operationContext.isReloadRequired());
  197. } else {
  198. if (badProfile) {
  199. Assert.assertTrue(operationContext.isReloadRequired());
  200. } else {
  201. Assert.assertFalse(operationContext.isReloadRequired());
  202. }
  203. }
  204. }
  205. @Test
  206. public void testUpdateServerGroupSocketBindingGroupMaster() throws Exception {
  207. testUpdateServerGroupSocketBindingGroup(true, false, false);
  208. }
  209. @Test
  210. public void testUpdateServerGroupSocketBindingGroupSlave() throws Exception {
  211. testUpdateServerGroupSocketBindingGroup(false, false, false);
  212. }
  213. @Test
  214. public void testUpdateServerGroupSocketBindingGroupMasterRollback() throws Exception {
  215. testUpdateServerGroupSocketBindingGroup(true, true, false);
  216. }
  217. @Test
  218. public void testUpdateServerGroupSocketBindingGroupSlaveRollback() throws Exception {
  219. testUpdateServerGroupSocketBindingGroup(false, true, false);
  220. }
  221. @Test(expected=OperationFailedException.class)
  222. public void testUpdateServerGroupBadSocketBindingGroupMaster() throws Exception {
  223. testUpdateServerGroupSocketBindingGroup(true, false, true);
  224. }
  225. @Test
  226. public void testUpdateServerGroupBadSocketBindingGroupSlave() throws Exception {
  227. testUpdateServerGroupSocketBindingGroup(false, false, true);
  228. }
  229. @Test(expected=OperationFailedException.class)
  230. public void testUpdateServerGroupBadSocketBindingGroupMasterRollback() throws Exception {
  231. testUpdateServerGroupSocketBindingGroup(true, true, true);
  232. }
  233. @Test
  234. public void testUpdateServerGroupBadSocketBindingGroupSlaveRollback() throws Exception {
  235. testUpdateServerGroupSocketBindingGroup(false, true, true);
  236. }
  237. private void testUpdateServerGroupSocketBindingGroup(boolean master, boolean rollback, boolean badSocketBindingGroup) throws Exception {
  238. PathAddress pa = PathAddress.pathAddress(PathElement.pathElement(SERVER_GROUP, "group-one"));
  239. final MockOperationContext operationContext = getOperationContext(rollback, pa);
  240. String socketBindingGroupName = badSocketBindingGroup ? "bad-group" : "binding-two";
  241. final ModelNode operation = new ModelNode();
  242. operation.get(OP_ADDR).set(pa.toModelNode());
  243. operation.get(OP).set(WRITE_ATTRIBUTE_OPERATION);
  244. operation.get(NAME).set(SOCKET_BINDING_GROUP);
  245. operation.get(VALUE).set(socketBindingGroupName);
  246. new ServerGroupSocketBindingGroupWriteAttributeHandler(master, null).execute(operationContext, operation);
  247. if (master && badSocketBindingGroup) {
  248. //master will throw an exception
  249. Assert.fail();
  250. }
  251. if (rollback) {
  252. Assert.assertFalse(operationContext.isReloadRequired());
  253. } else {
  254. if (badSocketBindingGroup) {
  255. Assert.assertTrue(operationContext.isReloadRequired());
  256. } else {
  257. Assert.assertFalse(operationContext.isReloadRequired());
  258. }
  259. }
  260. }
  261. MockOperationContext getOperationContext(final boolean rollback, final PathAddress operationAddress) {
  262. final Resource root = createRootResource();
  263. return new MockOperationContext(root, false, operationAddress, rollback);
  264. }
  265. private class MockOperationContext extends AbstractOperationTestCase.MockOperationContext {
  266. private boolean reloadRequired;
  267. private boolean rollback;
  268. private OperationStepHandler nextStep;
  269. protected MockOperationContext(final Resource root, final boolean booting, final PathAddress operationAddress, final boolean rollback) {
  270. super(root, booting, operationAddress);
  271. this.rollback = rollback;
  272. }
  273. public void completeStep(ResultHandler resultHandler) {
  274. if (nextStep != null) {
  275. stepCompleted();
  276. } else if (rollback) {
  277. resultHandler.handleResult(ResultAction.ROLLBACK, this, null);
  278. }
  279. }
  280. public void stepCompleted() {
  281. if (nextStep != null) {
  282. try {
  283. OperationStepHandler step = nextStep;
  284. nextStep = null;
  285. step.execute(this, null);
  286. } catch (OperationFailedException e) {
  287. throw new RuntimeException(e);
  288. }
  289. }
  290. }
  291. public void reloadRequired() {
  292. reloadRequired = true;
  293. }
  294. public boolean isReloadRequired() {
  295. return reloadRequired;
  296. }
  297. public void revertReloadRequired() {
  298. reloadRequired = false;
  299. }
  300. public void addStep(OperationStepHandler step, OperationContext.Stage stage) throws IllegalArgumentException {
  301. nextStep = step;
  302. }
  303. }
  304. }