ca4fb3bfb9ab718094d4426568533818fa007f38
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / DMaapClientUtilTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2018 IBM Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.client.impl;
26
27 import org.glassfish.jersey.client.ClientConfig;
28 import org.glassfish.jersey.client.ClientProperties;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34
35 import javax.ws.rs.client.Invocation.Builder;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.Response;
38 import java.util.Properties;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44
45 public class DMaapClientUtilTest {
46     @Mock
47     Response response;
48     @Mock
49     Builder builder;
50     @Mock
51     WebTarget target;
52     private ClientConfig config = null;
53
54     @Before
55     public void setup() {
56         MockitoAnnotations.initMocks(this);
57     }
58
59     @Test
60     public void testGetTarget() {
61         WebTarget actual = DmaapClientUtil.getTarget(getClientConfig(), "testpath");
62
63         assertEquals("testpath", actual.getUri().getPath());
64     }
65
66     @Test
67     public void testGetTargetWithParams() {
68         WebTarget actual = DmaapClientUtil.getTarget(getClientConfig(), "testpath", "testuser", "testpassword");
69
70         assertEquals("testpath", actual.getUri().getPath());
71     }
72
73     @Test
74     public void testGetResponsewtCambriaAuth() {
75         Mockito.when(target.request()).thenReturn(builder);
76         Mockito.when(builder.header("X-CambriaAuth", "testuser")).thenReturn(builder);
77         Mockito.when(builder.header("X-CambriaDate", "testpassword")).thenReturn(builder);
78         Mockito.when(builder.get()).thenReturn(response);
79
80         Response actual = DmaapClientUtil.getResponsewtCambriaAuth(target, "testuser", "testpassword");
81
82         assertEquals(response, actual);
83         verify(target).request();
84         verify(builder, times(2)).header((String) any(), any());
85     }
86
87     @Test
88     public void testSetHttpClientProperties() {
89         Properties properties = new Properties();
90         properties.setProperty(ClientProperties.PROXY_URI, "http://localhost:1234");
91         ClientConfig cConfig = DmaapClientUtil.getClientConfig(properties);
92
93         assertEquals("ApacheConnectorProvider", cConfig.getConnectorProvider().getClass().getSimpleName());
94     }
95
96     private ClientConfig getClientConfig() {
97         if (config == null) {
98             config = DmaapClientUtil.getClientConfig(null);
99         }
100         return config;
101
102     }
103
104 }