Package org.apache.clerezza.triaxrs

Examples of org.apache.clerezza.triaxrs.JaxRsHandler


    body = null;
  }

  @Test
  public void tesPost1() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyRootResource.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();

    String queryParam = "post1";
    final String bodyString = "body";
    uri.setPath("foo");
    uri.setQuery("name="+queryParam);
    request.setRequestURI(uri);
    String[] headervalues = new String[1];
    headervalues[0]="text/plain";
    request.setHeader(HeaderName.CONTENT_TYPE, headervalues);
    MessageBody messageBody = new MessageBody2Read() {

      @Override
      public ReadableByteChannel read() throws IOException {
        return Channels.newChannel(new ByteArrayInputStream(bodyString.getBytes()));
      }
    };

    request.setMessageBody(messageBody);

    request.setMethod(Method.POST);

    handler.handle(request, response);

    Assert.assertEquals(queryParam, value);
    Assert.assertEquals(bodyString, body);
  }
View Full Code Here


    Assert.assertEquals(bodyString, body);
  }

  @Test
  public void testPost2() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyRootResource.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();

    uri.setPath("foo");
    request.setRequestURI(uri);
    request.setMethod(Method.POST);

    handler.handle(request, response);

    Assert.assertEquals("post2", value);
    Assert.assertNull(body);
  }
View Full Code Here

    Assert.assertNull(body);
  }

  @Test
  public void tesPost3() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyRootResource.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();

    String queryParam = "post1";
    uri.setPath("foo");
    uri.setQuery("name="+queryParam);
    request.setRequestURI(uri);

    request.setMethod(Method.POST);

    handler.handle(request, response);

    Assert.assertEquals("post2", value);
    Assert.assertEquals(null, body);
  }
View Full Code Here

  }

  @Test
  public void performPut() throws Exception {

    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
   
    Request requestMock = EasyMock.createNiceMock(Request.class);
    Response responseMock = EasyMock.createNiceMock(Response.class);
    expect(requestMock.getMethod()).andReturn(Method.PUT).anyTimes();
    String[] contentTypeHeader = {"text/plain"};
    expect(requestMock.getHeaderValues(HeaderName.CONTENT_TYPE)).andReturn(contentTypeHeader).anyTimes();
    //this redundancy makes me prefer not to use mocks
    Set<HeaderName> headerNames = new HashSet<HeaderName>();
    headerNames.add(HeaderName.CONTENT_TYPE);
    expect(requestMock.getHeaderNames()).andReturn(headerNames).anyTimes();
    final String message = "The message in the body";
    expect(requestMock.getMessageBody()).andReturn(new MessageBody2Write() {

      @Override
      public void writeTo(WritableByteChannel out) throws IOException {
        out.write(ByteBuffer.wrap(message.getBytes()));
      }
    });
    RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
    expect(requestURI.getPath()).andReturn("/");
    expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
    replay(requestMock);
    replay(requestURI);
    replay(responseMock);
    handler.handle(requestMock, responseMock);
    Assert.assertEquals(message, receivedBody);

  }
View Full Code Here

  }

  @Test
  public void testResponseToOptionsRequest() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class, MyResource2.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();
    uri.setPath("/");
    request.setRequestURI(uri);
    request.setMethod(Method.OPTIONS);
    handler.handle(request, response);
    Map<HeaderName,String[]> headers = response.getHeaders();
    String[] allowHeader = headers.get(HeaderName.ALLOW);
    Assert.assertNotNull(allowHeader);
    String allow = "";
    for(String st : allowHeader){
View Full Code Here

    Assert.assertTrue(allow.contains("FOO"));
  }

  @Test
  public void testResponseToOptionsRequestOnSubresource() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();
    uri.setPath("/sub");
    request.setRequestURI(uri);
    request.setMethod(Method.OPTIONS);
    handler.handle(request, response);
    Map<HeaderName,String[]> headers = response.getHeaders();
    String[] allowHeader = headers.get(HeaderName.ALLOW);
    Assert.assertNotNull(allowHeader);
    String allow = "";
    for(String st : allowHeader){
View Full Code Here

    Assert.assertFalse(allow.contains("FOO"));
  }

  @Test
  public void testResponseToOptionsRequestWithNoResource() throws Exception {
    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class, MyResource2.class);
    RequestURIImpl uri = new RequestURIImpl();
    RequestImpl request = new RequestImpl();
    ResponseImpl response = new ResponseImpl();

    uri.setType(RequestURI.Type.NO_RESOURCE);
    request.setRequestURI(uri);
    request.setMethod(Method.OPTIONS);
    handler.handle(request, response);
    Map<HeaderName,String[]> headers = response.getHeaders();
    String[] allowHeader = headers.get(HeaderName.ALLOW);
    Assert.assertNotNull(allowHeader);
    String allow = "";
    for(String st : allowHeader){
View Full Code Here

  }

  @Test
  public void testOptions() throws Exception {

    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
   
    Request requestMock = EasyMock.createNiceMock(Request.class);
    Response responseMock = EasyMock.createNiceMock(Response.class);
    expect(requestMock.getMethod()).andReturn(Method.GET).anyTimes();
    RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
    expect(requestURI.getPath()).andReturn("/path");
    expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
    responseMock.setResponseStatus(ResponseStatus.NOT_FOUND);
    EasyMock.makeThreadSafe(responseMock, true);
    replay(requestMock);
    replay(requestURI);
    replay(responseMock);
    //handler.handle(requestMock, response);
    handler.handle(requestMock, responseMock);

    EasyMock.verify(responseMock);

  }
View Full Code Here

  }

  @Test
  public void testOptions() throws Exception {

    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);

    Request requestMock = EasyMock.createNiceMock(Request.class);
    Response responseMock = EasyMock.createNiceMock(Response.class);
    expect(requestMock.getMethod()).andReturn(Method.POST).anyTimes();
    RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
    expect(requestURI.getPath()).andReturn("/path");
    expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
    expect(requestURI.getType()).andReturn(null).anyTimes();
    replay(requestMock);
    replay(requestURI);
    replay(responseMock);
    handler.handle(requestMock, responseMock);
    assertTrue(methodInvokedForPost);

  }
View Full Code Here

  }

  @Test
  public void testResponseObject() throws Exception {

    JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class,
        IntegerWriter.class);

    Request requestMock = EasyMock.createNiceMock(Request.class);
    ResponseImpl responseImpl = new ResponseImpl();

    String stringMessage = "Hello"
    expect(requestMock.getMethod()).andReturn(Method.GET).anyTimes();
    RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
    expect(requestURI.getPath()).andReturn("/").anyTimes();
    expect(requestURI.getQuery()).andReturn("value=" + stringMessage)
        .anyTimes();
    expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
    replay(requestMock);
    replay(requestURI);
    handler.handle(requestMock, responseImpl);
    responseImpl.consumeBody();
    Assert.assertArrayEquals(stringMessage.getBytes(), responseImpl.getBodyBytes());

    reset(requestURI);
    responseImpl = new ResponseImpl();
    // now do the same with a decimal value
    String decimalString = "446544";

    expect(requestURI.getPath()).andReturn("/").anyTimes();
    expect(requestURI.getQuery()).andReturn("value=" + decimalString)
        .anyTimes();
    replay(requestURI);
    handler.handle(requestMock, responseImpl);
    responseImpl.consumeBody();
    Assert.assertArrayEquals(("int:" + decimalString).getBytes(), responseImpl.getBodyBytes());
  }
View Full Code Here

TOP

Related Classes of org.apache.clerezza.triaxrs.JaxRsHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.