2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.cdap.common.utils;
\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
31 import javax.annotation.Nonnull;
\r
32 import javax.annotation.Nullable;
\r
34 import static com.google.common.base.Preconditions.checkNotNull;
\r
37 * Utility methods to validate null checks, empty string etc
\r
39 * @author Rajiv Singla . Creation Date: 10/24/2016.
\r
41 public abstract class ValidationUtils {
\r
43 private static final Logger LOG = LoggerFactory.getLogger(ValidationUtils.class);
\r
45 private ValidationUtils() {
\r
50 * Checks if String is empty. For null string true is returned
\r
52 * @param stringValue string value
\r
53 * @return returns true is string is empty or null
\r
55 public static boolean isEmpty(@Nullable final String stringValue) {
\r
56 return stringValue == null || stringValue.isEmpty() || stringValue.trim().isEmpty();
\r
61 * Checks if String value is present. A null, empty, or blank values of string
\r
62 * are considered not present.
\r
64 * @param stringValue string value to check if it is present or not
\r
66 * @return true if string value is not null, empty or blank
\r
68 public static boolean isPresent(@Nullable final String stringValue) {
\r
69 return !isEmpty(stringValue);
\r
74 * Provides common functionality to Validates CDAP App Settings. Throws Runtime exception if validation fails
\r
76 * @param appSettings app Settings e.g. App Config, App Preferences etc
\r
77 * @param appSettingsValidator app Settings validator
\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
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
89 final String appSettingsClassName = appSettings.getClass().getSimpleName();
\r
90 final String appSettingsClassValidator = appSettingsValidator.getClass().getSimpleName();
\r
92 LOG.debug("Validating App Settings for: {}, with App Settings Validator: {} ",
\r
93 appSettingsClassName, appSettingsClassValidator);
\r
95 final R validationResponse = appSettingsValidator.validateAppSettings(appSettings);
\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
103 LOG.debug("App Settings Validation Successful for app Settings: {} with validator: {}", appSettingsClassName,
\r
104 appSettingsClassValidator);
\r