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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
package org.openecomp.sdc.be.components;
import com.google.common.collect.Sets;
import fj.data.Either;
import org.junit.Before;
import org.mockito.Mockito;
import org.openecomp.sdc.ElementOperationMock;
import org.openecomp.sdc.be.auditing.impl.AuditingManager;
import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.GroupInstance;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.jsontitan.operations.ForwardingPathOperation;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IElementOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation;
import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
public class BaseServiceBusinessLogicTest {
private static final String SERVICE_CATEGORY = "Mobility";
final ServletContext servletContext = Mockito.mock(ServletContext.class);
UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
ServiceBusinessLogic bl = new ServiceBusinessLogic();
ResponseFormatManager responseManager = null;
IElementOperation mockElementDao;
ComponentsUtils componentsUtils;
AuditCassandraDao auditingDao = Mockito.mock(AuditCassandraDao.class);
ArtifactsBusinessLogic artifactBl = Mockito.mock(ArtifactsBusinessLogic.class);
GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
CacheMangerOperation cacheManager = Mockito.mock(CacheMangerOperation.class);
GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
ForwardingPathOperation forwardingPathOperation = Mockito.mock(ForwardingPathOperation.class);
User user = null;
Service serviceResponse = null;
Resource genericService = null;
protected static final String CERTIFIED_VERSION = "1.0";
protected static final String UNCERTIFIED_VERSION = "0.2";
protected static final String COMPONNET_ID = "myUniqueId";
protected static final String GENERIC_SERVICE_NAME = "org.openecomp.resource.abstract.nodes.service";
protected static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_CERTIFIED_VERSION = new HashMap<>();
protected static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_UNCERTIFIED_VERSION_CURR = new HashMap<>();
protected static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_UNCERTIFIED_VERSION_PREV = new HashMap<>();
@Before
public void setup() {
ExternalConfiguration.setAppName("catalog-be");
// init Configuration
String appConfigDir = "src/test/resources/config/catalog-be";
ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
// Elements
mockElementDao = new ElementOperationMock();
// User data and management
user = new User();
user.setUserId("jh0003");
user.setFirstName("Jimmi");
user.setLastName("Hendrix");
user.setRole(Role.ADMIN.name());
Either<User, ActionStatus> eitherGetUser = Either.left(user);
when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
// Servlet Context attributes
when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
when(graphLockOperation.lockComponent(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
when(graphLockOperation.lockComponentByName(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
// artifact bussinesslogic
ArtifactDefinition artifactDef = new ArtifactDefinition();
when(artifactBl.createArtifactPlaceHolderInfo(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any(User.class), Mockito.any(ArtifactGroupTypeEnum.class))).thenReturn(artifactDef);
// createService
serviceResponse = createServiceObject(true);
Either<Component, StorageOperationStatus> eitherCreate = Either.left(serviceResponse);
when(toscaOperationFacade.createToscaComponent(Mockito.any(Component.class))).thenReturn(eitherCreate);
Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
when(toscaOperationFacade.validateComponentNameExists("Service", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCount);
Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
when(toscaOperationFacade.validateComponentNameExists("alreadyExist", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCountExist);
genericService = setupGenericServiceMock();
Either<Resource, StorageOperationStatus> findLatestGeneric = Either.left(genericService);
when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_SERVICE_NAME)).thenReturn(findLatestGeneric);
//forwardingPath
when(forwardingPathOperation.addForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
when(forwardingPathOperation.updateForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
when(forwardingPathOperation.deleteForwardingPath(any(),any())).thenReturn(Either.left(Sets.newHashSet("Wow-It-Works")));
when(toscaOperationFacade.getToscaElement("delete_forward_test")).thenReturn(Either.left(createServiceObject(true)));
bl = new ServiceBusinessLogic();
bl.setElementDao(mockElementDao);
bl.setUserAdmin(mockUserAdmin);
bl.setArtifactBl(artifactBl);
bl.setGraphLockOperation(graphLockOperation);
bl.setTitanGenericDao(mockTitanDao);
bl.setToscaOperationFacade(toscaOperationFacade);
bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
bl.setComponentsUtils(componentsUtils);
bl.setCassandraAuditingDao(auditingDao);
bl.setCacheManagerOperation(cacheManager);
bl.setForwardingPathOperation(forwardingPathOperation);
bl.setToscaOperationFacade(toscaOperationFacade);
mockAuditingDaoLogic();
responseManager = ResponseFormatManager.getInstance();
}
protected Component createNewService() {
Service service = new Service();
int listSize = 3;
service.setName("serviceName");
service.setUniqueId("serviceUniqueId");
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance ci;
for(int i= 0; i<listSize; ++i){
ci = new ComponentInstance();
ci.setName("ciName" + i);
ci.setUniqueId("ciId" + i);
List<GroupInstance> groupInstances= new ArrayList<>();
GroupInstance gi;
for(int j = 0; j<listSize; ++j){
gi = new GroupInstance();
gi.setName(ci.getName( )+ "giName" + j);
gi.setUniqueId(ci.getName() + "giId" + j);
groupInstances.add(gi);
}
ci.setGroupInstances(groupInstances);
componentInstances.add(ci);
}
service.setComponentInstances(componentInstances);
return service;
}
private void mockAuditingDaoLogic() {
FILTER_MAP_CERTIFIED_VERSION.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, UNCERTIFIED_VERSION);
FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_VERSION, UNCERTIFIED_VERSION);
final ResourceAdminEvent createResourceAudit = new ResourceAdminEvent();
createResourceAudit.setModifier("Carlos Santana(cs0008)");
createResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
createResourceAudit.setCurrVersion("0.1");
createResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
createResourceAudit.setRequestId("3e65cea1-7403-4bc7-b461-e2544d83799f");
createResourceAudit.setDesc("OK");
createResourceAudit.setResourceType("Resource");
createResourceAudit.setStatus("201");
createResourceAudit.setPrevVersion("");
createResourceAudit.setAction("Create");
createResourceAudit.setPrevState("");
createResourceAudit.setResourceName("MyTestResource");
final ResourceAdminEvent checkInResourceAudit = new ResourceAdminEvent();
checkInResourceAudit.setModifier("Carlos Santana(cs0008)");
checkInResourceAudit.setCurrState("NOT_CERTIFIED_CHECKIN");
checkInResourceAudit.setCurrVersion("0.1");
checkInResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
checkInResourceAudit.setRequestId("ffacbf5d-eeb1-43c6-a310-37fe7e1cc091");
checkInResourceAudit.setDesc("OK");
checkInResourceAudit.setComment("Stam");
checkInResourceAudit.setResourceType("Resource");
checkInResourceAudit.setStatus("200");
checkInResourceAudit.setPrevVersion("0.1");
checkInResourceAudit.setAction("Checkin");
checkInResourceAudit.setPrevState("NOT_CERTIFIED_CHECKOUT");
checkInResourceAudit.setResourceName("MyTestResource");
final ResourceAdminEvent checkOutResourceAudit = new ResourceAdminEvent();
checkOutResourceAudit.setModifier("Carlos Santana(cs0008)");
checkOutResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
checkOutResourceAudit.setCurrVersion("0.2");
checkOutResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
checkOutResourceAudit.setRequestId("7add5078-4c16-4d74-9691-cc150e3c96b8");
checkOutResourceAudit.setDesc("OK");
checkOutResourceAudit.setComment("");
checkOutResourceAudit.setResourceType("Resource");
checkOutResourceAudit.setStatus("200");
checkOutResourceAudit.setPrevVersion("0.1");
checkOutResourceAudit.setAction("Checkout");
checkOutResourceAudit.setPrevState("NOT_CERTIFIED_CHECKIN");
checkOutResourceAudit.setResourceName("MyTestResource");
List<ResourceAdminEvent> list = new ArrayList<ResourceAdminEvent>() {
{
add(createResourceAudit);
add(checkInResourceAudit);
add(checkOutResourceAudit);
}
};
Either<List<ResourceAdminEvent>, ActionStatus> result = Either.left(list);
Mockito.when(auditingDao.getByServiceInstanceId(Mockito.anyString())).thenReturn(result);
List<ResourceAdminEvent> listPrev = new ArrayList<ResourceAdminEvent>();
Either<List<ResourceAdminEvent>, ActionStatus> resultPrev = Either.left(listPrev);
Mockito.when(auditingDao.getAuditByServiceIdAndPrevVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultPrev);
List<ResourceAdminEvent> listCurr = new ArrayList<ResourceAdminEvent>() {
{
add(checkOutResourceAudit);
}
};
Either<List<ResourceAdminEvent>, ActionStatus> resultCurr = Either.left(listCurr);
Mockito.when(auditingDao.getAuditByServiceIdAndCurrVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultCurr);
}
protected Service createServiceObject(boolean afterCreate) {
Service service = new Service();
service.setName("Service");
CategoryDefinition category = new CategoryDefinition();
category.setName(SERVICE_CATEGORY);
List<CategoryDefinition> categories = new ArrayList<>();
categories.add(category);
service.setCategories(categories);
service.setDescription("description");
List<String> tgs = new ArrayList<String>();
tgs.add(service.getName());
service.setTags(tgs);
service.setIcon("MyIcon");
service.setContactId("aa1234");
service.setProjectCode("12345");
if (afterCreate) {
service.setVersion("0.1");
service.setUniqueId(service.getName() + ":" + service.getVersion());
service.setCreatorUserId(user.getUserId());
service.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
}
return service;
}
protected Resource setupGenericServiceMock(){
Resource genericService = new Resource();
genericService.setVersion("1.0");
genericService.setToscaResourceName(GENERIC_SERVICE_NAME);
return genericService;
}
protected static ForwardingPathDataDefinition forwardingPathDataDefinition;
protected static ForwardingPathDataDefinition createMockPath() {
if (forwardingPathDataDefinition != null){
return forwardingPathDataDefinition ;
}
forwardingPathDataDefinition = new ForwardingPathDataDefinition("Yoyo");
forwardingPathDataDefinition.setUniqueId(java.util.UUID.randomUUID().toString());
forwardingPathDataDefinition.setDestinationPortNumber("414155");
forwardingPathDataDefinition.setProtocol("http");
org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition> forwardingPathElementDataDefinitionListDataDefinition = new org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<>();
forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("fromNode","toNode", "333","444","2222","5555"));
forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("toNode","toNode2", "4444","44444","4","44"));
forwardingPathDataDefinition.setPathElements(forwardingPathElementDataDefinitionListDataDefinition);
return forwardingPathDataDefinition;
}
}
|