|
| 1 | +/* |
| 2 | + * Copyright 2014-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.restdocs.cookies; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.springframework.restdocs.operation.Operation; |
| 26 | +import org.springframework.restdocs.snippet.SnippetException; |
| 27 | +import org.springframework.restdocs.snippet.TemplatedSnippet; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * Abstract {@link TemplatedSnippet} subclass that provides a base for snippets that |
| 32 | + * document a RESTful resource's request or response cookies. |
| 33 | + * |
| 34 | + * @author Andreas Evers |
| 35 | + * @author Clyde Stubbs |
| 36 | + * @since 2.1 |
| 37 | + */ |
| 38 | +public abstract class AbstractCookiesSnippet extends TemplatedSnippet { |
| 39 | + |
| 40 | + private List<CookieDescriptor> cookieDescriptors; |
| 41 | + |
| 42 | + protected final boolean ignoreUndocumentedCookies; |
| 43 | + |
| 44 | + private String type; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new {@code AbstractCookiesSnippet} that will produce a snippet named |
| 48 | + * {@code <type>-cookies}. The cookies will be documented using the given |
| 49 | + * {@code descriptors} and the given {@code attributes} will be included in the model |
| 50 | + * during template rendering. |
| 51 | + * @param type the type of the cookies |
| 52 | + * @param descriptors the cookie descriptors |
| 53 | + * @param attributes the additional attributes |
| 54 | + * @param ignoreUndocumentedCookies whether undocumented cookies should be ignored |
| 55 | + */ |
| 56 | + protected AbstractCookiesSnippet(String type, List<CookieDescriptor> descriptors, Map<String, Object> attributes, |
| 57 | + boolean ignoreUndocumentedCookies) { |
| 58 | + super(type + "-cookies", attributes); |
| 59 | + for (CookieDescriptor descriptor : descriptors) { |
| 60 | + Assert.notNull(descriptor.getName(), "The name of the cookie must not be null"); |
| 61 | + if (!descriptor.isIgnored()) { |
| 62 | + Assert.notNull(descriptor.getDescription(), "The description of the cookie must not be null"); |
| 63 | + } |
| 64 | + } |
| 65 | + this.cookieDescriptors = descriptors; |
| 66 | + this.type = type; |
| 67 | + this.ignoreUndocumentedCookies = ignoreUndocumentedCookies; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected Map<String, Object> createModel(Operation operation) { |
| 72 | + validateCookieDocumentation(operation); |
| 73 | + |
| 74 | + Map<String, Object> model = new HashMap<>(); |
| 75 | + List<Map<String, Object>> cookies = new ArrayList<>(); |
| 76 | + model.put("cookies", cookies); |
| 77 | + for (CookieDescriptor descriptor : this.cookieDescriptors) { |
| 78 | + cookies.add(createModelForDescriptor(descriptor)); |
| 79 | + } |
| 80 | + return model; |
| 81 | + } |
| 82 | + |
| 83 | + private void validateCookieDocumentation(Operation operation) { |
| 84 | + List<CookieDescriptor> missingCookies = findMissingCookies(operation); |
| 85 | + if (!missingCookies.isEmpty()) { |
| 86 | + List<String> names = new ArrayList<>(); |
| 87 | + for (CookieDescriptor cookieDescriptor : missingCookies) { |
| 88 | + names.add(cookieDescriptor.getName()); |
| 89 | + } |
| 90 | + throw new SnippetException( |
| 91 | + "Cookies with the following names were not found" + " in the " + this.type + ": " + names); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Finds the cookies that are missing from the operation. A cookie is missing if it is |
| 97 | + * described by one of the {@code cookieDescriptors} but is not present in the |
| 98 | + * operation. |
| 99 | + * @param operation the operation |
| 100 | + * @return descriptors for the cookies that are missing from the operation |
| 101 | + */ |
| 102 | + protected List<CookieDescriptor> findMissingCookies(Operation operation) { |
| 103 | + List<CookieDescriptor> missingCookies = new ArrayList<>(); |
| 104 | + Set<String> actualCookies = extractActualCookies(operation); |
| 105 | + for (CookieDescriptor cookieDescriptor : this.cookieDescriptors) { |
| 106 | + if (!cookieDescriptor.isOptional() && !actualCookies.contains(cookieDescriptor.getName())) { |
| 107 | + missingCookies.add(cookieDescriptor); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return missingCookies; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Extracts the names of the cookies from the request or response of the given |
| 116 | + * {@code operation}. |
| 117 | + * @param operation the operation |
| 118 | + * @return the cookie names |
| 119 | + */ |
| 120 | + protected abstract Set<String> extractActualCookies(Operation operation); |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the list of {@link CookieDescriptor CookieDescriptors} that will be used to |
| 124 | + * generate the documentation. |
| 125 | + * @return the cookie descriptors |
| 126 | + */ |
| 127 | + protected final List<CookieDescriptor> getCookieDescriptors() { |
| 128 | + return this.cookieDescriptors; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns a model for the given {@code descriptor}. |
| 133 | + * @param descriptor the descriptor |
| 134 | + * @return the model |
| 135 | + */ |
| 136 | + protected Map<String, Object> createModelForDescriptor(CookieDescriptor descriptor) { |
| 137 | + Map<String, Object> model = new HashMap<>(); |
| 138 | + model.put("name", descriptor.getName()); |
| 139 | + model.put("description", descriptor.getDescription()); |
| 140 | + model.put("optional", descriptor.isOptional()); |
| 141 | + model.putAll(descriptor.getAttributes()); |
| 142 | + return model; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments