-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WFARQ-168] Re-throw exceptions for all errors thrown from a ServerSe…
…tupTask.setup. https://issues.redhat.com/browse/WFARQ-168 Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
8 changed files
with
430 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...t/java/org/wildfly/arquillian/integration/test/junit5/server/setup/SetupTaskTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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.wildfly.arquillian.integration.test.junit5.server.setup; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jboss.as.arquillian.container.ManagementClient; | ||
import org.jboss.as.controller.client.Operation; | ||
import org.jboss.as.controller.client.helpers.ClientConstants; | ||
import org.jboss.as.controller.client.helpers.Operations; | ||
import org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder; | ||
import org.jboss.dmr.ModelNode; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
import org.junit.platform.testkit.engine.EventConditions; | ||
import org.junit.platform.testkit.engine.TestExecutionResultConditions; | ||
import org.wildfly.arquillian.junit.annotations.WildFlyArquillian; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@WildFlyArquillian | ||
@RunAsClient | ||
public class SetupTaskTestCase { | ||
|
||
@ArquillianResource | ||
private ManagementClient client; | ||
|
||
@Deployment | ||
public static WebArchive createDeployment() { | ||
return ShrinkWrap.create(WebArchive.class, "setup-task-test.war") | ||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
} | ||
|
||
@BeforeEach | ||
public void clearSystemProperties() throws Exception { | ||
final CompositeOperationBuilder builder = CompositeOperationBuilder.create(); | ||
for (ModelNode name : getSystemProperties()) { | ||
final ModelNode address = Operations.createAddress("system-property", name.asString()); | ||
builder.addStep(Operations.createRemoveOperation(address)); | ||
} | ||
executeOperation(builder.build()); | ||
} | ||
|
||
@Test | ||
public void successThenAssertionFail() throws Exception { | ||
final var results = EngineTestKit.engine("junit-jupiter") | ||
.selectors(DiscoverySelectors.selectClass(SetupTaskTests.SuccessThenAssertionFail.class)) | ||
.execute(); | ||
// No tests should have been executed | ||
final var testEvents = results.testEvents(); | ||
testEvents.assertThatEvents().isEmpty(); | ||
// We should have one failure from the setup task | ||
final var events = results.allEvents(); | ||
events.assertStatistics((stats) -> stats.failed(1L)); | ||
events.assertThatEvents() | ||
.haveAtLeastOne(EventConditions.event( | ||
EventConditions.finishedWithFailure(TestExecutionResultConditions.instanceOf(AssertionError.class)))); | ||
assertOnlyProperties(SetupTaskTests.AssertionErrorSetupTask.PROPERTY_NAME); | ||
} | ||
|
||
@Test | ||
public void successThenRuntimeFail() throws Exception { | ||
final var results = EngineTestKit.engine("junit-jupiter") | ||
.selectors(DiscoverySelectors.selectClass(SetupTaskTests.SuccessThenRuntimeFail.class)) | ||
.execute(); | ||
// No tests should have been executed | ||
final var testEvents = results.testEvents(); | ||
testEvents.assertThatEvents().isEmpty(); | ||
// We should have one failure from the setup task | ||
final var events = results.allEvents(); | ||
events.assertStatistics((stats) -> stats.failed(1L)); | ||
events.assertThatEvents() | ||
.haveAtLeastOne(EventConditions.event( | ||
EventConditions.finishedWithFailure(TestExecutionResultConditions.instanceOf(RuntimeException.class)))); | ||
assertOnlyProperties(SetupTaskTests.RuntimeExceptionSetupTask.PROPERTY_NAME); | ||
} | ||
|
||
@Test | ||
public void successAndAfter() throws Exception { | ||
final var results = EngineTestKit.engine("junit-jupiter") | ||
.selectors(DiscoverySelectors.selectMethod(SetupTaskTests.SuccessAndAfter.class, "systemPropertiesExist")) | ||
.execute(); | ||
// The test should have been successful | ||
final var testEvents = results.testEvents(); | ||
testEvents.assertThatEvents().haveExactly(1, EventConditions.finishedSuccessfully()); | ||
// All properties should have been removed in the SetupServerTask.tearDown() | ||
assertNoSystemProperties(); | ||
} | ||
|
||
private void assertOnlyProperties(final String... names) throws IOException { | ||
final Set<String> expectedNames = Set.of(names); | ||
final Set<String> allProperties = getSystemProperties() | ||
.stream() | ||
.map(ModelNode::asString) | ||
.collect(Collectors.toCollection(LinkedHashSet<String>::new)); | ||
Assertions.assertTrue(allProperties.containsAll(expectedNames), () -> String | ||
.format("The following properties were expected in \"%s\", but not found; %s", allProperties, expectedNames)); | ||
// Remove the expected properties | ||
allProperties.removeAll(expectedNames); | ||
Assertions.assertTrue(allProperties.isEmpty(), | ||
() -> String.format("The following properties exist which should not exist: %s", allProperties)); | ||
} | ||
|
||
private void assertNoSystemProperties() throws IOException { | ||
final ModelNode op = Operations.createOperation("read-children-names"); | ||
op.get(ClientConstants.CHILD_TYPE).set("system-property"); | ||
final ModelNode result = executeOperation(op); | ||
Assertions.assertTrue(result.asList() | ||
.isEmpty(), () -> "Expected no system properties, found: " + result.asString()); | ||
} | ||
|
||
private List<ModelNode> getSystemProperties() throws IOException { | ||
final ModelNode op = Operations.createOperation("read-children-names"); | ||
op.get(ClientConstants.CHILD_TYPE).set("system-property"); | ||
return executeOperation(op).asList(); | ||
} | ||
|
||
private ModelNode executeOperation(final ModelNode op) throws IOException { | ||
return executeOperation(Operation.Factory.create(op)); | ||
} | ||
|
||
private ModelNode executeOperation(final Operation op) throws IOException { | ||
final ModelNode result = client.getControllerClient().execute(op); | ||
if (!Operations.isSuccessfulOutcome(result)) { | ||
Assertions.fail("Operation has failed: " + Operations.getFailureDescription(result).asString()); | ||
} | ||
return Operations.readResult(result); | ||
} | ||
} |
Oops, something went wrong.