f8f6b74319bcc7e4c4ddf2d6d3f2da56c9bcc55b
[sdc/sdc-workflow-designer.git] /
1 /*
2  * Copyright © 2016-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.validation;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.when;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import javax.validation.ConstraintValidatorContext;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.sdc.workflow.api.types.Parameter;
34
35 public class NoDuplicatesValidatorTest {
36
37     class AnnotationWrapper {
38
39         @NoDuplicates(message = "test message")
40         public Collection<Parameter> collection;
41     }
42
43     private NoDuplicatesValidator noDuplicatesValidator;
44
45     @Mock
46     private ConstraintValidatorContext context;
47     @Mock
48     private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder;
49     @Mock
50     private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext
51             nodeBuilderCustomizableContext;
52
53     @Before
54     public void init() throws NoSuchFieldException {
55         MockitoAnnotations.initMocks(this);
56         when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(constraintViolationBuilder);
57         when(constraintViolationBuilder.addPropertyNode(anyString())).thenReturn(nodeBuilderCustomizableContext);
58         noDuplicatesValidator = initializeValidator(AnnotationWrapper.class);
59     }
60
61     @Test
62     public void shouldFailIfCollectionHaveMoreThen1ParameterEntityWithSameName() {
63         Collection<Parameter> inputs = Arrays.asList(createParameter("name1"), createParameter("name1"));
64
65         assertFalse(noDuplicatesValidator.isValid(inputs, context));
66     }
67
68     @Test
69     public void shouldPassIfCollectionDontHaveMoreThen1ParameterEntityWithSameName() {
70         Collection<Parameter> inputs = Arrays.asList(createParameter("name2"), createParameter("name1"));
71
72         assertTrue(noDuplicatesValidator.isValid(inputs, context));
73     }
74
75     @Test
76     public void shouldPassIfCollectionContainsOnlyOneObject() {
77         Collection<Parameter> inputs = Collections.singletonList(createParameter("name2"));
78
79         assertTrue(noDuplicatesValidator.isValid(inputs, context));
80     }
81
82     @Test
83     public void shouldPassIfCollectionIsNull() {
84         assertTrue(noDuplicatesValidator.isValid(null, context));
85     }
86
87     @Test
88     public void shouldPassIfCollectionIsEmpty() {
89         assertTrue(noDuplicatesValidator.isValid(new ArrayList<>(), context));
90     }
91
92     private NoDuplicatesValidator initializeValidator(Class<?> classWithAnnotation) throws NoSuchFieldException {
93         NoDuplicates constraint = classWithAnnotation.getField("collection").getAnnotation(NoDuplicates.class);
94         NoDuplicatesValidator validator = new NoDuplicatesValidator();
95         validator.initialize(constraint);
96         return validator;
97     }
98
99     private Parameter createParameter(String name) {
100         Parameter parameter = new Parameter();
101         parameter.setName(name);
102         return parameter;
103     }
104 }