guru.nidi.ramltester.core.Usage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of raml-tester Show documentation
Show all versions of raml-tester Show documentation
Test if a request/response matches a given raml definition.
/*
* 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 java.util.*;
/**
*
*/
public class Usage implements Iterable> {
private Map resources = new HashMap<>();
private static T getOrCreate(Class clazz, Map map, String name) {
T res = map.get(name);
if (res == null) {
try {
res = clazz.newInstance();
map.put(name, res);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return res;
}
public Resource resource(String path) {
return getOrCreate(Resource.class, resources, path);
}
public void add(Usage usage) {
for (Map.Entry resourceEntry : usage) {
final Resource resource = resource(resourceEntry.getKey());
resource.incUses(resourceEntry.getValue().getUses());
for (Map.Entry actionEntry : resourceEntry.getValue()) {
final Action action = resource.action(actionEntry.getKey());
final Action usageAction = actionEntry.getValue();
action.incUses(usageAction.getUses());
action.addQueryParameters(usageAction.getQueryParameters());
action.addRequestHeaders(usageAction.getRequestHeaders());
action.addResponseCodes(usageAction.getResponseCodes());
for (Map.Entry responseEntry : usageAction.responses()) {
final Response response = action.response(responseEntry.getKey());
response.addResponseHeaders(responseEntry.getValue().getResponseHeaders());
}
for (Map.Entry mimeTypeEntry : usageAction.mimeTypes()) {
final MimeType mimeType = action.mimeType(mimeTypeEntry.getKey());
mimeType.addFormParameters(mimeTypeEntry.getValue().getFormParameters());
}
}
}
}
@Override
public String toString() {
return "Usage" + resources;
}
@Override
public Iterator> iterator() {
return resources.entrySet().iterator();
}
public Set getUnusedResources() {
final Set res = new HashSet<>();
for (Map.Entry resourceEntry : this) {
if (resourceEntry.getValue().getUses() == 0) {
res.add(resourceEntry.getKey());
}
}
return res;
}
private interface ActionCollector {
void collect(String key, Action action, Set result);
}
private Set collect(ActionCollector actionCollector) {
final Set res = new HashSet<>();
for (Map.Entry resourceEntry : this) {
for (Map.Entry actionEntry : resourceEntry.getValue()) {
actionCollector.collect(actionEntry.getKey() + " " + resourceEntry.getKey(), actionEntry.getValue(), res);
}
}
return res;
}
public Set getUnusedActions() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
if (action.getUses() == 0) {
result.add(key);
}
}
});
}
public Set getUnusedQueryParameters() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
for (Map.Entry queryEntry : action.getQueryParameters().values()) {
if (queryEntry.getValue() == 0) {
result.add(queryEntry.getKey() + " in " + key);
}
}
}
});
}
public Set getUnusedFormParameters() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
for (Map.Entry mimeTypeEntry : action.mimeTypes()) {
for (Map.Entry formEntry : mimeTypeEntry.getValue().getFormParameters().values()) {
if (formEntry.getValue() == 0) {
result.add(formEntry.getKey() + " in " + key + " (" + mimeTypeEntry.getKey() + ")");
}
}
}
}
});
}
public Set getUnusedRequestHeaders() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
for (Map.Entry requestEntry : action.getRequestHeaders().values()) {
if (requestEntry.getValue() == 0) {
result.add(requestEntry.getKey() + " in " + key);
}
}
}
});
}
public Set getUnusedResponseHeaders() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
for (Map.Entry responseEntry : action.responses()) {
for (Map.Entry headerEntry : responseEntry.getValue().getResponseHeaders().values()) {
if (headerEntry.getValue() == 0) {
result.add(headerEntry.getKey() + " in " + key + " -> " + responseEntry.getKey());
}
}
}
}
});
}
public Set getUnusedResponseCodes() {
return collect(new ActionCollector() {
@Override
public void collect(String key, Action action, Set result) {
for (Map.Entry responseCodeEntry : action.getResponseCodes().values()) {
if (responseCodeEntry.getValue() == 0) {
result.add(responseCodeEntry.getKey() + " in " + key);
}
}
}
});
}
static class UsageBase {
private int uses;
public void incUses(int count) {
uses += count;
}
public int getUses() {
return uses;
}
}
static class Resource extends UsageBase implements Iterable> {
private Map actions = new HashMap<>();
public Action action(String name) {
return getOrCreate(Action.class, actions, name);
}
@Override
public String toString() {
return "Resource" + actions;
}
@Override
public Iterator> iterator() {
return actions.entrySet().iterator();
}
}
static class Action extends UsageBase {
private Map responses = new HashMap<>();
private Map mimeTypes = new HashMap<>();
private CountSet queryParameters = new CountSet<>();
private CountSet requestHeaders = new CountSet<>();
private CountSet responseCodes = new CountSet<>();
public Response response(String name) {
return getOrCreate(Response.class, responses, name);
}
public Iterable> responses() {
return responses.entrySet();
}
public MimeType mimeType(String name) {
return getOrCreate(MimeType.class, mimeTypes, name);
}
public Iterable> mimeTypes() {
return mimeTypes.entrySet();
}
public void addQueryParameters(Set names) {
queryParameters.addAll(names);
}
public void initQueryParameters(Set names) {
queryParameters.addAll(names, 0);
}
public void addRequestHeaders(Set names) {
requestHeaders.addAll(names);
}
public void initRequestHeaders(Set names) {
requestHeaders.addAll(names, 0);
}
public void addResponseCode(String name) {
responseCodes.add(name);
}
public void addResponseCodes(Set names) {
responseCodes.addAll(names);
}
public void initResponseCodes(Set names) {
responseCodes.addAll(names, 0);
}
public CountSet getQueryParameters() {
return queryParameters;
}
public CountSet getRequestHeaders() {
return requestHeaders;
}
public CountSet getResponseCodes() {
return responseCodes;
}
@Override
public String toString() {
return "Action{" +
"responses=" + responses +
", mimeTypes=" + mimeTypes +
", queryParameters=" + queryParameters +
", requestHeaders=" + requestHeaders +
", responseCodes=" + responseCodes +
'}';
}
}
static class Response {
private CountSet responseHeaders = new CountSet<>();
public void addResponseHeaders(Set names) {
responseHeaders.addAll(names);
}
public void initResponseHeaders(Set names) {
responseHeaders.addAll(names, 0);
}
public CountSet getResponseHeaders() {
return responseHeaders;
}
@Override
public String toString() {
return "Response{" +
"responseHeaders=" + responseHeaders +
'}';
}
}
static class MimeType {
private CountSet formParameters = new CountSet<>();
public void addFormParameters(Set names) {
formParameters.addAll(names);
}
public void initFormParameters(Set names) {
formParameters.addAll(names, 0);
}
public CountSet getFormParameters() {
return formParameters;
}
@Override
public String toString() {
return "MimeType{" +
"formParameters=" + formParameters +
'}';
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy