1 package org.onap.vid.more;
3 import static org.hamcrest.CoreMatchers.containsString;
4 import static org.hamcrest.CoreMatchers.hasItem;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.equalToIgnoringCase;
7 import static org.onap.vid.api.CategoryParametersApiTest.MAINTENANCE_CATEGORY_PARAMETER;
8 import static org.onap.vid.api.pProbeMsoApiTest.MSO_CREATE_CONFIGURATION;
9 import static org.springframework.http.HttpHeaders.AUTHORIZATION;
10 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.List;
14 import java.util.UUID;
15 import org.apache.commons.lang3.StringUtils;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.junit.Assert;
18 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetBasicAuthPreset;
19 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetUrlServicePreset;
20 import org.onap.vid.api.BaseApiTest;
21 import org.onap.vid.api.OperationalEnvironmentControllerApiTest;
22 import org.onap.vid.api.ServiceInstanceMsoApiTest;
23 import org.onap.vid.more.LoggerFormatTest.LogName;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.MediaType;
28 import org.springframework.http.ResponseEntity;
29 import org.testng.annotations.BeforeClass;
30 import org.testng.annotations.Test;
31 import vid.automation.test.services.SimulatorApi;
33 public class RequestIdFilterInstalled extends BaseApiTest {
36 Tests whether every incoming request to VID goes through
37 the requestId filter. This happens by checking the log
38 AND by checking the echoed header.
40 The correctness of the Filter itself is done by unit-
43 The outgoing (outgress) headers are checked by the REST
47 private static final String REQUEST_ID_HEADER = "x-onap-requestid";
48 private final String ECOMP_REQUEST_ID_ECHO = "x-ecomp-requestid-echo";
56 public void frontendApi_doGET_RequestIdReceived() {
58 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
60 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
62 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
64 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit, responseAndUuid);
69 public void frontendApi_doPOST_RequestIdReceived() {
71 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
73 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
75 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
77 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit, responseAndUuid);
81 public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
83 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
85 "/" + MSO_CREATE_CONFIGURATION,
88 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.error, responseAndUuid);
93 @Test(groups = { "worksOnlyWithLocalhostVID" })
94 public void mopOwningEntityApi_doGET_RequestIdReceived() {
96 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
98 "/" + MAINTENANCE_CATEGORY_PARAMETER + "?familyName=PARAMETER_STANDARDIZATION",
101 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit, responseAndUuid);
106 x few FE errors requests;
107 - few UI elements requests;
108 x scheduler callback;
116 public void schedulerApi_doPOST_RequestIdReceived() {
118 final String anyInstanceId = "any instance id";
119 SimulatorApi.registerExpectation(
120 "mso_in_place_software_update_ok.json",
121 ImmutableMap.of("SERVICE_INSTANCE_ID", anyInstanceId, "VNF_INSTANCE_ID", anyInstanceId), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
122 SimulatorApi.registerExpectationFromPreset(
123 new AAFGetUrlServicePreset(),
124 SimulatorApi.RegistrationStrategy.APPEND);
125 SimulatorApi.registerExpectationFromPreset(
126 new AAFGetBasicAuthPreset(),
127 SimulatorApi.RegistrationStrategy.APPEND);
128 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
130 "/change-management/workflow/" + anyInstanceId,
133 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit, responseAndUuid);
138 public void healthcheck_doGET_RequestIdReceived() {
139 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
140 HttpMethod.GET, "/healthCheck", null
142 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit, responseAndUuid);
145 private void assertThatUuidInResponseAndUuidIsInARecentLog(LogName logName, Pair<HttpEntity, String> responseAndUuid) {
146 assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
147 assertThatTermIsInARecentLog(logName, responseAndUuid.getRight());
150 private void assertThatResponseHasUuid(HttpEntity response, String uuid) {
151 // THIS TEST IS NOT JUST NICE TO HAVE, it also lets us know
152 // that the request/response ran through our "promise request
153 // id" filter, which is great!
154 Assert.assertNotNull(response);
155 List<String> ecompRequestIdHeaderValues = response.getHeaders().get(ECOMP_REQUEST_ID_ECHO);
156 assertThat(ecompRequestIdHeaderValues, hasItem(equalToIgnoringCase(uuid)));
159 private void assertThatTermIsInARecentLog(LogName logName, String uuid) {
160 final String logLines = LoggerFormatTest.getLogLines(logName, 20, 0, restTemplate, uri);
162 assertThat("uuid not found in any log", logLines, containsString(uuid));
165 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
166 return makeRequest(httpMethod, url, body, null);
169 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body, String expectationFilename) {
170 final String uuid = UUID.randomUUID().toString();
171 final HttpHeaders headers = new HttpHeaders();
172 headers.add(REQUEST_ID_HEADER, uuid);
173 headers.add(AUTHORIZATION, "Basic " + AAFGetBasicAuthPreset.VALID_AUTH_VALUE);
174 headers.setContentType(MediaType.APPLICATION_JSON);
176 if (!StringUtils.isEmpty(expectationFilename)) {
177 SimulatorApi.registerExpectation(expectationFilename, APPEND);
179 SimulatorApi.registerExpectation("create_new_instance/aai_get_full_subscribers.json", APPEND);
180 SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
182 HttpEntity entity = new HttpEntity<>(body, headers);
183 ResponseEntity<String> response =
184 restTemplateErrorAgnostic.exchange(uri + url, httpMethod, entity, String.class);
186 return Pair.of(response, uuid);