summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java
blob: 08faf4a61d67587a55bf7b1dd4bf54f49549bc56 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019-2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.dcaegen2.services.pmmapper.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.net.URL;
import java.net.UnknownHostException;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpStatusCode;
import org.mockserver.verify.VerificationTimes;
import org.onap.dcaegen2.services.pmmapper.utils.RequestSender;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import utils.LoggingUtils;

@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestSender.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
public class RequestSenderTests {
    private static ClientAndServer mockServer;
    private MockServerClient client = mockClient();

    @BeforeClass
    public static void setup() {
        mockServer = startClientAndServer(35454);
    }

    @AfterClass
    public static void teardown() {
        mockServer.stop();
    }

    @Test
    public void send_success() throws Exception {
        String url = "http://127.0.0.1:35454/once";
        String uuidRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
        ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(RequestSender.class);
        HttpRequest req = HttpRequest.request();
        client.when(req
                .withHeader(ONAPLogConstants.Headers.REQUEST_ID, uuidRegex)
                .withHeader(ONAPLogConstants.Headers.INVOCATION_ID, uuidRegex))
                .respond(response()
                        .withStatusCode(HttpStatusCode.OK_200.code())
                        .withBody("ResponseBody"));
        String result = new RequestSender().send(url);

        client.verify(req, VerificationTimes.atLeast(1));
        assertEquals(result, "ResponseBody");
        assertTrue(logAppender.list.get(1).getMessage().contains("Sending"));
        assertTrue(logAppender.list.get(2).getMessage().contains("Received"));
        logAppender.stop();
        client.clear(req);
    }

    @Test
    public void host_unavailable_retry_mechanism() throws Exception {
        PowerMockito.mockStatic(Thread.class);

        client.when(request())
                .respond(response().withStatusCode(HttpStatusCode.SERVICE_UNAVAILABLE_503.code()));

        assertThrows(Exception.class, () -> {
            new RequestSender().send("http://127.0.0.1:35454/anypath");
        });

        client.verify(request(), VerificationTimes.exactly(5));
        client.clear(request());
    }

    @Test
    public void host_unknown() throws Exception {
        PowerMockito.mockStatic(Thread.class);
        String unknownHostUrl = "http://unknown-host:35454/host-is-unknown";
        PowerMockito.whenNew(URL.class).withArguments(unknownHostUrl)
            .thenThrow(UnknownHostException.class);

        assertThrows(Exception.class, () -> {
            new RequestSender().send(unknownHostUrl);
        });

        client.verify(request(), VerificationTimes.exactly(0));
        client.clear(request());
    }

    private MockServerClient mockClient() {
        return new MockServerClient("127.0.0.1", 35454);
    }

}