Migrate Junit4 to Junit5
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / tosca / parser / elements / queries / TopologyTemplateQueryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdc-tosca
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T 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
21 package org.onap.sdc.tosca.parser.elements.queries;
22
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.onap.sdc.tosca.parser.enums.SdcTypes;
33 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
34 import org.onap.sdc.toscaparser.api.NodeTemplate;
35 import org.onap.sdc.toscaparser.api.elements.Metadata;
36
37 @ExtendWith(MockitoExtension.class)
38 public class TopologyTemplateQueryTest {
39
40     @Mock
41     private Metadata metadata;
42
43     @Mock
44     private NodeTemplate nodeTemplate;
45
46     @Test
47     public void objectIsNotTopologyTemplate() {
48         assertThrows(IllegalArgumentException.class, () -> {
49             TopologyTemplateQuery.newBuilder(SdcTypes.CP).build();
50         });
51     }
52
53     @Test
54     public void templateIsFoundByTypeOnly() {
55         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
56             .build();
57         when(nodeTemplate.getMetaData()).thenReturn(metadata);
58         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.SERVICE.getValue());
59         assertTrue(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
60     }
61
62     @Test
63     public void templateIsNotFoundWhenMetadataIsNull() {
64         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.VF)
65             .build();
66         when(nodeTemplate.getMetaData()).thenReturn(null);
67         assertFalse(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
68     }
69
70     @Test
71     public void templateIsFoundIfItIsService() {
72         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE)
73             .build();
74         when(nodeTemplate.getMetaData()).thenReturn(metadata);
75         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.SERVICE.getValue());
76         assertTrue(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
77     }
78
79     @Test
80     public void templateIsFoundByTypeAndCUUID() {
81         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.CVFC)
82             .customizationUUID("345")
83             .build();
84         when(nodeTemplate.getMetaData()).thenReturn(metadata);
85         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.CVFC.getValue());
86         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)).thenReturn("345");
87         assertTrue(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
88     }
89
90     @Test
91     public void templateIsNotFoundWhenTypeIsNotMatchedAndCuuidIsNotSet() {
92         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.CVFC)
93             .build();
94         when(nodeTemplate.getMetaData()).thenReturn(metadata);
95         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.VF.getValue());
96         assertFalse(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
97     }
98
99     @Test
100     public void templateIsFoundWhenTypeIsMatchedCuuidIsProvidedAndCuuidIsNullInMetadata() {
101         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.VF)
102             .customizationUUID("2345")
103             .build();
104         when(nodeTemplate.getMetaData()).thenReturn(metadata);
105         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)).thenReturn(null);
106         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.VF.getValue());
107         assertFalse(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
108     }
109
110     @Test
111     public void templateIsFoundWhenTypeIsMatchedAndCuuidIsNullInMetadata() {
112         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.VF)
113             .build();
114         when(nodeTemplate.getMetaData()).thenReturn(metadata);
115         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)).thenReturn(null);
116         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.VF.getValue());
117         assertTrue(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
118     }
119
120     @Test
121     public void templateIsNotFoundWhenTypeIsMatchedAndCuuidIsSet() {
122         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.CVFC)
123             .customizationUUID("345")
124             .build();
125         when(nodeTemplate.getMetaData()).thenReturn(metadata);
126         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.CVFC.getValue());
127         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)).thenReturn("345");
128         assertTrue(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
129     }
130
131     @Test
132     public void templateIsNotFoundWhenTypeIsNotMatchedAndCuuidIsSet() {
133         TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.CR)
134             .customizationUUID("345")
135             .build();
136         when(nodeTemplate.getMetaData()).thenReturn(metadata);
137         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn(SdcTypes.CVFC.getValue());
138         assertFalse(topologyTemplateQuery.isMatchingSearchCriteria(nodeTemplate));
139     }
140
141
142 }