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

com.github.restdriver.clientdriver.ClientDriverExpectation Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
/**
 * Copyright © 2010-2011 Nokia
 *
 * 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.restdriver.clientdriver;

import com.github.restdriver.clientdriver.exception.ClientDriverInvalidExpectationException;

/**
 * An expectation made within the client driver.
 */
public class ClientDriverExpectation {
    
    private final ClientDriverRequestResponsePair pair;
    private int numberOfTimes = 1;
    private int numberOfMatches;
    private boolean matchAnyTimes;
    
    /**
     * Creates a new expectation instance.
     * 
     * @param pair The request/response pair the expectation covers.
     */
    public ClientDriverExpectation(ClientDriverRequestResponsePair pair) {
        this.pair = pair;
    }
    
    /**
     * Gets the request/response pair this expectation covers.
     * 
     * @return The request/response pair
     */
    public final ClientDriverRequestResponsePair getPair() {
        return pair;
    }
    
    /**
     * Indicate that this expectation should be matched a given number of times.
     * 
     * @param times The number of times this expectation should be matched
     */
    public final void times(int times) {
        if (times < 1) {
            throw new ClientDriverInvalidExpectationException("Expectation cannot be matched less than once");
        }
        numberOfTimes = times;
    }
    
    /**
     * Indicate that this expectation should be matched any number of times.
     */
    public final void anyTimes() {
        matchAnyTimes = true;
    }
    
    /**
     * Indicate that this expectation has been matched.
     */
    public final void match() {
        numberOfMatches += 1;
    }
    
    /**
     * Determine whether this expectation has been satisfied.
     * 
     * @return True if the expectation has been matched as many times as it should have
     */
    public final boolean isSatisfied() {
        return !matchAnyTimes && numberOfTimes == numberOfMatches;
    }
    
    /**
     * Whether this expectation should match any number of times.
     * 
     * @return True if the expectation should match any number of times
     */
    public final boolean shouldMatchAnyTimes() {
        return matchAnyTimes;
    }
    
    /**
     * Gets a string giving the status of the expectation.
     * 
     * @return The status string
     */
    public final String getStatusString() {
        
        String expectedString;
        
        if (matchAnyTimes) {
            expectedString = "any";
        } else {
            expectedString = String.valueOf(numberOfTimes);
        }
        
        return "expected: " + expectedString + ", actual: " + numberOfMatches;
        
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy