DynamicPropertyFilter extends CheckableBeanPropertyFilter { public boolean isSerializable(Object obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider, PropertyWriter writer) { DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); if (filterData != null) { List<String> path = new ArrayList<>(10); path.clear(); path.add(0, writer.getName()); JsonStreamContext context = jsonGenerator.getOutputContext(); while ((context = context.getParent()) != null) { String cname = context.getCurrentName(); if (cname != null) { path.add(0, cname); } } return !filterData.isAttributeExcluded(path); } return true; }  boolean isSerializable(Object obj, JsonGenerator jsonGenerator,
        SerializerProvider serializerProvider, PropertyWriter writer);  }
@Test public void emptyFilterData() { ResteasyContext.pushContext(DynamicFilterData.class, null); DynamicPropertyFilter propertyFilter = new DynamicPropertyFilter(); assertTrue(propertyFilter.isSerializable(null, jsonGenerator, null, writer)); verifyZeroInteractions(jsonGenerator, writer); }
@Test @SuppressWarnings("unchecked") public void nonEmptyIsSerializable() { when(dynamicFilterData.isAttributeExcluded(anyList())) .thenReturn(true); when(dynamicFilterData.isAttributeExcluded(Arrays.asList("CONTEXT_NAME_1", "WRITER_NAME"))) .thenReturn(false); when(jsonGenerator.getOutputContext()).thenReturn(context); when(context.getParent()).thenReturn(context).thenReturn(null); when(context.getCurrentName()).thenReturn("CONTEXT_NAME_1"); when(writer.getName()).thenReturn("WRITER_NAME"); ResteasyContext.pushContext(DynamicFilterData.class, dynamicFilterData); DynamicPropertyFilter propertyFilter = new DynamicPropertyFilter(); assertTrue(propertyFilter.isSerializable(null, jsonGenerator, null, writer)); verify(jsonGenerator).getOutputContext(); verify(context, times(2)).getParent(); verify(context).getCurrentName(); }
@Test public void nonEmptyIsNotSerializable() { when(dynamicFilterData.isAttributeExcluded(Arrays.asList("CONTEXT_NAME_1", "WRITER_NAME"))) .thenReturn(true); when(jsonGenerator.getOutputContext()).thenReturn(context); when(context.getParent()).thenReturn(context).thenReturn(null); when(context.getCurrentName()).thenReturn("CONTEXT_NAME_1"); when(writer.getName()).thenReturn("WRITER_NAME"); ResteasyContext.pushContext(DynamicFilterData.class, dynamicFilterData); DynamicPropertyFilter propertyFilter = new DynamicPropertyFilter(); assertFalse(propertyFilter.isSerializable(null, jsonGenerator, null, writer)); verify(jsonGenerator).getOutputContext(); verify(context, times(2)).getParent(); verify(context).getCurrentName(); }