Search in sources :

Example 16 with User

use of com.lonepulse.robozombie.model.User in project RoboZombie by sahan.

the class DeserializerEndpointTest method testDeserializeJsonToGenericType.

/**
	 * <p>Test for {@link Deserializers#JSON} with a generic type.</p>
	 * 
	 * @since 1.3.3
	 */
@Test
public final void testDeserializeJsonToGenericType() {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/jsonarray";
    User user1 = new User(0, "Tenzen0", "Yakushiji0", 300, true);
    User user2 = new User(1, "Tenzen1", "Yakushiji1", 300, true);
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(Arrays.asList(user1, user2)))));
    List<User> deserializedUsers = deserializerEndpoint.deserializeJsonToGenericType();
    verify(getRequestedFor(urlEqualTo(subpath)));
    for (int i = 0; i < deserializedUsers.size(); i++) {
        User user = deserializedUsers.get(i);
        assertEquals(i, user.getId());
        assertEquals("Tenzen" + String.valueOf(i), user.getFirstName());
        assertEquals("Yakushiji" + String.valueOf(i), user.getLastName());
        assertEquals(300, user.getAge());
        assertEquals(true, user.isImmortal());
    }
}
Also used : User(com.lonepulse.robozombie.model.User) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 17 with User

use of com.lonepulse.robozombie.model.User in project RoboZombie by sahan.

the class DeserializerEndpointTest method testDetachDeserializer.

/**
	 * <p>Test for detachment of the inherited deserializer.</p>
	 *
	 * @since 1.3.0
	 */
@Test
public final void testDetachDeserializer() {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    User user = new User(1, "Riza", "Hawkeye", 29, false);
    String subpath = "/detach", body = new Gson().toJson(user);
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withBody(body)));
    String responseContent = "";
    try {
        responseContent = deserializerEndpoint.detachDeserializer();
    } catch (Exception e) {
        fail("JSON deserialization was attempted.");
    }
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertEquals(body, responseContent);
}
Also used : User(com.lonepulse.robozombie.model.User) Gson(com.google.gson.Gson) ExpectedException(org.junit.rules.ExpectedException) InvocationException(com.lonepulse.robozombie.proxy.InvocationException) Test(org.junit.Test)

Example 18 with User

use of com.lonepulse.robozombie.model.User in project RoboZombie by sahan.

the class DeserializerEndpointTest method testParseJson.

/**
	 * <p>Test for {@link Deserializers#JSON}.
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testParseJson() {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/json";
    User user = new User(1, "Tenzen", "Yakushiji", 300, true);
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(user))));
    User deserializedUser = deserializerEndpoint.deserializeJson();
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertEquals(user.getId(), deserializedUser.getId());
    assertEquals(user.getFirstName(), deserializedUser.getFirstName());
    assertEquals(user.getLastName(), deserializedUser.getLastName());
    assertEquals(user.getAge(), deserializedUser.getAge());
    assertEquals(user.isImmortal(), deserializedUser.isImmortal());
}
Also used : User(com.lonepulse.robozombie.model.User) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 19 with User

use of com.lonepulse.robozombie.model.User in project RoboZombie by sahan.

the class DeserializerEndpointTest method testParseXml.

/**
	 * <p>Test for {@link Deserializers#XML}.
	 * 
	 * @throws Exception
	 * 			if the test terminated with an error
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testParseXml() throws Exception {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/xml";
    User user = new User(1, "Shiro", "Wretched-Egg", 17, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new Persister().write(user, baos);
    stubFor(get(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200).withBody(baos.toString())));
    User deserializedUser = deserializerEndpoint.deserializeXml();
    verify(getRequestedFor(urlEqualTo(subpath)));
    assertEquals(user.getId(), deserializedUser.getId());
    assertEquals(user.getFirstName(), deserializedUser.getFirstName());
    assertEquals(user.getLastName(), deserializedUser.getLastName());
    assertEquals(user.getAge(), deserializedUser.getAge());
    assertEquals(user.isImmortal(), deserializedUser.isImmortal());
}
Also used : User(com.lonepulse.robozombie.model.User) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Persister(org.simpleframework.xml.core.Persister) Test(org.junit.Test)

Example 20 with User

use of com.lonepulse.robozombie.model.User in project RoboZombie by sahan.

the class RequestParamEndpointTest method testQueryParamsFail.

/**
	 * <p>Test for a {@link Request} having illegal {@link QueryParam}s.</p>
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testQueryParamsFail() {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/queryparamsfail";
    stubFor(get(urlMatching(subpath)).willReturn(aResponse().withStatus(200)));
    expectedException.expect(Is.isA(InvocationException.class));
    requestEndpoint.queryParamsFail(new User(1, "Ra's", "al Ghul", 47, false));
}
Also used : User(com.lonepulse.robozombie.model.User) InvocationException(com.lonepulse.robozombie.proxy.InvocationException) Test(org.junit.Test)

Aggregations

User (com.lonepulse.robozombie.model.User)25 Test (org.junit.Test)25 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)14 Gson (com.google.gson.Gson)8 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Condition (java.util.concurrent.locks.Condition)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 HttpResponse (org.apache.http.HttpResponse)2 SerializableEntity (org.apache.http.entity.SerializableEntity)2 Persister (org.simpleframework.xml.core.Persister)2 ExpectedException (org.junit.rules.ExpectedException)1 TestHttpResponse (org.robolectric.tester.org.apache.http.TestHttpResponse)1