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