remove 2016 logger aspects + verify audit score
[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         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
140                 HttpMethod.GET, "/healthCheck", null
141         );
142         assertThatUuidInResponseAndUuidIsInARecentLog(LogName.audit2019, responseAndUuid);
143     }
144
145     private void assertThatUuidInResponseAndUuidIsInARecentLog(LogName logName, Pair<HttpEntity, String> responseAndUuid) {
146         assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
147         assertThatTermIsInARecentLog(logName, responseAndUuid.getRight());
148     }
149
150     private void assertThatResponseHasUuid(HttpEntity response, String uuid) {
151         // THIS TEST IS NOT JUST NICE TO HAVE, it also lets us know
152         // that the request/response ran through our "promise request
153         // id" filter, which is great!
154         Assert.assertNotNull(response);
155         List<String> ecompRequestIdHeaderValues = response.getHeaders().get(ECOMP_REQUEST_ID_ECHO);
156         assertThat(ecompRequestIdHeaderValues, hasItem(equalToIgnoringCase(uuid)));
157     }
158
159     private void assertThatTermIsInARecentLog(LogName logName, String uuid) {
160         final String logLines = LoggerFormatTest.getLogLines(logName, 20, 0, restTemplate, uri);
161
162         assertThat("uuid not found in any log", logLines, 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(REQUEST_ID_HEADER, 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 =
184             restTemplateErrorAgnostic.exchange(uri + url, httpMethod, entity, String.class);
185
186         return Pair.of(response, uuid);
187     }
188
189 }