|
| 1 | +/* |
| 2 | + Copyright (c) 2016 LinkedIn Corp. |
| 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 com.linkedin.restli.internal.server.response; |
| 18 | + |
| 19 | + |
| 20 | +import com.linkedin.data.template.RecordTemplate; |
| 21 | +import com.linkedin.restli.common.CollectionMetadata; |
| 22 | +import com.linkedin.restli.common.HttpStatus; |
| 23 | +import com.linkedin.restli.server.RestLiResponseData; |
| 24 | +import com.linkedin.restli.server.RestLiServiceException; |
| 25 | + |
| 26 | +import java.net.HttpCookie; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * This builder utility class is used to build response data for testing filter implementations. Some filters are used |
| 35 | + * to modify response data and need to generate response data in tests for those filters. |
| 36 | + * |
| 37 | + * Each build method will return a response data containing a response envelope. For example, the buildGetResponseData |
| 38 | + * method will build a response data containing a GET response envelope. Note that the invariants are maintained for |
| 39 | + * both {@link RestLiResponseData} and {@link RestLiResponseEnvelope} - please read their Javadocs for more information. |
| 40 | + * |
| 41 | + * This class is helpful for creating response data in tests because both the response envelope setter inside response |
| 42 | + * data and the response envelope constructors are package private. Without this class, you cannot create response data |
| 43 | + * with response envelopes inside of them. |
| 44 | + * |
| 45 | + * This class is intended to be used as a test utility only. |
| 46 | + * |
| 47 | + * @author gye |
| 48 | + */ |
| 49 | +public final class ResponseDataBuilderUtil |
| 50 | +{ |
| 51 | + private ResponseDataBuilderUtil() |
| 52 | + { |
| 53 | + // private constructor to disable instantiation. |
| 54 | + } |
| 55 | + |
| 56 | + public static RestLiResponseData buildGetResponseData(HttpStatus status, RecordTemplate getResponse) |
| 57 | + { |
| 58 | + final RestLiResponseDataImpl responseData = |
| 59 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 60 | + responseData.setResponseEnvelope(new GetResponseEnvelope(getResponse, responseData)); |
| 61 | + return responseData; |
| 62 | + } |
| 63 | + |
| 64 | + public static RestLiResponseData buildGetResponseData(RestLiServiceException exception) |
| 65 | + { |
| 66 | + final RestLiResponseDataImpl responseData = |
| 67 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 68 | + responseData.setResponseEnvelope(new GetResponseEnvelope(null, responseData)); |
| 69 | + return responseData; |
| 70 | + } |
| 71 | + |
| 72 | + public static RestLiResponseData buildCreateResponseData(HttpStatus status, RecordTemplate createResponse) |
| 73 | + { |
| 74 | + final RestLiResponseDataImpl responseData = |
| 75 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 76 | + responseData.setResponseEnvelope(new CreateResponseEnvelope(createResponse, responseData)); |
| 77 | + return responseData; |
| 78 | + } |
| 79 | + |
| 80 | + public static RestLiResponseData buildCreateResponseData(RestLiServiceException exception) |
| 81 | + { |
| 82 | + final RestLiResponseDataImpl responseData = |
| 83 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 84 | + responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData)); |
| 85 | + return responseData; |
| 86 | + } |
| 87 | + |
| 88 | + public static RestLiResponseData buildActionResponseData(HttpStatus status, RecordTemplate actionResponse) |
| 89 | + { |
| 90 | + final RestLiResponseDataImpl responseData = |
| 91 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 92 | + responseData.setResponseEnvelope(new ActionResponseEnvelope(actionResponse, responseData)); |
| 93 | + return responseData; |
| 94 | + } |
| 95 | + |
| 96 | + public static RestLiResponseData buildActionResponseData(RestLiServiceException exception) |
| 97 | + { |
| 98 | + final RestLiResponseDataImpl responseData = |
| 99 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 100 | + responseData.setResponseEnvelope(new ActionResponseEnvelope(null, responseData)); |
| 101 | + return responseData; |
| 102 | + } |
| 103 | + |
| 104 | + public static RestLiResponseData buildFinderResponseData(HttpStatus status, |
| 105 | + List<? extends RecordTemplate> collectionResponse, |
| 106 | + CollectionMetadata collectionResponsePaging, |
| 107 | + RecordTemplate collectionResponseCustomMetadata) |
| 108 | + { |
| 109 | + final RestLiResponseDataImpl responseData = |
| 110 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 111 | + responseData.setResponseEnvelope( |
| 112 | + new FinderResponseEnvelope(collectionResponse, collectionResponsePaging, collectionResponseCustomMetadata, |
| 113 | + responseData)); |
| 114 | + return responseData; |
| 115 | + } |
| 116 | + |
| 117 | + public static RestLiResponseData buildFinderResponseData(RestLiServiceException exception) |
| 118 | + { |
| 119 | + final RestLiResponseDataImpl responseData = |
| 120 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 121 | + responseData.setResponseEnvelope(new FinderResponseEnvelope(null, null, null, responseData)); |
| 122 | + return responseData; |
| 123 | + } |
| 124 | + |
| 125 | + public static RestLiResponseData buildGetAllResponseData(HttpStatus status, |
| 126 | + List<? extends RecordTemplate> collectionResponse, |
| 127 | + CollectionMetadata collectionResponsePaging, |
| 128 | + RecordTemplate collectionResponseCustomMetadata) |
| 129 | + { |
| 130 | + final RestLiResponseDataImpl responseData = |
| 131 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 132 | + responseData.setResponseEnvelope( |
| 133 | + new GetAllResponseEnvelope(collectionResponse, collectionResponsePaging, collectionResponseCustomMetadata, |
| 134 | + responseData)); |
| 135 | + return responseData; |
| 136 | + } |
| 137 | + |
| 138 | + public static RestLiResponseData buildGetAllResponseData(RestLiServiceException exception) |
| 139 | + { |
| 140 | + final RestLiResponseDataImpl responseData = |
| 141 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 142 | + responseData.setResponseEnvelope(new GetAllResponseEnvelope(null, null, null, responseData)); |
| 143 | + return responseData; |
| 144 | + } |
| 145 | + |
| 146 | + public static RestLiResponseData buildUpdateResponseData(HttpStatus status) |
| 147 | + { |
| 148 | + final RestLiResponseDataImpl responseData = |
| 149 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 150 | + responseData.setResponseEnvelope(new UpdateResponseEnvelope(responseData)); |
| 151 | + return responseData; |
| 152 | + } |
| 153 | + |
| 154 | + public static RestLiResponseData buildUpdateResponseData(RestLiServiceException exception) |
| 155 | + { |
| 156 | + final RestLiResponseDataImpl responseData = |
| 157 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 158 | + responseData.setResponseEnvelope(new UpdateResponseEnvelope(responseData)); |
| 159 | + return responseData; |
| 160 | + } |
| 161 | + |
| 162 | + public static RestLiResponseData buildPartialUpdateResponseData(HttpStatus status) |
| 163 | + { |
| 164 | + final RestLiResponseDataImpl responseData = |
| 165 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 166 | + responseData.setResponseEnvelope(new PartialUpdateResponseEnvelope(responseData)); |
| 167 | + return responseData; |
| 168 | + } |
| 169 | + |
| 170 | + public static RestLiResponseData buildPartialUpdateResponseData(RestLiServiceException exception) |
| 171 | + { |
| 172 | + final RestLiResponseDataImpl responseData = |
| 173 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 174 | + responseData.setResponseEnvelope(new PartialUpdateResponseEnvelope(responseData)); |
| 175 | + return responseData; |
| 176 | + } |
| 177 | + |
| 178 | + public static RestLiResponseData buildOptionsResponseData(HttpStatus status) |
| 179 | + { |
| 180 | + final RestLiResponseDataImpl responseData = |
| 181 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 182 | + responseData.setResponseEnvelope(new OptionsResponseEnvelope(responseData)); |
| 183 | + return responseData; |
| 184 | + } |
| 185 | + |
| 186 | + public static RestLiResponseData buildOptionsResponseData(RestLiServiceException exception) |
| 187 | + { |
| 188 | + final RestLiResponseDataImpl responseData = |
| 189 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 190 | + responseData.setResponseEnvelope(new OptionsResponseEnvelope(responseData)); |
| 191 | + return responseData; |
| 192 | + } |
| 193 | + |
| 194 | + public static RestLiResponseData buildDeleteResponseData(HttpStatus status) |
| 195 | + { |
| 196 | + final RestLiResponseDataImpl responseData = |
| 197 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 198 | + responseData.setResponseEnvelope(new DeleteResponseEnvelope(responseData)); |
| 199 | + return responseData; |
| 200 | + } |
| 201 | + |
| 202 | + public static RestLiResponseData buildDeleteResponseData(RestLiServiceException exception) |
| 203 | + { |
| 204 | + final RestLiResponseDataImpl responseData = |
| 205 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 206 | + responseData.setResponseEnvelope(new DeleteResponseEnvelope(responseData)); |
| 207 | + return responseData; |
| 208 | + } |
| 209 | + |
| 210 | + public static RestLiResponseData buildBatchCreateResponseData(HttpStatus status, |
| 211 | + List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> responseItems) |
| 212 | + { |
| 213 | + final RestLiResponseDataImpl responseData = |
| 214 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 215 | + responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(responseItems, responseData)); |
| 216 | + return responseData; |
| 217 | + } |
| 218 | + |
| 219 | + public static RestLiResponseData buildBatchCreateResponseData(RestLiServiceException exception) |
| 220 | + { |
| 221 | + final RestLiResponseDataImpl responseData = |
| 222 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 223 | + responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(null, responseData)); |
| 224 | + return responseData; |
| 225 | + } |
| 226 | + |
| 227 | + public static RestLiResponseData buildBatchGetResponseData(HttpStatus status, |
| 228 | + Map<? extends Object, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) |
| 229 | + { |
| 230 | + final RestLiResponseDataImpl responseData = |
| 231 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 232 | + responseData.setResponseEnvelope(new BatchUpdateResponseEnvelope(batchResponseMap, responseData)); |
| 233 | + return responseData; |
| 234 | + } |
| 235 | + |
| 236 | + public static RestLiResponseData buildBatchGetResponseData(RestLiServiceException exception) |
| 237 | + { |
| 238 | + final RestLiResponseDataImpl responseData = |
| 239 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 240 | + responseData.setResponseEnvelope(new BatchGetResponseEnvelope(null, responseData)); |
| 241 | + return responseData; |
| 242 | + } |
| 243 | + |
| 244 | + public static RestLiResponseData buildBatchUpdateResponseData(HttpStatus status, |
| 245 | + Map<? extends Object, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) |
| 246 | + { |
| 247 | + final RestLiResponseDataImpl responseData = |
| 248 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 249 | + responseData.setResponseEnvelope(new BatchUpdateResponseEnvelope(batchResponseMap, responseData)); |
| 250 | + return responseData; |
| 251 | + } |
| 252 | + |
| 253 | + public static RestLiResponseData buildBatchUpdateResponseData(RestLiServiceException exception) |
| 254 | + { |
| 255 | + final RestLiResponseDataImpl responseData = |
| 256 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 257 | + responseData.setResponseEnvelope(new BatchUpdateResponseEnvelope(null, responseData)); |
| 258 | + return responseData; |
| 259 | + } |
| 260 | + |
| 261 | + public static RestLiResponseData buildBatchPartialUpdateResponseData(HttpStatus status, |
| 262 | + Map<? extends Object, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) |
| 263 | + { |
| 264 | + final RestLiResponseDataImpl responseData = |
| 265 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 266 | + responseData.setResponseEnvelope(new BatchPartialUpdateResponseEnvelope(batchResponseMap, responseData)); |
| 267 | + return responseData; |
| 268 | + } |
| 269 | + |
| 270 | + public static RestLiResponseData buildBatchPartialUpdateResponseData(RestLiServiceException exception) |
| 271 | + { |
| 272 | + final RestLiResponseDataImpl responseData = |
| 273 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 274 | + responseData.setResponseEnvelope(new BatchPartialUpdateResponseEnvelope(null, responseData)); |
| 275 | + return responseData; |
| 276 | + } |
| 277 | + |
| 278 | + public static RestLiResponseData buildBatchDeleteResponseData(HttpStatus status, |
| 279 | + Map<? extends Object, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) |
| 280 | + { |
| 281 | + final RestLiResponseDataImpl responseData = |
| 282 | + new RestLiResponseDataImpl(status, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 283 | + responseData.setResponseEnvelope(new BatchDeleteResponseEnvelope(batchResponseMap, responseData)); |
| 284 | + return responseData; |
| 285 | + } |
| 286 | + |
| 287 | + public static RestLiResponseData buildBatchDeleteResponseData(RestLiServiceException exception) |
| 288 | + { |
| 289 | + final RestLiResponseDataImpl responseData = |
| 290 | + new RestLiResponseDataImpl(exception, new HashMap<String, String>(), new ArrayList<HttpCookie>()); |
| 291 | + responseData.setResponseEnvelope(new BatchDeleteResponseEnvelope(null, responseData)); |
| 292 | + return responseData; |
| 293 | + } |
| 294 | +} |
0 commit comments