5a0a0f8453ba7f9dd03f5efecfe28ba6d9a07066
[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.onap.vid.api.OperationalEnvironmentControllerApiTest;
8 import org.onap.vid.api.BaseApiTest;
9 import org.onap.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;
17
18 import java.util.UUID;
19
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.onap.vid.api.CategoryParametersApiTest.GET_CATEGORY_PARAMETER_PROPERTIES;
25 import static org.onap.vid.api.pProbeMsoApiTest.MSO_CREATE_CONFIGURATION;
26 import static org.springframework.http.HttpHeaders.AUTHORIZATION;
27 import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
28
29 public class RequestIdFilterInstalled extends BaseApiTest {
30
31     /*
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.
35
36     The correctness of the Filter itself is done by unit-
37     tests.
38
39     The outgoing (outgress) headers are checked by the REST
40     Clients unit-tests.
41      */
42
43     private static final String ECOMP_REQUEST_ID = "x-ecomp-requestid";
44     private final String ECOMP_REQUEST_ID_ECHO = ECOMP_REQUEST_ID + "-echo";
45
46     @BeforeClass
47     public void login() {
48         super.login();
49     }
50
51     @Test
52     public void frontendApi_doGET_RequestIdReceived() {
53
54         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
55                 HttpMethod.GET,
56                 "/operationalEnvironment/requestStatus?requestId=" + OperationalEnvironmentControllerApiTest.GET_STATUS_REQUEST_UUID,
57                 null,
58                 OperationalEnvironmentControllerApiTest.GET_CLOUD_RESOURCES_REQUEST_STATUS
59         );
60         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
61
62     }
63
64     @Test
65     public void frontendApi_doPOST_RequestIdReceived() {
66
67         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
68                 HttpMethod.POST,
69                 "/" + ServiceInstanceMsoApiTest.MSO_DEACTIVATE_SERVICE_INSTANCE,
70                 "{}",
71                 ServiceInstanceMsoApiTest.DEACTIVATE_OK_JSON
72         );
73         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
74     }
75
76     @Test
77     public void frontendApi_doPOSTWithClientError_RequestIdReceived() {
78
79         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
80                 HttpMethod.POST,
81                 "/" + MSO_CREATE_CONFIGURATION,
82                 "i'm not a json"
83         );
84         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
85
86     }
87
88
89     @Test(groups = { "worksOnlyWithLocalhostVID" })
90     public void mopOwningEntityApi_doGET_RequestIdReceived() {
91
92         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
93                 HttpMethod.GET,
94                 "/" + GET_CATEGORY_PARAMETER_PROPERTIES + "?familyName=PARAMETER_STANDARDIZATION",
95                 null
96         );
97
98         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
99
100         /*
101         test should be for:
102          x few FE requests;
103          x few FE errors requests;
104          - few UI elements requests;
105          x scheduler callback;
106          - MOP of workflows;
107          x MOP of OE;
108          - health-check
109          */
110     }
111
112     @Test
113     public void schedulerApi_doPOST_RequestIdReceived() {
114
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(
120                 HttpMethod.POST,
121                 "/change-management/workflow/" + anyInstanceId,
122                 "{}"
123         );
124         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
125
126     }
127
128     @Test
129     public void healthcheck_doGET_RequestIdReceived(){
130         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
131                 HttpMethod.GET, "/healthCheck", null
132         );
133         assertThatUuidInResponseAndUuidIsInARecentLog(responseAndUuid);
134     }
135
136     private void assertThatUuidInResponseAndUuidIsInARecentLog(Pair<HttpEntity, String> responseAndUuid) {
137         assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
138         assertThatTermIsInARecentLog(responseAndUuid.getRight());
139     }
140
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));
147     }
148
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)
153         );
154
155         // Assert that audit *OR* error has the uuid
156         assertThat("uuid not found in any log", logLines, hasItem(containsString(uuid)));
157     }
158
159     private Pair<HttpEntity, String> makeRequest(HttpMethod httpMethod, String url, String body) {
160         return makeRequest(httpMethod, url, body, null);
161     }
162
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==");
168
169         SimulatorApi.clearExpectations();
170         if (!StringUtils.isEmpty(expectationFilename)) {
171             SimulatorApi.registerExpectation(expectationFilename, APPEND);
172         }
173         SimulatorApi.registerExpectation("aai_get_full_subscribers.json", APPEND);
174         SimulatorApi.registerExpectation("ecompportal_getSessionSlotCheckInterval.json", APPEND);
175
176         HttpEntity entity = new HttpEntity<>(body, headers);
177         ResponseEntity<String> response = null;
178         response = restTemplateErrorAgnostic.exchange(uri + url,
179                 httpMethod, entity, String.class);
180
181         return Pair.of(response, uuid);
182     }
183
184 }