Final commit to master merge from
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / lifecycle / LifecycleTestBase.java
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.components.lifecycle;
22
23 import static org.mockito.Mockito.when;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.servlet.ServletContext;
29
30 import org.junit.BeforeClass;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mockito;
33 import org.mockito.invocation.InvocationOnMock;
34 import org.mockito.stubbing.Answer;
35 import org.openecomp.sdc.AuditingMockManager;
36 import org.openecomp.sdc.be.auditing.api.IAuditingManager;
37 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
39 import org.openecomp.sdc.be.config.ConfigurationManager;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
42 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
43 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
44 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
45 import org.openecomp.sdc.be.model.ArtifactDefinition;
46 import org.openecomp.sdc.be.model.Component;
47 import org.openecomp.sdc.be.model.ComponentMetadataDefinition;
48 import org.openecomp.sdc.be.model.LifecycleStateEnum;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.Service;
51 import org.openecomp.sdc.be.model.User;
52 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
53 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
54 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
55 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
56 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
59 import org.openecomp.sdc.be.user.Role;
60 import org.openecomp.sdc.be.user.UserBusinessLogic;
61 import org.openecomp.sdc.common.api.ConfigurationSource;
62 import org.openecomp.sdc.common.api.Constants;
63 import org.openecomp.sdc.common.impl.ExternalConfiguration;
64 import org.openecomp.sdc.common.impl.FSConfigurationSource;
65 import org.openecomp.sdc.exception.ResponseFormat;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68 import org.springframework.web.context.WebApplicationContext;
69
70 import fj.data.Either;
71
72 public class LifecycleTestBase {
73         private static Logger log = LoggerFactory.getLogger(LifecycleTestBase.class.getName());
74         @InjectMocks
75         protected final ServletContext servletContext = Mockito.mock(ServletContext.class);
76         protected IAuditingManager iAuditingManager = null;
77         protected UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
78         protected WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
79         protected WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
80         protected ToscaElementLifecycleOperation toscaElementLifecycleOperation = Mockito.mock(ToscaElementLifecycleOperation.class);
81         protected ArtifactsBusinessLogic artifactsManager = Mockito.mock(ArtifactsBusinessLogic.class);;
82         protected User user = null;
83         protected Resource resourceResponse;
84         protected Service serviceResponse;
85         protected static ConfigurationManager configurationManager = null;
86         protected ResponseFormatManager responseManager = null;
87         protected TitanDao titanDao = Mockito.mock(TitanDao.class);
88         protected ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
89
90         @BeforeClass
91         public static void setupClass() {
92                 ExternalConfiguration.setAppName("catalog-be");
93
94                 // Init Configuration
95                 String appConfigDir = "src/test/resources/config/catalog-be";
96                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
97                 configurationManager = new ConfigurationManager(configurationSource);
98         }
99
100         public void setup() {
101
102                 // Auditing
103                 iAuditingManager = new AuditingMockManager("lll");
104
105                 // User data and management
106                 user = new User();
107                 user.setUserId("jh003");
108                 user.setFirstName("Jimmi");
109                 user.setLastName("Hendrix");
110                 user.setRole(Role.ADMIN.name());
111
112                 Either<User, ActionStatus> eitherGetUser = Either.left(user);
113                 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
114
115                 // Servlet Context attributes
116                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
117                 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
118                 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
119                 when(webAppContext.getBean(ToscaElementLifecycleOperation.class)).thenReturn(toscaElementLifecycleOperation);
120                 when(webAppContext.getBean(ArtifactsBusinessLogic.class)).thenReturn(artifactsManager);
121
122                 // Resource Operation mock methods
123                 // getCount
124
125                 // createResource
126                 resourceResponse = createResourceObject();
127                 Either<ToscaElement, StorageOperationStatus> eitherComponent = Either.left(ModelConverter.convertToToscaElement(resourceResponse));
128                 when(toscaElementLifecycleOperation.checkoutToscaElement(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class)))
129                                 .thenAnswer(createAnswer(eitherComponent));
130                 
131                 when(toscaElementLifecycleOperation.checkinToscaELement(Mockito.any(LifecycleStateEnum.class), Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class)))
132                                 .thenAnswer(createAnswer(eitherComponent));
133
134                 when(toscaElementLifecycleOperation.requestCertificationToscaElement(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class)))
135                                 .thenAnswer(createAnswer(eitherComponent));
136
137                 Either<User, StorageOperationStatus> getOwnerResult = Either.left(user);
138                 when(toscaElementLifecycleOperation.getToscaElementOwner(Mockito.anyString())).thenReturn(getOwnerResult);
139                 
140                 Either<Component, StorageOperationStatus> eitherlatestDerived = Either.right(StorageOperationStatus.OK);
141                 when(toscaOperationFacade.shouldUpgradeToLatestDerived(Mockito.any(Resource.class))).thenReturn(eitherlatestDerived);
142                 
143                 responseManager = ResponseFormatManager.getInstance();
144
145         }
146
147         public static <T> Answer<T> createAnswer(final T value) {
148                 Answer<T> dummy = new Answer<T>() {
149                         @Override
150                         public T answer(InvocationOnMock invocation) throws Throwable {
151                                 return value;
152                         }
153
154                 };
155                 return dummy;
156         }
157
158         protected Resource createResourceObject() {
159                 Resource resource = new Resource();
160                 resource.setName("MyResourceName");
161                 resource.setUniqueId("uid");
162                 resource.addCategory("VoIP", "INfra");
163                 resource.setDescription("My short description");
164                 List<String> tgs = new ArrayList<String>();
165                 tgs.add("test");
166                 resource.setTags(tgs);
167                 List<String> template = new ArrayList<String>();
168                 template.add("Root");
169                 resource.setDerivedFrom(template);
170                 resource.setVendorName("Motorola");
171                 resource.setVendorRelease("1.0.0");
172                 resource.setContactId("yavivi");
173                 resource.setIcon("MyIcon.jpg");
174                 resource.setToscaType(ToscaElementTypeEnum.NodeType.getValue());
175                 
176                 return resource;
177         }
178         
179         protected Resource createResourceVFCMTObject() {
180                 ResourceMetadataDataDefinition rMetadataDataDefinition = new ResourceMetadataDataDefinition();
181                 rMetadataDataDefinition.setResourceType(ResourceTypeEnum.VFCMT);
182                 ComponentMetadataDefinition cMetadataDataDefinition = new ComponentMetadataDefinition(rMetadataDataDefinition) ;
183                 
184                 Resource resource = new Resource(cMetadataDataDefinition);
185                 resource.setUniqueId("rid");
186                 resource.setName("MyResourceVFCMTName");
187                 resource.addCategory("VoIP", "INfra");
188                 resource.setDescription("My short description");
189                 List<String> tgs = new ArrayList<String>();
190                 tgs.add("test1");
191                 resource.setTags(tgs);
192                 List<String> template = new ArrayList<String>();
193                 template.add("Root");
194                 resource.setDerivedFrom(template);
195                 resource.setVendorName("Motorola");
196                 resource.setVendorRelease("1.0.0");
197                 resource.setContactId("yavivi");
198                 resource.setIcon("MyIcon.jpg");
199                 resource.setToscaType(ToscaElementTypeEnum.NodeType.getValue());
200                                 
201                 return resource;
202         }
203
204         protected Service createServiceObject(boolean b) {
205                 Service service = new Service();
206                 service.setName("MyServiceName");
207                 service.setUniqueId("sid");
208                 service.addCategory("VoIP", null);
209                 service.setDescription("My short description");
210                 List<String> tgs = new ArrayList<String>();
211                 tgs.add("test");
212                 service.setTags(tgs);
213                 List<String> template = new ArrayList<String>();
214                 template.add("Root");
215                 service.setContactId("aa0001");
216                 service.setIcon("MyIcon.jpg");
217
218                 return service;
219         }
220
221         protected void assertResponse(Either<? extends Component, ResponseFormat> createResponse, ActionStatus expectedStatus, String... variables) {
222                 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
223                 ResponseFormat actualResponse = createResponse.right().value();
224         }
225
226         protected void assertServiceResponse(Either<Service, ResponseFormat> createResponse, ActionStatus expectedStatus, String... variables) {
227                 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
228                 ResponseFormat actualResponse = createResponse.right().value();
229         }
230
231         protected static ArtifactDefinition getArtifactPlaceHolder(String resourceId, String logicalName) {
232                 ArtifactDefinition artifact = new ArtifactDefinition();
233
234                 artifact.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, logicalName.toLowerCase()));
235                 artifact.setArtifactLabel(logicalName.toLowerCase());
236
237                 return artifact;
238         }
239 }