Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-web / src / main / java / org / onap / dcae / analytics / web / util / ValidationUtils.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.web.util;
21
22
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25
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;
32
33 /**
34  * Validation Utilities
35  *
36  * @author Rajiv Singla
37  */
38 public abstract class ValidationUtils {
39
40     private static final Logger log = LoggerFactory.getLogger(ValidationUtils.class);
41
42     private ValidationUtils() {
43
44     }
45
46     /**
47      * Checks if String is empty. For null string true is returned
48      *
49      * @param stringValue string value
50      *
51      * @return returns true is string is empty or null
52      */
53     public static boolean isEmpty(@Nullable final String stringValue) {
54         return stringValue == null || stringValue.isEmpty() || stringValue.trim().isEmpty();
55     }
56
57
58     /**
59      * Checks if String value is present. A null, empty, or blank values of string
60      * are considered not present.
61      *
62      * @param stringValue string value to check if it is present or not
63      *
64      * @return true if string value is not null, empty or blank
65      */
66     public static boolean isPresent(@Nullable final String stringValue) {
67         return !isEmpty(stringValue);
68     }
69
70
71     /**
72      * Provides common functionality to validate analytics objects.
73      * Throws {@link AnalyticsValidationException} exception if validation fails
74      *
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
80      *
81      * @return target object if validation is successful
82      */
83     public static <T, R extends ValidationResponse, V extends AnalyticsValidator<T, R>> T validate(
84             @Nonnull final T targetObject,
85             @Nonnull final V validator) {
86
87         Assert.notNull(targetObject, "target object that needs to validated must not be null");
88         Assert.notNull(validator, "validator must not be null");
89
90         final String targetObjectClass = targetObject.getClass().getSimpleName();
91         final String validatorClass = validator.getClass().getSimpleName();
92
93         log.debug("Validating target object of type: {} with validator type: {} ", targetObjectClass, validatorClass);
94
95         final R validationResponse = validator.apply(targetObject);
96
97         // If setting validation fails throw an exception
98         if (validationResponse.hasErrors()) {
99             throw new AnalyticsValidationException(validationResponse.getAllErrorMessage(),
100                     new IllegalArgumentException(validationResponse.getAllErrorMessage()));
101         }
102
103         log.info("Validation Successful for target object type: {} with validator type: {}", targetObjectClass,
104                 validatorClass);
105
106         return targetObject;
107     }
108 }