8bb4a2940d5b1a4e68d4021bcf586fbdcfaabff8
[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 com.thinkaurelius.titan.core.TitanVertex;
24 import fj.data.Either;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
32 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
34 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
35 import org.openecomp.sdc.be.model.ModelTestBase;
36 import org.openecomp.sdc.be.model.operations.api.IAdditionalInformationOperation;
37 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
38 import org.openecomp.sdc.be.resources.data.UserData;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41
42 import static org.junit.Assert.assertNotNull;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.ArgumentMatchers.eq;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration("classpath:application-context-test.xml")
50 public class AdditionalInformationOperationTest extends ModelTestBase {
51     private static final TitanGenericDao titanGenericDao = mock(TitanGenericDao.class);
52     private static String USER_ID = "muUserId";
53     private static String CATEGORY_NAME = "category/mycategory";
54     @Mock
55     private TitanVertex titanVertex;
56
57     @javax.annotation.Resource(name = "titan-generic-dao")
58     private TitanGenericDao titanDao;
59
60     @javax.annotation.Resource(name = "additional-information-operation")
61     private IAdditionalInformationOperation additionalInformationOperation;
62
63     @Before
64     public void createUserAndCategory() {
65         deleteAndCreateCategory(CATEGORY_NAME);
66         deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
67
68     }
69
70     @BeforeClass
71     public static void setupBeforeClass() {
72
73         ModelTestBase.init();
74
75     }
76
77     @Test
78     public void testDummy() {
79
80         assertNotNull(additionalInformationOperation);
81
82     }
83
84     @Test
85     public void testAddInfoParameter_InvalidId(){
86         Either<AdditionalInformationDefinition, TitanOperationStatus> result;
87         String uid = "uid";
88         String componentId = "componentId";
89         when(titanGenericDao.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(titanVertex));
90         result = additionalInformationOperation.addAdditionalInformationParameter
91                 (NodeTypeEnum.Resource,componentId,"key","value");
92         assertThat(result.isRight());
93     }
94
95     @Test
96     public void testUpdateInfoParameter_InvalidId(){
97         Either<AdditionalInformationDefinition, TitanOperationStatus> result;
98         String uid = "uid";
99         String componentId = "componentId";
100         when(titanGenericDao.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(titanVertex));
101         result = additionalInformationOperation.updateAdditionalInformationParameter
102                 (NodeTypeEnum.Resource,componentId,"id","key","value");
103         assertTrue(result.isRight());
104     }
105
106     private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
107         UserData userData = new UserData();
108         userData.setUserId(userId);
109         userData.setFirstName(firstName);
110         userData.setLastName(lastName);
111
112         titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
113         titanDao.createNode(userData, UserData.class);
114         titanDao.commit();
115
116         return userData;
117     }
118
119     private void deleteAndCreateCategory(String category) {
120         String[] names = category.split("/");
121         OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
122     }
123
124 }