re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / unittests / utils / FactoryUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.unittests.utils;
22
23 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
24 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.openecomp.sdc.be.model.*;
30 import org.openecomp.sdc.be.resources.data.*;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.UUID;
35
36 public final class FactoryUtils {
37     private static final String STRING = "string";
38
39         private FactoryUtils() {
40     }
41
42     public static final class Constants {
43         public static final String DEFAULT_CAPABILITY_TYPE = "tosca.capabilities.Node";
44     }
45
46     public static Resource createVFWithRI(String riVersion) {
47         Resource vf = createVF();
48         ComponentInstance ri = createResourceInstanceWithVersion(riVersion);
49         addComponentInstanceToVF(vf, ri);
50         return vf;
51     }
52
53     public static Resource createVF() {
54         Resource resource = new Resource();
55         String uniqueId = UUID.randomUUID().toString();
56         resource.setUniqueId(uniqueId);
57         return resource;
58     }
59
60     public static ResourceMetadataData createResourceByType(String resourceType) {
61         ResourceMetadataData resource = new ResourceMetadataData();
62         String uniqueId = UUID.randomUUID().toString();
63         resource.getMetadataDataDefinition().setHighestVersion(true);
64         resource.getMetadataDataDefinition().setUniqueId(uniqueId);
65         ((ResourceMetadataDataDefinition)resource.getMetadataDataDefinition()).setResourceType(ResourceTypeEnum.getTypeIgnoreCase(resourceType));
66         return resource;
67     }
68
69     public static void addComponentInstanceToVF(Resource vf, ComponentInstance resourceInstance) {
70         List<ComponentInstance> componentsInstances = vf.getComponentInstances() != null ? vf.getComponentInstances()
71                 : new ArrayList<>();
72         componentsInstances.add(resourceInstance);
73         vf.setComponentInstances(componentsInstances);
74     }
75
76     public static ComponentInstance createResourceInstance() {
77         ComponentInstance ri = new ComponentInstance();
78         ri.setComponentVersion("0.1");
79         String uniqueId = UUID.randomUUID().toString();
80         ri.setComponentUid(uniqueId);
81         ri.setUniqueId(uniqueId);
82         ri.setName("genericRI" + uniqueId);
83         ri.setOriginType(OriginTypeEnum.VF);
84         return ri;
85
86     }
87
88     public static ComponentInstance createResourceInstanceWithVersion(String riVersion) {
89         ComponentInstance ri = createResourceInstance();
90         ri.setComponentVersion(riVersion);
91         return ri;
92     }
93
94     public static CapabilityData createCapabilityData() {
95         CapabilityData capData = new CapabilityData();
96         String uniqueId = UUID.randomUUID().toString();
97         capData.setUniqueId(uniqueId);
98
99         capData.setType(Constants.DEFAULT_CAPABILITY_TYPE);
100         return capData;
101     }
102
103     public static RequirementData createRequirementData() {
104         RequirementData reqData = new RequirementData();
105         String uniqueId = UUID.randomUUID().toString();
106         reqData.setUniqueId(uniqueId);
107         return reqData;
108     }
109
110     public static CapabilityDefinition convertCapabilityDataToCapabilityDefinitionAddProperties(
111             CapabilityData capData) {
112         CapabilityDefinition capDef = new CapabilityDefinition();
113         capDef.setName("Cap2");
114         capDef.setDescription(capData.getDescription());
115         capDef.setUniqueId(capData.getUniqueId());
116         capDef.setValidSourceTypes(capData.getValidSourceTypes());
117         capDef.setType(capData.getType());
118         capDef.setProperties(new ArrayList<>());
119         ComponentInstanceProperty host = new ComponentInstanceProperty();
120         host.setUniqueId(UUID.randomUUID().toString());
121         host.setName("host");
122         host.setDefaultValue("defhost");
123         host.setType(STRING);
124
125         host.setSchema(new SchemaDefinition());
126         host.getSchema().setProperty(new PropertyDataDefinition());
127         host.getSchema().getProperty().setType(STRING);
128
129         capDef.getProperties().add(host);
130         ComponentInstanceProperty port = new ComponentInstanceProperty();
131         port.setName("port");
132         port.setDefaultValue("defport");
133         port.setUniqueId(UUID.randomUUID().toString());
134         port.setType(STRING);
135
136         port.setSchema(new SchemaDefinition());
137         port.getSchema().setProperty(new PropertyDataDefinition());
138         port.getSchema().getProperty().setType(STRING);
139
140         capDef.getProperties().add(port);
141         return capDef;
142     }
143
144     public static List<ComponentInstanceProperty> createComponentInstancePropertyList() {
145         List<ComponentInstanceProperty> properties = new ArrayList<>();
146         ComponentInstanceProperty host = new ComponentInstanceProperty();
147         host.setUniqueId(UUID.randomUUID().toString());
148         host.setName("host");
149         host.setValue("newhost");
150         host.setType(STRING);
151
152         host.setSchema(new SchemaDefinition());
153         host.getSchema().setProperty(new PropertyDataDefinition());
154         host.getSchema().getProperty().setType(STRING);
155
156         properties.add(host);
157         ComponentInstanceProperty port = new ComponentInstanceProperty();
158         port.setName("port");
159         port.setValue("newport");
160         port.setUniqueId(UUID.randomUUID().toString());
161         port.setType(STRING);
162
163         port.setSchema(new SchemaDefinition());
164         port.getSchema().setProperty(new PropertyDataDefinition());
165         port.getSchema().getProperty().setType(STRING);
166
167         properties.add(port);
168         return properties;
169     }
170
171     public static RequirementDefinition convertRequirementDataIDToRequirementDefinition(String reqDataId) {
172         RequirementDefinition reqDef = new RequirementDefinition();
173         reqDef.setUniqueId(reqDataId);
174         reqDef.setCapability(Constants.DEFAULT_CAPABILITY_TYPE);
175         return reqDef;
176     }
177
178     public static GraphEdge createGraphEdge() {
179         return new GraphEdge();
180     }
181
182     public static CapabilityInstData createCapabilityInstData() {
183         CapabilityInstData capInstData = new CapabilityInstData();
184         String uniqueId = UUID.randomUUID().toString();
185         capInstData.setUniqueId(uniqueId);
186         return capInstData;
187     }
188
189     public static PropertyValueData createPropertyData() {
190         PropertyValueData propData = new PropertyValueData();
191         String uniqueId = UUID.randomUUID().toString();
192         propData.setValue("localhost");
193         propData.setUniqueId(uniqueId);
194         return propData;
195     }
196
197     public static PropertyData convertCapabilityDefinitionToCapabilityData(PropertyDefinition propDef) {
198         PropertyData propData = new PropertyData();
199         propData.getPropertyDataDefinition().setUniqueId(propDef.getUniqueId());
200         propData.getPropertyDataDefinition().setDefaultValue(propDef.getDefaultValue());
201         return propData;
202     }
203
204     public static CapabilityDefinition convertCapabilityDataToCapabilityDefinitionRoot(CapabilityData capData) {
205         CapabilityDefinition capDef = new CapabilityDefinition();
206         capDef.setName("Cap1");
207         capDef.setDescription(capData.getDescription());
208         capDef.setUniqueId(capData.getUniqueId());
209         capDef.setValidSourceTypes(capData.getValidSourceTypes());
210         capDef.setType(capData.getType());
211         capDef.setProperties(new ArrayList<>());
212         ComponentInstanceProperty host = new ComponentInstanceProperty();
213         host.setUniqueId(UUID.randomUUID().toString());
214         host.setName("host");
215         host.setDefaultValue("roothost");
216         host.setType(STRING);
217
218         host.setSchema(new SchemaDefinition());
219         host.getSchema().setProperty(new PropertyDataDefinition());
220         host.getSchema().getProperty().setType(STRING);
221
222         capDef.getProperties().add(host);
223         ComponentInstanceProperty port = new ComponentInstanceProperty();
224         port.setName("port");
225         port.setDefaultValue("rootport");
226         port.setUniqueId(UUID.randomUUID().toString());
227         port.setType(STRING);
228
229         port.setSchema(new SchemaDefinition());
230         port.getSchema().setProperty(new PropertyDataDefinition());
231         port.getSchema().getProperty().setType(STRING);
232
233         capDef.getProperties().add(port);
234         return capDef;
235     }
236 }