eea11b66251c9123e3a6ad8d45cf1f815d525a29
[sdc.git] /
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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
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;
46
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;
52
53 import fj.data.Either;
54
55 @RunWith(SpringJUnit4ClassRunner.class)
56 @ContextConfiguration("classpath:application-context-test.xml")
57 public class AdditionalInformationOperationTest extends ModelTestBase {
58
59         private static String USER_ID = "muUserId";
60         private static String CATEGORY_NAME = "category/mycategory";
61
62         @javax.annotation.Resource(name = "titan-generic-dao")
63         private TitanGenericDao titanDao;
64
65         @javax.annotation.Resource(name = "resource-operation")
66         private ResourceOperation resourceOperation;
67
68         @javax.annotation.Resource(name = "additional-information-operation")
69         private IAdditionalInformationOperation additionalInformationOperation;
70
71         @Before
72         public void createUserAndCategory() {
73                 deleteAndCreateCategory(CATEGORY_NAME);
74                 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
75
76         }
77
78         @BeforeClass
79         public static void setupBeforeClass() {
80
81                 ModelTestBase.init();
82
83         }
84
85         @Test
86         public void testDummy() {
87
88                 assertTrue(additionalInformationOperation != null);
89
90         }
91
92         private int getNumberOfVerticesOnGraph() {
93                 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
94                 TitanGraph graph = graphResult.left().value();
95
96                 int i = 0;
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();
102                                 i++;
103                         }
104
105                 }
106
107                 titanDao.commit();
108
109                 return i;
110         }
111
112         @Test
113         public void testCreateAndDeleteResource() {
114
115                 int before = getNumberOfVerticesOnGraph();
116
117                 Resource newResource = createResource(USER_ID, CATEGORY_NAME, "testCreateAndDeleteResource", "0.1", null, false, true);
118                 String resourceId = newResource.getUniqueId();
119
120                 Either<Resource, StorageOperationStatus> deleteResource = resourceOperation.deleteResource(resourceId);
121                 assertTrue(deleteResource.isLeft());
122
123                 int after = getNumberOfVerticesOnGraph();
124
125                 assertEquals("check number of vertices not changed", before, after);
126         }
127
128         private Resource buildResourceMetadata(String userId, String category, String resourceName, String resourceVersion) {
129
130                 Resource resource = new Resource();
131                 resource.setName(resourceName);
132                 resource.setVersion(resourceVersion);
133                 ;
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>();
144                 // tags.add("TAG1");
145                 // tags.add("TAG2");
146                 // resource.setTags(tags);
147                 return resource;
148         }
149
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);
155
156                 titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
157                 titanDao.createNode(userData, UserData.class);
158                 titanDao.commit();
159
160                 return userData;
161         }
162
163         private void deleteAndCreateCategory(String category) {
164                 String[] names = category.split("/");
165                 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
166         }
167
168         public Resource createResource(String userId, String category, String resourceName, String resourceVersion, String parentResourceName, boolean isAbstract, boolean isHighestVersion) {
169
170                 List<String> derivedFrom = new ArrayList<String>();
171                 if (parentResourceName != null) {
172                         derivedFrom.add(parentResourceName);
173                 }
174                 Resource resource = buildResourceMetadata(userId, category, resourceName, resourceVersion);
175
176                 resource.setAbstract(isAbstract);
177                 resource.setHighestVersion(isHighestVersion);
178
179                 Either<Resource, StorageOperationStatus> result = resourceOperation.createResource(resource, true);
180
181                 assertTrue(result.isLeft());
182                 Resource resultResource = result.left().value();
183
184                 assertEquals("check resource state", LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, resultResource.getLifecycleState());
185
186                 return resultResource;
187
188         }
189
190 }