summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/api/tests/composition/SaveAndSubmitCompositionFullFlowTest.java
blob: c192531a0b27b196687b510a106604fab56ba97a (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.ci.api.tests.composition;


import com.aventstack.extentreports.Status;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
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.report.Report;
import org.onap.sdc.dcae.composition.restmodels.CreateMcResponse;
import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
import org.onap.sdc.dcae.composition.restmodels.MonitoringComponent;
import org.onap.sdc.dcae.composition.restmodels.VfcmtData;
import org.onap.sdc.dcae.composition.restmodels.sdc.Resource;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.onap.sdc.dcae.composition.services.Service;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

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

public class SaveAndSubmitCompositionFullFlowTest extends DcaeRestBaseTest {

    private Resource baseTemplate = null;


    @BeforeClass
    public void setup() {
        try {
			Report.log(Status.INFO, "running before class - find a base template");
            Resource[] resources = gson.fromJson(DcaeRestClient.getAllMonitoringTemplatesVfcmts().getResponse(), Resource[].class);
            if (resources.length > 0){
                baseTemplate = resources[0];
            }
        } catch (Exception e) {
			Report.log(Status.ERROR, e);
        }
    }


    @Test
    public void saveAndSubmitCompositionSuccessTest() throws IOException {

        CreateVFCMTRequest request = new CreateVFCMTRequest();
        // If you crashed here (below) it is because your environment has no Base Monitoring Templates
        request.setTemplateUuid(baseTemplate.getUuid());
        Service service = createServiceWithVFiAsSdcDesigner();
        request.setVfiName(service.getResources().get(0).getResourceInstanceName());
        request.setServiceUuid(service.getUuid());
        DcaeRestClient.fillCreateMcRequestMandatoryFields(request);

        RestResponse response = DcaeRestClient.createMc(gson.toJson(request));
        assertThat(response.getStatusCode())
                .as("status code")
                .isEqualTo(200);

        CreateMcResponse mcResponse = gson.fromJson(response.getResponse(), CreateMcResponse.class);
		Report.log(Status.INFO, "Vfcmt created successfully. About to update composition");
        String initialUuid =  mcResponse.getVfcmt().getUuid();
        response = DcaeRestClient.saveComposition(request.getServiceUuid(), request.getVfiName(), initialUuid, gson.toJson(mcResponse.getCdump()));
        assertThat(response.getStatusCode())
                .as("status code")
                .isEqualTo(200);

        ResourceDetailed mc = gson.fromJson(response.getResponse(), ResourceDetailed.class);
        // the save action should check the mc out then in - promoting the version
        assertThat(mc.getLifecycleState()).isEqualTo(DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
        assertThat(mc.getVersion()).isEqualTo("0.2");

		Report.log(Status.INFO, "About to submit the composition");
        response = DcaeRestClient.submitComposition(request.getServiceUuid(), request.getVfiName(), mc.getUuid());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// the submit action should certify the mc.
		Report.log(Status.INFO, "Save new composition version after submit");
		response = DcaeRestClient.saveComposition(request.getServiceUuid(), request.getVfiName(), initialUuid, gson.toJson(mcResponse.getCdump()));
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// the save action should promote the mc version to 1.1 and create a new reference to it - both references should be kept at this point
		mc = gson.fromJson(response.getResponse(), ResourceDetailed.class);
		assertThat(mc.getLifecycleState()).isEqualTo(DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
		assertThat(mc.getVersion()).isEqualTo("1.1");

		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		Report.log(Status.INFO, "Verify service vfi has references to both mc versions");
		Type typeToken = new TypeToken<Map<String, List<MonitoringComponent>>>(){}.getType();
		Map<String, List<MonitoringComponent>> monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		assertThat(monitoringComponents.get("monitoringComponents").size()).isEqualTo(1);
		MonitoringComponent monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		assertThat(monitoringComponent.getSubmittedUuid()).isEqualTo(initialUuid);
		Report.log(Status.INFO, "About to re-submit the composition");
		response = DcaeRestClient.submitComposition(request.getServiceUuid(), request.getVfiName(), mc.getUuid());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// a successful submission of the new version should result in the deletion of the previous reference
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		List<MonitoringComponent> mcList = monitoringComponents.get("monitoringComponents");
		assertThat(mcList.size()).isEqualTo(1);
		assertThat(mcList.get(0).getUuid()).isEqualTo(mc.getUuid());
    }


	@Test
	public void certifiedMcCheckoutAndBindToServiceSuccessTest() throws IOException {

		CreateVFCMTRequest request = new CreateVFCMTRequest();
		// If you crashed here (below) it is because your environment has no Base Monitoring Templates
		request.setTemplateUuid(baseTemplate.getUuid());
		Service service = createServiceWithVFiAsSdcDesigner();
		request.setVfiName(service.getResources().get(0).getResourceInstanceName());
		request.setServiceUuid(service.getUuid());
		DcaeRestClient.fillCreateMcRequestMandatoryFields(request);

		RestResponse response = DcaeRestClient.createMc(gson.toJson(request));
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);

		CreateMcResponse mcResponse = gson.fromJson(response.getResponse(), CreateMcResponse.class);
		String initialUuid = mcResponse.getVfcmt().getUuid();

		response = DcaeRestClient.getLatestMcUuid(request.getContextType(), request.getServiceUuid(), request.getVfiName(), initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		VfcmtData getLatestUuidRes = gson.fromJson(response.getResponse(), VfcmtData.class);
		assertThat(initialUuid).isEqualTo(getLatestUuidRes.getUuid());

		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		Report.log(Status.INFO, "Verify service vfi only references initial mc version");
		Type typeToken = new TypeToken<Map<String, List<MonitoringComponent>>>(){}.getType();
		Map<String, List<MonitoringComponent>> monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		assertThat(monitoringComponents.get("monitoringComponents").size()).isEqualTo(1);
		MonitoringComponent monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		assertThat(monitoringComponent.getSubmittedUuid()).isNull();
		Report.log(Status.INFO, "About to submit the composition");
		response = DcaeRestClient.submitComposition(request.getServiceUuid(), request.getVfiName(), initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);

		response = DcaeRestClient.getLatestMcUuid(request.getContextType(), request.getServiceUuid(), request.getVfiName(), initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		getLatestUuidRes = gson.fromJson(response.getResponse(), VfcmtData.class);
		Report.log(Status.INFO, "Verify latest Mc uuid is not the initial one");
		assertThat(initialUuid).isNotEqualTo(getLatestUuidRes.getUuid());
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		Report.log(Status.INFO, "Verify service vfi has references to both mc versions");

		monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		assertThat(monitoringComponents.get("monitoringComponents").size()).isEqualTo(1);
		monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		assertThat(monitoringComponent.getSubmittedUuid()).isEqualTo(initialUuid);

	}

	@Test
	public void revertOverwriteRevertedAndDeleteMcTest() throws IOException {

		CreateVFCMTRequest request = new CreateVFCMTRequest();
		// If you crashed here (below) it is because your environment has no Base Monitoring Templates
		request.setTemplateUuid(baseTemplate.getUuid());
		Service service = createServiceWithVFiAsSdcDesigner();
		request.setVfiName(service.getResources().get(0).getResourceInstanceName());
		request.setServiceUuid(service.getUuid());
		DcaeRestClient.fillCreateMcRequestMandatoryFields(request);

		RestResponse response = DcaeRestClient.createMc(gson.toJson(request));
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);

		CreateMcResponse mcResponse = gson.fromJson(response.getResponse(), CreateMcResponse.class);
		Report.log(Status.INFO, "Vfcmt created successfully. About to submit composition");
		String initialUuid =  mcResponse.getVfcmt().getUuid();

		Report.log(Status.INFO, "About to submit the composition");
		response = DcaeRestClient.submitComposition(request.getServiceUuid(), request.getVfiName(), initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// the submit action should certify the mc.
		Report.log(Status.INFO, "Save new composition version after submit");
		String nid = "12313";
		response = DcaeRestClient.saveComposition(request.getServiceUuid(), request.getVfiName(), initialUuid, String.format("{\"nid\":\"%s\"}", nid));
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// the save action should promote the mc version to 1.1 and a new reference should be created - both references should be kept at this point
		ResourceDetailed mc = gson.fromJson(response.getResponse(), ResourceDetailed.class);
		assertThat(mc.getLifecycleState()).isEqualTo(DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
		assertThat(mc.getVersion()).isEqualTo("1.1");
		response = DcaeRestClient.saveRule(mc.getUuid(), "dcaeComp", nid, "configParam",  "{version:4.1,eventType:syslogFields,description:newRule,actions:[{from:{state:closed,value:fromField,regex:\"\"},target:event.commonEventHeader.target,id:id,actionType:copy}]}");
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		Report.log(Status.INFO, "Verify service vfi has references to both mc versions");
		Type typeToken = new TypeToken<Map<String, List<MonitoringComponent>>>(){}.getType();
		Map<String, List<MonitoringComponent>> monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		assertThat(monitoringComponents.get("monitoringComponents").size()).isEqualTo(1);
		MonitoringComponent monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		String revertableUuid = monitoringComponent.getUuid();
		assertThat(monitoringComponent.getSubmittedUuid()).isEqualTo(initialUuid);
		Report.log(Status.INFO, "About to revert to the submitted composition");
		response = DcaeRestClient.revertToSubmittedComposition(request.getServiceUuid(), request.getVfiName(), monitoringComponent.getUuid(), initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		// a successful revert of the new version should result in the submitted MC referencing it
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		List<MonitoringComponent> mcList = monitoringComponents.get("monitoringComponents");
		assertThat(mcList.size()).isEqualTo(1);
		monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		assertThat(monitoringComponent.getUuid()).isEqualTo(initialUuid.concat("/").concat(revertableUuid));
		// before saving the composition the reverted MC has 3 artifacts - ref, cdump and rule.
		response = DcaeRestClient.getResourceDetails(revertableUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		mc = gson.fromJson(response.getResponse(), ResourceDetailed.class);
		assertThat(mc.getArtifacts().size()).isEqualTo(3);
		// after saving the composition the reverted MC should be overwritten by the initial version and only contain 2 artifacts - ref and cdump (no rule).
		response = DcaeRestClient.saveComposition(service.getUuid(), request.getVfiName(), monitoringComponent.getUuid(), gson.toJson(mcResponse.getCdump()));
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		response = DcaeRestClient.getResourceDetails(revertableUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		mc = gson.fromJson(response.getResponse(), ResourceDetailed.class);
		assertThat(mc.getArtifacts().size()).isEqualTo(2);
		// a save rule request should now fail with the provided nid as the composition has been reset to the initial one
		response = DcaeRestClient.saveRule(mc.getUuid(), "dcaeComp", nid, "configParam",  "{version:4.1,eventType:syslogFields,description:newRule,actions:[{from:{state:closed,value:fromField,regex:\"\"},target:event.commonEventHeader.target,id:id,actionType:copy}]}");
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(400);
		assertThat(response.getResponse()).isEqualTo("{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC6114\",\"text\":\"DCAE component %1 not found in composition\",\"variables\":[\"dcaeComp\"],\"formattedErrorMessage\":\"DCAE component dcaeComp not found in composition\"}},\"notes\":\"\"}");
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		mcList = monitoringComponents.get("monitoringComponents");
		assertThat(mcList.size()).isEqualTo(1);
		monitoringComponent = monitoringComponents.get("monitoringComponents").get(0);
		assertThat(monitoringComponent.getUuid()).isEqualTo(revertableUuid);
		// revert again and then delete. table should be empty
		Report.log(Status.INFO, "About to revert to the submitted composition");
		response = DcaeRestClient.revertToSubmittedComposition(request.getServiceUuid(), request.getVfiName(), revertableUuid, initialUuid);
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		monitoringComponent = gson.fromJson(response.getResponse(), MonitoringComponent.class);
		Report.log(Status.INFO, "About to delete the submitted MC");
		response = DcaeRestClient.deleteMcReferenceAndBP(monitoringComponent.getName(), request.getServiceUuid(), request.getVfiName(), monitoringComponent.getUuid());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		response = DcaeRestClient.getServiceExternalReferences(service.getUuid(), service.getVersion());
		assertThat(response.getStatusCode())
				.as("status code")
				.isEqualTo(200);
		monitoringComponents = gson.fromJson(response.getResponse(), typeToken);
		mcList = monitoringComponents.get("monitoringComponents");
		assertThat(mcList.size()).isEqualTo(0);
	}
}