@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()); } }
@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()); } }
@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); } }
@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()); } }
@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()); } }