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