[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / service / GetComponentAuditApiTest.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.ci.tests.execute.service;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31
32 import org.apache.http.HttpStatus;
33 import org.apache.log4j.lf5.util.ResourceUtils;
34 import org.junit.Rule;
35 import org.junit.rules.TestName;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
38 import org.openecomp.sdc.be.model.Resource;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
41 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
42 import org.openecomp.sdc.ci.tests.api.Urls;
43 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
44 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
51 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
52 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
53 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
54 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
55 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
56 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
57 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
58 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
59 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
60 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
61 import org.openecomp.sdc.common.api.Constants;
62 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
63 import org.openecomp.sdc.common.datastructure.Wrapper;
64 import org.testng.AssertJUnit;
65 import org.testng.annotations.BeforeMethod;
66 import org.testng.annotations.Test;
67
68 import com.google.gson.JsonArray;
69 import com.google.gson.JsonElement;
70 import com.google.gson.JsonObject;
71
72 import fj.data.Either;
73
74 public class GetComponentAuditApiTest extends ComponentBaseTest {
75
76         public static final String SERVICES_API = "services";
77         public static final String RESOURCES_API = "resources";
78
79         protected User sdncAdminUser;
80         protected User sdncDesignerUser;
81         protected User sdncTesterUser;
82
83         @Rule
84         public static TestName name = new TestName();
85
86         public GetComponentAuditApiTest() {
87                 super(name, GetComponentAuditApiTest.class.getName());
88         }
89
90         // in case tests fail, run this method as test to create mapping in ES
91         public void updateElasticSearchMapping() throws IOException {
92                 Either<String, Exception> fileContentUTF8 = FileUtils
93                                 .getFileContentUTF8("src\\test\\resources\\CI\\other\\mapping.json");
94                 AssertJUnit.assertTrue(fileContentUTF8.isLeft());
95
96                 final String ES_TEMPLATE_URL = "http://%s:%s/_template/audit_template";
97                 String url = String.format(ES_TEMPLATE_URL, config.getEsHost(), config.getEsPort());
98
99                 RestResponse sendHttpPost = new HttpRequest().sendHttpPost(url, fileContentUTF8.left().value(), null);
100                 AssertJUnit.assertTrue(sendHttpPost.getErrorCode() == HttpStatus.SC_OK);
101         }
102
103         @BeforeMethod
104         public void init() {
105                 sdncAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
106                 sdncDesignerUser = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
107                 sdncTesterUser = ElementFactory.getDefaultUser(UserRoleEnum.TESTER);
108                 ;
109
110         }
111
112         @Test
113         public void testServiceAuditCertifiedVersion() throws Exception {
114
115                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService();
116                 Wrapper<String> versionZeroOneIDWrapper = new Wrapper<String>(),
117                                 versionZeroTwoIDWrapper = new Wrapper<String>();
118
119                 createBasicServiceForAudit(versionZeroOneIDWrapper, versionZeroTwoIDWrapper, serviceDetails, true);
120                 // First Certification
121
122                 LifecycleRestUtils.certifyService(serviceDetails);
123                 // LCSbaseTest.certifyService(serviceDetails);
124                 AssertJUnit.assertTrue(serviceDetails.getVersion().equals("1.0"));
125
126                 // Second Certification
127                 increaseServiceVersion(serviceDetails, "1.1");
128                 increaseServiceVersion(serviceDetails, "1.2");
129                 increaseServiceVersion(serviceDetails, "1.3");
130                 increaseServiceVersion(serviceDetails, "1.4");
131                 LifecycleRestUtils.certifyService(serviceDetails);
132                 AssertJUnit.assertTrue(serviceDetails.getVersion().equals("2.0"));
133                 String certifiedId = serviceDetails.getUniqueId();
134                 LifecycleRestUtils.changeServiceState(serviceDetails, sdncDesignerUser, LifeCycleStatesEnum.CHECKOUT);
135                 LifecycleRestUtils.changeServiceState(serviceDetails, sdncDesignerUser, LifeCycleStatesEnum.CHECKIN);
136
137                 JsonElement element = getAuditJson(SERVICES_API, certifiedId);
138                 // audits kept: 5*check ins + 4*check outs + 2*artifact payload
139                 // updates(tosca) + certification request + certification start +
140                 // certification success
141                 // + 3 A&AI(ArtifactDelete, ArtifactUpload, ArtifactUpdate)
142                 List<String> actions = new ArrayList<>();
143                 JsonArray jsonArray = element.getAsJsonArray();
144                 for( int i =0 ; i < jsonArray.size(); i++){
145                         actions.add(jsonArray.get(i).getAsJsonObject().get(AuditingFieldsKeysEnum.AUDIT_ACTION.getDisplayName()).getAsString());
146                 }
147                 long checkinCount = actions.stream().filter( e -> e.equals(AuditingActionEnum.CHECKIN_RESOURCE.getName())).count();
148                 assertTrue(checkinCount == 5);
149                 
150                 long checkOutCount = actions.stream().filter( e -> e.equals(AuditingActionEnum.CHECKOUT_RESOURCE.getName())).count();
151                 assertTrue(checkOutCount == 4);
152                 
153                 long certificationRequestCount = actions.stream().filter( e -> e.equals(AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE.getName())).count();
154                 assertTrue(certificationRequestCount == 1);
155                 
156                 long certificationStartCount = actions.stream().filter( e -> e.equals(AuditingActionEnum.START_CERTIFICATION_RESOURCE.getName())).count();
157                 assertTrue(certificationStartCount == 1);
158                 
159                 long certificationSuccessCount = actions.stream().filter( e -> e.equals(AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE.getName())).count();
160                 assertTrue(certificationSuccessCount == 1);
161                 
162
163         }
164
165         protected void certifyResource(ResourceReqDetails defaultResource) throws IOException {
166                 RestResponse response = LifecycleRestUtils.changeResourceState(defaultResource, sdncDesignerUser,
167                                 LifeCycleStatesEnum.CERTIFICATIONREQUEST);
168                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
169                 response = LifecycleRestUtils.changeResourceState(defaultResource, sdncTesterUser,
170                                 LifeCycleStatesEnum.STARTCERTIFICATION);
171                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
172                 response = LifecycleRestUtils.changeResourceState(defaultResource, sdncTesterUser, LifeCycleStatesEnum.CERTIFY);
173                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
174         }
175
176         protected JsonElement getAuditJson(String componentType, String componentId) throws IOException {
177                 Map<String, String> headers = new HashMap<String, String>() {
178                         {
179                                 put(Constants.USER_ID_HEADER, UserRoleEnum.ADMIN.getUserId());
180                         }
181                 };
182                 String url = String.format(Urls.GET_COMPONENT_AUDIT_RECORDS, config.getCatalogBeHost(),
183                                 config.getCatalogBePort(), componentType, componentId);
184
185                 RestResponse httpSendGet = new HttpRequest().httpSendGet(url, headers);
186                 AssertJUnit.assertTrue(httpSendGet.getErrorCode() == HttpStatus.SC_OK);
187                 JsonElement element = ResponseParser.parseToObject(httpSendGet.getResponse(), JsonElement.class);
188                 AssertJUnit.assertTrue(element.isJsonArray());
189                 return element;
190         }
191
192         protected void createBasicServiceForAudit(Wrapper<String> versionZeroOneIDWrapper,
193                         Wrapper<String> versionZeroTwoIDWrapper, ServiceReqDetails serviceDetails, Boolean withResInst)
194                         throws Exception {
195
196                 User designerUser = sdncDesignerUser;
197
198                 RestResponse response = ServiceRestUtils.createService(serviceDetails, designerUser);
199                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_CREATED);
200                 versionZeroOneIDWrapper.setInnerElement(serviceDetails.getUniqueId());
201
202                 if (withResInst) {
203                         Resource resourceObj = AtomicOperationUtils
204                                         .createResourceByType(ResourceTypeEnum.VFC, UserRoleEnum.DESIGNER, true).left().value();
205                         AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT, resourceObj, UserRoleEnum.DESIGNER, true,
206                                         true);
207                         AtomicOperationUtils.changeComponentState(resourceObj, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY,
208                                         true);
209                         ResourceReqDetails resource = new ResourceReqDetails(resourceObj);
210                         ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory.getDefaultComponentInstance();
211                         resourceInstanceReqDetails.setComponentUid(resource.getUniqueId());
212                         ComponentInstanceRestUtils.createComponentInstance(resourceInstanceReqDetails, sdncDesignerUser,
213                                         serviceDetails.getUniqueId(), ComponentTypeEnum.SERVICE);
214
215                         // ServiceUtils.createCertResourceWithDeploymentArt(serviceDetails,
216                         // "myResource");
217                 }
218
219                 response = LifecycleRestUtils.changeServiceState(serviceDetails, designerUser, LifeCycleStatesEnum.CHECKIN);
220                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
221                 AssertJUnit.assertTrue(serviceDetails.getVersion().equals("0.1"));
222
223                 response = LifecycleRestUtils.changeServiceState(serviceDetails, designerUser, LifeCycleStatesEnum.CHECKOUT);
224                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
225                 // ServiceUtils.addServiceDeploymentArtifact(serviceDetails.getUniqueId(),
226                 // designerUser);
227                 versionZeroTwoIDWrapper.setInnerElement(serviceDetails.getUniqueId());
228                 AssertJUnit.assertTrue(serviceDetails.getVersion().equals("0.2"));
229                 response = LifecycleRestUtils.changeServiceState(serviceDetails, designerUser, LifeCycleStatesEnum.CHECKIN);
230                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
231
232                 increaseServiceVersion(serviceDetails, "0.3");
233
234                 increaseServiceVersion(serviceDetails, "0.4");
235
236                 increaseServiceVersion(serviceDetails, "0.5");
237
238         }
239
240         protected void increaseServiceVersion(ServiceReqDetails serviceDetails, String excpectedVersion) throws Exception {
241                 RestResponse response = LifecycleRestUtils.changeServiceState(serviceDetails, sdncDesignerUser,
242                                 LifeCycleStatesEnum.CHECKOUT);
243                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
244                 AssertJUnit.assertTrue(serviceDetails.getVersion().equals(excpectedVersion));
245                 response = LifecycleRestUtils.changeServiceState(serviceDetails, sdncDesignerUser, LifeCycleStatesEnum.CHECKIN);
246                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
247         }
248
249         protected void createBasicResourceForAudit(Wrapper<String> versionOnePointTwoIDWrapper,
250                         ResourceReqDetails defaultResource) throws Exception {
251
252                 RestResponse response = ResourceRestUtils.createResource(defaultResource, sdncDesignerUser);
253                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_CREATED);
254
255                 // ArtifactDefinition artifactDef = new
256                 // ArtifactUtils().constructDefaultArtifactInfo();
257                 // response = resourceUtils.add_artifact(defaultResource,
258                 // sdncDesignerUser, defaultResource.getVersion(), artifactDef);
259                 // assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
260
261                 ArtifactReqDetails heatArtifactDetails = ElementFactory
262                                 .getDefaultDeploymentArtifactForType(ArtifactTypeEnum.HEAT.getType());
263                 response = ArtifactRestUtils.addInformationalArtifactToResource(heatArtifactDetails, sdncDesignerUser,
264                                 defaultResource.getUniqueId());
265                 AssertJUnit.assertTrue("add HEAT artifact to resource request returned status:" + response.getErrorCode(),
266                                 response.getErrorCode() == 200);
267
268                 response = LifecycleRestUtils.changeResourceState(defaultResource, sdncDesignerUser,
269                                 LifeCycleStatesEnum.CHECKIN);
270
271                 increaseResourceVersion(defaultResource, "0.2");
272
273                 increaseResourceVersion(defaultResource, "0.3");
274
275                 increaseResourceVersion(defaultResource, "0.4");
276
277                 increaseResourceVersion(defaultResource, "0.5");
278
279                 certifyResource(defaultResource);
280                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
281                 AssertJUnit.assertTrue(defaultResource.getVersion().equals("1.0"));
282
283                 increaseResourceVersion(defaultResource, "1.1");
284
285                 increaseResourceVersion(defaultResource, "1.2");
286                 versionOnePointTwoIDWrapper.setInnerElement(defaultResource.getUniqueId());
287
288                 increaseResourceVersion(defaultResource, "1.3");
289
290                 increaseResourceVersion(defaultResource, "1.4");
291
292         }
293
294         protected void increaseResourceVersion(ResourceReqDetails defaultResource, String expectedVersion)
295                         throws IOException {
296                 RestResponse response = LifecycleRestUtils.changeResourceState(defaultResource, sdncDesignerUser,
297                                 LifeCycleStatesEnum.CHECKOUT);
298                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
299                 AssertJUnit.assertTrue(defaultResource.getVersion().equals(expectedVersion));
300                 response = LifecycleRestUtils.changeResourceState(defaultResource, sdncDesignerUser,
301                                 LifeCycleStatesEnum.CHECKIN);
302                 AssertJUnit.assertTrue(response.getErrorCode() == HttpStatus.SC_OK);
303         }
304
305         @Test
306         public void testServiceAuditLastUncertifiedVersion() throws Exception {
307
308                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService();
309                 Wrapper<String> versionZeroOneIDWrapper = new Wrapper<String>(),
310                                 versionZeroTwoIDWrapper = new Wrapper<String>();
311
312                 createBasicServiceForAudit(versionZeroOneIDWrapper, versionZeroTwoIDWrapper, serviceDetails, false);
313
314                 JsonElement element = getAuditJson(SERVICES_API, versionZeroTwoIDWrapper.getInnerElement());
315
316                 assertTrue(element.getAsJsonArray().size() == 3);
317
318         }
319
320         @Test
321         public void testServiceAuditFirstUncertifiedVersion() throws Exception {
322
323                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService();
324                 Wrapper<String> versionZeroOneIDWrapper = new Wrapper<String>(),
325                                 versionZeroTwoIDWrapper = new Wrapper<String>();
326
327                 createBasicServiceForAudit(versionZeroOneIDWrapper, versionZeroTwoIDWrapper, serviceDetails, false);
328
329                 JsonElement element = getAuditJson(SERVICES_API, versionZeroOneIDWrapper.getInnerElement());
330
331                 assertTrue(element.getAsJsonArray().size() == 3);
332
333         }
334
335         @Test
336         public void testResourceAuditUncertifiedVersion() throws Exception {
337
338                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResource();
339                 Wrapper<String> versionOnePointTwoIDWrapper = new Wrapper<String>();
340
341                 createBasicResourceForAudit(versionOnePointTwoIDWrapper, defaultResource);
342
343                 JsonElement element = getAuditJson(RESOURCES_API, versionOnePointTwoIDWrapper.getInnerElement());
344
345                 assertTrue(element.getAsJsonArray().size() == 3);
346
347         }
348
349         @Test
350         public void testResourceAuditCertifiedVersion() throws Exception {
351
352                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResource();
353                 Wrapper<String> versionOnePointTwoIDWrapper = new Wrapper<String>();
354
355                 createBasicResourceForAudit(versionOnePointTwoIDWrapper, defaultResource);
356
357                 certifyResource(defaultResource);
358                 assertTrue(defaultResource.getVersion().equals("2.0"));
359                 String certifiedId = defaultResource.getUniqueId();
360
361                 increaseResourceVersion(defaultResource, "2.1");
362
363                 increaseResourceVersion(defaultResource, "2.2");
364
365                 JsonElement element = getAuditJson(RESOURCES_API, certifiedId);
366
367                 assertTrue(element.getAsJsonArray().size() == 13);
368
369         }
370
371 }