2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.Entity;
33 import javax.ws.rs.client.Invocation;
34 import javax.ws.rs.client.ResponseProcessingException;
35 import javax.ws.rs.client.WebTarget;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.apache.commons.lang3.RandomStringUtils;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.ArgumentMatchers;
44 import org.mockito.MockedStatic;
45 import org.mockito.Mockito;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
47 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
49 public class PolicyUploadHandlerTest {
51 private static final String CMDLINE_UPLOAD_USERID = "MyUser";
52 private PolicyUploadHandler uploadHandler;
53 private AxArtifactKey axArtifactKey;
54 private String toscaServiceTemplate;
55 private MockedStatic<ClientBuilder> clientBuilderMockedStatic;
56 private ArgumentCaptor<Entity<UploadPolicyRequestDto>> dtoEntityCaptor;
59 * Prepares test environment.
61 * @throws IOException where there is problem with reading the file.
64 public void setUp() throws IOException {
65 uploadHandler = new PolicyUploadHandler();
66 final var name = "a" + RandomStringUtils.randomAlphabetic(5);
67 final var version = "0.0.1";
68 axArtifactKey = new AxArtifactKey(name, version);
69 final var path = Path.of("src/test/resources/models/", "PolicyModel.json");
70 toscaServiceTemplate = Files.readString(path);
74 * Cleaning up after the test.
77 public void tearDown() {
78 if (clientBuilderMockedStatic != null) {
79 clientBuilderMockedStatic.close();
84 public void testDoUploadNoUrl() {
85 final String[] args = {"--upload-userid", CMDLINE_UPLOAD_USERID};
86 final var outBaStream = new ByteArrayOutputStream();
87 final var outStream = new PrintStream(outBaStream);
88 new ApexEditorMain(args, outStream);
90 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
91 assertThat(result.isNok()).isTrue();
92 assertThat(result.getMessage()).contains("Model upload is disable");
96 public void testDoUploadConnectionError() {
97 final var response = Mockito.mock(Response.class);
98 mockRsHttpClient(response);
99 Mockito.doThrow(ResponseProcessingException.class).when(response).getStatus();
101 prepareApexEditorMain();
103 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
105 assertThat(result.isNok()).isTrue();
106 assertThat(result.getMessage()).contains("failed with error");
110 public void testDoResponse() {
111 final var response = Mockito.mock(Response.class);
112 mockRsHttpClient(response);
114 Mockito.doReturn(201).when(response).getStatus();
116 prepareApexEditorMain();
118 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
120 assertThat(result.isOk()).isTrue();
124 public void testDoResponseErrorCode500() {
125 final var response = Mockito.mock(Response.class);
126 mockRsHttpClient(response);
128 Mockito.doReturn(500).when(response).getStatus();
130 prepareApexEditorMain();
132 final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
134 assertThat(result.isNok()).isTrue();
135 assertThat(result.getMessage()).contains("failed with status 500");
139 public void testDoUploadUserId() {
140 final var response = Mockito.mock(Response.class);
141 mockRsHttpClient(response);
143 Mockito.doReturn(201).when(response).getStatus();
145 prepareApexEditorMain();
147 // If uploadUserId is specified, that value should be in DTO.
148 var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "",
150 assertThat(result.isOk()).isTrue();
151 var dto = dtoEntityCaptor.getValue().getEntity();
152 assertThat(dto.getUserId()).isEqualTo("OverrideUser");
154 // If uploadUserId is blank, the value from command line parameter 'upload-userid' is used.
155 result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
156 assertThat(result.isOk()).isTrue();
157 dto = dtoEntityCaptor.getValue().getEntity();
158 assertThat(dto.getUserId()).isEqualTo(CMDLINE_UPLOAD_USERID);
160 // If uploadUserId is null, the value from command line parameter 'upload-userid' is used.
161 result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", null);
162 assertThat(result.isOk()).isTrue();
163 dto = dtoEntityCaptor.getValue().getEntity();
164 assertThat(dto.getUserId()).isEqualTo(CMDLINE_UPLOAD_USERID);
167 private void mockRsHttpClient(Response response) {
168 final var webTarget = Mockito.mock(WebTarget.class);
169 final var client = Mockito.mock(Client.class);
170 final var invocationBuilder = Mockito.mock(Invocation.Builder.class);
173 clientBuilderMockedStatic = Mockito.mockStatic(ClientBuilder.class);
175 dtoEntityCaptor = ArgumentCaptor.forClass(Entity.class);
177 Mockito.when(ClientBuilder.newClient()).thenReturn(client);
178 Mockito.when(client.target(ArgumentMatchers.anyString())).thenReturn(webTarget);
179 Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
180 Mockito.when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);
181 Mockito.when(invocationBuilder.post(dtoEntityCaptor.capture())).thenReturn(response);
184 private void prepareApexEditorMain() {
185 final String[] args = {"--upload-userid", CMDLINE_UPLOAD_USERID, "--upload-url", "http://127.0.0.1"};
186 final var outBaStream = new ByteArrayOutputStream();
187 final var outStream = new PrintStream(outBaStream);
188 new ApexEditorMain(args, outStream);