|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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 | + * http://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.operation.preprocess; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.springframework.restdocs.operation.OperationRequest; |
| 24 | +import org.springframework.restdocs.operation.OperationRequestFactory; |
| 25 | +import org.springframework.restdocs.operation.Parameters; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * An {@link OperationPreprocessor} that can be used to modify a request's |
| 30 | + * {@link OperationRequest#getParameters()} by adding, setting, and removing parameters. |
| 31 | + * |
| 32 | + * @author Andy Wilkinson |
| 33 | + * @since 1.1.0 |
| 34 | + */ |
| 35 | +public final class ParametersModifyingOperationPreprocessor |
| 36 | + extends OperationPreprocessorAdapter { |
| 37 | + |
| 38 | + private final OperationRequestFactory requestFactory = new OperationRequestFactory(); |
| 39 | + |
| 40 | + private final List<Modification> modifications = new ArrayList<>(); |
| 41 | + |
| 42 | + @Override |
| 43 | + public OperationRequest preprocess(OperationRequest request) { |
| 44 | + Parameters parameters = new Parameters(); |
| 45 | + parameters.putAll(request.getParameters()); |
| 46 | + for (Modification modification : this.modifications) { |
| 47 | + modification.apply(parameters); |
| 48 | + } |
| 49 | + return this.requestFactory.createFrom(request, parameters); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Adds a parameter with the given {@code name} and {@code value}. |
| 54 | + * |
| 55 | + * @param name the name |
| 56 | + * @param value the value |
| 57 | + * @return {@code this} |
| 58 | + */ |
| 59 | + public ParametersModifyingOperationPreprocessor add(String name, String value) { |
| 60 | + this.modifications.add(new AddParameterModification(name, value)); |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the parameter with the given {@code name} to have the given {@code values}. |
| 66 | + * |
| 67 | + * @param name the name |
| 68 | + * @param values the values |
| 69 | + * @return {@code this} |
| 70 | + */ |
| 71 | + public ParametersModifyingOperationPreprocessor set(String name, String... values) { |
| 72 | + Assert.notEmpty(values, "At least one value must be provided"); |
| 73 | + this.modifications.add(new SetParameterModification(name, Arrays.asList(values))); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes the parameter with the given {@code name}. |
| 79 | + * |
| 80 | + * @param name the name of the parameter |
| 81 | + * @return {@code this} |
| 82 | + */ |
| 83 | + public ParametersModifyingOperationPreprocessor remove(String name) { |
| 84 | + this.modifications.add(new RemoveParameterModification(name)); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Removes the given {@code value} from the parameter with the given {@code name}. |
| 90 | + * |
| 91 | + * @param name the name |
| 92 | + * @param value the value |
| 93 | + * @return {@code this} |
| 94 | + */ |
| 95 | + public ParametersModifyingOperationPreprocessor remove(String name, String value) { |
| 96 | + this.modifications.add(new RemoveValueParameterModification(name, value)); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + private interface Modification { |
| 101 | + |
| 102 | + void apply(Parameters parameters); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private static final class AddParameterModification implements Modification { |
| 107 | + |
| 108 | + private final String name; |
| 109 | + |
| 110 | + private final String value; |
| 111 | + |
| 112 | + private AddParameterModification(String name, String value) { |
| 113 | + this.name = name; |
| 114 | + this.value = value; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void apply(Parameters parameters) { |
| 119 | + parameters.add(this.name, this.value); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + private static final class SetParameterModification implements Modification { |
| 125 | + |
| 126 | + private final String name; |
| 127 | + |
| 128 | + private final List<String> values; |
| 129 | + |
| 130 | + private SetParameterModification(String name, List<String> values) { |
| 131 | + this.name = name; |
| 132 | + this.values = values; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void apply(Parameters parameters) { |
| 137 | + parameters.put(this.name, this.values); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + private static final class RemoveParameterModification implements Modification { |
| 143 | + |
| 144 | + private final String name; |
| 145 | + |
| 146 | + private RemoveParameterModification(String name) { |
| 147 | + this.name = name; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void apply(Parameters parameters) { |
| 152 | + parameters.remove(this.name); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + private static final class RemoveValueParameterModification implements Modification { |
| 158 | + |
| 159 | + private final String name; |
| 160 | + |
| 161 | + private final String value; |
| 162 | + |
| 163 | + private RemoveValueParameterModification(String name, String value) { |
| 164 | + this.name = name; |
| 165 | + this.value = value; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void apply(Parameters parameters) { |
| 170 | + List<String> values = parameters.get(this.name); |
| 171 | + if (values != null) { |
| 172 | + values.remove(this.value); |
| 173 | + if (values.isEmpty()) { |
| 174 | + parameters.remove(this.name); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments