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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
23 import static org.assertj.core.api.Assertions.assertThat;
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;
47 public class PolicyUploadHandlerTest {
49 private PolicyUploadHandler uploadHandler;
50 private AxArtifactKey axArtifactKey;
51 private String toscaServiceTemplate;
52 private MockedStatic<ClientBuilder> clientBuilderMockedStatic;
55 * Prepares test environment.
57 * @throws IOException where there is problem with reading the file.
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);
70 * Cleaning up after the test.
73 public void tearDown() {
74 if (clientBuilderMockedStatic != null) {
75 clientBuilderMockedStatic.close();
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);
86 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
87 assertThat(result.isNok()).isTrue();
88 assertThat(result.getMessage()).contains("Model upload is disable");
92 public void testDoUploadConnectionError() {
93 final var response = Mockito.mock(Response.class);
94 mockRsHttpClient(response);
95 Mockito.doThrow(ResponseProcessingException.class).when(response).getStatus();
97 prepareApexEditorMain();
99 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
101 assertThat(result.isNok()).isTrue();
102 assertThat(result.getMessage()).contains("failed with error");
106 public void testDoResponse() {
107 final var response = Mockito.mock(Response.class);
108 mockRsHttpClient(response);
110 Mockito.doReturn(201).when(response).getStatus();
112 prepareApexEditorMain();
114 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
116 assertThat(result.isOk()).isTrue();
120 public void testDoResponseErrorCode500() {
121 final var response = Mockito.mock(Response.class);
122 mockRsHttpClient(response);
124 Mockito.doReturn(500).when(response).getStatus();
126 prepareApexEditorMain();
128 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "");
130 assertThat(result.isNok()).isTrue();
131 assertThat(result.getMessage()).contains("failed with status 500");
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);
140 clientBuilderMockedStatic = Mockito.mockStatic(ClientBuilder.class);
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);
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);