7c97734d2ad5d9a9d39b71eb6f6462a1f9e2bf46
[policy/gui.git] / gui-editors / gui-editor-apex / src / test / java / org / onap / policy / gui / editors / apex / rest / handling / plugin / upload / PolicyUploadHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.ClientBuilder;
32 import javax.ws.rs.client.Invocation;
33 import javax.ws.rs.client.ResponseProcessingException;
34 import javax.ws.rs.client.WebTarget;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import org.apache.commons.lang3.RandomStringUtils;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentMatchers;
42 import org.mockito.MockedStatic;
43 import org.mockito.Mockito;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
46
47 public class PolicyUploadHandlerTest {
48
49     private PolicyUploadHandler uploadHandler;
50     private AxArtifactKey axArtifactKey;
51     private String toscaServiceTemplate;
52     private MockedStatic<ClientBuilder> clientBuilderMockedStatic;
53
54     /**
55      * Prepares test environment.
56      *
57      * @throws IOException where there is problem with reading the file.
58      */
59     @Before
60     public void setUp() throws IOException {
61         uploadHandler = new PolicyUploadHandler();
62         final var name = "a" + RandomStringUtils.randomAlphabetic(5);
63         final var version = "0.0.1";
64         axArtifactKey = new AxArtifactKey(name, version);
65         final var path = Path.of("src/test/resources/converter/", "ToscaTemplate.json");
66         toscaServiceTemplate = Files.readString(path);
67     }
68
69     /**
70      * Cleaning up after the test.
71      */
72     @After
73     public void tearDown() {
74         if (clientBuilderMockedStatic != null) {
75             clientBuilderMockedStatic.close();
76         }
77     }
78
79     @Test
80     public void testDoUploadNoUrl() {
81         final String[] args = {"--upload-userid", "MyUser"};
82         final var outBaStream = new ByteArrayOutputStream();
83         final var outStream = new PrintStream(outBaStream);
84         new ApexEditorMain(args, outStream);
85
86         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
87         assertThat(result.isNok()).isTrue();
88         assertThat(result.getMessage()).contains("Model upload is disable");
89     }
90
91     @Test
92     public void testDoUploadConnectionError() {
93         final var response = Mockito.mock(Response.class);
94         mockRsHttpClient(response);
95         Mockito.doThrow(ResponseProcessingException.class).when(response).getStatus();
96
97         prepareApexEditorMain();
98
99         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
100
101         assertThat(result.isNok()).isTrue();
102         assertThat(result.getMessage()).contains("failed with error");
103     }
104
105     @Test
106     public void testDoResponse() {
107         final var response = Mockito.mock(Response.class);
108         mockRsHttpClient(response);
109
110         Mockito.doReturn(201).when(response).getStatus();
111
112         prepareApexEditorMain();
113
114         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
115
116         assertThat(result.isOk()).isTrue();
117     }
118
119     @Test
120     public void testDoResponseErrorCode500() {
121         final var response = Mockito.mock(Response.class);
122         mockRsHttpClient(response);
123
124         Mockito.doReturn(500).when(response).getStatus();
125
126         prepareApexEditorMain();
127
128         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
129
130         assertThat(result.isNok()).isTrue();
131         assertThat(result.getMessage()).contains("failed with status 500");
132     }
133
134     private void mockRsHttpClient(Response response) {
135         final var webTarget = Mockito.mock(WebTarget.class);
136         final var client = Mockito.mock(Client.class);
137         final var invocationBuilder = Mockito.mock(Invocation.Builder.class);
138
139
140         clientBuilderMockedStatic = Mockito.mockStatic(ClientBuilder.class);
141
142         Mockito.when(ClientBuilder.newClient()).thenReturn(client);
143         Mockito.when(client.target(ArgumentMatchers.anyString())).thenReturn(webTarget);
144         Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
145         Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
146         Mockito.when(invocationBuilder.post(ArgumentMatchers.any())).thenReturn(response);
147     }
148
149     private void prepareApexEditorMain() {
150         final String[] args = {"--upload-userid", "MyUser", "--upload-url", "http://127.0.0.1"};
151         final var outBaStream = new ByteArrayOutputStream();
152         final var outStream = new PrintStream(outBaStream);
153         new ApexEditorMain(args, outStream);
154     }
155 }