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.core5.testing.framework;
29  
30  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY;
31  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE;
32  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS;
33  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD;
34  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH;
35  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION;
36  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY;
37  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST;
38  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE;
39  import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS;
40  
41  import java.net.URI;
42  import java.net.URISyntaxException;
43  import java.nio.charset.StandardCharsets;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import org.apache.hc.core5.http.NameValuePair;
49  import org.apache.hc.core5.net.URIBuilder;
50  
51  public class FrameworkTest {
52  
53      private Map<String, Object> request = new HashMap<>();
54      private Map<String, Object> response = new HashMap<>();
55  
56      /**
57       * Constructs a test with default values.
58       */
59      public FrameworkTest() {
60          this(null);
61      }
62  
63      /**
64       * Constructs a test with values that are passed in as well as defaults
65       * for values that are not passed in.
66       *
67       * @param test Contains a REQUEST and an expected RESPONSE.
68       *             See {@link ClientPOJOAdapter} for details.
69       */
70      @SuppressWarnings("unchecked")
71      public FrameworkTest(final Map<String, Object> test) {
72          if (test != null) {
73              if (test.containsKey(REQUEST)) {
74                  request = (Map<String, Object>) test.get(REQUEST);
75              }
76              if (test.containsKey(RESPONSE)) {
77                  response = (Map<String, Object>) test.get(RESPONSE);
78              }
79          }
80      }
81  
82      /**
83       * Returns a request with defaults for any parameter that is not specified.
84       *
85       * @return a REQUEST map.
86       * @throws TestingFrameworkException a problem such as an invalid URL
87       */
88      public Map<String, Object> initRequest() throws TestingFrameworkException {
89          // initialize to some helpful defaults
90          final Map<String, Object> ret = new HashMap<>();
91          ret.put(PATH, TestingFramework.DEFAULT_REQUEST_PATH);
92          ret.put(BODY, TestingFramework.DEFAULT_REQUEST_BODY);
93          ret.put(CONTENT_TYPE, TestingFramework.DEFAULT_REQUEST_CONTENT_TYPE);
94          ret.put(QUERY, new HashMap<>(TestingFramework.DEFAULT_REQUEST_QUERY));
95          ret.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_REQUEST_HEADERS));
96          ret.put(PROTOCOL_VERSION, TestingFramework.DEFAULT_REQUEST_PROTOCOL_VERSION);
97  
98          // GET is the default method.
99          if (! request.containsKey(METHOD)) {
100             request.put(METHOD, "GET");
101         }
102         ret.putAll(request);
103 
104         moveAnyParametersInPathToQuery(ret);
105 
106         return ret;
107     }
108 
109     private void moveAnyParametersInPathToQuery(final Map<String, Object> request) throws TestingFrameworkException {
110         try {
111             final String path = (String) request.get(PATH);
112             if (path != null) {
113                 final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) :
114                                                  new URI("http://localhost:8080/");
115                 final URIBuilderlder.html#URIBuilder">URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8);
116                 final List<NameValuePair> params = uriBuilder.getQueryParams();
117                 @SuppressWarnings("unchecked")
118                 final Map<String, Object> queryMap = (Map<String, Object>) request.get(QUERY);
119                 for (final NameValuePair param : params) {
120                     queryMap.put(param.getName(), param.getValue());
121                 }
122                 if (! params.isEmpty()) {
123                     request.put(PATH, uri.getPath());
124                 }
125             }
126         } catch (final URISyntaxException e) {
127             throw new TestingFrameworkException(e);
128         }
129     }
130 
131     /**
132      * Returns an expected response with defaults for any parameter that is not specified.
133      *
134      * @return the RESPONSE map.
135      */
136     public Map<String, Object> initResponseExpectations() {
137         // 200 is the default status.
138         if (! response.containsKey(STATUS)) {
139             response.put(STATUS, 200);
140         }
141 
142         final Map<String, Object> responseExpectations = new HashMap<>();
143         // initialize to some helpful defaults
144         responseExpectations.put(BODY, TestingFramework.DEFAULT_RESPONSE_BODY);
145         responseExpectations.put(CONTENT_TYPE, TestingFramework.DEFAULT_RESPONSE_CONTENT_TYPE);
146         responseExpectations.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_RESPONSE_HEADERS));
147 
148         // Now override any defaults with what is requested.
149         responseExpectations.putAll(response);
150 
151         return responseExpectations;
152     }
153 
154     @Override
155     public String toString() {
156         return "request: " + request + "\nresponse: " + response;
157     }
158 }