summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/utilities/DcaeEntityClient.java
blob: d6b267f665539cd2accb4f70319eaf8f841c6ff0 (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
package org.onap.dcae.ci.utilities;

import com.aventstack.extentreports.Status;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.onap.dcae.ci.entities.RestResponse;
import org.onap.dcae.ci.report.Report;
import org.onap.sdc.dcae.composition.model.Requirement;
import org.onap.sdc.dcae.composition.model.Value;
import org.onap.sdc.dcae.composition.model.deserializer.RequirementDeserializer;
import org.onap.sdc.dcae.composition.model.deserializer.ValueDeserializer;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.composition.vfcmt.Vfcmt;

public class DcaeEntityClient {
	
	protected Gson gson;
	
	public DcaeEntityClient() {
		super();
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Requirement.class, new RequirementDeserializer());
		gsonBuilder.registerTypeAdapter(Value.class, new ValueDeserializer());
		gson = gsonBuilder.create();
	}
	
	/* VFCMT */
	
	/**
	 * 
	 * @param name
	 * @param description
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createVfcmt(String name, String description, String userId) throws Exception {
		RestResponse response = DcaeRestClient.createVfcmt(name, description, userId);
		Report.log(Status.DEBUG, "Create VFCMT Response: " + StringUtils.truncate(response));
		if (response.getStatusCode() != 200) {
			throw new Exception("Response: " + StringUtils.truncate(response));
		}
		return gson.fromJson(response.getResponse(), Vfcmt.class);
	}
	
	/**
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createVfcmt(String userId) throws Exception {
		String name = StringUtils.randomString("CI-", 20);
		String description = "This vfcmt was created by automated ci tests";
		Vfcmt createVfcmt = createVfcmt(name, description, userId);
		Report.log(Status.INFO, "createVfcmt result="+createVfcmt);
		return createVfcmt;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createVfcmt() throws Exception {
		return createVfcmt(DcaeRestClient.getDefaultUserId());
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vfcmt[] getAllVfcmts() throws Exception {
		Report.log(Status.INFO, "Fetching all vfcmt resources");
		RestResponse response = DcaeRestClient.getAllVfcmts();
		return convertResponseToVfcmt(response);
	}

	public Vfcmt[] getAllBaseVfcmts() throws Exception {
		Report.log(Status.INFO, "Fetching all vfcmt resources");
		RestResponse response = DcaeRestClient.getAllMonitoringTemplatesVfcmts();
		return convertResponseToVfcmt(response);
	}

	private Vfcmt[] convertResponseToVfcmt(RestResponse response) throws Exception {
		Report.log(Status.DEBUG, "Response: " + StringUtils.truncate(response));
		if (response.getStatusCode() != 200) {
			throw new Exception("Response: " + StringUtils.truncate(response));
		}
		return gson.fromJson(response.getResponse(), Vfcmt[].class);
	}

	/**
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createCheckedoutVfcmt(String userId) throws Exception {
		Report.log(Status.INFO, "Creating vfcmt...");
		Vfcmt vfcmt = createVfcmt(userId);
		if (vfcmt.getLifecycleState().equals(DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name()) == false) {
			throw new Exception("created vfcmt is not in checkout state!");
		}
		return vfcmt;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createCheckedoutVfcmt() throws Exception {
		String userId = DcaeRestClient.getDefaultUserId();
		Report.log(Status.INFO, "Going to create checked out VFCMT with user="+userId);
		return createCheckedoutVfcmt(userId);
	}
	
	/**
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createCheckedinVfcmt(String userId) throws Exception {
		Report.log(Status.INFO, "Creating vfcmt");
		Vfcmt vfcmt = createVfcmt(userId);
		Report.log(Status.INFO, "Checkin the vfcmt");
		RestResponse response = DcaeRestClient.checkinVfcmt(vfcmt.getUuid(), vfcmt.getLastUpdaterUserId());
		Report.log(Status.DEBUG, "Response: " + StringUtils.truncate(response));
		Vfcmt vfcmtAfterCheckin = gson.fromJson(response.getResponse(), Vfcmt.class);
		if (response.getStatusCode() != 200) {
			throw new Exception("Unable to checkin newly created vfcmt");
		}
		return vfcmtAfterCheckin;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vfcmt createCheckedinVfcmt() throws Exception {
		String userId = DcaeRestClient.getDefaultUserId();
		return createCheckedinVfcmt(userId);
	}
}