Upgrade SDC from Titan to Janus Graph
[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 static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertThat;
24
25 import fj.data.Either;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
37 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
38 import org.openecomp.sdc.be.datatypes.elements.Annotation;
39 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
42 import org.openecomp.sdc.be.model.DataTypeDefinition;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class AnnotationValidatorTest {
48
49     private static final String TYPE = "type";
50     private static final String UNIQUE_ID = "1";
51     private static final String OWNER = "owner";
52     private static final String DESC = "desc";
53     private static final String TEST = "test";
54     private static final String PROP_NAME = "propName";
55     private Map<String, DataTypeDefinition> allData;
56     private List<PropertyDefinition> annotationTypeProperties;
57     private AnnotationValidator annotationValidator;
58     private List<PropertyDataDefinition> propertyDataDefinitions = new ArrayList<>();
59     private PropertyDataDefinition propertyDataDefinition = new PropertyDataDefinition();;
60
61     @Mock
62     private ComponentsUtils componentsUtils;
63     @Mock
64     private ApplicationDataTypeCache dataTypeCache;
65     @Mock
66     private PropertyValidator propertyValidator;
67     @Mock
68     private ExceptionUtils exceptionUtils;
69
70     @Before
71     public void setUp() throws Exception {
72         annotationValidator = new AnnotationValidator(propertyValidator, exceptionUtils, dataTypeCache, componentsUtils);
73         allData = Collections.emptyMap();
74         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> cacheResponse = Either.left(allData);
75         Mockito.when(dataTypeCache.getAll()).thenReturn(cacheResponse);
76         annotationTypeProperties = Collections.emptyList();
77         propertyDataDefinitions = new ArrayList<>();
78     }
79
80     @Test
81     public void testValidateAnnotationsProperties() {
82         Annotation annotation = prepareAnnotation();
83         AnnotationTypeDefinition annotationTypeDefinition = prepareAnnotationTypeDefinition();
84         List<PropertyDefinition> properties = new ArrayList<>();
85         properties.add(new PropertyDefinition(propertyDataDefinition));
86
87         List<Annotation> annotations = annotationValidator
88             .validateAnnotationsProperties(annotation, annotationTypeDefinition);
89
90         Mockito.verify(propertyValidator).thinPropertiesValidator(properties, annotationTypeProperties, allData);
91         assertThat(annotations.get(0), is(annotation));
92     }
93
94     private AnnotationTypeDefinition prepareAnnotationTypeDefinition() {
95         AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
96         annotationTypeDefinition.setProperties(annotationTypeProperties);
97         annotationTypeDefinition.setCreationTime(System.currentTimeMillis());
98         annotationTypeDefinition.setType(TYPE);
99         annotationTypeDefinition.setHighestVersion(true);
100         annotationTypeDefinition.setUniqueId(UNIQUE_ID);
101         annotationTypeDefinition.setOwnerId(OWNER);
102         annotationTypeDefinition.setDescription(DESC);
103         return annotationTypeDefinition;
104     }
105
106     private Annotation prepareAnnotation(){
107         Annotation annotation = new Annotation();
108         annotation.setName(TEST);
109         annotation.setDescription(DESC);
110         propertyDataDefinition.setName(PROP_NAME);
111         propertyDataDefinitions.add(propertyDataDefinition);
112         annotation.setProperties(propertyDataDefinitions);
113         annotation.setType(TYPE);
114         return annotation;
115     }
116
117 }