b409c1f46dd7e303087f2f2a95aa86ecd6d063a9
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / PombaRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.vid.aai;
21
22
23 import org.mockito.Mock;
24 import org.onap.vid.aai.util.HttpClientMode;
25 import org.onap.vid.aai.util.HttpsAuthClient;
26 import org.onap.vid.aai.util.ServletRequestHelper;
27 import org.onap.vid.aai.util.SystemPropertyHelper;
28 import org.testng.annotations.BeforeMethod;
29 import org.testng.annotations.Test;
30
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.Entity;
33 import javax.ws.rs.client.Invocation;
34 import javax.ws.rs.client.WebTarget;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37
38 import java.io.IOException;
39 import java.security.GeneralSecurityException;
40 import java.util.UUID;
41 import java.util.regex.Pattern;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44
45 import static org.mockito.ArgumentMatchers.anyString;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.matches;
48 import static org.mockito.Mockito.doThrow;
49 import static org.mockito.Mockito.verify;
50 import static org.mockito.Mockito.when;
51 import static org.mockito.MockitoAnnotations.initMocks;
52 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.FROM_APP_ID_HEADER;
53 import static org.onap.vid.aai.AaiOverTLSClientInterface.HEADERS.TRANSACTION_ID_HEADER;
54 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
55
56 public class PombaRestInterfaceTest {
57     private static final String UUID_REGEX = "[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}";
58     private static final String SAMPLE_APP_ID = "vid";
59     private static final String SAMPLE_URL = "sampleUrl";
60     private static final String SAMPLE_PAYLOAD = "samplePayload";
61     private static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);
62
63     @Mock
64     private HttpsAuthClient authClient;
65
66     @Mock
67     private ServletRequestHelper requestHelper;
68
69     @Mock
70     private SystemPropertyHelper systemPropertyHelper;
71
72     @Mock
73     private Client client;
74
75     @Mock
76     private WebTarget webTarget;
77
78     @Mock
79     private Invocation.Builder builder;
80
81     @Mock
82     private Response response;
83
84     private PombaRestInterface pombaRestInterface;
85
86     @BeforeMethod
87     public void setUp() throws GeneralSecurityException, IOException {
88         initMocks(this);
89
90         when(requestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
91         when(authClient.getClient(HttpClientMode.WITH_KEYSTORE)).thenReturn(client);
92         setUpBuilder();
93         pombaRestInterface = new PombaRestInterface(authClient, requestHelper, systemPropertyHelper);
94     }
95
96
97     @Test
98     public void shouldProperlySendRequestWithAllRequiredHeaders() {
99         Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
100
101         assertThat(actualResponse).isEqualTo(response);
102
103         verify(builder).accept(MediaType.APPLICATION_JSON);
104         verify(builder).header(FROM_APP_ID_HEADER, SAMPLE_APP_ID);
105         verify(builder).header(matches(TRANSACTION_ID_HEADER), matches(UUID_PATTERN));
106         verify(builder).header(matches(REQUEST_ID_HEADER_KEY), matches(UUID_PATTERN));
107         verify(builder).post(any(Entity.class));
108     }
109
110     @Test
111     public void shouldReturnNullWhenExceptionWasRaised() {
112         doThrow(new RuntimeException()).when(client).target(anyString());
113
114         Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
115
116         assertThat(actualResponse).isNull();
117     }
118
119     private void setUpBuilder() {
120         when(client.target(anyString())).thenReturn(webTarget);
121         when(webTarget.request()).thenReturn(builder);
122         when(builder.accept(MediaType.APPLICATION_JSON)).thenReturn(builder);
123         when(builder.header(anyString(), any())).thenReturn(builder);
124         when(builder.post(any(Entity.class))).thenReturn(response);
125         when(response.getStatusInfo()).thenReturn(Response.Status.OK);
126     }
127 }