ComponentCache & ComponentCassandraDao removal
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / BaseServiceBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.components;
22
23 import com.google.common.collect.Sets;
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.mockito.Mockito;
27 import org.openecomp.sdc.ElementOperationMock;
28 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
29 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
30 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
31 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
36 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
37 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
43 import org.openecomp.sdc.be.model.*;
44 import org.openecomp.sdc.be.model.category.CategoryDefinition;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
46 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
47 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
48 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
49 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
50 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
51 import org.openecomp.sdc.be.user.Role;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
54 import org.openecomp.sdc.common.api.ConfigurationSource;
55 import org.openecomp.sdc.common.api.Constants;
56 import org.openecomp.sdc.common.datastructure.AuditingFieldsKey;
57 import org.openecomp.sdc.common.impl.ExternalConfiguration;
58 import org.openecomp.sdc.common.impl.FSConfigurationSource;
59 import org.springframework.web.context.WebApplicationContext;
60
61 import javax.servlet.ServletContext;
62 import java.util.ArrayList;
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Map;
66
67 import static org.mockito.ArgumentMatchers.any;
68 import static org.mockito.Mockito.when;
69
70 public abstract class BaseServiceBusinessLogicTest {
71     private static final String SERVICE_CATEGORY = "Mobility";
72     final ServletContext servletContext = Mockito.mock(ServletContext.class);
73     UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
74     WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
75     WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
76     ServiceBusinessLogic bl = new ServiceBusinessLogic();
77     ResponseFormatManager responseManager = null;
78     IElementOperation mockElementDao;
79     ComponentsUtils componentsUtils;
80     AuditCassandraDao auditingDao = Mockito.mock(AuditCassandraDao.class);
81     ArtifactsBusinessLogic artifactBl = Mockito.mock(ArtifactsBusinessLogic.class);
82     GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
83     JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class);
84     ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
85     GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
86     ForwardingPathOperation forwardingPathOperation  = Mockito.mock(ForwardingPathOperation.class);
87
88     User user = null;
89     Service serviceResponse = null;
90     Resource genericService = null;
91
92     protected static final String CERTIFIED_VERSION = "1.0";
93     protected static final String UNCERTIFIED_VERSION = "0.2";
94     protected static final String COMPONNET_ID = "myUniqueId";
95     protected static final String GENERIC_SERVICE_NAME = "org.openecomp.resource.abstract.nodes.service";
96     protected static Map<AuditingFieldsKey, Object> FILTER_MAP_CERTIFIED_VERSION = new HashMap<>();
97     protected static Map<AuditingFieldsKey, Object> FILTER_MAP_UNCERTIFIED_VERSION_CURR = new HashMap<>();
98     protected static Map<AuditingFieldsKey, Object> FILTER_MAP_UNCERTIFIED_VERSION_PREV = new HashMap<>();
99     @Before
100     public void setup() {
101
102         ExternalConfiguration.setAppName("catalog-be");
103         // init Configuration
104         String appConfigDir = "src/test/resources/config/catalog-be";
105         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
106         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
107         componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
108
109         // Elements
110         mockElementDao = new ElementOperationMock();
111
112         // User data and management
113         user = new User();
114         user.setUserId("jh0003");
115         user.setFirstName("Jimmi");
116         user.setLastName("Hendrix");
117         user.setRole(Role.ADMIN.name());
118
119         Either<User, ActionStatus> eitherGetUser = Either.left(user);
120         when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
121
122         // Servlet Context attributes
123         when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
124         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
125         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
126         when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
127         when(graphLockOperation.lockComponent(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
128         when(graphLockOperation.lockComponentByName(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
129
130         // artifact bussinesslogic
131         ArtifactDefinition artifactDef = new ArtifactDefinition();
132         when(artifactBl.createArtifactPlaceHolderInfo(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any(User.class), Mockito.any(ArtifactGroupTypeEnum.class))).thenReturn(artifactDef);
133
134         // createService
135         serviceResponse = createServiceObject(true);
136         Either<Component, StorageOperationStatus> eitherCreate = Either.left(serviceResponse);
137         when(toscaOperationFacade.createToscaComponent(Mockito.any(Component.class))).thenReturn(eitherCreate);
138         Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
139         when(toscaOperationFacade.validateComponentNameExists("Service", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCount);
140         Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
141         when(toscaOperationFacade.validateComponentNameExists("alreadyExist", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCountExist);
142
143         genericService = setupGenericServiceMock();
144         Either<Resource, StorageOperationStatus> findLatestGeneric = Either.left(genericService);
145         when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_SERVICE_NAME)).thenReturn(findLatestGeneric);
146
147         //forwardingPath
148         when(forwardingPathOperation.addForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
149         when(forwardingPathOperation.updateForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
150         when(forwardingPathOperation.deleteForwardingPath(any(),any())).thenReturn(Either.left(Sets.newHashSet("Wow-It-Works")));
151         when(toscaOperationFacade.getToscaElement("delete_forward_test")).thenReturn(Either.left(createServiceObject(true)));
152
153         bl = new ServiceBusinessLogic();
154         bl.setElementDao(mockElementDao);
155         bl.setUserAdmin(mockUserAdmin);
156         bl.setArtifactBl(artifactBl);
157         bl.setGraphLockOperation(graphLockOperation);
158         bl.setJanusGraphGenericDao(mockJanusGraphDao);
159         bl.setToscaOperationFacade(toscaOperationFacade);
160         bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
161         bl.setComponentsUtils(componentsUtils);
162         bl.setCassandraAuditingDao(auditingDao);
163         bl.setForwardingPathOperation(forwardingPathOperation);
164         bl.setToscaOperationFacade(toscaOperationFacade);
165         mockAuditingDaoLogic();
166
167         responseManager = ResponseFormatManager.getInstance();
168
169     }
170
171     protected Component createNewService() {
172
173         Service service = new Service();
174         int listSize = 3;
175         service.setName("serviceName");
176         service.setUniqueId("serviceUniqueId");
177         List<ComponentInstance> componentInstances = new ArrayList<>();
178         ComponentInstance ci;
179         for(int i= 0; i<listSize; ++i){
180             ci = new ComponentInstance();
181             ci.setName("ciName" + i);
182             ci.setUniqueId("ciId" + i);
183             List<GroupInstance>  groupInstances= new ArrayList<>();
184             GroupInstance gi;
185             for(int j = 0; j<listSize; ++j){
186                 gi = new GroupInstance();
187                 gi.setName(ci.getName( )+ "giName" + j);
188                 gi.setUniqueId(ci.getName() + "giId" + j);
189                 groupInstances.add(gi);
190             }
191             ci.setGroupInstances(groupInstances);
192             componentInstances.add(ci);
193         }
194         service.setComponentInstances(componentInstances);
195         return service;
196     }
197
198     private void mockAuditingDaoLogic() {
199         FILTER_MAP_CERTIFIED_VERSION.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
200         FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
201         FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
202
203         FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION, UNCERTIFIED_VERSION);
204         FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKey.AUDIT_RESOURCE_PREV_VERSION, UNCERTIFIED_VERSION);
205
206         final ResourceAdminEvent createResourceAudit = new ResourceAdminEvent();
207         createResourceAudit.setModifier("Carlos Santana(cs0008)");
208         createResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
209         createResourceAudit.setCurrVersion("0.1");
210         createResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
211         createResourceAudit.setRequestId("3e65cea1-7403-4bc7-b461-e2544d83799f");
212         createResourceAudit.setDesc("OK");
213         createResourceAudit.setResourceType("Resource");
214         createResourceAudit.setStatus("201");
215         createResourceAudit.setPrevVersion("");
216         createResourceAudit.setAction("Create");
217         createResourceAudit.setPrevState("");
218         createResourceAudit.setResourceName("MyTestResource");
219
220         final ResourceAdminEvent checkInResourceAudit = new ResourceAdminEvent();
221         checkInResourceAudit.setModifier("Carlos Santana(cs0008)");
222         checkInResourceAudit.setCurrState("NOT_CERTIFIED_CHECKIN");
223         checkInResourceAudit.setCurrVersion("0.1");
224         checkInResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
225         checkInResourceAudit.setRequestId("ffacbf5d-eeb1-43c6-a310-37fe7e1cc091");
226         checkInResourceAudit.setDesc("OK");
227         checkInResourceAudit.setComment("Stam");
228         checkInResourceAudit.setResourceType("Resource");
229         checkInResourceAudit.setStatus("200");
230         checkInResourceAudit.setPrevVersion("0.1");
231         checkInResourceAudit.setAction("Checkin");
232         checkInResourceAudit.setPrevState("NOT_CERTIFIED_CHECKOUT");
233         checkInResourceAudit.setResourceName("MyTestResource");
234
235         final ResourceAdminEvent checkOutResourceAudit = new ResourceAdminEvent();
236         checkOutResourceAudit.setModifier("Carlos Santana(cs0008)");
237         checkOutResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
238         checkOutResourceAudit.setCurrVersion("0.2");
239         checkOutResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
240         checkOutResourceAudit.setRequestId("7add5078-4c16-4d74-9691-cc150e3c96b8");
241         checkOutResourceAudit.setDesc("OK");
242         checkOutResourceAudit.setComment("");
243         checkOutResourceAudit.setResourceType("Resource");
244         checkOutResourceAudit.setStatus("200");
245         checkOutResourceAudit.setPrevVersion("0.1");
246         checkOutResourceAudit.setAction("Checkout");
247         checkOutResourceAudit.setPrevState("NOT_CERTIFIED_CHECKIN");
248         checkOutResourceAudit.setResourceName("MyTestResource");
249         List<ResourceAdminEvent> list = new ArrayList<ResourceAdminEvent>() {
250             {
251                 add(createResourceAudit);
252                 add(checkInResourceAudit);
253                 add(checkOutResourceAudit);
254             }
255         };
256         Either<List<ResourceAdminEvent>, ActionStatus> result = Either.left(list);
257         Mockito.when(auditingDao.getByServiceInstanceId(Mockito.anyString())).thenReturn(result);
258
259         List<ResourceAdminEvent> listPrev = new ArrayList<>();
260         Either<List<ResourceAdminEvent>, ActionStatus> resultPrev = Either.left(listPrev);
261         Mockito.when(auditingDao.getAuditByServiceIdAndPrevVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultPrev);
262
263         List<ResourceAdminEvent> listCurr = new ArrayList<ResourceAdminEvent>() {
264             {
265                 add(checkOutResourceAudit);
266             }
267         };
268         Either<List<ResourceAdminEvent>, ActionStatus> resultCurr = Either.left(listCurr);
269         Mockito.when(auditingDao.getAuditByServiceIdAndCurrVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultCurr);
270     }
271
272     protected Service createServiceObject(boolean afterCreate) {
273         Service service = new Service();
274         service.setName("Service");
275         CategoryDefinition category = new CategoryDefinition();
276         category.setName(SERVICE_CATEGORY);
277         List<CategoryDefinition> categories = new ArrayList<>();
278         categories.add(category);
279         service.setCategories(categories);
280
281         service.setDescription("description");
282         List<String> tgs = new ArrayList<>();
283         tgs.add(service.getName());
284         service.setTags(tgs);
285         service.setIcon("MyIcon");
286         service.setContactId("aa1234");
287         service.setProjectCode("12345");
288
289         if (afterCreate) {
290             service.setVersion("0.1");
291             service.setUniqueId(service.getName() + ":" + service.getVersion());
292             service.setCreatorUserId(user.getUserId());
293             service.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
294         }
295         return service;
296     }
297
298     protected Resource setupGenericServiceMock(){
299         Resource genericService = new Resource();
300         genericService.setVersion("1.0");
301         genericService.setToscaResourceName(GENERIC_SERVICE_NAME);
302         return genericService;
303     }
304
305     protected static ForwardingPathDataDefinition forwardingPathDataDefinition;
306
307     protected static ForwardingPathDataDefinition createMockPath() {
308         if (forwardingPathDataDefinition != null){
309             return forwardingPathDataDefinition ;
310         }
311         forwardingPathDataDefinition = new ForwardingPathDataDefinition("Yoyo");
312         forwardingPathDataDefinition.setUniqueId(java.util.UUID.randomUUID().toString());
313         forwardingPathDataDefinition.setDestinationPortNumber("414155");
314         forwardingPathDataDefinition.setProtocol("http");
315         org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition> forwardingPathElementDataDefinitionListDataDefinition = new org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<>();
316         forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("fromNode","toNode", "333","444","2222","5555"));
317         forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("toNode","toNode2", "4444","44444","4","44"));
318         forwardingPathDataDefinition.setPathElements(forwardingPathElementDataDefinitionListDataDefinition);
319         return forwardingPathDataDefinition;
320     }
321 }