### Question:
GlobalDecorator implements Decorator { @Override public Object decorate(final Object delegate, final JsonValue ignored, final Context context) throws HeapException { Object decorated = parent != null ? parent.decorate(delegate, ignored, context) : delegate; for (JsonValue decoration : decorators) { String decoratorName = decoration.getPointer().leaf(); Decorator decorator = context.getHeap().get(decoratorName, Decorator.class); if ((decorator != null) && decorator.accepts(delegate.getClass())) { decorated = decorator.decorate(decorated, decoration, context); } } return decorated; } GlobalDecorator(final Decorator parent, final JsonValue config,
            final String... reservedFieldNames); @Override boolean accepts(final Class<?> type); @Override Object decorate(final Object delegate, final JsonValue ignored, final Context context); static final String GLOBAL_DECORATOR_HEAP_KEY; }### Answer:
@Test public void shouldLookupDecoratorsBasedOnAttributesNames() throws Exception { JsonValue decorations = json(object(field("deco-1", true), field("deco-2", "value"))); GlobalDecorator globalDecorator = new GlobalDecorator(null, decorations); globalDecorator.decorate(new Object(), null, context); verify(heap).get("deco-1", Decorator.class); verify(heap).get("deco-2", Decorator.class); }
@Test public void shouldLookupInheritedDecoratorsBasedOnAttributesNames() throws Exception { JsonValue decorations1 = json(object(field("deco-1", true))); GlobalDecorator parentGlobalDecorator = new GlobalDecorator(null, decorations1); JsonValue decorations2 = json(object(field("deco-2", "value"))); GlobalDecorator globalDecorator = new GlobalDecorator(parentGlobalDecorator, decorations2); globalDecorator.decorate(new Object(), null, context); verify(heap).get("deco-1", Decorator.class); verify(heap).get("deco-2", Decorator.class); }
@Test public void shouldCallDecorators() throws Exception { when(heap.get("deco-1", Decorator.class)).thenReturn(decorator); when(heap.get("deco-2", Decorator.class)).thenReturn(decorator); JsonValue decorations = json(object(field("deco-1", "value-1"), field("deco-2", "value-2"))); GlobalDecorator globalDecorator = new GlobalDecorator(null, decorations); Object delegate = new Object(); globalDecorator.decorate(delegate, null, context); verify(decorator, times(2)).decorate(anyObject(), captor.capture(), eq(context)); assertThat(captor.getAllValues().get(0).asString()).isEqualTo("value-1"); assertThat(captor.getAllValues().get(1).asString()).isEqualTo("value-2"); }
@Test public void shouldIgnoreMissingDecorators() throws Exception { when(heap.get("deco-2", Decorator.class)).thenReturn(decorator); JsonValue decorations = json(object(field("deco-1", "value-1"), field("deco-2", "value-2"))); GlobalDecorator globalDecorator = new GlobalDecorator(null, decorations); Object delegate = new Object(); globalDecorator.decorate(delegate, null, context); verify(decorator).decorate(anyObject(), captor.capture(), eq(context)); assertThat(captor.getAllValues().get(0).asString()).isEqualTo("value-2"); }
@Test public void shouldIgnoreReservedFieldNames() throws Exception { JsonValue decorations = json(object(field("reserved", true), field("deco", "value"))); GlobalDecorator globalDecorator = new GlobalDecorator(null, decorations, "reserved"); globalDecorator.decorate(new Object(), null, context); verify(heap).get("deco", Decorator.class); verifyNoMoreInteractions(heap); }