Skip to content

Commit

Permalink
[RESTEASY-1361] Also adding new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asoldano committed Jul 8, 2016
1 parent 06a1350 commit 0895f1d
Show file tree
Hide file tree
Showing 206 changed files with 11,483 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pom.xml
Expand Up @@ -75,6 +75,7 @@
<version.org.yaml.snakeyaml>1.17</version.org.yaml.snakeyaml>
<version.javax.validation-api>1.1.0.Final</version.javax.validation-api>
<version.jboss-javaee-6.0.spec>1.0.0.Final</version.jboss-javaee-6.0.spec>
<version.weld21>2.1.0.Final</version.weld21>
</properties>

<url>http://rest-easy.org</url>
Expand Down Expand Up @@ -735,6 +736,16 @@
<artifactId>hamcrest-all</artifactId>
<version>${version.hamcrest}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>${version.weld21}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>${version.weld21}</version>
</dependency>
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion testsuite/pom.xml
Expand Up @@ -13,7 +13,7 @@

<modules>
<module>arquillian-utils</module>
<!-- <module>unit-tests</module> -->
<module>unit-tests</module>
<module>integration-tests</module>
</modules>

Expand Down
80 changes: 80 additions & 0 deletions testsuite/unit-tests/pom.xml
@@ -0,0 +1,80 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-testsuite</artifactId>
<version>3.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>resteasy-unit-tests</artifactId>
<name>RESTEasy testsuite: Unit tests</name>

<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>arquillian-utils</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-atom-provider</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-crypto</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jose-jwt</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,109 @@
package org.jboss.resteasy.test.client;

import org.jboss.resteasy.utils.TestUtil;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.Response;


import static org.jboss.resteasy.test.TestPortProvider.generateURL;

/**
* @tpSubChapter Resteasy-client
* @tpChapter Unit tests
* @tpSince EAP 7.0.0
*
*/
public class ClientBuilderTest {

/**
* @tpTestDetails Create string entity with unparsable media type
* @tpPassCrit IllegalArgumentException is raised
* @tpSince EAP 7.0.0
*/
@Test(expected = IllegalArgumentException.class)
public void entityStringThrowsExceptionWhenUnparsableTest() throws Exception {
Entity.entity("entity", "\\//\\");
Assert.fail();
}

/**
* @tpTestDetails Create client, set up custom property for it, get client configuration
* and create new client using that configuration
* @tpPassCrit Client is created
* @tpSince EAP 7.0.0
*/
@Test
public void testBuilder() throws Exception {
String property = "prop";
Client client = ClientBuilder.newClient();
client.property(property, property);
Configuration config = client.getConfiguration();
client = ClientBuilder.newClient(config);

}

/**
* @tpTestDetails Create client with custom property, check that property is set and remove the property
* from client configuration
* @tpPassCrit Property is added and removed from client configuration
* @tpSince EAP 7.0.0
*/
@Test
public void addAndRemovePropertyTest() throws Exception {
String property = "prop";
Client client = ClientBuilder.newClient();
client.property(property, property);
Object p = client.getConfiguration().getProperty(property);
Assert.assertEquals("prop", (String) p);
try {
client.property(property, null);
} catch (NullPointerException e) {
Assert.fail(TestUtil.getErrorMessageForKnownIssue("JBEAP-324", "Couldn't remove property"));
}
p = client.getConfiguration().getProperty(property);
Assert.assertEquals(null, p);
}

/**
* @tpTestDetails Invoke target method on the closed client
* @tpPassCrit IllegalStateException is raised
* @tpSince EAP 7.0.0
*/
@Test(expected = IllegalStateException.class)
public void closeClientSendRequestTest() throws Exception {
Client client = ClientBuilder.newClient();
client.close();
client.target(generateURL("/"));
}

/**
* @tpTestDetails Create Webtarget instance, close the client and execute get request on the original
* Webtarget instance
* @tpPassCrit IllegalStateException is raised
* @tpSince EAP 7.0.0
*/
@Test(expected = IllegalStateException.class)
public void closeClientWebTargetTest() throws Exception {
Client client = ClientBuilder.newClient();
WebTarget base = client.target(generateURL("/") + "/test");
client.close();
Response response = base.request().get();
}

public static class FeatureReturningFalse implements Feature {
@Override
public boolean configure(FeatureContext context) {
// false returning feature is not to be registered
return false;
}
}
}
@@ -0,0 +1,155 @@
package org.jboss.resteasy.test.client;

import org.jboss.resteasy.test.client.resource.NullStringBeanRuntimeDelegate;
import org.jboss.resteasy.test.client.resource.StringBean;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterAbortWith;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterNullHeaderString;
import org.jboss.resteasy.test.client.resource.StringBeanRuntimeDelegate;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterLength;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterAllowed;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterHeaders;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterStatusOverride;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterInterceptorReaderOne;
import org.jboss.resteasy.test.client.resource.ClientResponseFilterInterceptorReaderTwo;
import org.jboss.resteasy.util.HttpResponseCodes;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.RuntimeDelegate;

/**
* @tpSubChapter Resteasy-client
* @tpChapter Client tests
* @tpSince EAP 7.0.0
*/
public class ClientResponseFilterTest {

static Client client;
String dummyUrl = "dummyUrl";

@BeforeClass
public static void setupClient() {
client = ClientBuilder.newClient();

}

@AfterClass
public static void close() {
client.close();
}

/**
* @tpTestDetails Client registers ClientRequestFilter and ClientResponseFilter. The first returns Response provided
* as argument. The latter processes the response and checks that the header of the response is not null.
* Note: This test uses custom implementation of RuntimeDelegate, allowing to customize conversion between String
* representation of HTTP header and the corresponding JAX-RS type (StringBean in this case)
* @tpPassCrit Successful response is returned and response header is not null
* @tpSince EAP 7.0.0
*/
@Test
public void emptyHeaderStringTest() {

RuntimeDelegate original = RuntimeDelegate.getInstance();
RuntimeDelegate.setInstance(new NullStringBeanRuntimeDelegate(original));
try {
Response abortWith = Response.ok().header("header1", new StringBean("aa"))
.build();
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(abortWith))
.register(ClientResponseFilterNullHeaderString.class).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
} finally {
RuntimeDelegate.setInstance(original);
StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate();
}

}

/**
* @tpTestDetails Client registers ClientRequestFilter and ClientResponseFilter. The first returns Response provided
* as argument. The latter processes the response and checks that the length of the response is same
* as in the original response
* @tpPassCrit Successful response is returned and response length is same as original response
* @tpSince EAP 7.0.0
*/
@Test
public void lengthTest() {
Response abortWith = Response.ok()
.header(HttpHeaders.CONTENT_LENGTH, 10).build();
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(abortWith))
.register(ClientResponseFilterLength.class).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
}

/**
* @tpTestDetails Client registers ClientRequestFilter and ClientResponseFilter. The first returns Response provided
* as argument. The latter processes the response and checks that response contains header 'OPTIONS' as allowed method
* @tpPassCrit Successful response is returned and 'OPTIONS' method is allowed header
* @tpSince EAP 7.0.0
*/
@Test
public void allowedTest() {
Response abortWith = Response.ok().header(HttpHeaders.ALLOW, "get")
.header(HttpHeaders.ALLOW, "options").build();
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(abortWith))
.register(ClientResponseFilterAllowed.class).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());

}

/**
* @tpTestDetails Client registers ClientRequestFilter and ClientResponseFilter. The first returns Response provided
* as argument. The latter processes the response and changes the response code to 'FORBIDDEN'
* @tpPassCrit Response with status code 'FORBIDDEN' is returned
* @tpSince EAP 7.0.0
*/
@Test
public void statusOverrideTest() {
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(Response.ok().build()))
.register(ClientResponseFilterStatusOverride.class).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
}

/**
* @tpTestDetails Client registers ClientRequestFilter and ClientResponseFilter. The first returns Response provided
* as argument. The latter processes the response and prints all headers in the response
* @tpPassCrit The response with reponse code success is expected
* @tpSince EAP 7.0.0
*/
@Test
public void headersTest() {
Response.ResponseBuilder builder = Response.ok()
.header("header", MediaType.APPLICATION_ATOM_XML_TYPE)
.entity("entity");
Response abortWith = builder.build();
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(abortWith))
.register(ClientResponseFilterHeaders.class).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
}

/**
* @tpTestDetails Client registers ClientRequestFilter and two ReaderInterceptors. The first returns Response provided
* as argument. The ReaderInterceptorOne calls ReaderInterceptorTwo and catches IOException raised by ReaderInterceptorTwo
* @tpPassCrit The ReaderInterceptorOne catches IOException and sends successful response
* @tpSince EAP 7.0.0
*/
@Test
public void interceptorOrderTest() {
Response.ResponseBuilder builder = Response.ok()
.header("header", MediaType.APPLICATION_ATOM_XML_TYPE)
.entity("entity");
Response abortWith = builder.build();
Response response = client.target(dummyUrl).register(new ClientResponseFilterAbortWith(abortWith))
.register(ClientResponseFilterInterceptorReaderTwo.class)
.register(ClientResponseFilterInterceptorReaderOne.class)
.request().get();
String str = response.readEntity(String.class);
Assert.assertEquals("First ReaderInterceptor one didn't catch exception raised by ReaderInterceptor two", "OK", str);
}
}

0 comments on commit 0895f1d

Please sign in to comment.