CandlepinQueryInterceptor implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) { Object entity = responseContext.getEntity(); if (entity instanceof CandlepinQuery) { PageRequest pageRequest = ResteasyContext.getContextData(PageRequest.class); Session session = this.openSession(); try { CandlepinQuery query = (CandlepinQuery) entity; query.useSession(session); this.applyPaging(pageRequest, query); responseContext.setEntity(this.buildOutputStreamer(session, query)); } catch (RuntimeException e) { if (session != null) { session.close(); } throw e; } } } @Inject  CandlepinQueryInterceptor(
        final JsonProvider jsonProvider, final Provider<EntityManager> emProvider); @Override void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext);  }
@Test public void testWriteCandlepinQueryContents() throws IOException { List<Owner> owners = this.ownerCurator.listAll().list(); CandlepinQueryInterceptor cqi = new CandlepinQueryInterceptor(this.mockJsonProvider, this.emProvider); ContainerRequestContext requestContext = mock(ContainerRequestContext.class); ContainerResponseContext responseContext = mock(ContainerResponseContext.class); doReturn(this.ownerCurator.listAll()).when(responseContext).getEntity(); cqi.filter(requestContext, responseContext); ArgumentCaptor<StreamingOutput> captor = ArgumentCaptor.forClass(StreamingOutput.class); verify(responseContext, times(1)).setEntity(captor.capture()); ((StreamingOutput) captor.getValue()).write(this.mockOutputStream); verify(this.mockJsonGenerator, times(1)).writeStartArray(); for (Owner owner : owners) { verify(this.mockObjectMapper, times(1)).writeValue(eq(this.mockJsonGenerator), eq(owner)); } verify(this.mockJsonGenerator, times(1)).writeEndArray(); }
@Test public void testNonCandlepinQueryCollectionsAreIgnored() { List<Owner> owners = this.ownerCurator.listAll().list(); CandlepinQueryInterceptor cqi = new CandlepinQueryInterceptor(this.mockJsonProvider, this.emProvider); ContainerRequestContext requestContext = mock(ContainerRequestContext.class); ContainerResponseContext responseContext = mock(ContainerResponseContext.class); doReturn(owners).when(responseContext).getEntity(); cqi.filter(requestContext, responseContext); verify(responseContext, never()).setEntity(any()); }
@Test public void testNonCandlepinQueryObjectsAreIgnored() { Owner owner = this.ownerCurator.listAll().list().get(0); CandlepinQueryInterceptor cqi = new CandlepinQueryInterceptor(this.mockJsonProvider, this.emProvider); ContainerRequestContext requestContext = mock(ContainerRequestContext.class); ContainerResponseContext responseContext = mock(ContainerResponseContext.class); doReturn(owner).when(responseContext).getEntity(); cqi.filter(requestContext, responseContext); verify(responseContext, never()).setEntity(any()); }
@Test public void shouldCloseSessionWhenExceptionOccurs() { doThrow(new RuntimeException()).when(this.mockJsonProvider) .locateMapper(Object.class, MediaType.APPLICATION_JSON_TYPE); CandlepinQueryInterceptor cqi = new CandlepinQueryInterceptor(this.mockJsonProvider, this.emProvider); try { ContainerRequestContext requestContext = mock(ContainerRequestContext.class); ContainerResponseContext responseContext = mock(ContainerResponseContext.class); doReturn(this.ownerCurator.listAll()).when(responseContext).getEntity(); cqi.filter(requestContext, responseContext); fail("An expected exception was not thrown"); } catch (RuntimeException e) { verify(this.session).close(); } }