View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.wsba.participantcompletion.simple;
18  
19  import com.arjuna.mw.wst11.UserBusinessActivity;
20  import com.arjuna.mw.wst11.UserBusinessActivityFactory;
21  import org.junit.Assert;
22  import org.jboss.arquillian.container.test.api.Deployment;
23  import org.jboss.arquillian.junit.Arquillian;
24  import org.jboss.as.quickstarts.wsba.participantcompletion.simple.jaxws.SetServiceBA;
25  import org.jboss.shrinkwrap.api.ArchivePaths;
26  import org.jboss.shrinkwrap.api.ShrinkWrap;
27  import org.jboss.shrinkwrap.api.asset.EmptyAsset;
28  import org.jboss.shrinkwrap.api.asset.StringAsset;
29  import org.jboss.shrinkwrap.api.spec.WebArchive;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import javax.inject.Inject;
33  
34  @RunWith(Arquillian.class)
35  public class ClientTest {
36     
37      private static final String ManifestMF = "Manifest-Version: 1.0\n"
38            + "Dependencies: org.jboss.xts\n";
39     
40      @Inject
41      @ClientStub
42      public SetServiceBA client;
43  
44      @Deployment
45      public static WebArchive createTestArchive() {
46          return ShrinkWrap.create(WebArchive.class, "test.war")
47                  .addPackages(true, SetServiceBAImpl.class.getPackage().getName())
48                  .addAsResource("context-handlers.xml")
49                  .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
50                  .setManifest(new StringAsset(ManifestMF));
51      }
52  
53      /**
54       * Test the simple scenario where an item is added to the set within a Business Activity which is closed successfully.
55       * 
56       * @throws Exception if something goes wrong.
57       */
58      @Test
59      public void testSuccess() throws Exception {
60          System.out
61                  .println("\n\nStarting 'testSuccess'. This test invokes a WS within a BA. The BA is later closed, which causes the WS call to complete successfully.");
62          System.out.println("[CLIENT] Creating a new Business Activity");
63          UserBusinessActivity uba = UserBusinessActivityFactory.userBusinessActivity();
64          try {
65              String value = "1";
66  
67              System.out
68                      .println("[CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA wil be included in this activity)");
69              uba.begin();
70  
71              System.out.println("[CLIENT] invoking addValueToSet(1) on WS");
72              client.addValueToSet(value);
73  
74              System.out.println("[CLIENT] Closing Business Activity (This will cause the BA to complete successfully)");
75              uba.close();
76  
77              Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value));
78          } finally {
79              cancelIfActive(uba);
80              client.clear();
81          }
82      }
83  
84      /**
85       * Tests the scenario where an item is added to the set with in a business activity that is later cancelled. The test checks
86       * that the item is in the set after invoking addValueToSet on the Web service. After cancelling the Business Activity, the
87       * work should be compensated and thus the item should no longer be in the set.
88       * 
89       * @throws Exception if something goes wrong
90       */
91      @Test
92      public void testCancel() throws Exception {
93          System.out
94                  .println("\n\nStarting 'testCancel'. This test invokes a WS within a BA. The BA is later cancelled, which causes these WS call to be compensated.");
95          System.out.println("[CLIENT] Creating a new Business Activity");
96          UserBusinessActivity uba = UserBusinessActivityFactory.userBusinessActivity();
97          try {
98              String value = "1";
99  
100             System.out
101                     .println("[CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA will be included in this activity)");
102             uba.begin();
103 
104             System.out.println("[CLIENT] invoking addValueToSet(1) on WS");
105             client.addValueToSet(value);
106 
107             Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value));
108 
109             System.out.println("[CLIENT] Cancelling Business Activity (This will cause the work to be compensated)");
110             uba.cancel();
111 
112             Assert.assertTrue("Expected value to not be in the set, but it was", !client.isInSet(value));
113 
114         } finally {
115             cancelIfActive(uba);
116             client.clear();
117         }
118 
119     }
120 
121     /**
122      * Utility method for cancelling a Business Activity if it is currently active.
123      * 
124      * @param uba The User Business Activity to cancel.
125      */
126     private void cancelIfActive(UserBusinessActivity uba) {
127         try {
128             uba.cancel();
129         } catch (Throwable th2) {
130             // do nothing, already closed
131         }
132     }
133 }