Fix for Penetration test _ Session and cookie management
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / RequestIdFilterInstalled.java
1 package org.onap.vid.more;
2
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;
11
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;
32
33 public class RequestIdFilterInstalled extends BaseApiTest {
34
35     /*
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.
39
40     The correctness of the Filter itself is done by unit-
41     tests.
42
43     The outgoing (outgress) headers are checked by the REST
44     Clients unit-tests.
45      */
46
47     private static final String REQUEST_ID_HEADER = "x-onap-requestid";
48     private final String ECOMP_REQUEST_ID_ECHO = "x-ecomp-requestid-echo";
49
50     @BeforeClass
51     public void login() {
52         super.login();
53     }
54
55     @Test
56     public void frontendApi_doGET_RequestIdReceived() {
57
58         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
59                 HttpMethod.GET,
60                 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
61                 null,
62                 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
63         );
64         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
65
66     }
67
68     @Test
69     public void frontendApi_doPOST_RequestIdReceived() {
70
71         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
72                 HttpMethod.POST,
73                 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
74                 "{}",
75                 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
76         );
77         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
78     }
79
80     @Test
81     public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
82
83         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
84                 HttpMethod.POST,
85                 "/" + MSO_CREATE_CONFIGURATION,
86                 "i'm not a json"
87         );
88         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.error, responseAndUuid);
89
90     }
91
92
93     @Test(groups = { "worksOnlyWithLocalhostVID" })
94     public void mopOwningEntityApi_doGET_RequestIdReceived() {
95
96         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
97                 HttpMethod.GET,
98                 "/" + MAINTENANCE_CATEGORY_PARAMETER + "?familyName=PARAMETER_STANDARDIZATION",
99                 null
100         );
101         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
102
103         /*
104         test should be for:
105          x few FE requests;
106          x few FE errors requests;
107          - few UI elements requests;
108          x scheduler callback;
109          - MOP of workflows;
110          x MOP of OE;
111          - health-check
112          */
113     }
114
115     @Test
116     public void schedulerApi_doPOST_RequestIdReceived() {
117
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(
129                 HttpMethod.POST,
130                 "/change-management/workflow/" + anyInstanceId,
131                 "{}"
132         );
133         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
134
135     }
136
137     @Test
138     public void healthcheck_doGET_RequestIdReceived() {
139         String path = "/healthCheck";
140         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
141                 HttpMethod.GET, path, null
142         );
143         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
144         LoggerFormatTest
145             .verifyExistenceOfIncomingReqsInAuditLogs(restTemplate, uri,
146                 responseAndUuid.getKey().getHeaders().get("X-ECOMP-RequestID-echo").get(0).toString(), path);
147     }
148
149     private void assertThatUuidInResponseAndUuidIsInARecentLog(LogName logName, Pair<HttpEntity, String> responseAndUuid) {
150         assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
151         assertThatTermIsInARecentLog(logName, responseAndUuid.getRight());
152     }
153
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)));
161     }
162
163     private void assertThatTermIsInARecentLog(LogName logName, String uuid) {
164         final String logLines = LoggerFormatTest.getLogLines(logName, 20, 0, restTemplate, uri);
165
166         assertThat("uuid not found in any log", logLines, containsString(uuid));
167     }
168
169     private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
170         return makeRequest(httpMethod, url, body, null);
171     }
172
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);
179
180         if (!StringUtils.isEmpty(expectationFilename)) {
181             SimulatorApi.registerExpectation(expectationFilename, APPEND);
182         }
183         SimulatorApi.registerExpectation("create_new_instance/aai_get_full_subscribers.json", APPEND);
184         SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
185
186         HttpEntity entity = new HttpEntity<>(body, headers);
187         ResponseEntity<String> response =
188             restTemplateErrorAgnostic.exchange(uri + url, httpMethod, entity, String.class);
189
190         return Pair.of(response, uuid);
191     }
192
193 }