All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tomakehurst.wiremock.verification.InMemoryRequestJournal Maven / Gradle / Ivy

/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestListener;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.matching.RequestMatcherExtension;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.stubbing.ServedStub;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;

import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import static com.github.tomakehurst.wiremock.matching.RequestPattern.thatMatch;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.size;
import static com.google.common.collect.Iterables.transform;

public class InMemoryRequestJournal implements RequestListener, RequestJournal {

	private final Queue servedStubs = new ConcurrentLinkedQueue();

	private final Optional maxEntries;

	public InMemoryRequestJournal(Optional maxEntries) {
		if (maxEntries.isPresent() && maxEntries.get() < 0) {
			throw new IllegalArgumentException("Maximum number of entries of journal must be greater than zero");
		}
		this.maxEntries = maxEntries;
	}

	@Override
	public int countRequestsMatching(RequestPattern requestPattern) {
		return size(filter(getRequests(), thatMatch(requestPattern)));
	}

	@Override
	public List getRequestsMatching(RequestPattern requestPattern) {
		return ImmutableList.copyOf(filter(getRequests(), thatMatch(requestPattern)));
	}

    private Predicate matchedBy(final RequestPattern requestPattern) {
		return new Predicate() {
			public boolean apply(Request input) {
				return requestPattern.isMatchedBy(input, Collections.emptyMap());
			}
		};
	}

	@Override
	public void requestReceived(Request request, Response response) {
		servedStubs.add(ServedStub.exactMatch(LoggedRequest.createFrom(request), null));
		removeOldEntries();
	}

	@Override
	public void requestReceived(ServedStub servedStub) {
		servedStubs.add(servedStub);
        removeOldEntries();
	}

    @Override
    public List getAllServedStubs() {
        return ImmutableList.copyOf(servedStubs);
    }

	@Override
	public void reset() {
		servedStubs.clear();
	}

	private Iterable getRequests() {
		return transform(servedStubs, new Function() {
			public LoggedRequest apply(ServedStub input) {
				return input.getRequest();
			}
		});
	}

	private void removeOldEntries() {
		if (maxEntries.isPresent()) {
			while (servedStubs.size() > maxEntries.get()) {
				servedStubs.poll();
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy