|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package org.javaee7.jaxrs.endpoint.test; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URISyntaxException; |
| 8 | + |
| 9 | +import javax.ws.rs.client.Client; |
| 10 | +import javax.ws.rs.client.ClientBuilder; |
| 11 | +import javax.ws.rs.client.Entity; |
| 12 | +import javax.ws.rs.client.WebTarget; |
| 13 | + |
| 14 | +import junit.framework.Assert; |
| 15 | + |
| 16 | +import org.javaee7.jaxrs.endpoint.Database; |
| 17 | +import org.javaee7.jaxrs.endpoint.MyApplication; |
| 18 | +import org.javaee7.jaxrs.endpoint.MyResource; |
| 19 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 20 | +import org.jboss.arquillian.container.test.api.TargetsContainer; |
| 21 | +import org.jboss.arquillian.junit.Arquillian; |
| 22 | +import org.jboss.arquillian.junit.InSequence; |
| 23 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 24 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Nikolaos Ballas |
| 30 | + * |
| 31 | + */ |
| 32 | +@RunWith(Arquillian.class) |
| 33 | +public class JaxRSEndpointTest { |
| 34 | + |
| 35 | + private Client client = null; |
| 36 | + |
| 37 | + @Deployment(testable=false) @TargetsContainer("wildfly-arquillian") |
| 38 | + public static WebArchive createDeplymentI() { |
| 39 | + return ShrinkWrap.create(WebArchive.class, "jaxrs-endpoint.war"). |
| 40 | + addClass(MyApplication.class). |
| 41 | + addClass(Database.class). |
| 42 | + addClass(MyResource.class); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Test |
| 47 | + @InSequence(1) |
| 48 | + public void invokeEndpointPUTTest() throws URISyntaxException{ |
| 49 | + WebTarget targetEndpoint= getTargetEndpoint("fruit"); |
| 50 | + targetEndpoint.request().put(Entity.text("banana")); |
| 51 | + } |
| 52 | + @Test |
| 53 | + @InSequence(2) |
| 54 | + public void invokeEndpointGETTest() throws URISyntaxException { |
| 55 | + WebTarget targetEndPoint = getTargetEndpoint("fruit"); |
| 56 | + String received = targetEndPoint.request().get(String.class); |
| 57 | + Assert.assertEquals("[banana]" ,received); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * The method for obtaining a target endpoint. |
| 62 | + * @param endpointName the target endpoint name which we want to invoke |
| 63 | + * @return a WebTarget object |
| 64 | + * @throws URISyntaxException |
| 65 | + */ |
| 66 | + public WebTarget getTargetEndpoint(String endpointName) throws URISyntaxException { |
| 67 | + if(client == null) { |
| 68 | + client = ClientBuilder.newClient(); |
| 69 | + } |
| 70 | + return client.target(new URI("http://localhost:8080/jaxrs-endpoint/webresources/"+endpointName)); |
| 71 | + } |
| 72 | +} |
0 commit comments