summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/api/tests/composition/CompositionControllerApiTests.java
blob: a6e1525bacd8cde914c7b5d9eac79f316e7b0604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.onap.dcae.ci.api.tests.composition;

import com.aventstack.extentreports.Status;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.json.simple.parser.ParseException;
import org.onap.dcae.ci.api.tests.DcaeRestBaseTest;
import org.onap.dcae.ci.entities.RestResponse;
import org.onap.dcae.ci.utilities.DcaeRestClient;
import org.onap.dcae.ci.utilities.DcaeUtil;
import org.onap.dcae.ci.report.Report;
import org.onap.sdc.dcae.composition.services.Artifact;
import org.onap.sdc.dcae.composition.services.Resource;
import org.onap.sdc.dcae.composition.services.Service;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.composition.vfcmt.Vfcmt;

import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class CompositionControllerApiTests extends DcaeRestBaseTest {

    private static final String EVENT_PROC_BP_YAML = "event_proc_bp.yaml";
    private static final String CREATE_BLUEPRINT_DESCRIPTION = "creating new artifact blueprint on the service vfi";
    private static final String UPDATE_BLUEPRINT_DESCRIPTION = "updating artifact blueprint on the service vfi";
    private static final String VFCMT_NAME = "teSt.__.monitoring---TempLATE.";
    private static final String NORMALIZED_VFCMT_NAME = "TestMonitoringTemplate";

    @Test
	public void createMultipleBlueprintsTest() throws Exception {
    	Report.log(Status.INFO, "createMultipleBlueprintsTest start");
        String randomSuffix = WordUtils.capitalize(RandomStringUtils.randomAlphanumeric(4).toLowerCase());
        Vfcmt newVfcmt = createVfcmt(randomSuffix);

        // create a service in sdc
        Service newService = createServiceWithVFiAsSdcDesigner();

        String serviceUuid = newService.getUuid();
        String vfiName = newService.getResources().get(0).getResourceInstanceName();
        String vfcmtId = newVfcmt.getUuid();

        // attach VFCMT to vfi
        Report.log(Status.INFO, "Trying to attach the service+vfi to the vfcmt. serviceUuid="+serviceUuid+", vfiName="+vfiName+", vfcmtId="+vfcmtId);
        RestResponse res = DcaeRestClient.attachVfiRef(vfcmtId, serviceUuid, vfiName);
        assertThat(res.getStatusCode()).isEqualTo(200);

        //generate and save composition.yml(cdump)
        generateAndSaveCdump(vfcmtId);

        String bpType1 = "bpType t1";
        String bpType2 = "bpType T2";
        String normalizedVfcmtName = NORMALIZED_VFCMT_NAME + randomSuffix;
        List<ImmutablePair<String, String>> expectedBpDetails = Arrays.asList(buildBlueprintDetails(bpType1, normalizedVfcmtName), buildBlueprintDetails(bpType2, normalizedVfcmtName));

        // submit - generate and save type1 blueprint artifact
        Report.log(Status.INFO, "Going to Submit the composition with vfcmtId="+vfcmtId+", serviceUuid="+serviceUuid+", vfiName="+vfiName+", TYPE1="+bpType1);
        res = DcaeRestClient.submitComposition(vfcmtId, serviceUuid, vfiName, bpType1);
        assertBlueprintSuccessResponse(res);

        // 1806 US 374593 assert vfcmt is certified
        res = DcaeRestClient.getResourceDetails(vfcmtId);
        newVfcmt = gson.fromJson(res.getResponse(), Vfcmt.class);
        Report.log(Status.INFO, "Checking lifecycle state of vfcmt after first blueprint submission. State = {}", newVfcmt.getLifecycleState());
        assertThat(newVfcmt.getLifecycleState()).isEqualTo(DcaeBeConstants.LifecycleStateEnum.CERTIFIED.name());
        Report.log(Status.INFO, "Checking version of vfcmt after first blueprint submission. Version = {}", newVfcmt.getVersion());
        assertThat(newVfcmt.getVersion()).isEqualTo("1.0");

        res = DcaeRestClient.checkoutVfcmt(vfcmtId, newVfcmt.getLastUpdaterUserId());
        newVfcmt = gson.fromJson(res.getResponse(), Vfcmt.class);
        vfcmtId = newVfcmt.getUuid();
        // submit - generate and save type2 blueprint artifact
        Report.log(Status.INFO, "Going to Submit the composition with vfcmtId="+vfcmtId+", serviceUuid="+serviceUuid+", vfiName="+vfiName+", TYPE2="+bpType2);
        res = DcaeRestClient.submitComposition(vfcmtId,  serviceUuid, vfiName, bpType2);
        assertBlueprintSuccessResponse(res);
        res = DcaeRestClient.getResourceDetails(vfcmtId);
        newVfcmt = gson.fromJson(res.getResponse(), Vfcmt.class);
        Report.log(Status.INFO, "Checking lifecycle state of vfcmt after checkout and second blueprint submission. State = {}", newVfcmt.getLifecycleState());
        assertThat(newVfcmt.getLifecycleState()).isEqualTo(DcaeBeConstants.LifecycleStateEnum.CERTIFIED.name());
        Report.log(Status.INFO, "Checking version of vfcmt after checkout and second blueprint submission. Version = {}", newVfcmt.getVersion());
        assertThat(newVfcmt.getVersion()).isEqualTo("2.0");
        assertBlueprintsExistOnVfiAfterUpload(expectedBpDetails, serviceUuid, vfiName);
    }


    @Test
    public void updateBlueprintTest() throws Exception {
    	Report.log(Status.INFO, "updateBlueprintTest start");
        String randomSuffix = WordUtils.capitalize(RandomStringUtils.randomAlphanumeric(4).toLowerCase());
        Vfcmt newVfcmt = createVfcmt(randomSuffix);

        // create a service in sdc
        Service newService = createServiceWithVFiAsSdcDesigner();

        String serviceUuid = newService.getUuid();
        String vfiName = newService.getResources().get(0).getResourceInstanceName();
        String vfcmtId = newVfcmt.getUuid();

        // attach VFCMT to vfi
        RestResponse res = DcaeRestClient.attachVfiRef(vfcmtId, serviceUuid, vfiName);
        assertThat(res.getStatusCode()).isEqualTo(200);

        generateAndSaveCdump(vfcmtId);

        String normalizedVfcmtName = NORMALIZED_VFCMT_NAME + randomSuffix;
        String bpType1 = "bpType 1";
        ImmutablePair<String, String> expectedBpDetails = buildBlueprintDetails(bpType1, normalizedVfcmtName);

        // submit - generate and save type1 blueprint artifact
        Report.log(Status.INFO, "Going to Submit the composition with vfcmtId="+vfcmtId+", serviceUuid="+serviceUuid+", vfiName="+vfiName+", bpType1="+bpType1);
        res = DcaeRestClient.submitComposition(vfcmtId, serviceUuid, vfiName, bpType1);
        assertBlueprintSuccessResponse(res);
        // assert initial version exists
        assertBlueprintExistsOnVfi(expectedBpDetails, serviceUuid, vfiName, CREATE_BLUEPRINT_DESCRIPTION);

        // submit - regenerate and save type1 blueprint artifact
        Report.log(Status.INFO, "Going to Submit the composition with vfcmtId="+vfcmtId+", serviceUuid="+serviceUuid+", vfiName="+vfiName+", bpType1="+bpType1);
        res = DcaeRestClient.submitComposition(vfcmtId, serviceUuid, vfiName, bpType1);
        assertBlueprintSuccessResponse(res);
        // assert blueprint updated
        assertBlueprintExistsOnVfi(expectedBpDetails, serviceUuid, vfiName, UPDATE_BLUEPRINT_DESCRIPTION);

    }


    private void assertBlueprintSuccessResponse(RestResponse response){
        assertThat(response.getStatusCode()).isEqualTo(200);
        assertThat(DcaeUtil.getValueFromJsonResponse(response.getResponse(), "successResponse")).isNotNull();
    }

    private void assertBlueprintsExistOnVfiAfterUpload(List<ImmutablePair<String, String>> expectedBpDetails, String serviceId, String vfiName) throws IOException{
       List<Artifact> vfiArtifact = fetchVfiArtifacts(serviceId, vfiName);
       List<ImmutablePair<String, String>> vfiArtifactDetails = vfiArtifact.stream()
                                                        .map(a -> new ImmutablePair<>(a.getArtifactLabel(), a.getArtifactName()))
                                                        .collect(Collectors.toList());
       for(ImmutablePair<String,String> bp : expectedBpDetails){
           assertThat(vfiArtifactDetails).contains(bp);
       }
    }

    private void assertBlueprintExistsOnVfi(ImmutablePair<String, String> expectedBpDetails, String serviceId, String vfiName, String description) throws IOException{
        List<Artifact> vfiArtifact = fetchVfiArtifacts(serviceId, vfiName);
        Predicate<Artifact> artifactPredicate = p -> expectedBpDetails.getLeft().equals(p.getArtifactLabel()) && expectedBpDetails.getRight().equals(p.getArtifactName()) && description.equals(p.getArtifactDescription());
        Artifact artifact = vfiArtifact.stream()
                .filter(artifactPredicate)
                .findAny()
                .orElse(null);
        assertThat(artifact).isNotNull();
    }


    private List<Artifact> fetchVfiArtifacts(String serviceId, String vfiName) throws IOException {
        Report.log(Status.INFO, "fetchVfiArtifacts start");
        RestResponse serviceRes = DcaeRestClient.getServicesInstance(serviceId);
        Report.log(Status.INFO, "fetchVfiArtifacts response=%s", serviceRes);
        Service service = gson.fromJson(serviceRes.getResponse(), Service.class);
        Resource vfi = service.getResources().stream()
                .filter(p -> p.getResourceInstanceName().equals(vfiName))
                .findAny()
                .orElse(null);
        assertThat(vfi).isNotNull();
        return vfi.getArtifacts();
    }

    private ImmutablePair<String, String> buildBlueprintDetails(String bpType, String normalizedVfcmtName) {
        return new ImmutablePair<>("blueprint"+(bpType.replaceAll(" ", "")+normalizedVfcmtName).toLowerCase(),bpType.replaceAll(" ", "-")+"."+normalizedVfcmtName+"."+EVENT_PROC_BP_YAML);
    }


    private Vfcmt createVfcmt(String randomSuffix) throws IOException {
        String newName = VFCMT_NAME + randomSuffix;
        Report.log(Status.DEBUG, "createVfcmt start. VFCMT Name="+newName);
        RestResponse res = DcaeRestClient.createVfcmt(newName,"description");
        Report.log(Status.DEBUG, "Create VFCMT response=%s", res);
        assertThat(res.getStatusCode()).isEqualTo(200);
        return gson.fromJson(res.getResponse(), Vfcmt.class);
    }

    private void generateAndSaveCdump(String vfcmtId) throws ParseException, IOException {
    	Report.log(Status.DEBUG, "generateAndSaveCdump start");
        RestResponse res = DcaeUtil.SdcElementsModelType.getItemModelFromSdc(0);
        assertThat(res.getStatusCode()).isEqualTo(200);
        JsonObject cdump = DcaeRestClient.generateCdumpInput(vfcmtId);
        Report.log(Status.DEBUG, "Generated cdump="+cdump);
        cdump.add("nodes", DcaeUtil.parseNodesFromDcaeModelAndAssignUiNid(res.getResponse()));
        Report.log(Status.DEBUG, "save composition - generate and save composition.yml artifact");
        res = DcaeRestClient.saveComposition(vfcmtId, DcaeRestClient.getDefaultUserId(), cdump.toString());
        Report.log(Status.DEBUG, "Save composition result="+res);
        assertThat(res.getStatusCode()).isEqualTo(200);
    }


}