ab9445ed1a5c0909e23e934e204368b2f158b4d0
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.Invocation.Builder;
30 import javax.ws.rs.client.WebTarget;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
38 import org.onap.policy.gui.editors.apex.rest.handling.config.PolicyUploadPluginConfigKey;
39
40 public class UploadPluginClientTest {
41
42     private UploadPluginClient uploadPluginClient;
43
44     @Mock
45     private Client client;
46
47     private static final String url = "aUrl";
48
49     /**
50      * Init the mocks and system properties.
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         System.setProperty(PolicyUploadPluginConfigKey.URL.getKey(), url);
56         final UploadPluginConfigParameters uploadPluginConfigParameters = new UploadPluginConfigParameters();
57         uploadPluginClient = new UploadPluginClient(client, uploadPluginConfigParameters);
58     }
59
60     @Test
61     public void upload() {
62         final Builder mockBuilder = mock(Builder.class);
63         final WebTarget mockWebTarget = mock(WebTarget.class);
64         final Response mockResponse = mock(Response.class);
65         doReturn(mockWebTarget).when(client).target(url);
66         doReturn(mockBuilder).when(mockWebTarget).request(MediaType.APPLICATION_JSON);
67         when(mockBuilder.post(any())).thenReturn(mockResponse);
68         final Response actualResponse = uploadPluginClient.upload(new UploadPolicyRequestDto());
69         assertEquals(mockResponse, actualResponse);
70     }
71 }