Search in sources :

Example 1 with Resources

use of br.com.caelum.restfulie.Resources in project restfulie-java by caelum.

the class MappingConfigTest method shouldSerializeSimpleOrder.

@Test
public void shouldSerializeSimpleOrder() {
    String expected = "<?xml version='1.0' encoding='utf-8'?>" + "<order>" + "<location>takeAway</location>" + "<items></items>" + "<status>unpaid</status>" + "</order>";
    Resources resources = new MappingConfig().getServer();
    Order order = new Order("unpaid", new ArrayList<Item>(), Location.takeAway);
    StringWriter w = new StringWriter();
    resources.getSerializerFor(w, order).serialize();
    assertEquals(expected, w.getBuffer().toString());
}
Also used : Order(br.com.caelum.restbucks.model.Order) Item(br.com.caelum.restbucks.model.Item) StringWriter(java.io.StringWriter) Resources(br.com.caelum.restfulie.Resources) Test(org.junit.Test)

Example 2 with Resources

use of br.com.caelum.restfulie.Resources in project restfulie-java by caelum.

the class Entry method happyPathTest.

private static void happyPathTest(URI uri) throws Exception {
    Resources resources = new MappingConfig().getServer();
    // Place the order
    System.out.println(String.format("About to start happy path test. Placing order at [%s] via POST", uri.toString()));
    Order order = createOrder();
    order = resources.entryAt(uri).post(order);
    System.out.println(String.format("Order placed at [%s]", order.getSelfUri()));
    // Pay for the order
    Payment payment = new Payment("12345677878", "guilherme silveira", 12, 2999, order.getCost());
    System.out.println(String.format("About to create a payment resource at [%s] via PUT", resource(order).getRelation("payment").getHref()));
    payment = order.pay(payment);
    System.out.println("Payment made, receipt created at: " + payment.getCreatedAt());
    // Check on the order status
    System.out.println(String.format("About to check order status at [%s] via GET", order.getSelfUri()));
    order = (Order) resource(order).getRelation("self").accessAndRetrieve();
    System.out.println(String.format("Final order placed, current status [%s]", order.getStatus()));
    // Allow the barista some time to make the order
    System.out.println("Pausing the client, press a key to continue");
    System.in.read();
    // Take the order if possible
    System.out.println(String.format("Trying to take the ready order from [%s] via DELETE. Note: the internal state machine must progress the order to ready before this should work, otherwise expect a 405 response.", order.getSelfUri()));
    order = (Order) resource(order).getRelation("self").accessAndRetrieve();
    Response finalResponse = resource(order).getRelation("retrieve").method(HttpMethod.DELETE).access();
    System.out.println(String.format("Tried to take final order, HTTP status [%d]", finalResponse.getCode()));
    if (finalResponse.getCode() == 200) {
        System.out.println(String.format("Order status [%s], enjoy your drink", finalResponse.getCode()));
    }
}
Also used : Order(br.com.caelum.restbucks.model.Order) Response(br.com.caelum.restfulie.Response) Payment(br.com.caelum.restbucks.model.Payment) Resources(br.com.caelum.restfulie.Resources)

Example 3 with Resources

use of br.com.caelum.restfulie.Resources in project restfulie-java by caelum.

the class MappingConfigTest method shouldBeAbleToUnderstandFields.

@Test
public void shouldBeAbleToUnderstandFields() {
    String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<order>" + "  <created-at>2010-02-09T15:15:46Z</created-at>" + "  <id>360</id>" + "  <location>takeAway</location>" + "  <status>unpaid</status>" + "  <updated-at>2010-02-09T15:15:46Z</updated-at>" + "  <cost>20</cost>" + "  <items>" + "    <item>" + "      <created-at>2010-02-09T15:15:46Z</created-at>" + "      <drink>latte</drink>" + "      <id>784</id>" + "      <milk>semi</milk>" + "      <size>large</size>" + "      <updated-at>2010-02-09T15:15:46Z</updated-at>" + "      <atom:link href=\"http://localhost:3000/orders/360\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"self\"/>" + "    </item>" + "    <item>" + "      <created-at>2010-02-09T15:15:46Z</created-at>" + "      <drink>latte</drink>" + "      <id>785</id>" + "      <milk>whole</milk>" + "      <size>medium</size>" + "      <updated-at>2010-02-09T15:15:46Z</updated-at>" + "      <atom:link href=\"http://localhost:3000/orders/360\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"self\"/>" + "    </item>" + "  </items>" + "  <atom:link href=\"http://localhost:3000/orders/360\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"self\"/>" + "  <atom:link href=\"http://localhost:3000/orders/360\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"cancel\"/>" + "  <atom:link href=\"http://localhost:3000/orders/360/payment\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"pay\"/>" + "  <atom:link href=\"http://localhost:3000/orders/360\" xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"update\"/>" + "</order>";
    Resources resources = new MappingConfig().getServer();
    Order order = (Order) resources.getDeserializer().fromXml(content);
    // dumb test for config
    assertTrue(order != null);
}
Also used : Order(br.com.caelum.restbucks.model.Order) Resources(br.com.caelum.restfulie.Resources) Test(org.junit.Test)

Aggregations

Order (br.com.caelum.restbucks.model.Order)3 Resources (br.com.caelum.restfulie.Resources)3 Test (org.junit.Test)2 Item (br.com.caelum.restbucks.model.Item)1 Payment (br.com.caelum.restbucks.model.Payment)1 Response (br.com.caelum.restfulie.Response)1 StringWriter (java.io.StringWriter)1