View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache;
28  
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.verify;
31  import static org.mockito.Mockito.when;
32  
33  import java.io.IOException;
34  
35  import org.apache.http.Header;
36  import org.apache.http.HttpHost;
37  import org.apache.http.ProtocolException;
38  import org.apache.http.StatusLine;
39  import org.apache.http.client.cache.HeaderConstants;
40  import org.apache.http.client.cache.HttpCacheEntry;
41  import org.apache.http.client.methods.CloseableHttpResponse;
42  import org.apache.http.client.methods.HttpExecutionAware;
43  import org.apache.http.client.methods.HttpGet;
44  import org.apache.http.client.methods.HttpRequestWrapper;
45  import org.apache.http.client.protocol.HttpClientContext;
46  import org.apache.http.conn.routing.HttpRoute;
47  import org.apache.http.message.BasicHeader;
48  import org.junit.Before;
49  import org.junit.Test;
50  
51  @SuppressWarnings({"boxing","static-access"}) // test code
52  public class TestAsynchronousValidationRequest {
53  
54      private AsynchronousValidator mockParent;
55      private CachingExec mockClient;
56      private HttpRoute route;
57      private HttpRequestWrapper request;
58      private HttpClientContext context;
59      private HttpExecutionAware mockExecAware;
60      private HttpCacheEntry mockCacheEntry;
61      private CloseableHttpResponse mockResponse;
62      private StatusLine mockStatusLine;
63  
64      @Before
65      public void setUp() {
66          mockParent = mock(AsynchronousValidator.class);
67          mockClient = mock(CachingExec.class);
68          route = new HttpRoute(new HttpHost("foo.example.com", 80));
69          request = HttpRequestWrapper.wrap(new HttpGet("/"));
70          context = HttpClientContext.create();
71          mockExecAware = mock(HttpExecutionAware.class);
72          mockCacheEntry = mock(HttpCacheEntry.class);
73          mockResponse = mock(CloseableHttpResponse.class);
74          mockStatusLine = mock(StatusLine.class);
75      }
76  
77      @Test
78      public void testRunCallsCachingClientAndRemovesIdentifier() throws Exception {
79          final String identifier = "foo";
80  
81          final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
82                  mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
83                  identifier, 0);
84  
85          when(
86                  mockClient.revalidateCacheEntry(
87                          route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
88          when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
89          when(mockStatusLine.getStatusCode()).thenReturn(200);
90  
91          impl.run();
92  
93          verify(mockClient).revalidateCacheEntry(
94                  route, request, context, mockExecAware, mockCacheEntry);
95          verify(mockResponse).getStatusLine();
96          verify(mockParent).markComplete(identifier);
97          verify(mockParent).jobSuccessful(identifier);
98      }
99  
100     @Test
101     public void testRunReportsJobFailedForServerError() throws Exception {
102         final String identifier = "foo";
103 
104         final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
105                 mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
106                 identifier, 0);
107 
108         when(
109                 mockClient.revalidateCacheEntry(
110                         route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
111         when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
112         when(mockStatusLine.getStatusCode()).thenReturn(200);
113 
114         impl.run();
115 
116         verify(mockClient).revalidateCacheEntry(
117                 route, request, context, mockExecAware, mockCacheEntry);
118         verify(mockResponse).getStatusLine();
119         verify(mockStatusLine).getStatusCode();
120         verify(mockParent).markComplete(identifier);
121         verify(mockParent).jobSuccessful(identifier);
122     }
123 
124     @Test
125     public void testRunReportsJobFailedForStaleResponse() throws Exception {
126         final String identifier = "foo";
127         final Header[] warning = new Header[] {new BasicHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"")};
128 
129         final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
130                 mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
131                 identifier, 0);
132 
133         when(
134                 mockClient.revalidateCacheEntry(
135                         route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
136         when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
137         when(mockStatusLine.getStatusCode()).thenReturn(200);
138         when(mockResponse.getHeaders(HeaderConstants.WARNING)).thenReturn(warning);
139 
140         impl.run();
141 
142         verify(mockClient).revalidateCacheEntry(
143                 route, request, context, mockExecAware, mockCacheEntry);
144         verify(mockResponse).getStatusLine();
145         verify(mockStatusLine).getStatusCode();
146         verify(mockResponse).getHeaders(HeaderConstants.WARNING);
147         verify(mockParent).markComplete(identifier);
148         verify(mockParent).jobFailed(identifier);
149     }
150 
151     @Test
152     public void testRunGracefullyHandlesProtocolException() throws Exception {
153         final String identifier = "foo";
154 
155         final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
156                 mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
157                 identifier, 0);
158 
159         when(
160                 mockClient.revalidateCacheEntry(
161                         route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
162                 new ProtocolException());
163 
164         impl.run();
165 
166         verify(mockClient).revalidateCacheEntry(
167                 route, request, context, mockExecAware, mockCacheEntry);
168         verify(mockParent).markComplete(identifier);
169         verify(mockParent).jobFailed(identifier);
170     }
171 
172     @Test
173     public void testRunGracefullyHandlesIOException() throws Exception {
174         final String identifier = "foo";
175 
176         final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
177                 mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
178                 identifier, 0);
179 
180         when(
181                 mockClient.revalidateCacheEntry(
182                         route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
183                                 new IOException());
184 
185         impl.run();
186 
187         verify(mockClient).revalidateCacheEntry(
188                 route, request, context, mockExecAware, mockCacheEntry);
189         verify(mockParent).markComplete(identifier);
190         verify(mockParent).jobFailed(identifier);
191     }
192 
193     @Test
194     public void testRunGracefullyHandlesRuntimeException() throws Exception {
195         final String identifier = "foo";
196 
197         final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
198                 mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
199                 identifier, 0);
200 
201         when(
202                 mockClient.revalidateCacheEntry(
203                         route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
204                                 new RuntimeException());
205 
206         impl.run();
207 
208         verify(mockClient).revalidateCacheEntry(
209                 route, request, context, mockExecAware, mockCacheEntry);
210         verify(mockParent).markComplete(identifier);
211         verify(mockParent).jobFailed(identifier);
212     }
213 }