Merge "Let request-id value derive from alternative request-headers"
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / RequestIdFilterInstalled.java
1 package org.onap.vid.more;
2
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;
17
18 import java.util.List;
19 import java.util.UUID;
20
21 import static org.hamcrest.CoreMatchers.*;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.onap.vid.api.CategoryParametersApiTest.MAINTENANCE_CATEGORY_PARAMETER;
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;
27
28 public class RequestIdFilterInstalled extends BaseApiTest {
29
30     /*
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.
34
35     The correctness of the Filter itself is done by unit-
36     tests.
37
38     The outgoing (outgress) headers are checked by the REST
39     Clients unit-tests.
40      */
41
42     private static final String ECOMP_REQUEST_ID = "x-ecomp-requestid";
43     private final String ECOMP_REQUEST_ID_ECHO = ECOMP_REQUEST_ID + "-echo";
44
45     @BeforeClass
46     public void login() {
47         super.login();
48     }
49
50     @Test
51     public void frontendApi_doGET_RequestIdReceived() throws InterruptedException {
52
53         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
54                 HttpMethod.GET,
55                 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
56                 null,
57                 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
58         );
59         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
60
61     }
62
63     @Test
64     public void frontendApi_doPOST_RequestIdReceived() throws InterruptedException {
65
66         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
67                 HttpMethod.POST,
68                 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
69                 "{}",
70                 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
71         );
72         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
73     }
74
75     @Test
76     public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
77
78         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
79                 HttpMethod.POST,
80                 "/" + MSO_CREATE_CONFIGURATION,
81                 "i'm not a json"
82         );
83         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
84
85     }
86
87
88     @Test(groups = { "worksOnlyWithLocalhostVID" })
89     public void mopOwningEntityApi_doGET_RequestIdReceived() throws InterruptedException {
90
91         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
92                 HttpMethod.GET,
93                 "/" + MAINTENANCE_CATEGORY_PARAMETER + "?familyName=PARAMETER_STANDARDIZATION",
94                 null
95         );
96         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
97
98         /*
99         test should be for:
100          x few FE requests;
101          x few FE errors requests;
102          - few UI elements requests;
103          x scheduler callback;
104          - MOP of workflows;
105          x MOP of OE;
106          - health-check
107          */
108     }
109
110     @Test
111     public void schedulerApi_doPOST_RequestIdReceived() throws InterruptedException {
112
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(
124                 HttpMethod.POST,
125                 "/change-management/workflow/" + anyInstanceId,
126                 "{}"
127         );
128         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
129
130     }
131
132     @Test
133     public void healthcheck_doGET_RequestIdReceived() {
134         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
135                 HttpMethod.GET, "/healthCheck", null
136         );
137         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
138     }
139
140     private void assertThatUuidInResponseAndUuidIsInARecentLog(Pair<HttpEntity, String> responseAndUuid) {
141         assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
142         assertThatTermIsInARecentLog(responseAndUuid.getRight());
143     }
144
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));
153     }
154
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)
159         );
160
161         // Assert that audit *OR* error has the uuid
162         assertThat("uuid not found in any log", logLines, hasItem(containsString(uuid)));
163     }
164
165     private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
166         return makeRequest(httpMethod, url, body, null);
167     }
168
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);
175
176         if (!StringUtils.isEmpty(expectationFilename)) {
177             SimulatorApi.registerExpectation(expectationFilename, APPEND);
178         }
179         SimulatorApi.registerExpectation("create_new_instance/aai_get_full_subscribers.json", APPEND);
180         SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
181
182         HttpEntity entity = new HttpEntity<>(body, headers);
183         ResponseEntity<String> response = null;
184         response = restTemplateErrorAgnostic.exchange(uri + url,
185                 httpMethod, entity, String.class);
186
187         return Pair.of(response, uuid);
188     }
189
190 }