Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / component / ComponentDescriptionValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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 package org.openecomp.sdc.be.components.validation.component;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
24 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.ValidationUtils;
33 import org.openecomp.sdc.exception.ResponseFormat;
34
35 @org.springframework.stereotype.Component
36 public class ComponentDescriptionValidator implements ComponentFieldValidator {
37
38     private static final Logger log = Logger.getLogger(ComponentTagsValidator.class.getName());
39     private ComponentsUtils componentsUtils;
40
41     public ComponentDescriptionValidator(ComponentsUtils componentsUtils) {
42         this.componentsUtils = componentsUtils;
43     }
44
45     @Override
46     public void validateAndCorrectField(User user, Component component, AuditingActionEnum actionEnum) {
47         ComponentTypeEnum type = component.getComponentType();
48         String description = component.getDescription();
49         if (StringUtils.isEmpty(description)) {
50             auditErrorAndThrow(user, component, actionEnum, ActionStatus.COMPONENT_MISSING_DESCRIPTION);
51         }
52         description = ValidationUtils.cleanUpText(description);
53         try {
54             validateComponentDescription(description, type);
55         } catch (ComponentException e) {
56             ResponseFormat errorResponse = componentsUtils.getResponseFormat(e.getActionStatus(), component.getComponentType().getValue());
57             componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
58             throw e;
59         }
60         component.setDescription(description);
61     }
62
63     private void auditErrorAndThrow(User user, org.openecomp.sdc.be.model.Component component, AuditingActionEnum actionEnum,
64                                     ActionStatus actionStatus) {
65         ResponseFormat errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().getValue());
66         componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
67         throw new ByActionStatusComponentException(actionStatus, component.getComponentType().getValue());
68     }
69
70     private void validateComponentDescription(String description, ComponentTypeEnum type) {
71         if (description != null) {
72             if (!ValidationUtils.validateDescriptionLength(description)) {
73                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, type.getValue(),
74                     "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
75             }
76             if (!ValidationUtils.validateCommentPattern(description)) {
77                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_DESCRIPTION, type.getValue());
78             }
79         }
80     }
81 }