bfd7b3122fc8c58e939de470605fd7bd60b9d5f6
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / main / java / org / onap / dcae / apod / analytics / cdap / common / utils / ValidationUtils.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============================LICENSE_END===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.cdap.common.utils;
22
23
24 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
25 import org.onap.dcae.apod.analytics.cdap.common.settings.CDAPAppSettings;
26 import org.onap.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;
27 import org.onap.dcae.apod.analytics.common.validation.ValidationResponse;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33
34 import static com.google.common.base.Preconditions.checkNotNull;
35
36 /**
37  * Utility methods to validate null checks, empty string etc
38  *
39  * @author Rajiv Singla . Creation Date: 10/24/2016.
40  */
41 public abstract class ValidationUtils {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ValidationUtils.class);
44
45     private ValidationUtils() {
46
47     }
48
49     /**
50      * Checks if String is empty. For null string true is returned
51      *
52      * @param stringValue string value
53      * @return returns true is string is empty or null
54      */
55     public static boolean isEmpty(@Nullable final String stringValue) {
56         return stringValue == null || stringValue.isEmpty() || stringValue.trim().isEmpty();
57     }
58
59
60     /**
61      * Checks if String value is present. A null, empty, or blank values of string
62      * are considered not present.
63      *
64      * @param stringValue string value to check if it is present or not
65      *
66      * @return true if string value is not null, empty or blank
67      */
68     public static boolean isPresent(@Nullable final String stringValue) {
69         return !isEmpty(stringValue);
70     }
71
72
73     /**
74      * Provides common functionality to Validates CDAP App Settings. Throws Runtime exception if validation fails
75      *
76      * @param appSettings app Settings e.g. App Config, App Preferences etc
77      * @param appSettingsValidator app Settings validator
78      *
79      * @param <T> Settings type e.g. AppConfig or AppPreferences
80      * @param <R> Validation Response type
81      * @param <V> Validator Type
82      */
83     public static <T extends CDAPAppSettings, R extends ValidationResponse,
84             V extends CDAPAppSettingsValidator<T, R>> void validateSettings(@Nonnull final T appSettings,
85                                                                             @Nonnull final V appSettingsValidator) {
86         checkNotNull(appSettings, "App Settings must not be null");
87         checkNotNull(appSettingsValidator, "App Settings validator must not be null");
88
89         final String appSettingsClassName = appSettings.getClass().getSimpleName();
90         final String appSettingsClassValidator = appSettingsValidator.getClass().getSimpleName();
91
92         LOG.debug("Validating App Settings for: {}, with App Settings Validator: {} ",
93                 appSettingsClassName, appSettingsClassValidator);
94
95         final R validationResponse = appSettingsValidator.validateAppSettings(appSettings);
96
97         // If setting validation fails throw an exception
98         if (validationResponse.hasErrors()) {
99             throw new CDAPSettingsException(
100                     validationResponse.getAllErrorMessage(), LOG, new IllegalArgumentException());
101         }
102
103         LOG.debug("App Settings Validation Successful for app Settings: {} with validator: {}", appSettingsClassName,
104                 appSettingsClassValidator);
105     }
106
107 }