re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / InterfaceOperationTest.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.model.operations.impl;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
30 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32 import org.openecomp.sdc.be.model.ArtifactDefinition;
33 import org.openecomp.sdc.be.model.InterfaceDefinition;
34 import org.openecomp.sdc.be.model.ModelTestBase;
35 import org.openecomp.sdc.be.model.Operation;
36 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
37 import org.openecomp.sdc.be.resources.data.UserData;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration("classpath:application-context-test.xml")
48 public class InterfaceOperationTest {
49     private static final Logger log = LoggerFactory.getLogger(InterfaceOperationTest.class);
50     private Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
51
52     private static String USER_ID = "muUserId";
53     private static String CATEGORY_NAME = "category/mycategory";
54     // InterfaceLifecycleOperation interfaceOperation = new
55     // InterfaceLifecycleOperation();
56
57     // TitanGenericDao titanGenericDao = Mockito.mock(TitanGenericDao.class);
58     @javax.annotation.Resource(name = "titan-generic-dao")
59     private TitanGenericDao titanDao;
60
61     @javax.annotation.Resource(name = "interface-operation")
62     private InterfaceLifecycleOperation interfaceOperation;
63
64     @javax.annotation.Resource(name = "property-operation")
65     private PropertyOperation propertyOperation;
66
67     // @Resource(name = "artifact-operation")
68     // private ArtifactOperation artifactOperation;
69
70     @Before
71     public void createUserAndCategory() {
72         deleteAndCreateCategory(CATEGORY_NAME);
73         deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
74     }
75
76     @BeforeClass
77     public static void setupBeforeClass() {
78         // ExternalConfiguration.setAppName("catalog-model");
79         // String appConfigDir = "src/test/resources/config/catalog-model";
80         // ConfigurationSource configurationSource = new
81         // FSConfigurationSource(ExternalConfiguration.getChangeListener(),
82         // appConfigDir);
83
84         ModelTestBase.init();
85
86     }
87
88     @Test
89     public void testDummy() {
90
91         assertNotNull(interfaceOperation);
92
93     }
94
95 /*    @Test
96     public void addInterfaceToResourceTest() {
97
98         String capabilityTypeName = "mycapability1";
99         String reqName = "host";
100         String reqNodeName = "tosca.nodes.Compute1";
101         String rootName = "Root100";
102         String softwareCompName = "tosca.nodes.SoftwareComponent";
103         String computeNodeName = "tosca.nodes.Compute";
104         String myResourceVersion = "300.0";
105         String reqRelationship = "myrelationship";
106
107         ResourceOperationTest resourceOperationTest = new ResourceOperationTest();
108         resourceOperationTest.setOperations(titanDao, resourceOperation, propertyOperation);
109
110         Resource rootResource = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, rootName, "100.0", null, true, true);
111
112         String interfaceName = "standard";
113         InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
114
115         Operation op = buildOperationDefinition();
116         Map<String, Operation> operations = new HashMap<String, Operation>();
117         operations.put("Create", op);
118         interfaceDefinition.setOperations(operations);
119
120         Either<InterfaceDefinition, StorageOperationStatus> result = interfaceOperation.addInterfaceToResource(interfaceDefinition, rootResource.getUniqueId(), "standard");
121
122         assertTrue(result.isLeft());
123         log.debug("{}", result.left().value());
124
125         Either<Resource, StorageOperationStatus> getResourceRes = resourceOperation.getResource(rootResource.getUniqueId());
126         assertTrue(getResourceRes.isLeft());
127         Resource resourceWithInterface = getResourceRes.left().value();
128         Map<String, InterfaceDefinition> interfaces = resourceWithInterface.getInterfaces();
129         assertNotNull(interfaces);
130         assertFalse(interfaces.isEmpty());
131         InterfaceDefinition interfaceDefinition2 = interfaces.get(interfaceName);
132         assertNotNull(interfaceDefinition2.getOperations());
133         assertFalse(interfaceDefinition2.getOperations().isEmpty());
134
135     }
136
137     @Test
138     public void updateInterfaceToResourceTest() {
139
140         String reqName = "host";
141         String rootName = "Root200";
142         String softwareCompName = "tosca.nodes.SoftwareComponent";
143
144         ResourceOperationTest resourceOperationTest = new ResourceOperationTest();
145         resourceOperationTest.setOperations(titanDao, resourceOperation, propertyOperation);
146
147         Resource rootResource = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, rootName, "200.0", null, true, true);
148
149         String interfaceName = "standard";
150         InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
151
152         Operation op = buildOperationDefinition();
153         Map<String, Operation> operations = new HashMap<String, Operation>();
154         operations.put("create", op);
155         interfaceDefinition.setOperations(operations);
156
157         Either<InterfaceDefinition, StorageOperationStatus> result = interfaceOperation.addInterfaceToResource(interfaceDefinition, rootResource.getUniqueId(), "standard");
158
159         ResourceMetadataData resourceData = new ResourceMetadataData();
160         resourceData.getMetadataDataDefinition().setUniqueId(rootResource.getUniqueId());
161         resourceData.getMetadataDataDefinition().setState(LifecycleStateEnum.CERTIFIED.name());
162         Either<ResourceMetadataData, TitanOperationStatus> updateNode = titanDao.updateNode(resourceData, ResourceMetadataData.class);
163         assertTrue(updateNode.isLeft());
164
165         Either<Resource, StorageOperationStatus> fetchRootResource = resourceOperation.getResource(rootResource.getUniqueId());
166
167         assertTrue(fetchRootResource.isLeft());
168         String rootResourceJson = prettyGson.toJson(fetchRootResource.left().value());
169         log.debug(rootResourceJson);
170
171         Resource softwareComponent = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, softwareCompName, "400.0", rootResource.getName(), true, true);
172
173         assertTrue(result.isLeft());
174         log.debug("{}", result.left().value());
175
176         addImplementationToOperation(op);
177         // String resourceId, String interfaceName, String
178         // operationName,Operation interf
179
180         Either<Operation, StorageOperationStatus> opResult = interfaceOperation.updateInterfaceOperation(softwareComponent.getUniqueId(), "standard", "create", op);
181         // PrintGraph pg = new PrintGraph();
182         // System.out.println(pg.buildGraphForWebgraphWiz(titanDao.getGraph().left().value()));
183         assertTrue(opResult.isLeft());
184         log.debug("{}", opResult.left().value());
185
186         Either<Resource, StorageOperationStatus> getResourceRes = resourceOperation.getResource(softwareComponent.getUniqueId());
187         assertTrue(getResourceRes.isLeft());
188         Resource resourceWithInterface = getResourceRes.left().value();
189         Map<String, InterfaceDefinition> interfaces = resourceWithInterface.getInterfaces();
190         assertNotNull(interfaces);
191         assertFalse(interfaces.isEmpty());
192         InterfaceDefinition interfaceDefinition2 = interfaces.get(interfaceName);
193         assertNotNull(interfaceDefinition2.getOperations());
194         assertFalse(interfaceDefinition2.getOperations().isEmpty());
195         Operation operation = interfaceDefinition2.getOperations().get("create");
196         assertNotNull(operation);
197         assertNotNull(operation.getImplementation());
198     }
199 */
200     private void addImplementationToOperation(Operation op) {
201         ArtifactDataDefinition artifactDataDef = new ArtifactDataDefinition();
202         artifactDataDef.setArtifactChecksum("YTg2Mjg4MWJhNmI5NzBiNzdDFkMWI=");
203         artifactDataDef.setArtifactName("create_myRoot.sh");
204         artifactDataDef.setArtifactLabel("create_myRoot");
205         artifactDataDef.setArtifactType("SHELL");
206         artifactDataDef.setDescription("good description");
207         artifactDataDef.setEsId("esId");
208         artifactDataDef.setUniqueId(op.getUniqueId() + "." + artifactDataDef.getArtifactLabel());
209         ArtifactDefinition artifactDef = new ArtifactDefinition(artifactDataDef, "UEsDBAoAAAAIAAeLb0bDQz");
210         op.setImplementation(artifactDef);
211     }
212
213     private InterfaceDefinition buildInterfaceDefinition() {
214         InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
215         interfaceDefinition.setType("tosca.interfaces.standard");
216         interfaceDefinition.setCreationDate(new Long(101232));
217
218         return interfaceDefinition;
219     }
220
221     private Operation buildOperationDefinition() {
222         Operation op = new Operation();
223         op.setCreationDate(new Long(101232));
224         op.setDescription("asda");
225
226         return op;
227     }
228
229     private void deleteAndCreateCategory(String category) {
230         String[] names = category.split("/");
231         OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
232
233         /*
234          * CategoryData categoryData = new CategoryData(); categoryData.setName(category);
235          *
236          * titanDao.deleteNode(categoryData, CategoryData.class); Either<CategoryData, TitanOperationStatus> createNode = titanDao .createNode(categoryData, CategoryData.class); System.out.println("after creating caetgory " + createNode);
237          */
238
239     }
240
241     private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
242         UserData userData = new UserData();
243         userData.setUserId(userId);
244         userData.setFirstName(firstName);
245         userData.setLastName(lastName);
246
247         titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
248         titanDao.createNode(userData, UserData.class);
249         titanDao.commit();
250
251         return userData;
252     }
253
254 }