[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci-dev / src / main / java / org / openecomp / sdc / uici / tests / verificator / VfVerificator.java
1 package org.openecomp.sdc.uici.tests.verificator;
2
3 import static org.testng.AssertJUnit.assertTrue;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.function.Function;
8 import java.util.function.Supplier;
9 import java.util.stream.Collectors;
10
11 import org.apache.commons.lang3.tuple.ImmutablePair;
12 import org.apache.http.HttpStatus;
13 import org.json.simple.JSONArray;
14 import org.json.simple.JSONObject;
15 import org.json.simple.JSONValue;
16 import org.openecomp.sdc.uici.tests.utilities.ResourceUIUtils;
17 import org.openecomp.sdc.uici.tests.utilities.RestCDUtils;
18
19 import org.openecomp.sdc.be.model.LifecycleStateEnum;
20 import org.openecomp.sdc.be.model.Resource;
21 import org.openecomp.sdc.be.model.User;
22 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
23 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
24 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
25 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
26 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
27 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
28
29 /**
30  * Class to hold Test Verifications relevant for VF
31  * 
32  * @author mshitrit
33  *
34  */
35 public final class VfVerificator {
36         private VfVerificator() {
37                 throw new UnsupportedOperationException();
38         }
39
40         /**
41          * Verifies that the resource contains a certain number of component
42          * instances
43          * 
44          * @param createResourceInUI
45          * @param numOfVFC
46          */
47         public static void verifyNumOfComponentInstances(ResourceReqDetails createResourceInUI, int numOfVFC) {
48                 Supplier<Boolean> verificator = () -> {
49                         String responseAfterDrag = RestCDUtils.getResource(createResourceInUI).getResponse();
50                         JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
51                         int size = ((JSONArray) jsonResource.get("componentInstances")).size();
52                         return size == numOfVFC;
53                 };
54                 VerificatorUtil.verifyWithRetry(verificator);
55         }
56
57         /**
58          * Verifies That the createResourceInUI is different that prevRIPos.
59          * 
60          * @param createResourceInUI
61          * @param prevRIPos
62          * @param user
63          */
64         public static void verifyRILocationChanged(ResourceReqDetails createResourceInUI,
65                         ImmutablePair<String, String> prevRIPos, User user) {
66                 Supplier<Boolean> verificator = () -> {
67                         ImmutablePair<String, String> currRIPos = ResourceUIUtils.getRIPosition(createResourceInUI, user);
68                         final boolean isXLocationChanged = !prevRIPos.left.equals(currRIPos.left);
69                         final boolean isYLocationChange = !prevRIPos.right.equals(currRIPos.right);
70                         return isXLocationChanged || isYLocationChange;
71                 };
72                 VerificatorUtil.verifyWithRetry(verificator);
73         }
74
75         /**
76          * Verifies That resource contains two connected instances
77          * 
78          * @param createResourceInUI
79          */
80         public static void verifyLinkCreated(ResourceReqDetails createResourceInUI) {
81                 Supplier<Boolean> verificator = () -> {
82                         String responseAfterDrag = RestCDUtils.getResource(createResourceInUI).getResponse();
83                         JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
84                         return ((JSONArray) jsonResource.get("componentInstancesRelations")).size() == 1;
85                 };
86                 VerificatorUtil.verifyWithRetry(verificator);
87
88         }
89
90         /**
91          * Verifies That the VF is certified to version 1.0
92          * 
93          * @param vfToVerify
94          */
95         public static void verifyResourceIsCertified(ResourceReqDetails vfToVerify) {
96                 RestResponse certifiedResourceResopnse = RestCDUtils
97                                 .getResourceByNameAndVersionRetryOnFail(UserRoleEnum.ADMIN.getUserId(), vfToVerify.getName(), "1.0");
98                 assertTrue(certifiedResourceResopnse.getErrorCode().equals(HttpStatus.SC_OK));
99
100         }
101
102         /**
103          * Verifies That the VF exist
104          * 
105          * @param vfToVerify
106          */
107         public static void verifyResourceIsCreated(ResourceReqDetails vfToVerify) {
108                 assertTrue(RestCDUtils.getResource(vfToVerify).getErrorCode() == HttpStatus.SC_OK);
109         }
110
111         /**
112          * Verify the resource contains the deployment artifacts in the list
113          * 
114          * @param vfToVerify
115          * @param artifactTypeEnums
116          */
117         public static void verifyResourceContainsDeploymentArtifacts(ResourceReqDetails vfToVerify,
118                         List<ArtifactTypeEnum> artifactTypeEnums) {
119                 String resourceString = RestCDUtils.getResource(vfToVerify).getResponse();
120                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
121                 List<String> foundArtifacts = new ArrayList<>();
122                 if (resource.getDeploymentArtifacts() != null) {
123                         foundArtifacts = resource.getDeploymentArtifacts().values().stream()
124                                         .map(artifact -> artifact.getArtifactType()).collect(Collectors.toList());
125                 }
126                 List<String> excpectedArtifacts = artifactTypeEnums.stream().map(e -> e.getType()).collect(Collectors.toList());
127                 assertTrue(foundArtifacts.containsAll(excpectedArtifacts));
128
129         }
130
131         /**
132          * Verifies The life cycle State of the resource
133          * 
134          * @param createResourceInUI
135          * @param requestedLifeCycleState
136          */
137         public static void verifyState(ResourceReqDetails createResourceInUI, LifecycleStateEnum requestedLifeCycleState) {
138                 Resource resource = ResourceUIUtils.waitForState(createResourceInUI, requestedLifeCycleState);
139                 assertTrue(resource.getLifecycleState() == requestedLifeCycleState);
140
141         }
142
143 }