EntityExtensionWriterInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException { Optional.ofNullable( _providers.getContextResolver( ExtensionContext.class, writerInterceptorContext.getMediaType()) ).map( contextResolver -> contextResolver.getContext( writerInterceptorContext.getType()) ).ifPresent( extensionContext -> _extendEntity( extensionContext, writerInterceptorContext) ); writerInterceptorContext.proceed(); }  @Override void aroundWriteTo(WriterInterceptorContext writerInterceptorContext);  }
@Test public void testAroundWriteWithExtensionContextWithExtendedType() throws IOException { JAXRSExtensionContextUtil.TestObject testObject = JAXRSExtensionContextUtil.getTestObject(); ArgumentCaptor<ExtendedEntity> argumentCaptor = ArgumentCaptor.forClass( ExtendedEntity.class); Mockito.when( _mockedWriterInterceptorContext.getEntity() ).thenReturn( testObject ); Mockito.when( _mockedWriterInterceptorContext.getType() ).thenReturn( (Class)testObject.getClass() ); _entityExtensionWriterInterceptor.aroundWriteTo( _mockedWriterInterceptorContext); Mockito.verify( _mockedWriterInterceptorContext ).setEntity( argumentCaptor.capture() ); ExtendedEntity extendedEntity = argumentCaptor.getValue(); Assert.assertEquals(testObject, extendedEntity.getEntity()); Assert.assertEquals( JAXRSExtensionContextUtil.getTestExtendedProperties(), extendedEntity.getExtendedProperties()); Mockito.verify( _mockedWriterInterceptorContext ).setGenericType( eq(ExtendedEntity.class) ); Mockito.verify( _mockedWriterInterceptorContext ).proceed(); }
@Test public void testAroundWriteWithExtensionContextWithNoExtendedType() throws IOException { Mockito.when( _mockedWriterInterceptorContext.getEntity() ).thenReturn( new Object() ); Mockito.when( _mockedWriterInterceptorContext.getType() ).thenReturn( (Class)Object.class ); _entityExtensionWriterInterceptor.aroundWriteTo( _mockedWriterInterceptorContext); Mockito.verify( _mockedWriterInterceptorContext, Mockito.never() ).setEntity( any() ); Mockito.verify( _mockedWriterInterceptorContext, Mockito.never() ).setGenericType( any() ); Mockito.verify( _mockedWriterInterceptorContext ).proceed(); }
@Test public void testAroundWriteWithoutExtensionContextResolver() throws IOException { ReflectionTestUtil.setFieldValue( _entityExtensionWriterInterceptor, "_providers", JAXRSExtensionContextUtil.getNoContextResolverProviders()); _entityExtensionWriterInterceptor.aroundWriteTo( _mockedWriterInterceptorContext); Mockito.verify( _mockedWriterInterceptorContext, Mockito.never() ).setEntity( any() ); Mockito.verify( _mockedWriterInterceptorContext, Mockito.never() ).setGenericType( any() ); Mockito.verify( _mockedWriterInterceptorContext ).proceed(); }