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