@@ -30,7 +30,7 @@ public void testApply() {
3030 boolean isOperator = randomBoolean ();
3131 final ThreadContext threadContext = getTestThreadContext (isOperator );
3232 String action = "test.action" ;
33- ApiFilteringActionFilter <TestResponse > filter = new TestFilter (threadContext );
33+ ApiFilteringActionFilter <TestResponse > filter = new TestFilter (threadContext , false );
3434 Task task = null ;
3535 TestRequest request = new TestRequest ();
3636 AtomicBoolean listenerCalled = new AtomicBoolean (false );
@@ -59,6 +59,37 @@ public void onFailure(Exception e) {
5959 assertThat (responseModified .get (), equalTo (isOperator == false ));
6060 }
6161
62+ public void testApplyAsOperator () {
63+ final ThreadContext threadContext = getTestThreadContext (true );
64+ ApiFilteringActionFilter <TestResponse > filter = new TestFilter (threadContext , true );
65+ Task task = null ;
66+ TestRequest request = new TestRequest ();
67+ AtomicBoolean listenerCalled = new AtomicBoolean (false );
68+ AtomicBoolean responseModified = new AtomicBoolean (false );
69+ ActionListener <TestResponse > listener = new ActionListener <>() {
70+ @ Override
71+ public void onResponse (TestResponse testResponse ) {
72+ listenerCalled .set (true );
73+ responseModified .set (testResponse .modified );
74+ }
75+
76+ @ Override
77+ public void onFailure (Exception e ) {
78+ fail (Strings .format ("Unexpected exception: %s" , e .getMessage ()));
79+ }
80+ };
81+ ActionFilterChain <TestRequest , TestResponse > chain = (task1 , action1 , request1 , listener1 ) -> {
82+ listener1 .onResponse (new TestResponse ());
83+ };
84+ filter .apply (task , "wrong.action" , request , listener , chain );
85+ assertThat (listenerCalled .get (), equalTo (true ));
86+ assertThat (responseModified .get (), equalTo (false ));
87+ filter .apply (task , "test.action" , request , listener , chain );
88+ assertThat (listenerCalled .get (), equalTo (true ));
89+ // The response should always be modified
90+ assertThat (responseModified .get (), equalTo (true ));
91+ }
92+
6293 public void testApplyWithException () {
6394 /*
6495 * This test makes sure that we have correct behavior if the filter function throws an exception. In that case we expect
@@ -94,8 +125,8 @@ public void onFailure(Exception e) {
94125
95126 private static class TestFilter extends ApiFilteringActionFilter <TestResponse > {
96127
97- TestFilter (ThreadContext threadContext ) {
98- super (threadContext , "test.action" , TestResponse .class );
128+ TestFilter (ThreadContext threadContext , boolean filterOperatorRequests ) {
129+ super (threadContext , "test.action" , TestResponse .class , filterOperatorRequests );
99130 }
100131
101132 @ Override
0 commit comments