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

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

There is a newer version: 3.0.1
Show newest version
/*
 * Copyright (C) 2016-2022 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 static com.google.common.collect.FluentIterable.from;
import static java.lang.Math.min;

import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.MemoizingMatchResult;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.stubbing.*;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import java.util.Comparator;
import java.util.List;

public class NearMissCalculator {

  public static final int NEAR_MISS_COUNT = 3;

  public static final Comparator NEAR_MISS_ASCENDING_COMPARATOR =
      new Comparator() {
        public int compare(NearMiss o1, NearMiss o2) {
          return o1.compareTo(o2);
        }
      };

  private final StubMappings stubMappings;
  private final RequestJournal requestJournal;
  private final Scenarios scenarios;

  public NearMissCalculator(
      StubMappings stubMappings, RequestJournal requestJournal, Scenarios scenarios) {
    this.stubMappings = stubMappings;
    this.requestJournal = requestJournal;
    this.scenarios = scenarios;
  }

  public List findNearestTo(final LoggedRequest request) {
    List allMappings = stubMappings.getAll();

    return sortAndTruncate(
        from(allMappings)
            .transform(
                new Function() {
                  public NearMiss apply(StubMapping stubMapping) {
                    MatchResult matchResult =
                        new MemoizingMatchResult(stubMapping.getRequest().match(request));
                    String actualScenarioState = getScenarioStateOrNull(stubMapping);
                    return new NearMiss(request, stubMapping, matchResult, actualScenarioState);
                  }
                }),
        allMappings.size());
  }

  private String getScenarioStateOrNull(StubMapping stubMapping) {
    if (!stubMapping.isInScenario()) {
      return null;
    }

    Scenario scenario = scenarios.getByName(stubMapping.getScenarioName());
    return scenario != null ? scenario.getState() : null;
  }

  public List findNearestTo(final RequestPattern requestPattern) {
    List serveEvents = requestJournal.getAllServeEvents();
    return sortAndTruncate(
        from(serveEvents)
            .transform(
                new Function() {
                  public NearMiss apply(ServeEvent serveEvent) {
                    MatchResult matchResult =
                        new MemoizingMatchResult(requestPattern.match(serveEvent.getRequest()));
                    return new NearMiss(serveEvent.getRequest(), requestPattern, matchResult);
                  }
                }),
        serveEvents.size());
  }

  private static List sortAndTruncate(
      FluentIterable nearMisses, int originalSize) {
    return nearMisses
        .toSortedList(NEAR_MISS_ASCENDING_COMPARATOR)
        .subList(0, min(NEAR_MISS_COUNT, originalSize));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy