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.wshelloworld;
18  
19  import static org.junit.Assert.*;
20  
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.jboss.as.quickstarts.wshelloworld.HelloWorldService;
27  import org.junit.Before;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  
31  /**
32   * Simple set of tests for the HelloWorld Web Service to demonstrate accessing the web service using a client
33   * 
34   * @author lnewson@redhat.com
35   */
36  public class ClientTest {
37      /**
38       * The name of the WAR Archive that will be used by Arquillian to deploy the application.
39       */
40      private static final String APP_NAME = "jboss-helloworld-ws";
41      /**
42       * The path of the WSDL endpoint in relation to the deployed web application.
43       */
44      private static final String WSDL_PATH = "HelloWorldService?wsdl";
45      
46      /**
47       * The name for the Server URL System Property.
48       */
49      private static final String SERVER_URL_PROPERTY = "serverUrl";
50      /**
51       * The Default Server URL if one isn't specified as a System Property
52       */
53      private static final String DEFAULT_SERVER_URL = "http://localhost:8080/";
54  
55      private static URL deploymentUrl;
56  
57      private HelloWorldService client;
58      
59      @BeforeClass
60      public static void beforeClass() throws MalformedURLException
61      {
62          String deploymentUrl = System.getProperty(SERVER_URL_PROPERTY);
63          
64          // Check that the server URL property was set. If it wasn't then use the default.
65          if (deploymentUrl == null || deploymentUrl.isEmpty()) {
66              deploymentUrl = DEFAULT_SERVER_URL;
67          }
68          
69          // Ensure that the URL ends with a forward slash
70          if (!deploymentUrl.endsWith("/")) {
71              deploymentUrl += "/";
72          }
73          
74          // Ensure the App Name is specified in the URL
75          if (!deploymentUrl.matches(".*" + APP_NAME + ".*"))
76          {
77              deploymentUrl += APP_NAME + "/";
78          }
79          
80          // Add the WDSL Document location to the URL
81          deploymentUrl += WSDL_PATH;
82          
83          System.out.println("WSDL Deployment URL: " + deploymentUrl);
84          
85          // Set the deployment url
86          ClientTest.deploymentUrl = new URL(deploymentUrl);
87      }
88  
89      @Before
90      public void setup() {
91          try {
92              client = new Client(new URL(deploymentUrl, WSDL_PATH));
93          } catch (MalformedURLException e) {
94              e.printStackTrace();
95          }
96      }
97  
98      @Test
99      public void testHello() {
100         System.out.println("[Client] Requesting the WebService to say Hello.");
101 
102         // Get a response from the WebService
103         final String response = client.sayHello();
104         assertEquals(response, "Hello World!");
105 
106         System.out.println("[WebService] " + response);
107 
108     }
109 
110     @Test
111     public void testHelloName() {
112         System.out.println("[Client] Requesting the WebService to say Hello to John.");
113 
114         // Get a response from the WebService
115         final String response = client.sayHelloToName("John");
116         assertEquals(response, "Hello John!");
117 
118         System.out.println("[WebService] " + response);
119     }
120 
121     @Test
122     public void testHelloNames() {
123         System.out.println("[Client] Requesting the WebService to say Hello to John, Mary and Mark.");
124 
125         // Create the array of names for the WebService to say hello to.
126         final List<String> names = new ArrayList<String>();
127         names.add("John");
128         names.add("Mary");
129         names.add("Mark");
130 
131         // Get a response from the WebService
132         final String response = client.sayHelloToNames(names);
133         assertEquals(response, "Hello John, Mary & Mark!");
134 
135         System.out.println("[WebService] " + response);
136     }
137 }