View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * 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, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.testing.http;
21  
22  import io.wcm.caravan.io.http.response.CaravanHttpResponse;
23  import io.wcm.caravan.io.http.response.CaravanHttpResponseBuilder;
24  
25  import java.util.regex.Pattern;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.hamcrest.BaseMatcher;
29  import org.hamcrest.Description;
30  import org.hamcrest.Matcher;
31  import org.hamcrest.core.IsEqual;
32  import org.hamcrest.core.StringStartsWith;
33  import org.osgi.annotation.versioning.ProviderType;
34  
35  import com.google.common.base.Charsets;
36  
37  /**
38   * Defines which requests should match.
39   * Only one service and one url matcher can be defined.
40   */
41  @ProviderType
42  public final class RequestMatcher {
43  
44    private String serviceId;
45    private Matcher<String> urlMatcher;
46    private CaravanHttpResponse response;
47  
48    /**
49     * @param value Service ID
50     * @return this
51     */
52    public RequestMatcher serviceId(String value) {
53      this.serviceId = value;
54      return this;
55    }
56  
57    /**
58     * @param value Service ID
59     * @return this
60     * @deprecated Please use {@link #serviceId(String)}
61     */
62    @Deprecated
63    public RequestMatcher serviceName(String value) {
64      return serviceId(value);
65    }
66  
67    /**
68     * @param value Exact URL
69     * @return this
70     */
71    public RequestMatcher url(String value) {
72      this.urlMatcher = IsEqual.<String>equalTo(value);
73      return this;
74    }
75  
76    /**
77     * @param value Starting part of URL
78     * @return this
79     */
80    public RequestMatcher urlStartsWith(String value) {
81      this.urlMatcher = StringStartsWith.startsWith(value);
82      return this;
83    }
84  
85    /**
86     * @param value Matcher for URL
87     * @return this
88     */
89    public RequestMatcher urlMatches(Matcher<String> value) {
90      this.urlMatcher = value;
91      return this;
92    }
93  
94    /**
95     * @param pattern Regex pattern to mathc URL
96     * @return this
97     */
98    public RequestMatcher urlMatches(final Pattern pattern) {
99      this.urlMatcher = new BaseMatcher<String>() {
100       @Override
101       public boolean matches(Object item) {
102         return pattern.matcher(item.toString()).matches();
103       }
104       @Override
105       public void describeTo(Description description) {
106         description.appendText("Pattern " + pattern.toString());
107       }
108     };
109     return this;
110   }
111 
112   /**
113    * Set the response that this request matcher should return.
114    * @param payload Payload
115    */
116   public void response(String payload) {
117     response(toResponse(payload));
118   }
119 
120   /**
121    * Set the response that this request matcher should return.
122    * @param payload Payload
123    */
124   public void response(CaravanHttpResponse payload) {
125     this.response = payload;
126   }
127 
128   boolean matches(String expectedServiceId, String expectedUrl) {
129     return (this.serviceId == null || StringUtils.equals(expectedServiceId, this.serviceId))
130         && (this.urlMatcher == null || urlMatcher.matches(expectedUrl))
131         && (response != null);
132   }
133 
134   CaravanHttpResponse getResponse() {
135     return response;
136   }
137 
138   private static CaravanHttpResponse toResponse(String payload) {
139     return new CaravanHttpResponseBuilder()
140     .status(200)
141     .reason("OK")
142     .body(payload.toString(), Charsets.UTF_8)
143     .build();
144   }
145 
146 }