2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.analytics.web.util;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
26 import org.onap.dcae.analytics.web.exception.AnalyticsValidationException;
27 import org.onap.dcae.analytics.web.validation.AnalyticsValidator;
28 import org.onap.dcae.analytics.web.validation.ValidationResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.util.Assert;
34 * Validation Utilities
36 * @author Rajiv Singla
38 public abstract class ValidationUtils {
40 private static final Logger log = LoggerFactory.getLogger(ValidationUtils.class);
42 private ValidationUtils() {
47 * Checks if String is empty. For null string true is returned
49 * @param stringValue string value
51 * @return returns true is string is empty or null
53 public static boolean isEmpty(@Nullable final String stringValue) {
54 return stringValue == null || stringValue.isEmpty() || stringValue.trim().isEmpty();
59 * Checks if String value is present. A null, empty, or blank values of string
60 * are considered not present.
62 * @param stringValue string value to check if it is present or not
64 * @return true if string value is not null, empty or blank
66 public static boolean isPresent(@Nullable final String stringValue) {
67 return !isEmpty(stringValue);
72 * Provides common functionality to validate analytics objects.
73 * Throws {@link AnalyticsValidationException} exception if validation fails
75 * @param targetObject target object that needs to be validated
76 * @param validator validator that will be used to validate the target object
77 * @param <T> target object type that needs to be validated
78 * @param <R> Validation Response type
79 * @param <V> Validator Type
81 * @return target object if validation is successful
83 public static <T, R extends ValidationResponse, V extends AnalyticsValidator<T, R>> T validate(
84 @Nonnull final T targetObject,
85 @Nonnull final V validator) {
87 Assert.notNull(targetObject, "target object that needs to validated must not be null");
88 Assert.notNull(validator, "validator must not be null");
90 final String targetObjectClass = targetObject.getClass().getSimpleName();
91 final String validatorClass = validator.getClass().getSimpleName();
93 log.debug("Validating target object of type: {} with validator type: {} ", targetObjectClass, validatorClass);
95 final R validationResponse = validator.apply(targetObject);
97 // If setting validation fails throw an exception
98 if (validationResponse.hasErrors()) {
99 throw new AnalyticsValidationException(validationResponse.getAllErrorMessage(),
100 new IllegalArgumentException(validationResponse.getAllErrorMessage()));
103 log.info("Validation Successful for target object type: {} with validator type: {}", targetObjectClass,