1 package org.opencomp.vid.more;
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.collect.ImmutableMap;
5 import org.apache.commons.lang3.StringUtils;
6 import org.apache.commons.lang3.tuple.Pair;
7 import org.opencomp.vid.api.BaseApiTest;
8 import org.opencomp.vid.api.OperationalEnvironmentControllerApiTest;
9 import org.opencomp.vid.api.ServiceInstanceMsoApiTest;
10 import org.springframework.http.HttpEntity;
11 import org.springframework.http.HttpHeaders;
12 import org.springframework.http.HttpMethod;
13 import org.springframework.http.ResponseEntity;
14 import org.testng.annotations.BeforeClass;
15 import org.testng.annotations.Test;
16 import vid.automation.test.services.SimulatorApi;
18 import java.util.UUID;
20 import static org.hamcrest.CoreMatchers.*;
21 import static org.hamcrest.Matchers.containsInAnyOrder;
22 import static org.hamcrest.Matchers.nullValue;
23 import static org.junit.Assert.assertThat;
24 import static org.opencomp.vid.api.CategoryParametersApiTest.GET_CATEGORY_PARAMETER_PROPERTIES;
25 import static org.opencomp.vid.api.pProbeMsoApiTest.MSO_CREATE_CONFIGURATION;
26 import static org.springframework.http.HttpHeaders.AUTHORIZATION;
27 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
29 public class RequestIdFilterInstalled extends BaseApiTest {
32 Tests whether every incoming request to VID goes through
33 the requestId filter. This happens by checking the log
34 AND by checking the echoed header.
36 The correctness of the Filter itself is done by unit-
39 The outgoing (outgress) headers are checked by the REST
43 private static final String ECOMP_REQUEST_ID = "x-ecomp-requestid";
44 private final String ECOMP_REQUEST_ID_ECHO = ECOMP_REQUEST_ID + "-echo";
52 public void frontendApi_doGET_RequestIdReceived() {
54 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
56 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
58 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
60 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
65 public void frontendApi_doPOST_RequestIdReceived() {
67 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
69 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
71 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
73 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
77 public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
79 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
81 "/" + MSO_CREATE_CONFIGURATION,
84 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
89 @Test(groups = { "worksOnlyWithLocalhostVID" })
90 public void mopOwningEntityApi_doGET_RequestIdReceived() {
92 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
94 "/" + GET_CATEGORY_PARAMETER_PROPERTIES + "?familyName=PARAMETER_STANDARDIZATION",
98 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
103 x few FE errors requests;
104 - few UI elements requests;
105 x scheduler callback;
113 public void schedulerApi_doPOST_RequestIdReceived() {
115 final String anyInstanceId = "any instance id";
116 SimulatorApi.registerExpectation(
117 "mso_in_place_software_update_ok.json",
118 ImmutableMap.of("SERVICE_INSTANCE_ID", anyInstanceId, "VNF_INSTANCE_ID", anyInstanceId), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
119 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
121 "/change-management/workflow/" + anyInstanceId,
124 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
129 public void healthcheck_doGET_RequestIdReceived(){
130 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
131 HttpMethod.GET, "/healthCheck", null
133 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
136 private void assertThatUuidInResponseAndUuidIsInARecentLog(Pair<HttpEntity, String> responseAndUuid) {
137 assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
138 assertThatTermIsInARecentLog(responseAndUuid.getRight());
141 private void assertThatResponseHasUuid(HttpEntity response, String uuid) {
142 // THIS TEST IS NOT JUST NICE TO HAVE, it also lets us know
143 // that the request/response ran through our "promise request
144 // id" filter, which is great!
145 assertThat(response, not(nullValue()));
146 assertThat(response.getHeaders().get(ECOMP_REQUEST_ID_ECHO), containsInAnyOrder(uuid));
149 private void assertThatTermIsInARecentLog(String uuid) {
150 final ImmutableList<String> logLines = ImmutableList.of(
151 LoggerFormatTest.getLogLines("audit", 5, 0, restTemplate, uri),
152 LoggerFormatTest.getLogLines("error", 5, 0, restTemplate, uri)
155 // Assert that audit *OR* error has the uuid
156 assertThat("uuid not found in any log", logLines, hasItem(containsString(uuid)));
159 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
160 return makeRequest(httpMethod, url, body, null);
163 private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body, String expectationFilename) {
164 final String uuid = UUID.randomUUID().toString();
165 final HttpHeaders headers = new HttpHeaders();
166 headers.add(ECOMP_REQUEST_ID, uuid);
167 headers.add(AUTHORIZATION, "Basic 123==");
169 SimulatorApi.clearExpectations();
170 if (!StringUtils.isEmpty(expectationFilename)) {
171 SimulatorApi.registerExpectation(expectationFilename, APPEND);
173 SimulatorApi.registerExpectation("aai_get_full_subscribers.json", APPEND);
174 SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
176 HttpEntity entity = new HttpEntity<>(body, headers);
177 ResponseEntity<String> response = null;
178 response = restTemplateErrorAgnostic.exchange(uri + url,
179 httpMethod, entity, String.class);
181 return Pair.of(response, uuid);