Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / AnnotationValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.validation;
21
22 import fj.data.Either;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
30 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
31 import org.openecomp.sdc.be.datatypes.elements.Annotation;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
35 import org.openecomp.sdc.be.model.DataTypeDefinition;
36 import org.openecomp.sdc.be.model.PropertyDefinition;
37 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
38
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.hamcrest.CoreMatchers.is;
45 import static org.junit.Assert.assertThat;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class AnnotationValidatorTest {
49
50     private static final String TYPE = "type";
51     private static final String UNIQUE_ID = "1";
52     private static final String OWNER = "owner";
53     private static final String DESC = "desc";
54     private static final String TEST = "test";
55     private static final String PROP_NAME = "propName";
56     private Map<String, DataTypeDefinition> allData;
57     private List<PropertyDefinition> annotationTypeProperties;
58     private AnnotationValidator annotationValidator;
59     private List<PropertyDataDefinition> propertyDataDefinitions = new ArrayList<>();
60     private PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();;
61
62     @Mock
63     private ComponentsUtils componentsUtils;
64     @Mock
65     private ApplicationDataTypeCache dataTypeCache;
66     @Mock
67     private PropertyValidator propertyValidator;
68     @Mock
69     private ExceptionUtils exceptionUtils;
70
71     @Before
72     public void setUp() throws Exception {
73         annotationValidator = new AnnotationValidator(propertyValidator, exceptionUtils, dataTypeCache, componentsUtils);
74         allData = Collections.emptyMap();
75         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> cacheResponse = Either.left(allData);
76         Mockito.when(dataTypeCache.getAll()).thenReturn(cacheResponse);
77         annotationTypeProperties = Collections.emptyList();
78         propertyDataDefinitions = new ArrayList<>();
79     }
80
81     @Test
82     public void testValidateAnnotationsProperties() {
83         Annotation annotation = prepareAnnotation();
84         AnnotationTypeDefinition annotationTypeDefinition = prepareAnnotationTypeDefinition();
85         List<PropertyDefinition> properties = new ArrayList<>();
86         properties.add(new PropertyDefinition(propertyDataDefinition));
87
88         List<Annotation> annotations = annotationValidator
89             .validateAnnotationsProperties(annotation, annotationTypeDefinition);
90
91         Mockito.verify(propertyValidator).thinPropertiesValidator(properties, annotationTypeProperties, allData);
92         assertThat(annotations.get(0), is(annotation));
93     }
94
95     private AnnotationTypeDefinition prepareAnnotationTypeDefinition() {
96         AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
97         annotationTypeDefinition.setProperties(annotationTypeProperties);
98         annotationTypeDefinition.setCreationTime(System.currentTimeMillis());
99         annotationTypeDefinition.setType(TYPE);
100         annotationTypeDefinition.setHighestVersion(true);
101         annotationTypeDefinition.setUniqueId(UNIQUE_ID);
102         annotationTypeDefinition.setOwnerId(OWNER);
103         annotationTypeDefinition.setDescription(DESC);
104         return annotationTypeDefinition;
105     }
106
107     private Annotation prepareAnnotation(){
108         Annotation annotation = new Annotation();
109         annotation.setName(TEST);
110         annotation.setDescription(DESC);
111         propertyDataDefinition.setName(PROP_NAME);
112         propertyDataDefinitions.add(propertyDataDefinition);
113         annotation.setProperties(propertyDataDefinitions);
114         annotation.setType(TYPE);
115         return annotation;
116     }
117
118 }