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.audit2019, 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.audit2019, 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.audit2019, 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.audit2019, responseAndUuid);
138 public void healthcheck_doGET_RequestIdReceived() {
139 String path = "/healthCheck";
140 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
141 HttpMethod.GET, path, null
143 assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
145 .verifyExistenceOfIncomingReqsInAuditLogs(restTemplate, uri,
146 responseAndUuid.getKey().getHeaders().get("X-ECOMP-RequestID-echo").get(0).toString(), path);
149 private void assertThatUuidInResponseAndUuidIsInARecentLog(LogName logName, Pair<HttpEntity, String> responseAndUuid) {
150 assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
151 assertThatTermIsInARecentLog(logName, responseAndUuid.getRight());
154 private void assertThatResponseHasUuid(HttpEntity response, String uuid) {
155 // THIS TEST IS NOT JUST NICE TO HAVE, it also lets us know
156 // that the request/response ran through our "promise request
157 // id" filter, which is great!
158 Assert.assertNotNull(response);
159 List<String> ecompRequestIdHeaderValues = response.getHeaders().get(ECOMP_REQUEST_ID_ECHO);
160 assertThat(ecompRequestIdHeaderValues, hasItem(equalToIgnoringCase(uuid)));
163 private void assertThatTermIsInARecentLog(LogName logName, String uuid) {
164 final String logLines = LoggerFormatTest.getLogLines(logName, 20, 0, restTemplate, uri);
166 assertThat("uuid not found in any log", logLines, containsString(uuid));
169 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
170 return makeRequest(httpMethod, url, body, null);
173 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body, String expectationFilename) {
174 final String uuid = UUID.randomUUID().toString();
175 final HttpHeaders headers = new HttpHeaders();
176 headers.add(REQUEST_ID_HEADER, uuid);
177 headers.add(AUTHORIZATION, "Basic " + AAFGetBasicAuthPreset.VALID_AUTH_VALUE);
178 headers.setContentType(MediaType.APPLICATION_JSON);
180 if (!StringUtils.isEmpty(expectationFilename)) {
181 SimulatorApi.registerExpectation(expectationFilename, APPEND);
183 SimulatorApi.registerExpectation("create_new_instance/aai_get_full_subscribers.json", APPEND);
184 SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
186 HttpEntity entity = new HttpEntity<>(body, headers);
187 ResponseEntity<String> response =
188 restTemplateErrorAgnostic.exchange(uri + url, httpMethod, entity, String.class);
190 return Pair.of(response, uuid);