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