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  
28  package org.apache.hc.client5.testing.async;
29  
30  import java.net.InetSocketAddress;
31  import java.util.function.Consumer;
32  
33  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
34  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
35  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
36  import org.apache.hc.client5.testing.extension.async.TestAsyncClientBuilder;
37  import org.apache.hc.client5.testing.extension.async.TestAsyncResources;
38  import org.apache.hc.client5.testing.extension.async.TestAsyncServer;
39  import org.apache.hc.client5.testing.extension.async.TestAsyncServerBootstrap;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.URIScheme;
42  import org.apache.hc.core5.util.Timeout;
43  import org.junit.jupiter.api.extension.RegisterExtension;
44  
45  public abstract class AbstractIntegrationTestBase {
46  
47      public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
48  
49      @RegisterExtension
50      private final TestAsyncResources testResources;
51  
52      protected AbstractIntegrationTestBase(final URIScheme scheme, final ClientProtocolLevel clientProtocolLevel, final ServerProtocolLevel serverProtocolLevel) {
53          this.testResources = new TestAsyncResources(scheme, clientProtocolLevel, serverProtocolLevel, TIMEOUT);
54      }
55  
56      public URIScheme scheme() {
57          return testResources.scheme();
58      }
59  
60      public ServerProtocolLevel getServerProtocolLevel() {
61          return testResources.getServerProtocolLevel();
62      }
63  
64      public ClientProtocolLevel getClientProtocolLevel() {
65          return testResources.getClientProtocolLevel();
66      }
67  
68      public void configureServer(final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
69          testResources.configureServer(serverCustomizer);
70      }
71  
72      public HttpHost startServer() throws Exception {
73          final TestAsyncServer server = testResources.server();
74          final InetSocketAddress inetSocketAddress = server.start();
75          return new HttpHost(testResources.scheme().id, "localhost", inetSocketAddress.getPort());
76      }
77  
78      public void configureClient(final Consumer<TestAsyncClientBuilder> clientCustomizer) {
79          testResources.configureClient(clientCustomizer);
80      }
81  
82      public TestAsyncClient startClient() throws Exception {
83          final TestAsyncClient client = testResources.client();
84          client.start();
85          return client;
86      }
87  
88  }