View Javadoc

1   package com.atlassian.plugins.rest.common.feature.jersey;
2   
3   import com.atlassian.plugins.rest.common.feature.RequiresDarkFeature;
4   import com.atlassian.sal.api.features.DarkFeatureManager;
5   import com.sun.jersey.api.model.AbstractMethod;
6   import com.sun.jersey.api.model.AbstractResource;
7   import com.sun.jersey.spi.container.ContainerRequest;
8   import org.junit.Before;
9   import org.mockito.Mock;
10  
11  import java.lang.annotation.Annotation;
12  
13  import static com.atlassian.plugins.rest.common.feature.jersey.test.DarkFeatureTestResource.FEATURE_KEY;
14  import static com.atlassian.plugins.rest.common.feature.jersey.test.DarkFeatureTestResource.getAnnotatedMethod;
15  import static com.atlassian.plugins.rest.common.feature.jersey.test.DarkFeatureTestResource.getNonAnnotatedMethod;
16  import static org.mockito.Matchers.eq;
17  import static org.mockito.Mockito.doReturn;
18  import static org.mockito.Mockito.when;
19  import static org.mockito.MockitoAnnotations.initMocks;
20  
21  public abstract class AbstractDarkFeatureResourceFilterTest {
22      @Mock
23      protected AbstractMethod abstractMethod;
24  
25      @Mock
26      protected AbstractResource resource;
27  
28      @Mock
29      protected DarkFeatureManager darkFeatureManager;
30  
31      @Mock
32      protected ContainerRequest request;
33  
34      @Mock
35      protected RequiresDarkFeature annotation;
36  
37      @Before
38      public void setup() {
39          initMocks(this);
40  
41          when(abstractMethod.getResource()).thenReturn(resource);
42          doReturn(RequiresDarkFeature.class).when(annotation).annotationType();
43  
44          setResourceAnnotated(false);
45          setMethodAnnotated(false);
46          setResourceAnnotation(FEATURE_KEY);
47      }
48  
49      protected void setFeatureEnabled(final boolean enabled) {
50          setFeatureEnabled(FEATURE_KEY, enabled);
51      }
52  
53      protected void setFeatureEnabled(final String key, final boolean enabled) {
54          when(darkFeatureManager.isFeatureEnabledForCurrentUser(eq(key))).thenReturn(enabled);
55      }
56  
57      protected void setResourceAnnotated(final boolean annotated) {
58          when(resource.getAnnotation(eq(RequiresDarkFeature.class))).thenReturn(annotated ? annotation : null);
59          when(resource.getAnnotations()).thenReturn(annotated ? new Annotation[]{annotation} : new Annotation[0]);
60      }
61  
62      protected void setResourceAnnotation(final String... keys) {
63          when(annotation.value()).thenReturn(keys);
64      }
65  
66      protected void setMethodAnnotated(final boolean annotated) {
67          when(abstractMethod.getAnnotations()).thenReturn(annotated ? new Annotation[]{annotation} : new Annotation[0]);
68          when(abstractMethod.getMethod()).thenReturn(annotated ? getAnnotatedMethod() : getNonAnnotatedMethod());
69      }
70  }