Added unit tests fo MappingRulesValidator
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / rule / editor / validators / MappingRulesValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.sdc.dcae.rule.editor.validators;
22
23 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.MappingRules;
24 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.Rule;
25 import org.onap.sdc.dcae.errormng.ActionStatus;
26 import org.onap.sdc.dcae.errormng.ErrConfMgr;
27 import org.onap.sdc.dcae.errormng.ResponseFormat;
28 import org.onap.sdc.dcae.rule.editor.utils.ValidationUtils;
29 import org.onap.sdc.dcae.ves.VesStructureLoader;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35
36 public class MappingRulesValidator implements IRuleElementValidator<MappingRules> {
37
38         private RuleValidator ruleValidator = RuleValidator.getInstance();
39
40         private static MappingRulesValidator mappingRulesValidator = new MappingRulesValidator();
41
42         public static MappingRulesValidator getInstance() {
43                 return mappingRulesValidator;
44         }
45
46         private MappingRulesValidator(){}
47
48         public boolean validate(MappingRules rules, List<ResponseFormat> errors) {
49                 boolean valid = true;
50                 if(rules.isEmpty()) {
51                         valid = false;
52                         errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.INVALID_RULE_FORMAT, "", "no rules found"));
53                 }
54                 // TODO consider using 'allMatch' which will stop on the first error
55                 return rules.getRules().values().stream().map(r -> ruleValidator.validate(r, errors))
56                                 .reduce(true, (x,y) -> x && y) && valid;
57         }
58
59     public boolean validateVersionAndType(MappingRules rules) {
60         Map<String, Set<String>> supportedVersions =
61             VesStructureLoader.getAvailableVersionsAndEventTypes();
62
63         return ValidationUtils.validateNotEmpty(rules.getVersion())
64             && supportedVersions.containsKey(rules.getVersion())
65             && ValidationUtils.validateNotEmpty(rules.getEventType())
66             && supportedVersions.get(rules.getVersion()).contains(rules.getEventType());
67     }
68
69
70         public boolean validateGroupDefinitions(MappingRules rules) {
71         return rules.getRules().values().stream().allMatch(r -> ValidationUtils.validateNotEmpty(r.getGroupId()) &&
72                                 ValidationUtils.validateNotEmpty(r.getPhase())) &&
73                                 rules.getRules().values().stream().collect(
74                                                 Collectors.groupingBy(Rule::getGroupId, Collectors.mapping(Rule::getPhase, Collectors.toSet())))
75                                                 .values().stream().allMatch(p -> 1 == p.size());
76         }
77
78
79     public boolean validateTranslationPhaseNames(MappingRules rules, List<ResponseFormat> errors) {
80         boolean valid = true;
81         Set<String> phases =
82             rules.getRules().values().stream().map(Rule::getPhase).collect(Collectors.toSet());
83         if (phases.contains(rules.getEntryPhase())) {
84             valid = false;
85             errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
86                 "entry phase name already exists"));
87         }
88         if (phases.contains(rules.getPublishPhase())) {
89             valid = false;
90             errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
91                 "publish phase name already exists"));
92         }
93         return valid;
94     }
95 }