aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/aai/PombaRestInterfaceTest.java
blob: 5de993c73aec802cd109b791aa56bf5942fe28a7 (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
125
126
127
128
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2019 Nokia
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.vid.aai;


import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.UUID;
import java.util.regex.Pattern;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mockito.Mock;
import org.onap.vid.aai.util.HttpClientMode;
import org.onap.vid.aai.util.HttpsAuthClient;
import org.onap.vid.aai.util.ServletRequestHelper;
import org.onap.vid.aai.util.SystemPropertyHelper;
import org.onap.vid.utils.Logging;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class PombaRestInterfaceTest {
    private static final String UUID_REGEX = "[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}";
    private static final String SAMPLE_APP_ID = "vid";
    private static final String SAMPLE_URL = "sampleUrl";
    private static final String SAMPLE_PAYLOAD = "samplePayload";
    private static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);

    @Mock
    private HttpsAuthClient authClient;

    @Mock
    private ServletRequestHelper requestHelper;

    @Mock
    private SystemPropertyHelper systemPropertyHelper;

    @Mock
    private Client client;

    @Mock
    private WebTarget webTarget;

    @Mock
    private Invocation.Builder builder;

    @Mock
    private Response response;

    @Mock
    private Logging loggingService;

    private PombaRestInterface pombaRestInterface;

    @BeforeMethod
    public void setUp() throws GeneralSecurityException, IOException {
        initMocks(this);

        when(requestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
        when(authClient.getClient(HttpClientMode.WITH_KEYSTORE)).thenReturn(client);
        setUpBuilder();
        pombaRestInterface = new PombaRestInterface(authClient, requestHelper, systemPropertyHelper, loggingService);
    }


    @Test
    public void shouldProperlySendRequestWithAllRequiredHeaders() {
        Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);

        assertThat(actualResponse).isEqualTo(response);

        verify(builder).accept(MediaType.APPLICATION_JSON);
        verify(builder).header(FROM_APP_ID_HEADER, SAMPLE_APP_ID);
        verify(builder).header(matches(TRANSACTION_ID_HEADER), matches(UUID_PATTERN));
        verify(builder).header(matches(REQUEST_ID_HEADER_KEY), matches(UUID_PATTERN));
        verify(builder).post(any(Entity.class));
    }

    @Test
    public void shouldReturnNullWhenExceptionWasRaised() {
        doThrow(new RuntimeException()).when(client).target(anyString());

        Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);

        assertThat(actualResponse).isNull();
    }

    private void setUpBuilder() {
        when(client.target(anyString())).thenReturn(webTarget);
        when(webTarget.request()).thenReturn(builder);
        when(builder.accept(MediaType.APPLICATION_JSON)).thenReturn(builder);
        when(builder.header(anyString(), any())).thenReturn(builder);
        when(builder.post(any(Entity.class))).thenReturn(response);
        when(response.getStatusInfo()).thenReturn(Response.Status.OK);
    }
}