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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
35 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
36 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
37 import org.openecomp.sdc.be.model.LifecycleStateEnum;
38 import org.openecomp.sdc.be.model.ModelTestBase;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.operations.api.IAdditionalInformationOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
43 import org.openecomp.sdc.be.resources.data.UserData;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import com.google.gson.Gson;
48 import com.google.gson.GsonBuilder;
49 import com.thinkaurelius.titan.core.TitanGraph;
50 //import com.tinkerpop.blueprints.Vertex;
51 import com.thinkaurelius.titan.core.TitanVertex;
53 import fj.data.Either;
55 @RunWith(SpringJUnit4ClassRunner.class)
56 @ContextConfiguration("classpath:application-context-test.xml")
57 public class AdditionalInformationOperationTest extends ModelTestBase {
59 private static String USER_ID = "muUserId";
60 private static String CATEGORY_NAME = "category/mycategory";
62 @javax.annotation.Resource(name = "titan-generic-dao")
63 private TitanGenericDao titanDao;
65 @javax.annotation.Resource(name = "resource-operation")
66 private ResourceOperation resourceOperation;
68 @javax.annotation.Resource(name = "additional-information-operation")
69 private IAdditionalInformationOperation additionalInformationOperation;
72 public void createUserAndCategory() {
73 deleteAndCreateCategory(CATEGORY_NAME);
74 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
79 public static void setupBeforeClass() {
86 public void testDummy() {
88 assertTrue(additionalInformationOperation != null);
92 private int getNumberOfVerticesOnGraph() {
93 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
94 TitanGraph graph = graphResult.left().value();
97 Iterable<TitanVertex> vertices = graph.query().vertices();
98 if (vertices != null) {
99 Iterator<TitanVertex> iterator = vertices.iterator();
100 while (iterator.hasNext()) {
101 TitanVertex vertex = iterator.next();
113 public void testCreateAndDeleteResource() {
115 int before = getNumberOfVerticesOnGraph();
117 Resource newResource = createResource(USER_ID, CATEGORY_NAME, "testCreateAndDeleteResource", "0.1", null, false, true);
118 String resourceId = newResource.getUniqueId();
120 Either<Resource, StorageOperationStatus> deleteResource = resourceOperation.deleteResource(resourceId);
121 assertTrue(deleteResource.isLeft());
123 int after = getNumberOfVerticesOnGraph();
125 assertEquals("check number of vertices not changed", before, after);
128 private Resource buildResourceMetadata(String userId, String category, String resourceName, String resourceVersion) {
130 Resource resource = new Resource();
131 resource.setName(resourceName);
132 resource.setVersion(resourceVersion);
134 resource.setDescription("description 1");
135 resource.setAbstract(false);
136 resource.setCreatorUserId(userId);
137 resource.setContactId("contactId@sdc.com");
138 resource.setVendorName("vendor 1");
139 resource.setVendorRelease("1.0.0");
140 String[] categoryArr = category.split("/");
141 resource.addCategory(categoryArr[0], categoryArr[1]);
142 resource.setIcon("images/my.png");
143 // List<String> tags = new ArrayList<String>();
146 // resource.setTags(tags);
150 private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
151 UserData userData = new UserData();
152 userData.setUserId(userId);
153 userData.setFirstName(firstName);
154 userData.setLastName(lastName);
156 titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
157 titanDao.createNode(userData, UserData.class);
163 private void deleteAndCreateCategory(String category) {
164 String[] names = category.split("/");
165 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
168 public Resource createResource(String userId, String category, String resourceName, String resourceVersion, String parentResourceName, boolean isAbstract, boolean isHighestVersion) {
170 List<String> derivedFrom = new ArrayList<String>();
171 if (parentResourceName != null) {
172 derivedFrom.add(parentResourceName);
174 Resource resource = buildResourceMetadata(userId, category, resourceName, resourceVersion);
176 resource.setAbstract(isAbstract);
177 resource.setHighestVersion(isHighestVersion);
179 Either<Resource, StorageOperationStatus> result = resourceOperation.createResource(resource, true);
181 assertTrue(result.isLeft());
182 Resource resultResource = result.left().value();
184 assertEquals("check resource state", LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, resultResource.getLifecycleState());
186 return resultResource;