2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
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;
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
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();
52 private static String USER_ID = "muUserId";
53 private static String CATEGORY_NAME = "category/mycategory";
54 // InterfaceLifecycleOperation interfaceOperation = new
55 // InterfaceLifecycleOperation();
57 // TitanGenericDao titanGenericDao = Mockito.mock(TitanGenericDao.class);
58 @javax.annotation.Resource(name = "titan-generic-dao")
59 private TitanGenericDao titanDao;
61 @javax.annotation.Resource(name = "interface-operation")
62 private InterfaceLifecycleOperation interfaceOperation;
64 @javax.annotation.Resource(name = "property-operation")
65 private PropertyOperation propertyOperation;
67 // @Resource(name = "artifact-operation")
68 // private ArtifactOperation artifactOperation;
71 public void createUserAndCategory() {
72 deleteAndCreateCategory(CATEGORY_NAME);
73 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
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(),
89 public void testDummy() {
91 assertNotNull(interfaceOperation);
96 public void addInterfaceToResourceTest() {
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";
107 ResourceOperationTest resourceOperationTest = new ResourceOperationTest();
108 resourceOperationTest.setOperations(titanDao, resourceOperation, propertyOperation);
110 Resource rootResource = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, rootName, "100.0", null, true, true);
112 String interfaceName = "standard";
113 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
115 Operation op = buildOperationDefinition();
116 Map<String, Operation> operations = new HashMap<String, Operation>();
117 operations.put("Create", op);
118 interfaceDefinition.setOperations(operations);
120 Either<InterfaceDefinition, StorageOperationStatus> result = interfaceOperation.addInterfaceToResource(interfaceDefinition, rootResource.getUniqueId(), "standard");
122 assertTrue(result.isLeft());
123 log.debug("{}", result.left().value());
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());
138 public void updateInterfaceToResourceTest() {
140 String reqName = "host";
141 String rootName = "Root200";
142 String softwareCompName = "tosca.nodes.SoftwareComponent";
144 ResourceOperationTest resourceOperationTest = new ResourceOperationTest();
145 resourceOperationTest.setOperations(titanDao, resourceOperation, propertyOperation);
147 Resource rootResource = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, rootName, "200.0", null, true, true);
149 String interfaceName = "standard";
150 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
152 Operation op = buildOperationDefinition();
153 Map<String, Operation> operations = new HashMap<String, Operation>();
154 operations.put("create", op);
155 interfaceDefinition.setOperations(operations);
157 Either<InterfaceDefinition, StorageOperationStatus> result = interfaceOperation.addInterfaceToResource(interfaceDefinition, rootResource.getUniqueId(), "standard");
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());
165 Either<Resource, StorageOperationStatus> fetchRootResource = resourceOperation.getResource(rootResource.getUniqueId());
167 assertTrue(fetchRootResource.isLeft());
168 String rootResourceJson = prettyGson.toJson(fetchRootResource.left().value());
169 log.debug(rootResourceJson);
171 Resource softwareComponent = resourceOperationTest.createResource(USER_ID, CATEGORY_NAME, softwareCompName, "400.0", rootResource.getName(), true, true);
173 assertTrue(result.isLeft());
174 log.debug("{}", result.left().value());
176 addImplementationToOperation(op);
177 // String resourceId, String interfaceName, String
178 // operationName,Operation interf
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());
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());
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);
213 private InterfaceDefinition buildInterfaceDefinition() {
214 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
215 interfaceDefinition.setType("tosca.interfaces.standard");
216 interfaceDefinition.setCreationDate(new Long(101232));
218 return interfaceDefinition;
221 private Operation buildOperationDefinition() {
222 Operation op = new Operation();
223 op.setCreationDate(new Long(101232));
224 op.setDescription("asda");
229 private void deleteAndCreateCategory(String category) {
230 String[] names = category.split("/");
231 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
234 * CategoryData categoryData = new CategoryData(); categoryData.setName(category);
236 * titanDao.deleteNode(categoryData, CategoryData.class); Either<CategoryData, TitanOperationStatus> createNode = titanDao .createNode(categoryData, CategoryData.class); System.out.println("after creating caetgory " + createNode);
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);
247 titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
248 titanDao.createNode(userData, UserData.class);