2 * Copyright © 2016-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.validation;
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;
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;
35 public class NoDuplicatesValidatorTest {
37 class AnnotationWrapper {
39 @NoDuplicates(message = "test message")
40 public Collection<Parameter> collection;
43 private NoDuplicatesValidator noDuplicatesValidator;
46 private ConstraintValidatorContext context;
48 private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder;
50 private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext
51 nodeBuilderCustomizableContext;
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);
62 public void shouldFailIfCollectionHaveMoreThen1ParameterEntityWithSameName() {
63 Collection<Parameter> inputs = Arrays.asList(createParameter("name1"), createParameter("name1"));
65 assertFalse(noDuplicatesValidator.isValid(inputs, context));
69 public void shouldPassIfCollectionDontHaveMoreThen1ParameterEntityWithSameName() {
70 Collection<Parameter> inputs = Arrays.asList(createParameter("name2"), createParameter("name1"));
72 assertTrue(noDuplicatesValidator.isValid(inputs, context));
76 public void shouldPassIfCollectionContainsOnlyOneObject() {
77 Collection<Parameter> inputs = Collections.singletonList(createParameter("name2"));
79 assertTrue(noDuplicatesValidator.isValid(inputs, context));
83 public void shouldPassIfCollectionIsNull() {
84 assertTrue(noDuplicatesValidator.isValid(null, context));
88 public void shouldPassIfCollectionIsEmpty() {
89 assertTrue(noDuplicatesValidator.isValid(new ArrayList<>(), context));
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);
99 private Parameter createParameter(String name) {
100 Parameter parameter = new Parameter();
101 parameter.setName(name);