public static MockHttpRequest deepCopy(HttpRequest request) throws IOException {
   MockHttpRequest mock = new MockHttpRequest();
   mock.uri = request.getUri();
   mock.httpHeaders = (ResteasyHttpHeaders) request.getHttpHeaders();
   mock.httpMethod = request.getHttpMethod();
   byte[] bytes = ReadFromStream.readFromStream(1024, request.getInputStream());
   mock.inputStream = new ByteArrayInputStream(bytes);
   return mock;
 }
  private static MockHttpRequest initWithUri(URI absoluteUri, URI baseUri) {
    if (baseUri == null) baseUri = EMPTY_URI;
    MockHttpRequest request = new MockHttpRequest();
    request.httpHeaders = new ResteasyHttpHeaders(new CaseInsensitiveMap<String>());
    // request.uri = new UriInfoImpl(absoluteUri, absoluteUri, absoluteUri.getPath(),
    // absoluteUri.getQuery(), PathSegmentImpl.parseSegments(absoluteUri.getPath()));

    // remove query part
    URI absolutePath = UriBuilder.fromUri(absoluteUri).replaceQuery(null).build();
    // path must be relative to the application's base uri
    URI relativeUri = baseUri.relativize(absoluteUri);
    relativeUri =
        UriBuilder.fromUri(relativeUri.getRawPath())
            .replaceQuery(absoluteUri.getRawQuery())
            .build();

    request.uri = new ResteasyUriInfo(baseUri, relativeUri);
    return request;
  }
Esempio n. 3
0
  @Test
  public void testNotMappedInSubresource() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    POJOResourceFactory noDefaults = new POJOResourceFactory(LocatingResource.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);

    {
      MockHttpRequest request = MockHttpRequest.get("/locating/notmatching");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
    }
  }
  @Test
  public void getTripTest() throws Exception {
    User user = new User();
    user.setId(1l);
    user.setUsername("damijano");
    user.setFirstName("New");
    user.setLastName("User");
    user.setPassword("1234");
    user.setEmail("*****@*****.**");
    user.setResponsible("system");
    user.setCreated(new Date());
    user.setUpdated(new Date());

    Trip trip = new Trip();
    trip.setId(1l);
    trip.setTitle("Uživancija");
    trip.setDestination("Ljubljana");
    trip.setFreeSpaces(2);
    trip.setMeetingPoint("Celje");
    trip.setStart(new Date());
    trip.setFinish(new Date());
    trip.setResponsible("damijano");
    trip.setLeader(user);

    TripServiceImpl service = mock(TripServiceImpl.class);
    when(service.getTripById(1L)).thenReturn(trip);

    TripResource resource = new TripResource();
    resource.service = service;

    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
    dispatcher.getRegistry().addSingletonResource(resource);

    {
      MockHttpRequest request = MockHttpRequest.get(URI + "/1");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
      assertThat(response.getOutputHeaders().getFirst("Content-Type"))
          .isEqualTo(MediaType.APPLICATION_JSON_TYPE);
      assertThat(response.getContentAsString()).contains("{\"id\":1");
      assertThat(response.getContentAsString()).excludes("password");
      System.out.println(response.getContentAsString());
    }
  }
Esempio n. 5
0
  @Test
  public void testSubSubresource() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    POJOResourceFactory noDefaults = new POJOResourceFactory(LocatingResource.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);

    {
      MockHttpRequest request = MockHttpRequest.get("/subresource/subresource/subresource/basic");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
      Assert.assertEquals("basic", response.getContentAsString());
    }
  }
Esempio n. 6
0
  @Test
  public void testLocatingResource() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    POJOResourceFactory noDefaults = new POJOResourceFactory(LocatingResource.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);

    {
      MockHttpRequest request = MockHttpRequest.get("/locating/basic");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
      Assert.assertEquals("basic", response.getContentAsString());
    }
    {
      MockHttpRequest request = MockHttpRequest.put("/locating/basic");
      request.content("basic".getBytes());
      request.contentType("text/plain");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(204, response.getStatus());
    }
    {
      MockHttpRequest request =
          MockHttpRequest.get(
              "/locating/queryParam?" + "param=" + URLEncoder.encode("hello world", "UTF-8"));
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
      Assert.assertEquals("hello world", response.getContentAsString());
    }
    {
      MockHttpRequest request = MockHttpRequest.get("/locating/uriParam/1234");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
      Assert.assertEquals("1234", response.getContentAsString());
    }
  }
Esempio n. 7
0
  @Test
  public void testContextParam() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    POJOResourceFactory noDefaults = new POJOResourceFactory(LocatingResource.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);

    {
      System.out.println("Expect to see WARN about not injecting in subresources");

      MockHttpRequest request = MockHttpRequest.get("/subresource/testContextParam");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(204, response.getStatus());
    }
  }
  @Test
  public void getNonExistingTripTest() throws Exception {
    TripServiceImpl service = mock(TripServiceImpl.class);
    doThrow(new PersistenceException()).when(service).getTripById(100l);

    TripResource resource = new TripResource();
    resource.service = service;

    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
    dispatcher.getRegistry().addSingletonResource(resource);

    {
      MockHttpRequest request = MockHttpRequest.get(URI + "/100");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
    }
  }
Esempio n. 9
0
  @Test
  public void testBadNumericMatch() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    POJOResourceFactory noDefaults = new POJOResourceFactory(LocatingResource.class);
    dispatcher.getRegistry().addResourceFactory(noDefaults);

    {
      MockHttpRequest request = MockHttpRequest.get("/locating/uriParam/x123");
      MockHttpResponse response = new MockHttpResponse();

      dispatcher.invoke(request, response);

      Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());

      // XXX Should this be SC_NOT_FOUND?
      // Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
      Assert.assertEquals("", response.getContentAsString());
    }
  }
Esempio n. 10
0
 public static MockHttpRequest get(String uri) throws URISyntaxException {
   MockHttpRequest request = initWithUri(uri);
   request.httpMethod = "GET";
   return request;
 }
Esempio n. 11
0
 public static MockHttpRequest create(String httpMethod, URI uriObj, URI baseUri) {
   MockHttpRequest request = initWithUri(uriObj, baseUri);
   request.httpMethod = httpMethod;
   return request;
 }
Esempio n. 12
0
 public static MockHttpRequest create(String httpMethod, String uri) throws URISyntaxException {
   MockHttpRequest request = initWithUri(uri);
   request.httpMethod = httpMethod;
   return request;
 }
Esempio n. 13
0
 public static MockHttpRequest delete(String uri) throws URISyntaxException {
   MockHttpRequest request = initWithUri(uri);
   request.httpMethod = "DELETE";
   return request;
 }