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