2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.sdc.workflow.api.validator;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Objects;
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;
30 @Component("workflowVersionValidator")
31 public class WorkflowVersionValidator implements Validator{
34 public boolean supports(Class<?> aClass) {
35 return WorkflowVersion.class.equals(aClass);
39 public void validate(Object o, Errors errors) {
41 WorkflowVersion workflowVersion = (WorkflowVersion) o;
42 Collection<ParameterEntity> inputs = workflowVersion.getInputs();
43 Collection<ParameterEntity> outputs = workflowVersion.getOutputs();
45 if(containsDuplicates(inputs)){
46 errors.rejectValue("inputs", "duplicateName", new Object[] {inputs}, "Input name must be unique");
49 if(containsDuplicates(outputs)){
50 errors.rejectValue("outputs", "duplicateName", new Object[] {outputs}, "Output name must be unique");
54 private boolean containsDuplicates(Collection<ParameterEntity> parameters){
55 if(Objects.isNull(parameters) || parameters.size() < 2 ) {
58 Set<String> testSet = new HashSet<>();
59 return parameters.stream().anyMatch(s -> !testSet.add(s.getName()));