summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/api/tests/services/instance/GetServiceInstancePositive.java
blob: b22e2f7702e027901f64f5c4a2d7495045af169b (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
package org.onap.dcae.ci.api.tests.services.instance;

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

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.assertj.core.api.SoftAssertions;
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.dcae.ci.utilities.StringUtils;
import org.onap.sdc.dcae.composition.restmodels.sdc.ServiceDetailed;
import org.onap.sdc.dcae.composition.services.Resource;
import org.onap.sdc.dcae.composition.vfcmt.Vfcmt;
import org.onap.sdc.dcae.composition.restmodels.DcaeMinimizedService;
import org.springframework.util.CollectionUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.aventstack.extentreports.Status;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class GetServiceInstancePositive extends DcaeRestBaseTest {
	
	private RestResponse response;
	
	@BeforeClass
	public void executeApiCall() throws Exception {
		// arrange
		DcaeMinimizedService service = arrangeService();
		try {
			// act
			Report.log(Status.INFO, "Get all VFIs for service [" + service.getUuid() + "]");
			response = DcaeRestClient.getServicesInstance(service.getUuid());
			Report.log(Status.DEBUG, "Response: " + StringUtils.truncate(response));
		} catch (Exception err) {
			Report.log(Status.FAIL, "Unable to execute api call: " + err.toString());
			err.printStackTrace();
		}
	}

	private DcaeMinimizedService arrangeService() throws Exception {
		DcaeMinimizedService service = null;
		try {
			Predicate<DcaeMinimizedService> hasVfi = p -> !CollectionUtils.isEmpty(getService(p.getUuid()).getResources());
			Vfcmt vfcmt = client.createCheckedoutVfcmt();
			Report.log(Status.INFO, "Created vfcmt [" + vfcmt.getUuid() + "]");
			Report.log(Status.INFO, "Get all services for vfcmt [" + vfcmt.getUuid() + "]");
			RestResponse responseServices =  DcaeRestClient.getServices(vfcmt.getUuid(), vfcmt.getLastUpdaterUserId());
			Report.log(Status.DEBUG, "Response: " + StringUtils.truncate(responseServices));
			DcaeMinimizedService[] servicesList = gson.fromJson(responseServices.getResponse(), DcaeMinimizedService[].class);
			// TODO: create a service instead of picking a random one
			// find a service with a vfi
			service = Arrays.stream(servicesList).filter(hasVfi).findAny().orElse(null);
		} catch (Exception err) {
			Report.log(Status.ERROR, "Could not arrange test: " + err.toString());
		}
		return service;
	}

	private ServiceDetailed getService(String serviceId) {
		ServiceDetailed service = null;
		try {
			service = gson.fromJson(DcaeRestClient.getServicesInstance(serviceId).getResponse(), ServiceDetailed.class);
		} catch (Exception e) {
			Report.log(Status.ERROR, "Could not arrange test: " + e.toString());
		}
		return service;
	}
	
	@Test
	public void test_responseStatusOk() throws IOException{
		// assert
		Report.log(Status.INFO, "Verifing response status is 200");
		assertThat(response.getStatusCode()).as("response status").isEqualTo(200);
	}
	
	@Test 
	public void test_atLeastOneOrMoreResources() throws IOException{
		// assert
		Report.log(Status.INFO, "Parsing response to a one service instance");
		List<Resource> resourceList = getResourceListFromJsonResponse();
		Report.log(Status.INFO, "validating parsed response structure");
		assertThat(resourceList).size().isGreaterThanOrEqualTo(1); // TODO: create a VFI for the service instead of picking a random one
	}
	
	@Test 
	public void  test_responseIsValidStructure() throws IOException{
		// assert
		Report.log(Status.INFO, "Parsing response to a one service instance");
		
		List<Resource> resourceList = getResourceListFromJsonResponse();

		Report.log(Status.INFO, "validating parsed response structure");
		
		SoftAssertions.assertSoftly(softly -> {
			softly.assertThat(resourceList.get(0).getResourceInstanceName()).isNotEmpty();
			softly.assertThat(resourceList.get(0).getResourceInvariantUUID()).isNotEmpty();
		});
	}
	
	
	/*** private method ***/
	private List<Resource> getResourceListFromJsonResponse() {
		JsonParser jsonParser = new JsonParser();
		JsonObject responseJson = (JsonObject)jsonParser.parse(response.getResponse());
		JsonArray resources = responseJson.getAsJsonArray("resources");
		Type listType = new TypeToken<List<Resource>>(){}.getType();
		List<Resource> resourceList = gson.fromJson(resources, listType);
		return resourceList;
	}

}