1 package org.onap.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.junit.Assert;
8 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetBasicAuthPreset;
9 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetUrlServicePreset;
10 import org.onap.vid.api.BaseApiTest;
11 import org.onap.vid.api.OperationalEnvironmentControllerApiTest;
12 import org.onap.vid.api.ServiceInstanceMsoApiTest;
13 import org.springframework.http.*;
14 import org.testng.annotations.BeforeClass;
15 import org.testng.annotations.Test;
16 import vid.automation.test.services.SimulatorApi;
18 import java.util.List;
19 import java.util.UUID;
21 import static org.hamcrest.CoreMatchers.*;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.onap.vid.api.CategoryParametersApiTest.GET_CATEGORY_PARAMETER_PROPERTIES;
24 import static org.onap.vid.api.pProbeMsoApiTest.MSO_CREATE_CONFIGURATION;
25 import static org.springframework.http.HttpHeaders.AUTHORIZATION;
26 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
28 public class RequestIdFilterInstalled extends BaseApiTest {
31 Tests whether every incoming request to VID goes through
32 the requestId filter. This happens by checking the log
33 AND by checking the echoed header.
35 The correctness of the Filter itself is done by unit-
38 The outgoing (outgress) headers are checked by the REST
42 private static final String ECOMP_REQUEST_ID = "x-ecomp-requestid";
43 private final String ECOMP_REQUEST_ID_ECHO = ECOMP_REQUEST_ID + "-echo";
51 public void frontendApi_doGET_RequestIdReceived() throws InterruptedException {
53 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
55 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
57 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
59 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
64 public void frontendApi_doPOST_RequestIdReceived() throws InterruptedException {
66 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
68 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
70 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
72 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
76 public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
78 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
80 "/" + MSO_CREATE_CONFIGURATION,
83 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
88 @Test(groups = { "worksOnlyWithLocalhostVID" })
89 public void mopOwningEntityApi_doGET_RequestIdReceived() throws InterruptedException {
91 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
93 "/" + GET_CATEGORY_PARAMETER_PROPERTIES + "?familyName=PARAMETER_STANDARDIZATION",
96 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
101 x few FE errors requests;
102 - few UI elements requests;
103 x scheduler callback;
111 public void schedulerApi_doPOST_RequestIdReceived() throws InterruptedException {
113 final String anyInstanceId = "any instance id";
114 SimulatorApi.registerExpectation(
115 "mso_in_place_software_update_ok.json",
116 ImmutableMap.of("SERVICE_INSTANCE_ID", anyInstanceId, "VNF_INSTANCE_ID", anyInstanceId), SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET);
117 SimulatorApi.registerExpectationFromPreset(
118 new AAFGetUrlServicePreset(),
119 SimulatorApi.RegistrationStrategy.APPEND);
120 SimulatorApi.registerExpectationFromPreset(
121 new AAFGetBasicAuthPreset(),
122 SimulatorApi.RegistrationStrategy.APPEND);
123 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
125 "/change-management/workflow/" + anyInstanceId,
128 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
133 public void healthcheck_doGET_RequestIdReceived() {
134 final Pair<HttpEntity, String> responseAndUuid = makeRequest(
135 HttpMethod.GET, "/healthCheck", null
137 assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
140 private void assertThatUuidInResponseAndUuidIsInARecentLog(Pair<HttpEntity, String> responseAndUuid) {
141 assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
142 assertThatTermIsInARecentLog(responseAndUuid.getRight());
145 private void assertThatResponseHasUuid(HttpEntity response, String uuid) {
146 // THIS TEST IS NOT JUST NICE TO HAVE, it also lets us know
147 // that the request/response ran through our "promise request
148 // id" filter, which is great!
149 Assert.assertNotNull(response);
150 List<String> ecompRequestIdHeaderValues = response.getHeaders().get(ECOMP_REQUEST_ID_ECHO);
151 Assert.assertNotNull(ecompRequestIdHeaderValues);
152 Assert.assertTrue(ecompRequestIdHeaderValues.contains(uuid));
155 private void assertThatTermIsInARecentLog(String uuid) {
156 final ImmutableList<String> logLines = ImmutableList.of(
157 LoggerFormatTest.getLogLines("audit", 20, 0, restTemplate, uri),
158 LoggerFormatTest.getLogLines("error", 20, 0, restTemplate, uri)
161 // Assert that audit *OR* error has the uuid
162 assertThat("uuid not found in any log", logLines, hasItem(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(ECOMP_REQUEST_ID, 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 = null;
184 response = restTemplateErrorAgnostic.exchange(uri + url,
185 httpMethod, entity, String.class);
187 return Pair.of(response, uuid);