Initial OpenECOMP SDC commit
[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.mockito.InjectMocks;
31 import org.mockito.Mockito;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.openecomp.sdc.AuditingMockManager;
35 import org.openecomp.sdc.be.auditing.api.IAuditingManager;
36 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
42 import org.openecomp.sdc.be.model.ArtifactDefinition;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.Service;
46 import org.openecomp.sdc.be.model.User;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.LifecycleOperation;
49 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
50 import org.openecomp.sdc.be.user.Role;
51 import org.openecomp.sdc.be.user.UserBusinessLogic;
52 import org.openecomp.sdc.common.api.ConfigurationSource;
53 import org.openecomp.sdc.common.api.Constants;
54 import org.openecomp.sdc.common.impl.ExternalConfiguration;
55 import org.openecomp.sdc.common.impl.FSConfigurationSource;
56 import org.openecomp.sdc.exception.ResponseFormat;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.web.context.WebApplicationContext;
60
61 import com.google.gson.Gson;
62 import com.google.gson.GsonBuilder;
63
64 import fj.data.Either;
65 import junit.framework.Assert;
66
67 public class LifecycleTestBase {
68         private static Logger log = LoggerFactory.getLogger(LifecycleTestBase.class.getName());
69         @InjectMocks
70         protected final ServletContext servletContext = Mockito.mock(ServletContext.class);
71         protected IAuditingManager iAuditingManager = null;
72         protected UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
73         protected WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
74         protected WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
75         protected LifecycleOperation lcOperation = Mockito.mock(LifecycleOperation.class);
76         protected ArtifactsBusinessLogic artifactsManager = Mockito.mock(ArtifactsBusinessLogic.class);;
77         protected User user = null;
78         protected Resource resourceResponse;
79         protected Service serviceResponse;
80         protected ConfigurationManager configurationManager = null;
81         protected ResponseFormatManager responseManager = null;
82
83         public void setup() {
84
85                 ExternalConfiguration.setAppName("catalog-be");
86
87                 // Init Configuration
88                 String appConfigDir = "src/test/resources/config/catalog-be";
89                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
90                 configurationManager = new ConfigurationManager(configurationSource);
91
92                 // Auditing
93                 iAuditingManager = new AuditingMockManager("lll");
94
95                 // User data and management
96                 user = new User();
97                 user.setUserId("jh003");
98                 user.setFirstName("Jimmi");
99                 user.setLastName("Hendrix");
100                 user.setRole(Role.ADMIN.name());
101
102                 Either<User, ActionStatus> eitherGetUser = Either.left(user);
103                 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
104
105                 // Servlet Context attributes
106                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
107                 // when(servletContext.getAttribute(Constants.AUDITING_MANAGER)).thenReturn(iAuditingManager);
108                 // when(servletContext.getAttribute(Constants.RESOURCE_OPERATION_MANAGER)).thenReturn(resourceOperation);
109                 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
110                 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
111                 when(webAppContext.getBean(LifecycleOperation.class)).thenReturn(lcOperation);
112                 when(webAppContext.getBean(ArtifactsBusinessLogic.class)).thenReturn(artifactsManager);
113
114                 // Resource Operation mock methods
115                 // getCount
116
117                 // createResource
118                 resourceResponse = createResourceObject(true);
119                 serviceResponse = createServiceObject(true);
120                 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
121                 Either<? extends Component, StorageOperationStatus> eitherCreateComponent = Either.left(resourceResponse);
122                 when((Either<Resource, StorageOperationStatus>) lcOperation.checkoutComponent(Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Resource.class), Mockito.any(User.class), Mockito.any(User.class), Mockito.any(Boolean.class)))
123                                 .thenAnswer(createAnswer(eitherCreate));
124                 when(lcOperation.checkoutComponent(Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Resource.class), Mockito.any(User.class), Mockito.any(User.class), Mockito.any(Boolean.class))).thenAnswer(createAnswer(eitherCreateComponent));
125
126                 when((Either<Resource, StorageOperationStatus>) lcOperation.checkinComponent(Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Resource.class), Mockito.any(User.class), Mockito.any(User.class), Mockito.any(Boolean.class)))
127                                 .thenAnswer(createAnswer(eitherCreate));
128
129                 when((Either<Resource, StorageOperationStatus>) lcOperation.requestCertificationComponent(Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Resource.class), Mockito.any(User.class), Mockito.any(User.class), Mockito.any(Boolean.class)))
130                                 .thenAnswer(createAnswer(eitherCreate));
131                 when(lcOperation.requestCertificationComponent(Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Component.class), Mockito.any(User.class), Mockito.any(User.class), Mockito.any(Boolean.class))).thenAnswer(createAnswer(eitherCreateComponent));
132
133                 Either<User, StorageOperationStatus> getOwnerResult = Either.left(user);
134                 when(lcOperation.getComponentOwner(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Resource), Mockito.any(Boolean.class))).thenReturn(getOwnerResult);
135                 when(lcOperation.getComponentOwner(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service), Mockito.any(Boolean.class))).thenReturn(getOwnerResult);
136
137                 responseManager = ResponseFormatManager.getInstance();
138
139         }
140
141         public static <T> Answer<T> createAnswer(final T value) {
142                 Answer<T> dummy = new Answer<T>() {
143                         @Override
144                         public T answer(InvocationOnMock invocation) throws Throwable {
145                                 return value;
146                         }
147
148                 };
149                 return dummy;
150         }
151
152         protected Resource createResourceObject(boolean afterCreate) {
153                 Resource resource = new Resource();
154                 resource.setName("MyResourceName");
155                 resource.addCategory("VoIP", "INfra");
156                 resource.setDescription("My short description");
157                 List<String> tgs = new ArrayList<String>();
158                 tgs.add("test");
159                 resource.setTags(tgs);
160                 List<String> template = new ArrayList<String>();
161                 template.add("Root");
162                 resource.setDerivedFrom(template);
163                 resource.setVendorName("Motorola");
164                 resource.setVendorRelease("1.0.0");
165                 resource.setContactId("yavivi");
166                 resource.setIcon("MyIcon.jpg");
167
168                 Gson gson = new GsonBuilder().setPrettyPrinting().create();
169                 log.debug(gson.toJson(resource));
170                 return resource;
171         }
172
173         protected Service createServiceObject(boolean b) {
174                 Service service = new Service();
175                 service.setName("MyServiceName");
176                 service.addCategory("VoIP", null);
177                 service.setDescription("My short description");
178                 List<String> tgs = new ArrayList<String>();
179                 tgs.add("test");
180                 service.setTags(tgs);
181                 List<String> template = new ArrayList<String>();
182                 template.add("Root");
183                 service.setContactId("aa0001");
184                 service.setIcon("MyIcon.jpg");
185
186                 Gson gson = new GsonBuilder().setPrettyPrinting().create();
187                 log.debug(gson.toJson(service));
188                 return service;
189         }
190
191         protected void assertResponse(Either<? extends Component, ResponseFormat> createResponse, ActionStatus expectedStatus, String... variables) {
192                 Assert.assertTrue(createResponse.isRight());
193                 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
194                 ResponseFormat actualResponse = createResponse.right().value();
195                 Assert.assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
196                 Assert.assertEquals("assert error description", expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
197         }
198
199         protected void assertServiceResponse(Either<Service, ResponseFormat> createResponse, ActionStatus expectedStatus, String... variables) {
200                 Assert.assertTrue(createResponse.isRight());
201                 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
202                 ResponseFormat actualResponse = createResponse.right().value();
203                 Assert.assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
204                 Assert.assertEquals("assert error description", expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
205         }
206
207         protected static ArtifactDefinition getArtifactPlaceHolder(String resourceId, String logicalName) {
208                 ArtifactDefinition artifact = new ArtifactDefinition();
209
210                 artifact.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, logicalName.toLowerCase()));
211                 artifact.setArtifactLabel(logicalName.toLowerCase());
212
213                 return artifact;
214         }
215 }