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