View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.query;
21  
22  import static org.mockito.Matchers.*;
23  import static org.mockito.Mockito.*;
24  
25  import java.util.Map;
26  
27  import org.apache.mina.api.IoFuture;
28  import org.apache.mina.api.IoSession;
29  import org.apache.mina.filterchain.ReadFilterChainController;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * 
35   * Unit test for {@link RequestFilter}
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class RequestFilterTest {
40  
41      @SuppressWarnings("rawtypes")
42      private RequestFilter rq = new RequestFilter();
43  
44      @Test
45      public void session_open_initialize_in_flight_container() {
46          IoSession session = mock(IoSession.class);
47  
48          // run
49          rq.sessionOpened(session);
50  
51          // verify
52          verify(session).setAttribute(same(RequestFilter.IN_FLIGHT_REQUESTS), any(Map.class));
53          verifyNoMoreInteractions(session);
54      }
55  
56      @SuppressWarnings({ "rawtypes", "unchecked" })
57      @Test
58      public void request_and_produce_a_future() {
59          IoSession session = mock(IoSession.class);
60  
61          Request r = mock(Request.class);
62          when(r.requestId()).thenReturn("ID");
63  
64          Map m = mock(Map.class);
65  
66          when(session.getAttribute(RequestFilter.IN_FLIGHT_REQUESTS)).thenReturn(m);
67  
68          // run
69          IoFuture f = rq.request(session, r, 12345);
70  
71          // verify
72          Assert.assertFalse(f.isDone());
73          Assert.assertFalse(f.isCancelled());
74  
75          verify(r, times(2)).requestId();
76          verify(session).write(r);
77          verify(m).put("ID", f);
78          verify(session).getAttribute(RequestFilter.IN_FLIGHT_REQUESTS);
79          verifyNoMoreInteractions(session, m);
80      }
81  
82      @SuppressWarnings({ "rawtypes", "unchecked" })
83      @Test
84      public void receive_a_messagre_and_find_the_future_to_complete() {
85          IoSession session = mock(IoSession.class);
86  
87          Response r = mock(Response.class);
88          when(r.requestId()).thenReturn("ID");
89  
90          Map m = mock(Map.class);
91  
92          when(session.getAttribute(RequestFilter.IN_FLIGHT_REQUESTS)).thenReturn(m);
93  
94          RequestFuture f = mock(RequestFuture.class);
95  
96          when(m.remove("ID")).thenReturn(f);
97  
98          ReadFilterChainController ctl = mock(ReadFilterChainController.class);
99  
100         // run
101         rq.messageReceived(session, r, ctl);
102 
103         // verify
104         verify(session).getAttribute(RequestFilter.IN_FLIGHT_REQUESTS);
105         verify(m).remove("ID");
106         verify(r).requestId();
107         verify(f).complete(r);
108 
109         verify(ctl).callReadNextFilter(r);
110         verifyNoMoreInteractions(r, m, session, f, ctl);
111     }
112 }