|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE |
| 3 | + * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file |
| 4 | + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * <p> |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package org.apache.atlas.common; |
| 14 | + |
| 15 | +import org.apache.atlas.AtlasErrorCode; |
| 16 | +import org.apache.atlas.exception.AtlasBaseException; |
| 17 | + |
| 18 | +import javax.servlet.http.HttpServletRequest; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.testng.Assert.assertEquals; |
| 25 | + |
| 26 | +public class TestUtility { |
| 27 | + private TestUtility() { |
| 28 | + } |
| 29 | + |
| 30 | + public static void assertInvalidQueryLength(AtlasBaseException exception, String msgParams) { |
| 31 | + assertEquals(exception.getAtlasErrorCode(), AtlasErrorCode.INVALID_QUERY_LENGTH); |
| 32 | + assertEquals(exception.getMessage(), AtlasErrorCode.INVALID_QUERY_LENGTH.getFormattedErrorMessage(msgParams)); |
| 33 | + } |
| 34 | + |
| 35 | + public static void assertInvalidParamLength(AtlasBaseException exception, String paramName) { |
| 36 | + assertEquals(exception.getAtlasErrorCode(), AtlasErrorCode.INVALID_QUERY_PARAM_LENGTH); |
| 37 | + assertEquals(exception.getMessage(), AtlasErrorCode.INVALID_QUERY_PARAM_LENGTH.getFormattedErrorMessage(paramName)); |
| 38 | + } |
| 39 | + |
| 40 | + public static void assertInvalidParameters(AtlasBaseException exception, String msgParams) { |
| 41 | + assertEquals(exception.getAtlasErrorCode(), AtlasErrorCode.INVALID_PARAMETERS); |
| 42 | + assertEquals(exception.getMessage(), AtlasErrorCode.INVALID_PARAMETERS.getFormattedErrorMessage(msgParams)); |
| 43 | + } |
| 44 | + |
| 45 | + public static void assertBadRequests(AtlasBaseException exception, String msgParams) { |
| 46 | + assertEquals(exception.getAtlasErrorCode(), AtlasErrorCode.BAD_REQUEST); |
| 47 | + assertEquals(exception.getMessage(), AtlasErrorCode.BAD_REQUEST.getFormattedErrorMessage(msgParams)); |
| 48 | + } |
| 49 | + |
| 50 | + public static void assertGUIDNotFoundException(AtlasBaseException exception, String... guids) { |
| 51 | + assertEquals(exception.getAtlasErrorCode(), AtlasErrorCode.INSTANCE_GUID_NOT_FOUND); |
| 52 | + assertEquals(exception.getMessage(), AtlasErrorCode.INSTANCE_GUID_NOT_FOUND.getFormattedErrorMessage(guids)); |
| 53 | + } |
| 54 | + |
| 55 | + public static HttpServletRequest buildAndGetMockServletRequest(Map<String, String[]> params) { |
| 56 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 57 | + if (params != null) { |
| 58 | + when(request.getParameterMap()).thenReturn(params); |
| 59 | + for (Map.Entry<String, String[]> entry : params.entrySet()) { |
| 60 | + when(request.getParameterValues(entry.getKey())).thenReturn(entry.getValue()); |
| 61 | + when(request.getParameter(entry.getKey())) |
| 62 | + .thenReturn(entry.getValue() != null && entry.getValue().length > 0 ? entry.getValue()[0] : null); |
| 63 | + } |
| 64 | + } |
| 65 | + return request; |
| 66 | + } |
| 67 | + |
| 68 | + public static String generateString(int n, char c) { |
| 69 | + StringBuilder sb = new StringBuilder(n); |
| 70 | + for (int i = 0; i < n; i++) { |
| 71 | + sb.append(c); |
| 72 | + } |
| 73 | + return sb.toString(); |
| 74 | + } |
| 75 | +} |
0 commit comments