Migrate sdc-sdc-workflow-designer docs
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-be / src / test / java / org / onap / sdc / workflow / api / validation / ArchivingStatusValidatorTest.java
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.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.verify;
23 import static org.mockito.Mockito.when;
24
25 import javax.validation.ConstraintValidatorContext;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32
33 @RunWith(SpringJUnit4ClassRunner.class)
34 public class ArchivingStatusValidatorTest {
35
36     class AnnotationWrapper {
37
38         @ValidName(message = "test message")
39         public String status;
40     }
41
42     @Mock
43     private ConstraintValidatorContext context;
44     @Mock
45     private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder;
46     @Mock
47     private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderCustomizableContext;
48
49     private ArchivingStatusValidator validator;
50
51     @Before
52     public void setup() throws NoSuchFieldException {
53         MockitoAnnotations.initMocks(this);
54         when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(constraintViolationBuilder);
55         when(constraintViolationBuilder.addPropertyNode(anyString())).thenReturn(nodeBuilderCustomizableContext);
56         validator = initializeValidator(ArchivingStatusValidatorTest.AnnotationWrapper.class);
57     }
58
59     @Test
60     public void shouldFailIfValueIsNull() {
61     assertFalse(validator.isValid(null, context));
62   }
63
64     @Test
65     public void shouldFailIfValueInvalid() {
66     assertFalse(validator.isValid("blahblah", context));
67   }
68
69     @Test
70     public void shouldPassIfValueIsActive() {
71     assertTrue(validator.isValid("ACTIVE", context));
72   }
73
74     @Test
75     public void shouldPassIfValueIsArchived() {
76     assertTrue(validator.isValid("ARCHIVED", context));
77   }
78
79
80
81
82   private ArchivingStatusValidator initializeValidator(Class<?> classWithAnnotation)
83             throws NoSuchFieldException {
84         ValidStatus constraint = classWithAnnotation.getField("status").getAnnotation(ValidStatus.class);
85         ArchivingStatusValidator validator = new ArchivingStatusValidator();
86         validator.initialize(constraint);
87         return validator;
88     }
89
90
91 }