how to perform unit testing jersey webservice - web-services

Actully I am trying to test my Jersey web service with the jersey test framework. the web server I am using is Websphere 7 and java version 6. This is my project requirement I cannot upgrade the java version .
My issue is how to build the unit test for my web service . I want to test them on WebSphere but I am not sure how do I setup the environment for unit testing like junit .
more specifically I just need to call an URL from the test class and check the response. But how to call the URL from the test class on the websphere I am not getting direction for it.

Have a look at the documentation for Jersey Test Framework.
First thing you need is one of the Supported Containers dependencies. Any of those will pull in the core framework e.g.
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>
Then you need a test class that extends JerseyTest. You can override Application configure() to provide a ResourceConfig as well as any other providers or properties. For example
#Path("/test")
public class TestResource {
#GET
public String get() { return "hello"; }
}
public class TestResourceTest extends JerseyTest {
#Override
public Application configure() {
ResourceConfig config = new ResourceConfig();
config.register(TestResource.class);
}
#Test
public void doTest() {
Response response = target("test").request().get();
assertEquals(200, response.getStatus());
assertEquals("hello", response.readEntity(String.class));
}
}
You should visit the link provided to learn more and see more examples.

Related

RESTful application without web.xml

I have created a sample project and used EJB 3.1 with a RESTful web service. In the sample I have a class which extends Application. I expect the class works like a servlet and dispatch requests to appropriate classes but it does not. When I use web.xml my sample project works fine. What is wrong with my sample project?
#ApplicationPath("/rest")
public class ApplicationServlet extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(UserWS.class);
return classes;
}
}
I use UserWS as a EJB session bean which exposes web service:
#Stateless
#LocalBean
#Path("/user")
public class UserWS {
private int count;
public UserWS() {
this.count=0;
}
#GET
#Path("/name/{username}")
public void getUserName(#PathParam("username") String username) {
count++;
System.out.println("count is:"+ count);
}
}
I'm afraid it won't be possible once JBoss 5.0 supports only Servlet 2.5. For more details, see here.
To avoid the web.xml deployment descriptor, you need a servlet container the supports at least Servlet 3.0.
So, what could you do to solve it?
These are the options that came up to my mind:
You could try upgrading the JBoss Web (Tomcat fork used by JBoss AS) as described here, but try that at you own risk.
Consider using a recent version of JBoss/WildFly.

JBoss WebServices and EJB

My application should show a list of stuff over RESTful WebServices.
This list of stuff is generated via an EJB which is deployed in JBoss AS 6 in a .jar.
My question is, how do I get this list of stuff in order to show it via Web Services?
If I try to inject #EJB in the stateless bean annotated with #WebService, and try to invoke the method that generates the list, I get an error saying that #EJB cannot be resolved (or imported). (fixed it, needed to modify maven dependencies).
Now I get a NullPointerException when I call the supposedly injected EJB...
I am using the webapp archetype from Maven, which contains an EAR, a JAR and a WAR.
I am at a total loss here, been trying to figure this for almost a week.
Thanks in advance!
--EDIT-- code and typo fixed
This is my stateless bean which SHOULD be exposed as a WebService (according to:
http://www.adam-bien.com/roller/abien/entry/restful_calculator_with_javascript_and )
#Stateless
#Path("/MyRESTApplication")
public class HelloWorldResource {
#EJB
private TestBean testBean;
#GET()
#Produces("text/plain")
public String sayHello(String name) {
return testBean.doMoreStuff();
}
}
the doMoreStuff() function simply returns "HELLO!".

Webservice call from Jenkins

I need to write a webservice client and call it from Jenkins. Below are my questions:
What is the best way to call a web service from Jenkins? Any default plug in available? I need to pass a XML data as input to the web service.
If plug in is not the option, can you please let me know what are the other ways we can achieve this (ANT+JAVA etc)?
If you have any sample code, that would be great.
Thanks
Aravind
It would be great to know you just need to call your client as part of some complex flow, implemented as a Jenkins job, or you want to concentrate on webservice testing.
WillieT has pointed you to several simple recipes which can be used to solve some basic tasks. If you need more power, better reporting, some additional features please consider the following:
Apache JMeter (details)
Building a WebService Test Plan
Web Service Testing in JMeter
Webservice testing with JMeter
JMeter can be integrated into Jenkins using Performance plugin. Report example:
Grinder (details)
I prefer to use this tool, but it might be to complex/heavy for you.
Grinder script gallery
How to Test REST Web Service Using The Grinder
Grinder can be integrated into Jenkins using Grinder plugin. Report example:
If you develop a plugin, e.g. extends hudson.tasks.Builder, include the following in pom.xml for JAX-RS Client:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
A sample JAX-RS Client:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class RestClient {
private static String BASE_URL = "http://localhost:8090/rest";
private static String ACCESS_TOKEN = "8900***bc1";
public static String query(String path) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
// token authentication
String result = target.path(path).request().header("Authorization", "Token " + ACCESS_TOKEN)
.accept(MediaType.APPLICATION_JSON).get(String.class);
return result;
}
private static URI getBaseURI() {
return UriBuilder.fromUri(BASE_URL).build();
}
}
where http://localhost:8090/rest is the base rest url outside of Jenkins environment. Anywhere in your plugin code, you can simple call this as needed:
String rsData = RestClient.query("/project_type");
assume the full rest web service url is
http://localhost:8090/rest/project_type
You may also use Apache HttpClient, or OkHttp
I used 'HTTP Request' Plugin. This plugin works for REST as well as SOAP api.
enter image description here
Plugin image

Singleton class with #Startup annotation WL 11g

i have a problem deploying my webservice to Weblogic 11g.
JAVA: JRockit 1.6.x
I need to run a method on webservice deployment and i made this code:
#Singleton
#Startup
public class StartupBean {
Logger logger = Logger.getLogger(StartupBean.class);
#PostConstruct
private void postConstruct() {
logger.error("WS started.");
}
#PreDestroy
private void preDestroy() {
logger.error("WS stoped.");
}
}
without any additional xml config.
It works normally on 12c but i need it on 11g.
What is the workaround?
Thanks
No, you can't do this in pre-3.1-EJB without XML config.
The common practice in EJB 3.0 for implementing a #Startup bean was to instantiate in in a servlet, which is configured to load on startup in web.xml. If you need it just for log4j you can initialize it directly from such servlet.

Access EJB 3.0 as an EJB and as a web service on Websphere 7

I want to deploy an EJB 3.0 stateless bean to WAS7 so I can access it as an EJB through a local interface and also as a jax-ws web service.
My bean looks as following:
#Stateless
#WebService
public class UserManagerImpl implements UserManager {
public UserManagerImpl() {
}
#WebMethod
public String getName(){
return "UserName";
}
}
The problem is that if I package it into an EJB-JAR and deploy, it doesn't work as a web service on WAS-7.
The only working configuration for me is if I put the EJB-JAR into a EAR and put this EJB-JAR to a WAR that is also in the EAR, like this:
EAR/
|--EJB-JAR
|--WAR/
|WEB-INF/lib/
|EJB-JAR
So my bean is duplicated.
Is there any problem with this design? If so, is there a better solution?
If your application contains #WebService annotated EJBs, then you need to process the EAR with the endptEnabler tool shipped with WebSphere before deploying it. Note that this doesn't apply to #WebService annotated classes in Web modules.