2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.vid.aai;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.matches;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.MockitoAnnotations.initMocks;
31 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
32 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
33 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
35 import java.io.IOException;
36 import java.security.GeneralSecurityException;
37 import java.util.UUID;
38 import java.util.regex.Pattern;
39 import javax.ws.rs.client.Client;
40 import javax.ws.rs.client.Entity;
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.client.WebTarget;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.mockito.Mock;
46 import org.onap.vid.aai.util.HttpClientMode;
47 import org.onap.vid.aai.util.HttpsAuthClient;
48 import org.onap.vid.aai.util.ServletRequestHelper;
49 import org.onap.vid.aai.util.SystemPropertyHelper;
50 import org.onap.vid.utils.Logging;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
54 public class PombaRestInterfaceTest {
55 private static final String UUID_REGEX = "[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}";
56 private static final String SAMPLE_APP_ID = "vid";
57 private static final String SAMPLE_URL = "sampleUrl";
58 private static final String SAMPLE_PAYLOAD = "samplePayload";
59 private static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);
62 private HttpsAuthClient authClient;
65 private ServletRequestHelper requestHelper;
68 private SystemPropertyHelper systemPropertyHelper;
71 private Client client;
74 private WebTarget webTarget;
77 private Invocation.Builder builder;
80 private Response response;
83 private Logging loggingService;
85 private PombaRestInterface pombaRestInterface;
88 public void setUp() throws GeneralSecurityException, IOException {
91 when(requestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
92 when(authClient.getClient(HttpClientMode.WITH_KEYSTORE)).thenReturn(client);
94 pombaRestInterface = new PombaRestInterface(authClient, requestHelper, systemPropertyHelper, loggingService);
99 public void shouldProperlySendRequestWithAllRequiredHeaders() {
100 Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
102 assertThat(actualResponse).isEqualTo(response);
104 verify(builder).accept(MediaType.APPLICATION_JSON);
105 verify(builder).header(FROM_APP_ID_HEADER, SAMPLE_APP_ID);
106 verify(builder).header(matches(TRANSACTION_ID_HEADER), matches(UUID_PATTERN));
107 verify(builder).header(matches(REQUEST_ID_HEADER_KEY), matches(UUID_PATTERN));
108 verify(builder).post(any(Entity.class));
112 public void shouldReturnNullWhenExceptionWasRaised() {
113 doThrow(new RuntimeException()).when(client).target(anyString());
115 Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
117 assertThat(actualResponse).isNull();
120 private void setUpBuilder() {
121 when(client.target(anyString())).thenReturn(webTarget);
122 when(webTarget.request()).thenReturn(builder);
123 when(builder.accept(MediaType.APPLICATION_JSON)).thenReturn(builder);
124 when(builder.header(anyString(), any())).thenReturn(builder);
125 when(builder.post(any(Entity.class))).thenReturn(response);
126 when(response.getStatusInfo()).thenReturn(Response.Status.OK);