|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package org.javaee7.jaxrs.singelton.application; |
| 7 | + |
| 8 | +import java.util.StringTokenizer; |
| 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 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import static org.junit.Assert.*; |
| 16 | +import org.junit.FixMethodOrder; |
| 17 | +import org.junit.runners.MethodSorters; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Arun Gupta |
| 21 | + */ |
| 22 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 23 | +public class MyResourceTest { |
| 24 | + |
| 25 | + WebTarget target; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setUp() { |
| 29 | + Client client = ClientBuilder.newClient(); |
| 30 | + target = client.target("http://localhost:8080/singleton-application/webresources/myresource"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void test1Post() { |
| 35 | + target.request().post(Entity.text("pineapple")); |
| 36 | + target.request().post(Entity.text("mango")); |
| 37 | + target.request().post(Entity.text("kiwi")); |
| 38 | + target.request().post(Entity.text("passion fruit")); |
| 39 | + |
| 40 | + String list = target.request().get(String.class); |
| 41 | + StringTokenizer tokens = new StringTokenizer(list, ","); |
| 42 | + assertEquals(4, tokens.countTokens()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void test2Get() { |
| 47 | + String response = target.path("2").request().get(String.class); |
| 48 | + assertEquals("kiwi", response); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void test3Delete() { |
| 53 | + target.path("kiwi").request().delete(); |
| 54 | + |
| 55 | + String list = target.request().get(String.class); |
| 56 | + StringTokenizer tokens = new StringTokenizer(list, ","); |
| 57 | + assertEquals(3, tokens.countTokens()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void test4Put() { |
| 62 | + target.request().put(Entity.text("apple")); |
| 63 | + |
| 64 | + String list = target.request().get(String.class); |
| 65 | + StringTokenizer tokens = new StringTokenizer(list, ","); |
| 66 | + assertEquals(4, tokens.countTokens()); |
| 67 | + } |
| 68 | +} |
0 commit comments