0310104259d6e826b539fa7975a73494a9b9f89a
[sdc/sdc-workflow-designer.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.api.validator;
18
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Objects;
22 import java.util.Set;
23 import org.onap.sdc.workflow.persistence.types.ParameterEntity;
24 import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
25 import org.springframework.stereotype.Component;
26 import org.springframework.validation.Errors;
27 import org.springframework.validation.Validator;
28
29
30 @Component("workflowVersionValidator")
31 public class WorkflowVersionValidator implements Validator{
32
33     @Override
34     public boolean supports(Class<?> aClass) {
35         return WorkflowVersion.class.equals(aClass);
36     }
37
38     @Override
39     public void validate(Object o, Errors errors) {
40
41         WorkflowVersion workflowVersion = (WorkflowVersion) o;
42         Collection<ParameterEntity> inputs = workflowVersion.getInputs();
43         Collection<ParameterEntity> outputs = workflowVersion.getOutputs();
44
45         if(containsDuplicates(inputs)){
46             errors.rejectValue("inputs", "duplicateName", new Object[] {inputs}, "Input name must be unique");
47         }
48
49         if(containsDuplicates(outputs)){
50             errors.rejectValue("outputs", "duplicateName", new Object[] {outputs}, "Output name must be unique");
51         }
52     }
53
54     private boolean containsDuplicates(Collection<ParameterEntity> parameters){
55         if(Objects.isNull(parameters) ||  parameters.size() < 2 ) {
56             return false;
57         }
58         Set<String> testSet = new HashSet<>();
59       return parameters.stream().anyMatch(s -> !testSet.add(s.getName()));
60     }
61 }