make Logging a service and inject it to SyncRestClient
[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 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;
34
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;
53
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);
60
61     @Mock
62     private HttpsAuthClient authClient;
63
64     @Mock
65     private ServletRequestHelper requestHelper;
66
67     @Mock
68     private SystemPropertyHelper systemPropertyHelper;
69
70     @Mock
71     private Client client;
72
73     @Mock
74     private WebTarget webTarget;
75
76     @Mock
77     private Invocation.Builder builder;
78
79     @Mock
80     private Response response;
81
82     @Mock
83     private Logging loggingService;
84
85     private PombaRestInterface pombaRestInterface;
86
87     @BeforeMethod
88     public void setUp() throws GeneralSecurityException, IOException {
89         initMocks(this);
90
91         when(requestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
92         when(authClient.getClient(HttpClientMode.WITH_KEYSTORE)).thenReturn(client);
93         setUpBuilder();
94         pombaRestInterface = new PombaRestInterface(authClient, requestHelper, systemPropertyHelper, loggingService);
95     }
96
97
98     @Test
99     public void shouldProperlySendRequestWithAllRequiredHeaders() {
100         Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
101
102         assertThat(actualResponse).isEqualTo(response);
103
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));
109     }
110
111     @Test
112     public void shouldReturnNullWhenExceptionWasRaised() {
113         doThrow(new RuntimeException()).when(client).target(anyString());
114
115         Response actualResponse = pombaRestInterface.RestPost(SAMPLE_APP_ID, SAMPLE_URL, SAMPLE_PAYLOAD);
116
117         assertThat(actualResponse).isNull();
118     }
119
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);
127     }
128 }