PageEntityExtensionWriterInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException { if (Page.class.isAssignableFrom(writerInterceptorContext.getType())) { ParameterizedType parameterizedType = (ParameterizedType)writerInterceptorContext.getGenericType(); Type entityType = parameterizedType.getActualTypeArguments()[0]; Optional.ofNullable( _providers.getContextResolver( ExtensionContext.class, writerInterceptorContext.getMediaType()) ).map( contextResolver -> contextResolver.getContext((Class)entityType) ).ifPresent( extensionContext -> _extendPageEntities( extensionContext, writerInterceptorContext) ); } writerInterceptorContext.proceed(); }  @Override void aroundWriteTo(WriterInterceptorContext writerInterceptorContext);  }
@Test public void testAroundWriteNotPage() throws IOException { Mockito.when( _writerInterceptorContext.getType() ).thenReturn( (Class)JAXRSExtensionContextUtil.TestObject.class ); _pageEntityExtensionWriterInterceptor.aroundWriteTo( _writerInterceptorContext); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setEntity( any() ); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setGenericType( any() ); Mockito.verify( _writerInterceptorContext ).proceed(); }
@Test public void testAroundWritePageWithExtensionContextWithExtendedType() throws IOException { JAXRSExtensionContextUtil.TestObject testObject = JAXRSExtensionContextUtil.getTestObject(); Page<JAXRSExtensionContextUtil.TestObject> page = Page.of( Collections.singleton(testObject)); ArgumentCaptor<Page> argumentCaptor = ArgumentCaptor.forClass( Page.class); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( page ); Mockito.when( _writerInterceptorContext.getGenericType() ).thenReturn( new GenericType<Page<JAXRSExtensionContextUtil.TestObject>>() { }.getType() ); Mockito.when( _writerInterceptorContext.getType() ).thenReturn( (Class)page.getClass() ); _pageEntityExtensionWriterInterceptor.aroundWriteTo( _writerInterceptorContext); Mockito.verify( _writerInterceptorContext ).setEntity( argumentCaptor.capture() ); Page<ExtendedEntity> extendedEntityPage = argumentCaptor.getValue(); Assert.assertEquals(page.getActions(), extendedEntityPage.getActions()); Collection<ExtendedEntity> extendedEntities = extendedEntityPage.getItems(); ExtendedEntity extendedEntity = extendedEntities.toArray(new ExtendedEntity[0])[0]; Assert.assertEquals(testObject, extendedEntity.getEntity()); Assert.assertEquals( JAXRSExtensionContextUtil.getTestExtendedProperties(), extendedEntity.getExtendedProperties()); Assert.assertEquals(page.getLastPage(), page.getLastPage()); Assert.assertEquals(page.getPage(), extendedEntityPage.getPage()); Assert.assertEquals( page.getPageSize(), extendedEntityPage.getPageSize()); Assert.assertEquals(page.getTotalCount(), page.getTotalCount()); Mockito.verify( _writerInterceptorContext ).setGenericType( eq( new GenericType<Page<ExtendedEntity>>() { }.getType()) ); Mockito.verify( _writerInterceptorContext ).proceed(); }
@Test public void testAroundWritePageWithExtensionContextWithNoExtendedType() throws IOException { Page<Object> page = Page.of(Collections.singleton(new Object())); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( page ); Mockito.when( _writerInterceptorContext.getGenericType() ).thenReturn( new GenericType<Page<Object>>() { }.getType() ); Mockito.when( _writerInterceptorContext.getType() ).thenReturn( (Class)Object.class ); _pageEntityExtensionWriterInterceptor.aroundWriteTo( _writerInterceptorContext); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setEntity( any() ); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setGenericType( any() ); Mockito.verify( _writerInterceptorContext ).proceed(); }
@Test public void testAroundWritePageWithoutExtensionContextResolver() throws IOException { ReflectionTestUtil.setFieldValue( _pageEntityExtensionWriterInterceptor, "_providers", JAXRSExtensionContextUtil.getNoContextResolverProviders()); Page<Object> page = Page.of( Collections.singleton(JAXRSExtensionContextUtil.getTestObject())); Mockito.when( _writerInterceptorContext.getEntity() ).thenReturn( page ); Mockito.when( _writerInterceptorContext.getGenericType() ).thenReturn( new GenericType<Page<JAXRSExtensionContextUtil.TestObject>>() { }.getType() ); Mockito.when( _writerInterceptorContext.getType() ).thenReturn( (Class)page.getClass() ); _pageEntityExtensionWriterInterceptor.aroundWriteTo( _writerInterceptorContext); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setEntity( any() ); Mockito.verify( _writerInterceptorContext, Mockito.never() ).setGenericType( any() ); Mockito.verify( _writerInterceptorContext ).proceed(); }