Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PropertyDeclaratorTestBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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.openecomp.sdc.be.components.property;
22
23 import org.junit.Before;
24 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
25 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
26 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
27 import org.openecomp.sdc.be.dao.utils.MapUtil;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
31 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34
35 import java.util.List;
36 import java.util.Map;
37 import java.util.stream.Collectors;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.openecomp.sdc.common.api.Constants.GET_INPUT;
41
42 public class PropertyDeclaratorTestBase {
43
44     static final String INNER_PROP1 = "ecomp_generated_naming";
45     static final String INNER_PROP2 = "naming_policy";
46     static final String RESOURCE_ID = "resourceId";
47     static final String INPUT_ID = "inputId";
48     static final String INSTANCE_ID = "inst1";
49     static final String ORIGIN_INSTANCE_ID = "originInst1";
50     PropertyDataDefinition prop1, prop2, complexProperty;
51     Resource resource;
52
53     @Before
54     public void setUp() throws Exception {
55         prop1 = new PropertyDataDefinitionBuilder()
56                 .setUniqueId("prop1")
57                 .setType("string")
58                 .setName("prop1")
59                 .setValue("value1")
60                 .build();
61
62         prop2 = new PropertyDataDefinitionBuilder()
63                 .setUniqueId("prop2")
64                 .setType("string")
65                 .setSchemaType("string")
66                 .setName("prop2")
67                 .setValue("[\"a\", \"b\"]")
68                 .build();
69
70         complexProperty = new PropertyDataDefinitionBuilder()
71                 .setUniqueId("prop3")
72                 .setType("org.openecomp.type1")
73                 .setName("prop3")
74                 .setValue("{\"ecomp_generated_naming\":true\",\"naming_policy\":\"abc\"}")
75                 .build();
76
77         ComponentInstance inst1 = new ComponentInstanceBuilder()
78                 .setComponentUid(ORIGIN_INSTANCE_ID)
79                 .setId(INSTANCE_ID)
80                 .setNormalizedName(INSTANCE_ID)
81                 .build();
82
83         resource = new ResourceBuilder()
84                 .setUniqueId(RESOURCE_ID)
85                 .addComponentInstance(inst1)
86                 .build();
87
88     }
89
90     List<ComponentInstancePropInput> createInstancePropInputList(List<PropertyDataDefinition> properties) {
91         return properties.stream().map(prop -> new ComponentInstancePropInput(new ComponentInstanceProperty(prop)))
92                 .collect(Collectors.toList());
93     }
94
95     void verifyInputPropertiesList(List<InputDefinition> createdInputs, List<PropertyDataDefinition> capturedUpdatedProperties) {
96         Map<String, InputDefinition> propertyIdToCreatedInput = MapUtil.toMap(createdInputs, InputDefinition::getPropertyId);
97         capturedUpdatedProperties.forEach(updatedProperty -> verifyInputPropertiesList(updatedProperty, propertyIdToCreatedInput.get(updatedProperty.getUniqueId())));
98     }
99
100     String generateGetInputValue(String value) {
101         return String.format("{\"%s\":\"%s\"}", GET_INPUT, value);
102     }
103
104     String generateGetInputValueAsListInput(String inputName, String inputProperty) {
105         return String.format("{\"%s\":[\"%s\",\"INDEX\",\"%s\"]}", GET_INPUT, inputName, inputProperty);
106     }
107
108     private void verifyInputPropertiesList(PropertyDataDefinition updatedProperty, InputDefinition input) {
109         assertThat(input.getProperties()).hasSize(1);
110         assertThat(new ComponentInstanceProperty(updatedProperty)).isEqualTo(input.getProperties().get(0));
111     }
112
113 }