aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/test/java/org/onap/vid/more/AuditLoggerTest.java
blob: 1cd6077ffd85d92e3c50fbd3317c4610f6c4bf6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.onap.vid.more;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.collection.IsMapContaining.hasKey;
import static org.onap.vid.api.TestUtils.assertAndRetryIfNeeded;
import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.APPEND;
import static vid.automation.test.services.SimulatorApi.RegistrationStrategy.CLEAR_THEN_SET;
import static vid.automation.test.services.SimulatorApi.registerExpectationFromPreset;

import java.util.List;
import java.util.function.Supplier;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetVpnsByType;
import org.onap.vid.api.BaseApiTest;
import org.onap.vid.more.LoggerFormatTest.LogName;
import org.springframework.http.ResponseEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import vid.automation.test.services.SimulatorApi;

public class AuditLoggerTest extends BaseApiTest {

    private final String ECOMP_REQUEST_ID_ECHO = "x-ecomp-requestid-echo";

    @BeforeClass
    public void login() {
        super.login();
    }

    @BeforeMethod
    public void resetPreset() {
        SimulatorApi.registerExpectation("create_new_instance/aai_get_full_subscribers.json", CLEAR_THEN_SET);
    }

    @Test
    public void aaiController2$GetVpnList_requestIdIsAuditedInEntryAndInExit() {
        registerExpectationFromPreset(new PresetAAIGetVpnsByType(), APPEND);
        String requestId = getRequestId(() -> restTemplate.getForEntity(buildUri("aai_get_vpn_list"), String.class));

        assertAndRetryIfNeeded(5, () -> assertThat("request id must be found in exactly two rows", getRequestLogLines(requestId),
            contains(
                allOf(containsString(requestId), containsString("Entering")),
                allOf(containsString(requestId), containsString("Exiting"))
            )));
    }

    private List<String> getRequestLogLines(String requestId) {
        return LoggerFormatTest.getRequestLogLines(requestId, LogName.audit2019, restTemplate, uri);
    }

    private String getRequestId(Supplier<ResponseEntity<?>> request) {
        ResponseEntity<?> response = request.get();

        assertThat(response.getHeaders(), hasKey(equalToIgnoringCase(ECOMP_REQUEST_ID_ECHO)));
        List<String> requestIds = response.getHeaders().get(ECOMP_REQUEST_ID_ECHO);

        assertThat(requestIds, hasSize(1));
        return requestIds.get(0);
    }

}