001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.client;
018
019import java.io.IOException;
020import java.util.Iterator;
021
022import org.springframework.http.client.ClientHttpRequest;
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025
026/**
027 * Simple {@code RequestExpectationManager} that matches requests to expectations
028 * sequentially, i.e. in the order of declaration of expectations.
029 *
030 * <p>When request expectations have an expected count greater than one,
031 * only the first execution is expected to match the order of declaration.
032 * Subsequent request executions may be inserted anywhere thereafter.
033 *
034 * @author Rossen Stoyanchev
035 * @author Juergen Hoeller
036 * @since 4.3
037 */
038public class SimpleRequestExpectationManager extends AbstractRequestExpectationManager {
039
040        /** Expectations in the order of declaration (count may be > 1). */
041        @Nullable
042        private Iterator<RequestExpectation> expectationIterator;
043
044        /** Track expectations that have a remaining count. */
045        private final RequestExpectationGroup repeatExpectations = new RequestExpectationGroup();
046
047
048        @Override
049        protected void afterExpectationsDeclared() {
050                Assert.state(this.expectationIterator == null, "Expectations already declared");
051                this.expectationIterator = getExpectations().iterator();
052        }
053
054        @Override
055        protected RequestExpectation matchRequest(ClientHttpRequest request) throws IOException {
056                RequestExpectation expectation = this.repeatExpectations.findExpectation(request);
057                if (expectation == null) {
058                        if (this.expectationIterator == null || !this.expectationIterator.hasNext()) {
059                                throw createUnexpectedRequestError(request);
060                        }
061                        expectation = this.expectationIterator.next();
062                        expectation.match(request);
063                }
064                this.repeatExpectations.update(expectation);
065                return expectation;
066        }
067
068        @Override
069        public void reset() {
070                super.reset();
071                this.expectationIterator = null;
072                this.repeatExpectations.reset();
073        }
074
075}