1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
* Modifications copyright (c) 2019 Nokia
* ================================================================================
*/
package org.openecomp.sdc.be.components.impl;
import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.components.validation.ValidationUtils;
import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Product;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.exception.ResponseFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
public class ProductBusinessLogicTest extends ComponentBusinessLogicMock {
private Product product;
private User user;
private List<String> contacts;
private List<String> tags;
private String pId;
private String pName;
private String uId;
private String pCode;
private String pIcon;
private String desc;
private String role;
@InjectMocks
private ProductBusinessLogic productBusinessLogic;
@Mock
private ToscaOperationFacade toscaOperationFacade;
@Mock
private JanusGraphDao janusGraphDao;
@Mock
private ValidationUtils validationUtils;
@Mock
private ComponentsUtils componentsUtils;
@Mock
private IGraphLockOperation iGraphLockOperation;
@Mock
private UserValidations userValidations;
@Mock
private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
@Mock
ComponentNameValidator componentNameValidator;
@Before
public void setUp() {
productBusinessLogic = new ProductBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
artifactsBusinessLogic, componentInstanceBusinessLogic, artifactToscaOperation, componentContactIdValidator,
componentNameValidator, componentTagsValidator, componentValidator,
componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator );
MockitoAnnotations.initMocks(this);
product = new Product();
user = new User();
contacts = new ArrayList<>();
tags = new ArrayList<>();
pName = "product1";
pId = "productId";
uId = "userId";
pCode = "productCode";
pIcon = "projectIcon";
desc = "Testing Product Business Logic";
role = "PROJECT_MANAGER";
user.setUserId(uId);
user.setRole(role);
}
@Test(expected = ComponentException.class)
public void testCreateProduct_givenEmptyUserId_thenReturnsException() {
when(userValidations.validateUserNotEmpty(Mockito.any(User.class), Mockito.anyString()))
.thenThrow(new ByResponseFormatComponentException(new ResponseFormat()));
productBusinessLogic.createProduct(product, user);
}
@Test(expected = ComponentException.class)
public void testCreateProduct_givenInvalidUserRole_thenReturnsException() {
user.setRole("CREATOR");
doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserRole(any(), anyList());
assertTrue(productBusinessLogic.createProduct(product, user).isRight());
}
@Test
public void testCreateProduct_givenProductIsNull_thenReturnsError() {
product = null;
assertTrue(productBusinessLogic.createProduct(product, user).isRight());
}
@Test
public void testValidateProductName_givenValidName_thenReturnsSuccessful() {
when(userValidations.validateUserExists(anyString()))
.thenReturn(user);
when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
.thenReturn(Either.left(Boolean.TRUE));
Map result = productBusinessLogic.validateProductNameExists(pName, uId).left().value();
assertEquals(Boolean.TRUE, result.get("isValid"));
}
@Test
public void testValidateProductName_givenInvalidName_thenReturnsError() {
String invalidProductName = "~~";
when(userValidations.validateUserExists(anyString()))
.thenReturn(user);
when(toscaOperationFacade.validateComponentNameUniqueness(eq(invalidProductName), any(), any(ComponentTypeEnum.class)))
.thenReturn(Either.left(Boolean.FALSE));
Map result = productBusinessLogic.validateProductNameExists(invalidProductName, uId).left().value();
assertEquals(Boolean.FALSE, result.get("isValid"));
}
@Test
public void testValidateProductName_givenNameUniquenessCheckFails_thenReturnsError() {
when(userValidations.validateUserExists(anyString()))
.thenReturn(user);
when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
.thenReturn(Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS));
assertTrue(productBusinessLogic.validateProductNameExists(pName, uId).isRight());
}
@Test
public void testGetProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
when(toscaOperationFacade.getToscaElement(eq(pName)))
.thenReturn(Either.left(product));
assertTrue(productBusinessLogic.getProduct(pName, user).isLeft());
}
@Test
public void testGetProduct_givenInvalidProductId_thenReturnsError() {
when(toscaOperationFacade.getToscaElement(eq(pName)))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
assertTrue(productBusinessLogic.getProduct(pName, user).isRight());
}
@Test
public void testDeleteProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
when(toscaOperationFacade.deleteToscaComponent(pId))
.thenReturn(Either.left(product));
assertTrue(productBusinessLogic.deleteProduct(pId, user).isLeft());
}
@Test
public void testDeleteProduct_givenInvalidProductId_thenReturnsError() {
when(toscaOperationFacade.deleteToscaComponent(pId))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
assertTrue(productBusinessLogic.deleteProduct(pId, user).isRight());
}
@Test
public void testUpdateProductMetadata_givenUpdateProductNull_thenReturnsError() {
Product updateProduct = null;
String productId = null;
assertTrue(productBusinessLogic.updateProductMetadata(productId, updateProduct, user).isRight());
}
@Test
public void testUpdateProductMetadata_givenProductDoesNotExist_thenReturnsError() {
String productId = "product1";
when(toscaOperationFacade.getToscaElement(eq(productId)))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
assertTrue(productBusinessLogic.updateProductMetadata(productId, product, user).isRight());
}
@Test
public void testGetProductByNameAndVersion_givenValidNameAndVersion_thenReturnsSuccessful() {
String productVersion = "2.0";
when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
.thenReturn(Either.left(product));
assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isLeft());
}
@Test
public void testGetProductByNameAndVersion_givenInvalidDetails_thenReturnsError() {
String productVersion = "2.0";
when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isRight());
}
private List<String> getContacts() {
contacts.add("user1");
return contacts;
}
private List<String> getTags() {
tags.add("product1");
return tags;
}
}
|