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