4525f423019acdd726ae80b8d71074b93f67b141
[policy/gui.git] /
1 /*-
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
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.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;
48
49 public class PolicyUploadHandlerTest {
50
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;
57
58     /**
59      * Prepares test environment.
60      *
61      * @throws IOException where there is problem with reading the file.
62      */
63     @Before
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);
71     }
72
73     /**
74      * Cleaning up after the test.
75      */
76     @After
77     public void tearDown() {
78         if (clientBuilderMockedStatic != null) {
79             clientBuilderMockedStatic.close();
80         }
81     }
82
83     @Test
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);
89
90         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
91         assertThat(result.isNok()).isTrue();
92         assertThat(result.getMessage()).contains("Model upload is disable");
93     }
94
95     @Test
96     public void testDoUploadConnectionError() {
97         final var response = Mockito.mock(Response.class);
98         mockRsHttpClient(response);
99         Mockito.doThrow(ResponseProcessingException.class).when(response).getStatus();
100
101         prepareApexEditorMain();
102
103         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
104
105         assertThat(result.isNok()).isTrue();
106         assertThat(result.getMessage()).contains("failed with error");
107     }
108
109     @Test
110     public void testDoResponse() {
111         final var response = Mockito.mock(Response.class);
112         mockRsHttpClient(response);
113
114         Mockito.doReturn(201).when(response).getStatus();
115
116         prepareApexEditorMain();
117
118         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
119
120         assertThat(result.isOk()).isTrue();
121     }
122
123     @Test
124     public void testDoResponseErrorCode500() {
125         final var response = Mockito.mock(Response.class);
126         mockRsHttpClient(response);
127
128         Mockito.doReturn(500).when(response).getStatus();
129
130         prepareApexEditorMain();
131
132         final var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "", "");
133
134         assertThat(result.isNok()).isTrue();
135         assertThat(result.getMessage()).contains("failed with status 500");
136     }
137
138     @Test
139     public void testDoUploadUserId() {
140         final var response = Mockito.mock(Response.class);
141         mockRsHttpClient(response);
142
143         Mockito.doReturn(201).when(response).getStatus();
144
145         prepareApexEditorMain();
146
147         // If uploadUserId is specified, that value should be in DTO.
148         var result = uploadHandler.doUpload(toscaServiceTemplate, axArtifactKey, "",
149             "OverrideUser");
150         assertThat(result.isOk()).isTrue();
151         var dto = dtoEntityCaptor.getValue().getEntity();
152         assertThat(dto.getUserId()).isEqualTo("OverrideUser");
153
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);
159
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);
165     }
166
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);
171
172
173         clientBuilderMockedStatic = Mockito.mockStatic(ClientBuilder.class);
174
175         dtoEntityCaptor = ArgumentCaptor.forClass(Entity.class);
176
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);
182     }
183
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);
189     }
190 }