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

org.ehoffman.test.aspects.RestValidator Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.ehoffman.test.aspects;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Expects an server that supports basic authentication and correct header responses (404 for non-existent resources)
 * and 200 for existing resources.
 * 
 * @author rex
 */
public class RestValidator extends Validator {

    private final HttpClientLookup lookup;
    private final String replacement;
    private final String userValidationPattern;
    private final String issueValidationPattern;
    private final String username;
    private final String password;

    public RestValidator(final String replacement,
                         final String userValidationPattern,
                         final String issueValidationPattern,
                         final String username,
                         final String password,
                         final String initialUrl) {
        this.replacement = replacement;
        this.userValidationPattern = userValidationPattern;
        this.issueValidationPattern = issueValidationPattern;
        this.username = username;
        this.password = password;
        lookup = new HttpClientLookup(buildURI(initialUrl), getUsername(), getPassword());
    }

    public String getUserValidationPattern() {
        return userValidationPattern;
    }
    public String getIssueValidationPattern() {
        return issueValidationPattern;
    }
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }

    private boolean verifydURI(final String uri, final String toReplace, final String token) {
        return verifydURI(uri.replace(toReplace, token));
    }

    private URI buildURI(final String uri) {
        try {
            return new URI(uri);
        } catch (final URISyntaxException e) {
            return null;
        }
    }

    private boolean verifydURI(final String uri) {
        try {
            return verifyExists(new URI(uri));
        } catch (final URISyntaxException | IOException e) {
            return false;
        }
    }

    private boolean verifyExists(final URI uri) throws IOException {
        return lookup.verifyExists(uri);
    }

    @Override
    public void close() throws IOException {
        lookup.close();
    }

    @Override
    public boolean validUser(final String user) {
       return verifydURI(getUserValidationPattern(), replacement, user);
    }

    @Override
    public boolean validIssue(final String issue) {
        return verifydURI(getIssueValidationPattern(), replacement, issue);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy