change management cypress test
[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.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.List;
15 import java.util.UUID;
16 import org.apache.commons.lang3.StringUtils;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.junit.Assert;
19 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetBasicAuthPreset;
20 import org.onap.simulator.presetGenerator.presets.aaf.AAFGetUrlServicePreset;
21 import org.onap.vid.api.BaseApiTest;
22 import org.onap.vid.api.OperationalEnvironmentControllerApiTest;
23 import org.onap.vid.api.ServiceInstanceMsoApiTest;
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() throws InterruptedException {
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(responseAndUuid);
65
66     }
67
68     @Test
69     public void frontendApi_doPOST_RequestIdReceived() throws InterruptedException {
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(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(responseAndUuid);
89
90     }
91
92
93     @Test(groups = { "worksOnlyWithLocalhostVID" })
94     public void mopOwningEntityApi_doGET_RequestIdReceived() throws InterruptedException {
95
96         final Pair<HttpEntity, String> responseAndUuid = makeRequest(
97                 HttpMethod.GET,
98                 "/" + MAINTENANCE_CATEGORY_PARAMETER + "?familyName=PARAMETER_STANDARDIZATION",
99                 null
100         );
101         assertThatUuidInResponseAndUuidIsInARecentLog(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() throws InterruptedException {
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(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(responseAndUuid);
143     }
144
145     private void assertThatUuidInResponseAndUuidIsInARecentLog(Pair<HttpEntity, String> responseAndUuid) {
146         assertThatResponseHasUuid(responseAndUuid.getLeft(), responseAndUuid.getRight());
147         assertThatTermIsInARecentLog(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         Assert.assertThat(ecompRequestIdHeaderValues, hasItem(equalToIgnoringCase(uuid)));
157     }
158
159     private void assertThatTermIsInARecentLog(String uuid) {
160         final ImmutableList<String> logLines = ImmutableList.of(
161                 LoggerFormatTest.getLogLines("audit", 20, 0, restTemplate, uri),
162                 LoggerFormatTest.getLogLines("error", 20, 0, restTemplate, uri)
163         );
164
165         // Assert that audit *OR* error has the uuid
166         assertThat("uuid not found in any log", logLines, hasItem(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 = null;
188         response = restTemplateErrorAgnostic.exchange(uri + url,
189                 httpMethod, entity, String.class);
190
191         return Pair.of(response, uuid);
192     }
193
194 }