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

guru.nidi.ramltester.core.RamlViolationsPerSecurity Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/*
 * Copyright (C) 2014 Stefan Niederhauser ([email protected])
 *
 * 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 guru.nidi.ramltester.core;

import org.raml.model.SecurityScheme;

import java.util.*;

/**
 *
 */
class RamlViolationsPerSecurity {
    private static final Comparator SCHEME_COMPARATOR = new Comparator() {
        @Override
        public int compare(SecurityScheme s1, SecurityScheme s2) {
            return s1.getType().compareToIgnoreCase(s2.getType());
        }
    };

    private final List schemes;
    private final Map requestViolations, responseViolations;

    public RamlViolationsPerSecurity(SecurityExtractor security) {
        schemes = new ArrayList<>(security.getSchemes());
        Collections.sort(schemes, SCHEME_COMPARATOR);
        requestViolations = new HashMap<>();
        responseViolations = new HashMap<>();
        for (final SecurityScheme scheme : schemes) {
            requestViolations.put(scheme.getType(), new RamlViolations());
            responseViolations.put(scheme.getType(), new RamlViolations());
        }
    }

    public RamlViolations requestViolations(SecurityScheme scheme) {
        return requestViolations.get(scheme.getType());
    }

    public RamlViolations responseViolations(SecurityScheme scheme) {
        return responseViolations.get(scheme.getType());
    }

    public List leastViolations() {
        int best = Integer.MAX_VALUE;
        final List res = new ArrayList<>();
        for (final SecurityScheme scheme : schemes) {
            final int violations = requestViolations(scheme).size() + responseViolations(scheme).size();
            if (violations <= best) {
                if (violations < best) {
                    res.clear();
                }
                res.add(scheme);
                best = violations;
            }
        }
        return res;
    }

    public void addLeastViolations(RamlViolations request, RamlViolations response) {
        for (final SecurityScheme scheme : leastViolations()) {
            addAll(scheme, requestViolations(scheme), request);
            addAll(scheme, responseViolations(scheme), response);
        }
    }

    private void addAll(SecurityScheme scheme, RamlViolations source, RamlViolations target) {
        if (schemes.size() == 1) {
            target.addAll(source);
        } else {
            for (final String s : source) {
                target.add("scheme", scheme.getType(), s);
            }
        }
    }

    @Override
    public String toString() {
        return "RamlViolationsPerSecurity{" +
                "requestViolations=" + requestViolations +
                ", responseViolations=" + responseViolations +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy